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, etype=None, nexus_port=None) # Check if a router interface has already been created try: nxos_db.get_nexusvm_bindings(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_bindings(npb21.vlan, npb21.instance)[0] self._assert_equal(npb, npb21) npb = nxdb.get_nexusvm_bindings(npb22.vlan, npb22.instance)[0] self._assert_equal(npb, npb22) with testtools.ExpectedException(c_exc.NexusPortBindingNotFound): nxdb.get_nexusvm_bindings(npb21.vlan, "dummyInstance")
def verify_portbinding(self, host_id1, host_id2, vlan, device_id, binding_port): """Verify a port binding entry in the DB is correct.""" self.assertEqual(host_id1, host_id2) pb = nexus_db_v2.get_nexusvm_bindings(vlan, device_id) self.assertEqual(len(pb), 1) self.assertEqual(pb[0].port_id, binding_port) self.assertEqual(pb[0].switch_ip, NEXUS_IP_ADDR)
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(s) for this port try: rows = nxos_db.get_nexusvm_bindings(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) instance_id = False for row in rows: instance_id = row["instance_id"] switch_ip = row.switch_ip etype, nexus_port = "", "" if row["port_id"] == "router": etype, nexus_port = "vlan", row["port_id"] auto_untrunk = False else: etype, nexus_port = row["port_id"].split(":") nxos_db.remove_nexusport_binding(row.port_id, row.vlan_id, row.switch_ip, row.instance_id) # Check whether there are any remaining instances using this # vlan on this Nexus port. try: nxos_db.get_port_vlan_switch_binding(row.port_id, row.vlan_id, row.switch_ip) except cisco_exc.NexusPortBindingNotFound: try: if nexus_port and auto_untrunk: # Untrunk the vlan from this Nexus interface self._client.disable_vlan_on_trunk_int(switch_ip, row.vlan_id, etype, nexus_port) # Check whether there are any remaining instances # using this vlan on the Nexus switch. if auto_delete: try: nxos_db.get_nexusvlan_binding(row.vlan_id, row.switch_ip) except cisco_exc.NexusPortBindingNotFound: # Delete this vlan from this switch 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 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(s) for this port try: rows = nxos_db.get_nexusvm_bindings(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) instance_id = False for row in rows: instance_id = row['instance_id'] switch_ip = row.switch_ip etype, nexus_port = '', '' if row['port_id'] == 'router': etype, nexus_port = 'vlan', row['port_id'] else: etype, nexus_port = row['port_id'].split(':') 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, etype, 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 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(s) for this port try: rows = nxos_db.get_nexusvm_bindings(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) instance_id = False for row in rows: instance_id = row['instance_id'] switch_ip = row.switch_ip etype, nexus_port = '', '' if row['port_id'] == 'router': etype, nexus_port = 'vlan', row['port_id'] auto_untrunk = False else: etype, nexus_port = row['port_id'].split(':') 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, etype, 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 instance_id
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_bindings(vlan_id, router_id)[0].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)