def get_nsx_router_id(session, cluster, neutron_router_id): """Return the NSX router uuid for a given neutron router. First, look up the Neutron database. If not found, execute a query on NSX platform as the mapping might be missing. """ if not neutron_router_id: return nsx_router_id = nsx_db.get_nsx_router_id( session, neutron_router_id) if not nsx_router_id: # Find logical router from backend. # This is a rather expensive query, but it won't be executed # more than once for each router in Neutron's lifetime nsx_routers = routerlib.query_lrouters( cluster, '*', filters={'tag': neutron_router_id, 'tag_scope': 'q_router_id'}) # Only one result expected # NOTE(salv-orlando): Not handling the case where more than one # port is found with the same neutron port tag if not nsx_routers: LOG.warn(_LW("Unable to find NSX router for Neutron router %s"), neutron_router_id) return nsx_router = nsx_routers[0] nsx_router_id = nsx_router['uuid'] with session.begin(subtransactions=True): # Create DB mapping nsx_db.add_neutron_nsx_router_mapping( session, neutron_router_id, nsx_router_id) return nsx_router_id
def delete_router(self, context, router_id): nsx_router_id = nsx_db.get_nsx_router_id(context.session, router_id) ret_val = super(NsxV3Plugin, self).delete_router(context, router_id) # Remove logical router from the NSX backend # It is safe to do now as db-level checks for resource deletion were # passed (and indeed the resource was removed from the Neutron DB try: nsxlib.delete_logical_router(nsx_router_id) except nsx_exc.LogicalRouterNotFound: # If the logical router was not found on the backend do not worry # about it. The conditions has already been logged, so there is no # need to do further logging pass except nsx_exc.NsxPluginException: # if there is a failure in deleting the router do not fail the # operation, especially since the router object has already been # removed from the neutron DB. Take corrective steps to ensure the # resulting zombie object does not forward any traffic and is # eventually removed. LOG.warning(_LW("Backend router deletion for neutron router %s " "failed. The object was however removed from the " "Neutron datanase"), router_id) return ret_val
def add_router_interface(self, context, router_id, interface_info): # NOTE(arosen): I think there is a bug here since I believe we # can also get a port or ip here.... subnet = self.get_subnet(context, interface_info['subnet_id']) port = {'port': {'network_id': subnet['network_id'], 'name': '', 'admin_state_up': True, 'device_id': '', 'device_owner': l3_db.DEVICE_OWNER_ROUTER_INTF, 'mac_address': attributes.ATTR_NOT_SPECIFIED, 'fixed_ips': [{'subnet_id': subnet['id'], 'ip_address': subnet['gateway_ip']}]}} port = self.create_port(context, port) _net_id, nsx_port_id = nsx_db.get_nsx_switch_and_port_id( context.session, port['id']) nsx_router_id = nsx_db.get_nsx_router_id(context.session, router_id) result = nsxlib.create_logical_router_port( logical_router_id=nsx_router_id, logical_switch_port_id=nsx_port_id, resource_type="LogicalRouterDownLinkPort", cidr_length=24, ip_address=subnet['gateway_ip']) interface_info['port_id'] = port['id'] del interface_info['subnet_id'] result = super(NsxV3Plugin, self).add_router_interface( context, router_id, interface_info) return result
def add_router_interface(self, context, router_id, interface_info): # NOTE(arosen): I think there is a bug here since I believe we # can also get a port or ip here.... subnet = self.get_subnet(context, interface_info["subnet_id"]) port = { "port": { "network_id": subnet["network_id"], "name": "", "admin_state_up": True, "device_id": "", "device_owner": l3_db.DEVICE_OWNER_ROUTER_INTF, "mac_address": attributes.ATTR_NOT_SPECIFIED, "fixed_ips": [{"subnet_id": subnet["id"], "ip_address": subnet["gateway_ip"]}], } } port = self.create_port(context, port) _net_id, nsx_port_id = nsx_db.get_nsx_switch_and_port_id(context.session, port["id"]) nsx_router_id = nsx_db.get_nsx_router_id(context.session, router_id) result = nsxlib.create_logical_router_port( logical_router_id=nsx_router_id, logical_switch_port_id=nsx_port_id, resource_type="LogicalRouterDownLinkPort", cidr_length=24, ip_address=subnet["gateway_ip"], ) interface_info["port_id"] = port["id"] del interface_info["subnet_id"] result = super(NsxV3Plugin, self).add_router_interface(context, router_id, interface_info) return result