~ubuntu-branches/ubuntu/vivid/neutron/vivid-updates

« back to all changes in this revision

Viewing changes to neutron/services/l3_router/brocade/l3_router_plugin.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-03-30 11:17:19 UTC
  • mfrom: (1.1.21)
  • Revision ID: package-import@ubuntu.com-20150330111719-h0gx7233p4jkkgfh
Tags: 1:2015.1~b3-0ubuntu1
* New upstream milestone release:
  - d/control: Align version requirements with upstream.
  - d/control: Add new dependency on oslo-log.
  - d/p/*: Rebase.
  - d/control,d/neutron-plugin-hyperv*: Dropped, decomposed into
    separate project upstream.
  - d/control,d/neutron-plugin-openflow*: Dropped, decomposed into
    separate project upstream.
  - d/neutron-common.install: Add neutron-rootwrap-daemon and 
    neutron-keepalived-state-change binaries.
  - d/rules: Ignore neutron-hyperv-agent when installing; only for Windows.
  - d/neutron-plugin-cisco.install: Drop neutron-cisco-cfg-agent as
    decomposed into separate project upstream.
  - d/neutron-plugin-vmware.install: Drop neutron-check-nsx-config and
    neutron-nsx-manage as decomposed into separate project upstream.
  - d/control: Add dependency on python-neutron-fwaas to neutron-l3-agent.
* d/pydist-overrides: Add overrides for oslo packages.
* d/control: Fixup type in package description (LP: #1263539).
* d/p/fixup-driver-test-execution.patch: Cherry pick fix from upstream VCS
  to support unit test exection in out-of-tree vendor drivers.
* d/neutron-common.postinst: Allow general access to /etc/neutron but limit
  access to root/neutron to /etc/neutron/neutron.conf to support execution
  of unit tests in decomposed vendor drivers.
* d/control: Add dependency on python-neutron-fwaas to neutron-l3-agent
  package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Implentation of Brocade SVI service Plugin."""
19
19
 
20
 
from oslo.config import cfg
21
 
from oslo.utils import excutils
22
 
 
23
 
from neutron.common import constants as l3_constants
24
 
from neutron.i18n import _LE, _LI
25
 
from neutron.openstack.common import log as logging
26
 
from neutron.plugins.ml2 import db
27
 
from neutron.plugins.ml2.drivers.brocade.db import models as brocade_db
28
 
from neutron.plugins.ml2.drivers.brocade.nos import nosdriver as driver
29
 
from neutron.services.l3_router import l3_router_plugin as router
30
 
 
31
 
 
32
 
DEVICE_OWNER_ROUTER_INTF = l3_constants.DEVICE_OWNER_ROUTER_INTF
33
 
DEVICE_OWNER_ROUTER_GW = l3_constants.DEVICE_OWNER_ROUTER_GW
34
 
DEVICE_OWNER_FLOATINGIP = l3_constants.DEVICE_OWNER_FLOATINGIP
 
20
from oslo_config import cfg
35
21
 
36
22
ML2_BROCADE = [cfg.StrOpt('address', default='',
37
23
                          help=_('The address of the host to SSH to')),
44
30
               ]
45
31
 
46
32
cfg.CONF.register_opts(ML2_BROCADE, "ml2_brocade")
47
 
 
48
 
LOG = logging.getLogger(__name__)
49
 
 
50
 
 
51
 
class BrocadeSVIPlugin(router.L3RouterPlugin):
52
 
    """Brocade SVI service Plugin."""
53
 
 
54
 
    def __init__(self):
55
 
        """Initialize Brocade Plugin
56
 
 
57
 
        Specify switch address and db configuration.
58
 
        """
59
 
        super(BrocadeSVIPlugin, self).__init__()
60
 
        self._switch = None
61
 
        self._driver = None
62
 
        self.brocade_init()
63
 
 
64
 
    def brocade_init(self):
65
 
        """Brocade specific initialization."""
66
 
        LOG.debug("brocadeSVIPlugin::brocade_init()")
67
 
 
68
 
        self._switch = {'address': cfg.CONF.ml2_brocade.address,
69
 
                        'username': cfg.CONF.ml2_brocade.username,
70
 
                        'password': cfg.CONF.ml2_brocade.password,
71
 
                        'rbridge_id': cfg.CONF.ml2_brocade.rbridge_id
72
 
                        }
73
 
        self._driver = driver.NOSdriver()
74
 
        LOG.info(_LI("rbridge id %s"), self._switch['rbridge_id'])
75
 
 
76
 
    def create_router(self, context, router):
77
 
        """Creates a vrf on NOS device."""
78
 
        LOG.debug("BrocadeSVIPlugin.create_router called: ")
79
 
        with context.session.begin(subtransactions=True):
80
 
            new_router = super(BrocadeSVIPlugin, self).create_router(context,
81
 
                                                                     router)
82
 
            # Router on VDX
83
 
            try:
84
 
                switch = self._switch
85
 
                self._driver.create_router(switch['address'],
86
 
                                           switch['username'],
87
 
                                           switch['password'],
88
 
                                           switch['rbridge_id'],
89
 
                                           str(new_router['id']))
90
 
            except Exception:
91
 
                with excutils.save_and_reraise_exception():
92
 
                    with context.session.begin(subtransactions=True):
93
 
                        super(BrocadeSVIPlugin, self).delete_router(
94
 
                            context,
95
 
                            new_router['id'])
96
 
 
97
 
        LOG.debug("BrocadeSVIPlugin.create_router: "
98
 
                  "router created on VDX switch")
99
 
        return new_router
100
 
 
101
 
    def delete_router(self, context, router_id):
102
 
        """Delete a vrf on NOS device."""
103
 
        router = super(BrocadeSVIPlugin, self).get_router(context, router_id)
104
 
        super(BrocadeSVIPlugin, self).delete_router(context, router_id)
105
 
 
106
 
        switch = self._switch
107
 
        self._driver.delete_router(switch['address'],
108
 
                                   switch['username'],
109
 
                                   switch['password'],
110
 
                                   switch['rbridge_id'],
111
 
                                   str(router['id']))
112
 
 
113
 
    def add_router_interface(self, context, router_id, interface_info):
114
 
        """creates svi on NOS device and assigns ip addres to SVI."""
115
 
        LOG.debug("BrocadeSVIPlugin.add_router_interface on VDX: "
116
 
                  "router_id=%(router_id)s "
117
 
                  "interface_info=%(interface_info)r",
118
 
                  {'router_id': router_id, 'interface_info': interface_info})
119
 
 
120
 
        with context.session.begin(subtransactions=True):
121
 
 
122
 
            info = super(BrocadeSVIPlugin, self).add_router_interface(
123
 
                context, router_id, interface_info)
124
 
 
125
 
            port = db.get_port(context.session, info["port_id"])
126
 
 
127
 
            # shutting down neutron port to allow NOS to do Arp/Routing
128
 
            port['admin_state_up'] = False
129
 
            port['port'] = port
130
 
            self._core_plugin.update_port(context, info["port_id"], port)
131
 
 
132
 
            interface_info = info
133
 
            subnet = self._core_plugin._get_subnet(context,
134
 
                                                   interface_info["subnet_id"])
135
 
            cidr = subnet["cidr"]
136
 
            net_addr, net_len = self.net_addr(cidr)
137
 
            gateway_ip = subnet["gateway_ip"]
138
 
            network_id = subnet['network_id']
139
 
            bnet = brocade_db.get_network(context, network_id)
140
 
            vlan_id = bnet['vlan']
141
 
            gateway_ip_cidr = gateway_ip + '/' + str(net_len)
142
 
            LOG.debug("Allocated cidr %(cidr)s from the pool, "
143
 
                      "network_id %(net_id)s "
144
 
                      "bnet %(bnet)s "
145
 
                      "vlan %(vlan_id)d ", {'cidr': gateway_ip_cidr,
146
 
                                            'net_id': network_id,
147
 
                                            'bnet': bnet,
148
 
                                            'vlan_id': int(vlan_id)})
149
 
            port_filters = {'network_id': [network_id],
150
 
                            'device_owner': [DEVICE_OWNER_ROUTER_INTF]}
151
 
            port_count = self._core_plugin.get_ports_count(context,
152
 
                                                           port_filters)
153
 
            LOG.info(_LI("BrocadeSVIPlugin.add_router_interface ports_count "
154
 
                         "%d"),
155
 
                     port_count)
156
 
 
157
 
            # port count is checked against 2 since the current port is already
158
 
            # added to db
159
 
            if port_count == 2:
160
 
                # This subnet is already part of some router
161
 
                # (this is not supported in this version of brocade svi plugin)
162
 
                msg = _("BrocadeSVIPlugin: adding redundant router interface "
163
 
                        "is not supported")
164
 
                LOG.error(msg)
165
 
                raise Exception(msg)
166
 
 
167
 
        try:
168
 
            switch = self._switch
169
 
            self._driver.create_svi(switch['address'],
170
 
                                    switch['username'],
171
 
                                    switch['password'],
172
 
                                    switch['rbridge_id'],
173
 
                                    vlan_id,
174
 
                                    gateway_ip_cidr,
175
 
                                    str(router_id))
176
 
        except Exception:
177
 
            LOG.error(_LE("Failed to create Brocade resources to add router "
178
 
                          "interface. info=%(info)s, router_id=%(router_id)s"),
179
 
                      {"info": info, "router_id": router_id})
180
 
            with excutils.save_and_reraise_exception():
181
 
                with context.session.begin(subtransactions=True):
182
 
                    self.remove_router_interface(context, router_id,
183
 
                                                 interface_info)
184
 
        return info
185
 
 
186
 
    def remove_router_interface(self, context, router_id, interface_info):
187
 
        """Deletes svi from NOS device."""
188
 
        LOG.debug("BrocadeSVIPlugin.remove_router_interface called: "
189
 
                  "router_id=%(router_id)s "
190
 
                  "interface_info=%(interface_info)r",
191
 
                  {'router_id': router_id, 'interface_info': interface_info})
192
 
 
193
 
        with context.session.begin(subtransactions=True):
194
 
            info = super(BrocadeSVIPlugin, self).remove_router_interface(
195
 
                context, router_id, interface_info)
196
 
            try:
197
 
                subnet = self._core_plugin._get_subnet(context,
198
 
                                                       info['subnet_id'])
199
 
                cidr = subnet['cidr']
200
 
                net_addr, net_len = self.net_addr(cidr)
201
 
                gateway_ip = subnet['gateway_ip']
202
 
                network_id = subnet['network_id']
203
 
                bnet = brocade_db.get_network(context, network_id)
204
 
                vlan_id = bnet['vlan']
205
 
                gateway_ip_cidr = gateway_ip + '/' + str(net_len)
206
 
                LOG.debug("remove_router_interface removed cidr %(cidr)s"
207
 
                          " from the pool,"
208
 
                          " network_id %(net_id)s bnet %(bnet)s"
209
 
                          " vlan %(vlan_id)d",
210
 
                          {'cidr': gateway_ip_cidr,
211
 
                           'net_id': network_id,
212
 
                           'bnet': bnet,
213
 
                           'vlan_id': int(vlan_id)})
214
 
                switch = self._switch
215
 
                self._driver.delete_svi(switch['address'],
216
 
                                        switch['username'],
217
 
                                        switch['password'],
218
 
                                        switch['rbridge_id'],
219
 
                                        vlan_id,
220
 
                                        gateway_ip_cidr,
221
 
                                        str(router_id))
222
 
            except Exception:
223
 
                with excutils.save_and_reraise_exception():
224
 
                    LOG.error(_LE("Fail remove of interface from brocade "
225
 
                                  "router interface. info=%(info)s, "
226
 
                                  "router_id=%(router_id)s"),
227
 
                              {"info": info, "router_id": router_id})
228
 
        return True
229
 
 
230
 
    @staticmethod
231
 
    def net_addr(addr):
232
 
        """Get network address prefix and length from a given address."""
233
 
        if addr is None:
234
 
            return None, None
235
 
        nw_addr, nw_len = addr.split('/')
236
 
        nw_len = int(nw_len)
237
 
        return nw_addr, nw_len