def add_router_interface(self, context, router_id, interface_info): router_port_info = super( DFL3AgentlessRouterPlugin, self, ).add_router_interface(context, router_id, interface_info) router = self.get_router(context, router_id) port = self.core_plugin.get_port(context, router_port_info['port_id']) subnet = self.core_plugin.get_subnet(context, router_port_info['subnet_id']) cidr = netaddr.IPNetwork(subnet['cidr']) network = "%s/%s" % (port['fixed_ips'][0]['ip_address'], str(cidr.prefixlen)) port_topic = df_utils.get_obj_topic(port) logical_port = self.nb_api.get( l2.LogicalPort(id=port['id'], topic=port_topic)) router_topic = df_utils.get_obj_topic(router) logical_router_port = neutron_l3.build_logical_router_port( router_port_info, mac=port['mac_address'], network=network, unique_key=logical_port.unique_key) lrouter = self.nb_api.get( l3.LogicalRouter(id=router_id, topic=router_topic)) lrouter.version = router['revision_number'] lrouter.add_router_port(logical_router_port) self.nb_api.update(lrouter) self.core_plugin.update_port_status(context, port['id'], const.PORT_STATUS_ACTIVE) return router_port_info
def build_logical_router_port(router_port_info, mac, network, unique_key): return l3.LogicalRouterPort(id=router_port_info['port_id'], topic=utils.get_obj_topic(router_port_info), lswitch=router_port_info['network_id'], mac=mac, network=network, unique_key=unique_key)
def update_port_postcommit(self, context): updated_port = context.current topic = df_utils.get_obj_topic(updated_port) lean_port = l2.LogicalPort(id=updated_port['id'], topic=topic) if not self.nb_api.get(lean_port): # REVISIT(xiaohhui): Should we unify the check before update nb db? LOG.debug( "The port %s has been deleted from dragonflow NB DB, " "by concurrent operation.", updated_port['id']) return # Here we do not want port status update to trigger # sending event to other compute node. if (cfg.CONF.df.enable_neutron_notifier and n_const.DEVICE_OWNER_COMPUTE_PREFIX in updated_port['device_owner'] and context.status != context.original_status and (context.status == n_const.PORT_STATUS_DOWN or context.status == n_const.PORT_STATUS_ACTIVE)): return None lport = neutron_l2.logical_port_from_neutron_port(updated_port) # Update topic for FIP ports if lport.topic == '': lport.topic = self._get_lswitch_topic(updated_port) self.nb_api.update(lport) LOG.info("DFMechDriver: update port %s", updated_port['id']) return updated_port
def bgp_speaker_from_neutron_bgp_speaker(speaker): topic = df_utils.get_obj_topic(speaker) return bgp.BGPSpeaker(id=speaker.get('id'), topic=topic, name=speaker.get('name'), local_as=int(speaker.get('local_as')), peers=speaker.get('peers', []), ip_version=speaker.get('ip_version'))
def bgp_peer_from_neutron_bgp_peer(peer): topic = df_utils.get_obj_topic(peer) return bgp.BGPPeer(id=peer.get('id'), topic=topic, name=peer.get('name'), peer_ip=peer.get('peer_ip'), remote_as=int(peer.get('remote_as')), auth_type=peer.get('auth_type'), password=peer.get('password'))
def delete_floatingip(self, context, fip_id): floatingip = self.get_floatingip(context, fip_id) super(DFL3AgentlessRouterPlugin, self).delete_floatingip(context, fip_id) try: topic = df_utils.get_obj_topic(floatingip) self.nb_api.delete(l3.FloatingIp(id=fip_id, topic=topic), ) except df_exceptions.DBKeyNotFound: LOG.exception("floatingip %s is not found in DF DB", fip_id)
def delete_bgp_peer(self, context, bgp_peer_id): speakers = self._get_bgp_speakers_by_bgp_peer(context, bgp_peer_id) super(DFBgpPlugin, self).delete_bgp_peer(context, bgp_peer_id) self.nb_api.delete(bgp.BGPPeer(id=bgp_peer_id), skip_send_event=True) for s in speakers: topic = df_utils.get_obj_topic(s) self._remove_bgp_peer_from_bgp_speaker(context, s['id'], bgp_peer_id, topic)
def build_floating_ip_from_neutron_floating_ip(floating_ip): return l3.FloatingIp( id=floating_ip['id'], topic=utils.get_obj_topic(floating_ip), version=floating_ip['revision_number'], lrouter=floating_ip.get('router_id', None), lport=floating_ip.get('port_id', None), fixed_ip_address=floating_ip.get('fixed_ip_address', None), )
def security_group_from_neutron_obj(secgroup): sg_name = secgroup.get('name') rules = secgroup.get('security_group_rules', []) rules_mdls = [security_group_rule_from_neutron_obj(rule) for rule in rules] topic = df_utils.get_obj_topic(secgroup) return secgroups.SecurityGroup( id=secgroup['id'], topic=topic, name=sg_name, rules=rules_mdls, version=secgroup['revision_number'])
def update_subnet_postcommit(self, context): new_subnet = context.current subnet = neutron_l2.subnet_from_neutron_subnet(new_subnet) self.nb_api.update(subnet) network = context.network.current topic = df_utils.get_obj_topic(network) self.nb_api.update(l2.LogicalSwitch( id=network['id'], topic=topic, version=network['revision_number'])) LOG.info("DFMechDriver: update subnet %s", new_subnet['id']) return new_subnet
def subnet_from_neutron_subnet(subnet): return l2.Subnet( id=subnet['id'], topic=utils.get_obj_topic(subnet), name=subnet.get('name'), enable_dhcp=subnet['enable_dhcp'], cidr=subnet['cidr'], gateway_ip=subnet['gateway_ip'], dns_nameservers=subnet.get('dns_nameservers', []), host_routes=subnet.get('host_routes', []), version=subnet['revision_number'], lswitch=subnet['network_id'])
def logical_switch_from_neutron_network(network): return l2.LogicalSwitch( id=network['id'], topic=utils.get_obj_topic(network), name=network.get('name'), network_type=network.get('provider:network_type'), physical_network=network.get('provider:physical_network'), segmentation_id=network.get('provider:segmentation_id'), is_external=network['router:external'], mtu=network.get('mtu'), version=network['revision_number'], qos_policy=network.get('qos_policy_id'))
def create_subnet_postcommit(self, context): subnet = context.current network = context.network.current net_id = subnet['network_id'] df_subnet = neutron_l2.subnet_from_neutron_subnet(subnet) self.nb_api.create(df_subnet) topic = df_utils.get_obj_topic(network) self.nb_api.update(l2.LogicalSwitch( id=net_id, topic=topic, version=network['revision_number'])) LOG.info("DFMechDriver: create subnet %s", subnet['id']) return subnet
def _get_bgp_speakers_by_bgp_peer(self, context, bgp_peer_id): bgp_binding = bgp_db.BgpSpeakerPeerBinding filters = [ bgp_binding.bgp_speaker_id == bgp_db.BgpSpeaker.id, bgp_binding.bgp_peer_id == bgp_peer_id ] with context.session.begin(subtransactions=True): query = context.session.query(bgp_db.BgpSpeaker) query = query.filter(*filters) return [{ 'id': x['id'], 'project_id': df_utils.get_obj_topic(x) } for x in query.all()]
def delete_network_postcommit(self, context): network = context.current network_id = network['id'] topic = df_utils.get_obj_topic(network) try: self.nb_api.delete(l2.LogicalSwitch(id=network_id, topic=topic)) except df_exceptions.DBKeyNotFound: LOG.debug( "lswitch %s is not found in DF DB, might have " "been deleted concurrently", network_id) return LOG.info("DFMechDriver: delete network %s", network_id)
def delete_port_postcommit(self, context): port = context.current port_id = port['id'] topic = df_utils.get_obj_topic(port) lean_port = l2.LogicalPort(id=port_id, topic=topic) # Update topic for FIP ports if lean_port.topic == '': lean_port.topic = self._get_lswitch_topic(port) try: self.nb_api.delete(lean_port) except df_exceptions.DBKeyNotFound: LOG.debug( "port %s is not found in DF DB, might have " "been deleted concurrently", port_id) return LOG.info("DFMechDriver: delete port %s", port_id)
def remove_router_interface(self, context, router_id, interface_info): router_port_info = ( super(DFL3AgentlessRouterPlugin, self).remove_router_interface( context, router_id, interface_info)) router = self.get_router(context, router_id) try: topic = df_utils.get_obj_topic(router) lrouter = self.nb_api.get(l3.LogicalRouter( id=router_id, topic=topic)) lrouter.remove_router_port(router_port_info['port_id']) lrouter.version = router['revision_number'] self.nb_api.update(lrouter) except df_exceptions.DBKeyNotFound: LOG.exception("logical router %s is not found in DF DB, " "suppressing delete_lrouter_port " "exception", router_id) return router_port_info
def _create_dhcp_port(self, context, subnet): subnet_project = df_utils.get_obj_topic(subnet) port = { 'port': { 'project_id': subnet_project, 'tenant_id': subnet_project, 'network_id': subnet['network_id'], 'name': '', 'admin_state_up': True, 'device_id': '', 'device_owner': n_const.DEVICE_OWNER_DHCP, 'mac_address': n_const.ATTR_NOT_SPECIFIED, 'fixed_ips': [{ 'subnet_id': subnet['id'] }], 'status': n_const.PORT_STATUS_ACTIVE } } self.core_plugin.create_port(context, port)
def delete_subnet_postcommit(self, context): subnet = context.current net_id = subnet['network_id'] subnet_id = subnet['id'] # The network in context is still the network before deleting subnet network = self.core_plugin.get_network(context._plugin_context, net_id) try: topic = df_utils.get_obj_topic(network) self.nb_api.delete(l2.Subnet(id=subnet_id)) self.nb_api.update(l2.LogicalSwitch( id=net_id, topic=topic, version=network['revision_number'])) except df_exceptions.DBKeyNotFound: LOG.debug("network %s is not found in DB, might have " "been deleted concurrently", net_id) return LOG.info("DFMechDriver: delete subnet %s", subnet_id)
def logical_port_from_neutron_port(port): return l2.LogicalPort( id=port['id'], lswitch=port['network_id'], topic=utils.get_obj_topic(port), macs=[port['mac_address']], ips=[ip['ip_address'] for ip in port.get('fixed_ips', [])], subnets=[ip['subnet_id'] for ip in port.get('fixed_ips', [])], name=port.get('name'), enabled=port.get('admin_state_up', False), version=port['revision_number'], device_owner=port.get('device_owner'), device_id=port.get('device_id'), security_groups=port.get('security_groups', []), port_security_enabled=port.get(psec.PORTSECURITY, False), allowed_address_pairs=_validate_ip_prefix_allowed_address_pairs( port.get(addr_apidef.ADDRESS_PAIRS, [])), binding_vnic_type=port.get(portbindings.VNIC_TYPE), qos_policy=port.get('qos_policy_id'), dhcp_params=_build_dhcp_params(port), binding=build_port_binding(port), )
def logical_router_from_neutron_router(router): return l3.LogicalRouter(id=router['id'], topic=utils.get_obj_topic(router), name=router.get('name'), version=router['revision_number'], routes=router.get('routes', []))
def delete_security_group(self, resource, event, trigger, **kwargs): sg = kwargs['security_group'] topic = df_utils.get_obj_topic(sg) sg_obj = secgroups.SecurityGroup(id=sg['id'], topic=topic) self.nb_api.delete(sg_obj) LOG.info("DFMechDriver: delete security group %s", sg['id'])
def delete_security_group(self, resource, event, trigger, payload=None): sg = payload.latest_state topic = df_utils.get_obj_topic(sg) sg_obj = secgroups.SecurityGroup(id=sg['id'], topic=topic) self.nb_api.delete(sg_obj) LOG.info("DFMechDriver: delete security group %s", sg['id'])