~ubuntu-branches/ubuntu/saucy/quantum/saucy

« back to all changes in this revision

Viewing changes to quantum/plugins/cisco/nexus/cisco_nexus_plugin_v2.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2013-05-31 09:37:25 UTC
  • mfrom: (2.1.22)
  • Revision ID: package-import@ubuntu.com-20130531093725-bf9jom93l7jm57iv
Tags: 1:2013.2~b1-0ubuntu1
* New upstream release.
* debian/patches/fix-quantum-configuration.patch: Refreshed
* debian/control: Add testrepository.
* debian/control: Add subunit.
* debian/control: Add python-d21o1 and python-pbr as build-depends.
* debian/control: Add python-stevedore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
from quantum.openstack.common import importutils
31
31
from quantum.plugins.cisco.common import cisco_constants as const
32
32
from quantum.plugins.cisco.common import cisco_credentials_v2 as cred
33
 
from quantum.plugins.cisco.common import cisco_exceptions as excep
 
33
from quantum.plugins.cisco.common import cisco_exceptions as cisco_exc
 
34
from quantum.plugins.cisco.common import config as conf
34
35
from quantum.plugins.cisco.db import network_db_v2 as cdb
35
36
from quantum.plugins.cisco.db import nexus_db_v2 as nxos_db
36
37
from quantum.plugins.cisco.l2device_plugin_base import L2DevicePluginBase
37
 
from quantum.plugins.cisco.nexus import cisco_nexus_configuration as conf
38
38
 
39
39
 
40
40
LOG = logging.getLogger(__name__)
41
41
 
42
42
 
43
43
class NexusPlugin(L2DevicePluginBase):
44
 
    """
45
 
    Nexus PLugIn Main Class
46
 
    """
 
44
    """Nexus PlugIn Main Class."""
47
45
    _networks = {}
48
46
 
49
47
    def __init__(self):
50
 
        """
51
 
        Extracts the configuration parameters from the configuration file
52
 
        """
53
 
        self._client = importutils.import_object(conf.NEXUS_DRIVER)
54
 
        LOG.debug(_("Loaded driver %s"), conf.NEXUS_DRIVER)
55
 
        self._nexus_switches = conf.NEXUS_DETAILS
 
48
        """Extract configuration parameters from the configuration file."""
 
49
        self._client = importutils.import_object(conf.CISCO.nexus_driver)
 
50
        LOG.debug(_("Loaded driver %s"), conf.CISCO.nexus_driver)
 
51
        self._nexus_switches = conf.get_nexus_dictionary()
56
52
        self.credentials = {}
57
53
 
58
54
    def get_credential(self, nexus_ip):
66
62
        return self.credentials[nexus_ip]
67
63
 
68
64
    def get_all_networks(self, tenant_id):
69
 
        """
70
 
        Returns a dictionary containing all
71
 
        <network_uuid, network_name> for
 
65
        """Get all networks.
 
66
 
 
67
        Returns a dictionary containing all <network_uuid, network_name> for
72
68
        the specified tenant.
73
69
        """
74
70
        LOG.debug(_("NexusPlugin:get_all_networks() called"))
76
72
 
77
73
    def create_network(self, tenant_id, net_name, net_id, vlan_name, vlan_id,
78
74
                       host, instance):
79
 
        """
80
 
        Create a VLAN in the appropriate switch/port,
81
 
        and configure the appropriate interfaces
82
 
        for this VLAN
 
75
        """Create network.
 
76
 
 
77
        Create a VLAN in the appropriate switch/port, and configure the
 
78
        appropriate interfaces for this VLAN.
83
79
        """
84
80
        LOG.debug(_("NexusPlugin:create_network() called"))
85
81
        # Grab the switch IP and port for this host
86
 
        switch_ip = ''
87
 
        port_id = ''
88
 
        for switch in self._nexus_switches.keys():
89
 
            for hostname in self._nexus_switches[switch].keys():
90
 
                if str(hostname) == str(host):
91
 
                    switch_ip = switch
92
 
                    port_id = self._nexus_switches[switch][hostname]['ports']
 
82
        for switch_ip, attr in self._nexus_switches:
 
83
            if str(attr) == str(host):
 
84
                port_id = self._nexus_switches[switch_ip, attr]
 
85
                break
 
86
        else:
 
87
            raise cisco_exc.NexusComputeHostNotConfigured(host=host)
 
88
 
93
89
        # Check if this network is already in the DB
94
 
        binding = nxos_db.get_port_vlan_switch_binding(
95
 
            port_id, vlan_id, switch_ip)
96
 
        if not binding:
 
90
        vlan_created = False
 
91
        vlan_enabled = False
 
92
 
 
93
        try:
 
94
            nxos_db.get_port_vlan_switch_binding(port_id, vlan_id, switch_ip)
 
95
        except cisco_exc.NexusPortBindingNotFound:
