def add_router_interface(self, vlan_name, vlan_id, subnet_id, gateway_ip, router_id): """Create VLAN SVI on the Nexus switch.""" # Find a switch to create the SVI on switch_ip = self._find_switch_for_svi() if not switch_ip: raise cisco_exc.NoNexusSviSwitch() # Check if this vlan exists on the switch already try: nxos_db.get_nexusvlan_binding(vlan_id, switch_ip) except cisco_exc.NexusPortBindingNotFound: # Create vlan and trunk vlan on the port self._client.create_and_trunk_vlan( switch_ip, vlan_id, vlan_name, nexus_port=None) # Check if a router interface has already been created try: nxos_db.get_nexusvm_binding(vlan_id, router_id) raise cisco_exc.SubnetInterfacePresent(subnet_id=subnet_id, router_id=router_id) except cisco_exc.NexusPortBindingNotFound: self._client.create_vlan_svi(switch_ip, vlan_id, gateway_ip) nxos_db.add_nexusport_binding('router', str(vlan_id), switch_ip, router_id) return True
def add_router_interface(self, vlan_name, vlan_id, subnet_id, gateway_ip, router_id): """Create VLAN SVI on the Nexus switch.""" # Find a switch to create the SVI on switch_ip = self._find_switch_for_svi() if not switch_ip: raise cisco_exc.NoNexusSviSwitch() # Check if this vlan exists on the switch already try: nxos_db.get_nexusvlan_binding(vlan_id, switch_ip) except cisco_exc.NexusPortBindingNotFound: # Create vlan and trunk vlan on the port self._client.create_and_trunk_vlan(switch_ip, vlan_id, vlan_name, nexus_port=None) # Check if a router interface has already been created try: nxos_db.get_nexusvm_binding(vlan_id, router_id) raise cisco_exc.SubnetInterfacePresent(subnet_id=subnet_id, router_id=router_id) except cisco_exc.NexusPortBindingNotFound: self._client.create_vlan_svi(switch_ip, vlan_id, gateway_ip) nxos_db.add_nexusport_binding('router', str(vlan_id), switch_ip, router_id) return True
def test_nexusvmbinding_get(self): npb11 = self._npb_test_obj(10, 100) npb21 = self._npb_test_obj(20, 100) npb22 = self._npb_test_obj(20, 200) self._add_to_db([npb11, npb21, npb22]) npb = nxdb.get_nexusvm_binding(npb21.vlan, npb21.instance) self._assert_equal(npb, npb21) npb = nxdb.get_nexusvm_binding(npb22.vlan, npb22.instance) self._assert_equal(npb, npb22) with testtools.ExpectedException(c_exc.NexusPortBindingNotFound): nxdb.get_nexusvm_binding(npb21.vlan, "dummyInstance")
def remove_router_interface(self, vlan_id, router_id): """Remove VLAN SVI from the Nexus Switch.""" # Grab switch_ip from database switch_ip = nxos_db.get_nexusvm_binding(vlan_id, router_id).switch_ip # Delete the SVI interface from the switch self._client.delete_vlan_svi(switch_ip, vlan_id) # Invoke delete_port to delete this row # And delete vlan if required return self.delete_port(router_id, vlan_id)
def delete_port(self, device_id, vlan_id): """Delete port. Delete port bindings from the database and scan whether the network is still required on the interfaces trunked. """ LOG.debug(_("NexusPlugin:delete_port() called")) # Delete DB row for this port try: row = nxos_db.get_nexusvm_binding(vlan_id, device_id) except cisco_exc.NexusPortBindingNotFound: return auto_delete = True auto_untrunk = True if cdb.is_provider_vlan(vlan_id): auto_delete = conf.CISCO.provider_vlan_auto_create auto_untrunk = conf.CISCO.provider_vlan_auto_trunk LOG.debug("delete_network(): provider vlan %s" % vlan_id) switch_ip = row.switch_ip nexus_port = None if row.port_id != 'router': nexus_port = row.port_id nxos_db.remove_nexusport_binding(row.port_id, row.vlan_id, row.switch_ip, row.instance_id) # Check for any other bindings with the same vlan_id and switch_ip try: nxos_db.get_nexusvlan_binding(row.vlan_id, row.switch_ip) except cisco_exc.NexusPortBindingNotFound: try: # Delete this vlan from this switch if nexus_port and auto_untrunk: self._client.disable_vlan_on_trunk_int( switch_ip, row.vlan_id, nexus_port) if auto_delete: self._client.delete_vlan(switch_ip, row.vlan_id) except Exception: # The delete vlan operation on the Nexus failed, # so this delete_port request has failed. For # consistency, roll back the Nexus database to what # it was before this request. with excutils.save_and_reraise_exception(): nxos_db.add_nexusport_binding(row.port_id, row.vlan_id, row.switch_ip, row.instance_id) return row.instance_id
def delete_port(self, device_id, vlan_id): """Delete port. Delete port bindings from the database and scan whether the network is still required on the interfaces trunked. """ LOG.debug(_("NexusPlugin:delete_port() called")) # Delete DB row for this port try: row = nxos_db.get_nexusvm_binding(vlan_id, device_id) except cisco_exc.NexusPortBindingNotFound: return auto_delete = True auto_untrunk = True if cdb.is_provider_vlan(vlan_id): auto_delete = conf.CISCO.provider_vlan_auto_create auto_untrunk = conf.CISCO.provider_vlan_auto_trunk LOG.debug("delete_network(): provider vlan %s" % vlan_id) switch_ip = row['switch_ip'] nexus_port = None if row['port_id'] != 'router': nexus_port = row['port_id'] nxos_db.remove_nexusport_binding(row['port_id'], row['vlan_id'], row['switch_ip'], row['instance_id']) # Check for any other bindings with the same vlan_id and switch_ip try: nxos_db.get_nexusvlan_binding(row['vlan_id'], row['switch_ip']) except cisco_exc.NexusPortBindingNotFound: try: # Delete this vlan from this switch if nexus_port and auto_untrunk: self._client.disable_vlan_on_trunk_int( switch_ip, row['vlan_id'], nexus_port) if auto_delete: self._client.delete_vlan(switch_ip, row['vlan_id']) except Exception: # The delete vlan operation on the Nexus failed, # so this delete_port request has failed. For # consistency, roll back the Nexus database to what # it was before this request. with excutils.save_and_reraise_exception(): nxos_db.add_nexusport_binding(row['port_id'], row['vlan_id'], row['switch_ip'], row['instance_id']) return row['instance_id']
def add_router_interface(self, vlan_name, vlan_id, subnet_id, gateway_ip, router_id): """Create VLAN SVI on the Nexus switch.""" # Find a switch to create the SVI on switch_ip = self._find_switch_for_svi() if not switch_ip: raise cisco_exc.NoNexusSwitch() _nexus_ip = switch_ip _nexus_ssh_port = self._nexus_switches[switch_ip, 'ssh_port'] _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] # Check if this vlan exists on the switch already try: nxos_db.get_nexusvlan_binding(vlan_id, switch_ip) except cisco_exc.NexusPortBindingNotFound: # Create vlan and trunk vlan on the port self._client.create_vlan( vlan_name, str(vlan_id), _nexus_ip, _nexus_username, _nexus_password, [], _nexus_ssh_port, vlan_id) # Check if a router interface has already been created try: nxos_db.get_nexusvm_binding(vlan_id, router_id) raise cisco_exc.SubnetInterfacePresent(subnet_id=subnet_id, router_id=router_id) except cisco_exc.NexusPortBindingNotFound: self._client.create_vlan_svi(vlan_id, _nexus_ip, _nexus_username, _nexus_password, _nexus_ssh_port, gateway_ip) nxos_db.add_nexusport_binding('router', str(vlan_id), switch_ip, router_id) return True
def delete_port(self, device_id, vlan_id): """Delete port. Delete port bindings from the database and scan whether the network is still required on the interfaces trunked. """ LOG.debug(_("NexusPlugin:delete_port() called")) # Delete DB row for this port try: row = nxos_db.get_nexusvm_binding(vlan_id, device_id) except cisco_exc.NexusPortBindingNotFound: return nxos_db.remove_nexusport_binding(row['port_id'], row['vlan_id'], row['switch_ip'], row['instance_id']) # Check for any other bindings with the same vlan_id and switch_ip try: nxos_db.get_nexusvlan_binding(row['vlan_id'], row['switch_ip']) except cisco_exc.NexusPortBindingNotFound: try: # Delete this vlan from this switch _nexus_ip = row['switch_ip'] _nexus_ports = () if row['port_id'] != 'router': _nexus_ports = (row['port_id'],) _nexus_ssh_port = (self._nexus_switches[_nexus_ip, 'ssh_port']) _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] self._client.delete_vlan( str(row['vlan_id']), _nexus_ip, _nexus_username, _nexus_password, _nexus_ports, _nexus_ssh_port) except Exception: # The delete vlan operation on the Nexus failed, # so this delete_port request has failed. For # consistency, roll back the Nexus database to what # it was before this request. with excutils.save_and_reraise_exception(): nxos_db.add_nexusport_binding(row['port_id'], row['vlan_id'], row['switch_ip'], row['instance_id']) return row['instance_id']
def remove_router_interface(self, vlan_id, router_id): """Remove VLAN SVI from the Nexus Switch.""" # Grab switch_ip from database row = nxos_db.get_nexusvm_binding(vlan_id, router_id) # Delete the SVI interface from the switch _nexus_ip = row['switch_ip'] _nexus_ssh_port = self._nexus_switches[_nexus_ip, 'ssh_port'] _nexus_creds = self.get_credential(_nexus_ip) _nexus_username = _nexus_creds['username'] _nexus_password = _nexus_creds['password'] self._client.delete_vlan_svi(vlan_id, _nexus_ip, _nexus_username, _nexus_password, _nexus_ssh_port) # Invoke delete_port to delete this row # And delete vlan if required return self.delete_port(router_id, vlan_id)