97
96
            _nexus_ip = switch_ip
98
97
            _nexus_ports = (port_id,)
99
98
            _nexus_ssh_port = \
100
 
                self._nexus_switches[switch_ip]['ssh_port']['ssh_port']
 
99
                self._nexus_switches[switch_ip, 'ssh_port']
101
100
            _nexus_creds = self.get_credential(_nexus_ip)
102
101
            _nexus_username = _nexus_creds['username']
103
102
            _nexus_password = _nexus_creds['password']
104
103
            # Check for vlan/switch binding
105
 
            vbinding = nxos_db.get_nexusvlan_binding(vlan_id, switch_ip)
106
 
            if not vbinding:
 
104
            try:
 
105
                nxos_db.get_nexusvlan_binding(vlan_id, switch_ip)
 
106
            except cisco_exc.NexusPortBindingNotFound:
107
107
                # Create vlan and trunk vlan on the port
108
108
                self._client.create_vlan(
109
109
                    vlan_name, str(vlan_id), _nexus_ip,
110
110
                    _nexus_username, _nexus_password,
111
111
                    _nexus_ports, _nexus_ssh_port, vlan_id)
 
112
                vlan_created = True
112
113
            else:
113
114
                # Only trunk vlan on the port
114
115
                man = self._client.nxos_connect(_nexus_ip,
118
119
                self._client.enable_vlan_on_trunk_int(man,
119
120
                                                      port_id,
120
121
                                                      vlan_id)
121
 
 
122
 
        nxos_db.add_nexusport_binding(port_id, str(vlan_id),
123
 
                                      switch_ip, instance)
 
122
                vlan_enabled = True
 
123
 
 
124
        try:
 
125
            nxos_db.add_nexusport_binding(port_id, str(vlan_id),
 
126
                                          switch_ip, instance)
 
127
        except Exception as e:
 
128
            try:
 
129
                # Add binding failed, roll back any vlan creation/enabling
 
130
                if vlan_created:
 
131
                    self._client.delete_vlan(
 
132
                        str(vlan_id), _nexus_ip,
 
133
                        _nexus_username, _nexus_password,
 
134
                        _nexus_ports, _nexus_ssh_port)
 
135
                if vlan_enabled:
 
136
                    self._client.disable_vlan_on_trunk_int(man,
 
137
                                                           port_id,
 
138
                                                           vlan_id)
 
139
            finally:
 
140
                # Raise the original exception
 
141
                raise e
 
142
 
124
143
        new_net_dict = {const.NET_ID: net_id,
125
144
                        const.NET_NAME: net_name,
126
145
                        const.NET_PORTS: {},
130
149
        return new_net_dict
131
150
 
132
151
    def delete_network(self, tenant_id, net_id, **kwargs):
133
 
        """
 
152
        """Delete network.
 
153
 
134
154
        Deletes the VLAN in all switches, and removes the VLAN configuration
135
 
        from the relevant interfaces
 
155
        from the relevant interfaces.
136
156
        """
137
157
        LOG.debug(_("NexusPlugin:delete_network() called"))
138
158
 
139
159
    def get_network_details(self, tenant_id, net_id, **kwargs):
140
 
        """
141
 
        Returns the details of a particular network
142
 
        """
 
160
        """Return the details of a particular network."""
143
161
        LOG.debug(_("NexusPlugin:get_network_details() called"))
144
162
        network = self._get_network(tenant_id, net_id)
145
163
        return network
146
164
 
147
165
    def update_network(self, tenant_id, net_id, **kwargs):
148
 
        """
149
 
        Updates the properties of a particular
150
 
        Virtual Network.
151
 
        """
 
166
        """Update the properties of a particular Virtual Network."""
152
167
        LOG.debug(_("NexusPlugin:update_network() called"))
153
168
 
154
169
    def get_all_ports(self, tenant_id, net_id, **kwargs):
155
 
        """
 
170
        """Get all ports.
 
171
 
156
172
        This is probably not applicable to the Nexus plugin.
157
173
        Delete if not required.
158
174
        """
159
175
        LOG.debug(_("NexusPlugin:get_all_ports() called"))
160
176
 
161
177
    def create_port(self, tenant_id, net_id, port_state, port_id, **kwargs):
162
 
        """
 
178
        """Create port.
 
179
 
163
180
        This is probably not applicable to the Nexus plugin.
164
181
        Delete if not required.
165
182
        """
166
183
        LOG.debug(_("NexusPlugin:create_port() called"))
167
184
 
168
185
    def delete_port(self, device_id, vlan_id):
169
 
        """
170
 
        Delete port bindings from the database and scan
171
 
        whether the network is still required on
172
 
        the interfaces trunked
 
186
        """Delete port.
 
187
 
 
188
        Delete port bindings from the database and scan whether the network
 
189
        is still required on the interfaces trunked.
173
190
        """
174
191
        LOG.debug(_("NexusPlugin:delete_port() called"))
175
192
        # Delete DB row for this port
176
 
        row = nxos_db.get_nexusvm_binding(vlan_id, device_id)
177
 
        if row:
178
 
            nxos_db.remove_nexusport_binding(row['port_id'], row['vlan_id'],
179
 
                                             row['switch_ip'],
180
 
                                             row['instance_id'])
181
 
            # Check for any other bindings with the same vlan_id and switch_ip
182
 
            bindings = nxos_db.get_nexusvlan_binding(
183
 
                row['vlan_id'], row['switch_ip'])
 
193
        try:
 
194
            row = nxos_db.get_nexusvm_binding(vlan_id, device_id)
 
195
        except cisco_exc.NexusPortBindingNotFound:
 
196
            return
184
197
 
185
 
            if not bindings:
 
198
        nxos_db.remove_nexusport_binding(row['port_id'], row['vlan_id'],
 
199
                                         row['switch_ip'],
 
200
                                         row['instance_id'])
 
201
        # Check for any other bindings with the same vlan_id and switch_ip
 
202
        try:
 
203
            nxos_db.get_nexusvlan_binding(row['vlan_id'], row['switch_ip'])
 
204
        except cisco_exc.NexusPortBindingNotFound:
 
205
            try:
186
206
                # Delete this vlan from this switch
187
207
                _nexus_ip = row['switch_ip']
188
208
                _nexus_ports = (row['port_id'],)
189
 
                _nexus_ssh_port = \
190
 
                    self._nexus_switches[_nexus_ip]['ssh_port']['ssh_port']
 
209
                _nexus_ssh_port = (self._nexus_switches[_nexus_ip,
 
210
                                                        'ssh_port'])
191
211
                _nexus_creds = self.get_credential(_nexus_ip)
192
212
                _nexus_username = _nexus_creds['username']
193
213
                _nexus_password = _nexus_creds['password']
195
215
                    str(row['vlan_id']), _nexus_ip,
196
216
                    _nexus_username, _nexus_password,
197
217
                    _nexus_ports, _nexus_ssh_port)
 
218
            except Exception as e:
 
219
                # The delete vlan operation on the Nexus failed,
 
220
                # so this delete_port request has failed. For
 
221
                # consistency, roll back the Nexus database to what
 
222
                # it was before this request.
 
223
                try:
 
224
                    nxos_db.add_nexusport_binding(row['port_id'],
 
225
                                                  row['vlan_id'],
 
226
                                                  row['switch_ip'],
 
227
                                                  row['instance_id'])
 
228
                finally:
 
229
                    # Raise the original exception
 
230
                    raise e
198
231
 
199
 
            return row['instance_id']
 
232
        return row['instance_id']
200
233
 
201
234
    def update_port(self, tenant_id, net_id, port_id, port_state, **kwargs):
202
 
        """
 
235
        """Update port.
 
236
 
203
237
        This is probably not applicable to the Nexus plugin.
204
238
        Delete if not required.
205
239
        """
206
240
        LOG.debug(_("NexusPlugin:update_port() called"))
207
241
 
208
242
    def get_port_details(self, tenant_id, net_id, port_id, **kwargs):
209
 
        """
 
243
        """Get port details.
 
244
 
210
245
        This is probably not applicable to the Nexus plugin.
211
246
        Delete if not required.
212
247
        """
214
249
 
215
250
    def plug_interface(self, tenant_id, net_id, port_id, remote_interface_id,
216
251
                       **kwargs):
217
 
        """
 
252
        """Plug interfaces.
 
253
 
218
254
        This is probably not applicable to the Nexus plugin.
219
255
        Delete if not required.
220
256
        """
221
257
        LOG.debug(_("NexusPlugin:plug_interface() called"))
222
258
 
223
259
    def unplug_interface(self, tenant_id, net_id, port_id, **kwargs):
224
 
        """
 
260
        """Unplug interface.
 
261
 
225
262
        This is probably not applicable to the Nexus plugin.
226
263
        Delete if not required.
227
264
        """
229
266
 
230
267
    def _get_vlan_id_for_network(self, tenant_id, network_id, context,
231
268
                                 base_plugin_ref):
232
 
        """
233
 
        Obtain the VLAN ID given the Network ID
234
 
        """
 
269
        """Obtain the VLAN ID given the Network ID."""
235
270
        vlan = cdb.get_vlan_binding(network_id)
236
271
        return vlan.vlan_id
237
272
 
238
273
    def _get_network(self, tenant_id, network_id, context, base_plugin_ref):
239
 
        """
240
 
        Gets the NETWORK ID
241
 
        """
 
274
        """Get the Network ID."""
242
275
        network = base_plugin_ref._get_network(context, network_id)
243
276
        if not network:
244
277
            raise exc.NetworkNotFound(net_id=network_id)