Пример #1
0
 def __init__(self, uuid, obj_dict=None):
     self.uuid = uuid
     self.virtual_networks = set()
     self.bgp_router = None
     self.update(obj_dict)
     self.config_manager = PhysicalRouterConfig(
         self.management_ip, self.user_credentials, self._logger)
Пример #2
0
 def __init__(self, uuid, obj_dict=None):
     self.uuid = uuid
     self.virtual_networks = set()
     self.bgp_router = None
     self.config_manager = None
     self.nc_q = queue.Queue(maxsize=1)
     self.nc_handler_gl = gevent.spawn(self.nc_handler)
     self.update(obj_dict)
     self.config_manager = PhysicalRouterConfig(
         self.management_ip, self.user_credentials, self.vendor,
         self.product, self.vnc_managed, self._logger)
     self.uve_send()
Пример #3
0
 def __init__(self, uuid, obj_dict=None):
     self.uuid = uuid
     self.virtual_networks = set()
     self.bgp_router = None
     self.config_manager = None
     self.nc_q = queue.Queue(maxsize=1)
     self.nc_handler_gl = gevent.spawn(self.nc_handler)
     self.update(obj_dict)
     self.config_manager = PhysicalRouterConfig(
         self.management_ip, self.user_credentials, self.vendor,
         self.product, self.vnc_managed, self._logger)
Пример #4
0
 def __init__(self, uuid, obj_dict=None):
     self.uuid = uuid
     self.virtual_networks = set()
     self.bgp_router = None
     self.config_manager = None
     self.nc_q = queue.Queue(maxsize=1)
     self.vn_ip_map = {}
     self.init_cs_state()
     self.update(obj_dict)
     self.config_manager = PhysicalRouterConfig(
         self.management_ip, self.user_credentials, self.vendor,
         self.product, self._logger)
     self.set_conf_sent_state(False)
     self.config_repush_interval = PushConfigState.get_repush_interval()
     self.nc_handler_gl = gevent.spawn(self.nc_handler)
     self.uve_send()
Пример #5
0
class PhysicalRouterDM(DBBase):
    _dict = {}
    obj_type = 'physical_router'
    _sandesh = None

    def __init__(self, uuid, obj_dict=None):
        self.uuid = uuid
        self.virtual_networks = set()
        self.bgp_router = None
        self.config_manager = None
        self.nc_q = queue.Queue(maxsize=1)
        self.nc_handler_gl = gevent.spawn(self.nc_handler)
        self.update(obj_dict)
        self.config_manager = PhysicalRouterConfig(
            self.management_ip, self.user_credentials, self.vendor,
            self.product, self.vnc_managed, self._logger)
        self.uve_send()
    # end __init__

    def update(self, obj=None):
        if obj is None:
            obj = self.read_obj(self.uuid)
        self.name = obj['fq_name'][-1]
        self.management_ip = obj.get('physical_router_management_ip')
        self.dataplane_ip = obj.get('physical_router_dataplane_ip')
        self.vendor = obj.get('physical_router_vendor_name')
        self.product = obj.get('physical_router_product_name')
        self.vnc_managed = obj.get('physical_router_vnc_managed')
        self.user_credentials = obj.get('physical_router_user_credentials')
        self.junos_service_ports = obj.get('physical_router_junos_service_ports')
        self.update_single_ref('bgp_router', obj)
        self.update_multiple_refs('virtual_network', obj)
        self.physical_interfaces = set([pi['uuid'] for pi in
                                        obj.get('physical_interfaces', [])])
        self.logical_interfaces = set([li['uuid'] for li in
                                       obj.get('logical_interfaces', [])])
        if self.config_manager is not None:
            self.config_manager.update(
                self.management_ip, self.user_credentials, self.vendor,
                self.product, self.vnc_managed)
    # end update

    @classmethod
    def delete(cls, uuid):
        if uuid not in cls._dict:
            return
        obj = cls._dict[uuid]
        obj.config_manager.delete_bgp_config()
        obj.uve_send(True)
        obj.update_single_ref('bgp_router', {})
        obj.update_multiple_refs('virtual_network', {})
        del cls._dict[uuid]
    # end delete

    def is_junos_service_ports_enabled(self):
        if self.junos_service_ports is not None and self.junos_service_ports.get('service_port') is not None:
            return True
        return False
    #end is_junos_service_ports_enabled

    def set_config_state(self):
        try:
            self.nc_q.put_nowait(1)
        except queue.Full:
            pass
    #end

    def nc_handler(self):
        while self.nc_q.get() is not None:
            try:
                self.push_config()
            except Exception as e:
                self._logger.error("Exception: " + str(e))
    #end

    def is_valid_ip(self, ip_str):
        try:
            socket.inet_aton(ip_str)
            return True
        except socket.error:
            return False
    #end

    def push_config(self):
        self.config_manager.reset_bgp_config()
        bgp_router = BgpRouterDM.get(self.bgp_router)
        if bgp_router:
            for peer_uuid, params in bgp_router.bgp_routers.items():
                peer = BgpRouterDM.get(peer_uuid)
                if peer is None:
                    continue
                external = (bgp_router.params['autonomous_system'] !=
                            peer.params['autonomous_system'])
                self.config_manager.add_bgp_peer(peer.params['address'],
                                                 params, external)
            self.config_manager.set_bgp_config(bgp_router.params)
            bgp_router_ips = bgp_router.get_all_bgp_router_ips()
            if self.dataplane_ip is not None and self.is_valid_ip(self.dataplane_ip):
                self.config_manager.add_dynamic_tunnels(self.dataplane_ip,
                              GlobalSystemConfigDM.ip_fabric_subnets, bgp_router_ips)
        vn_dict = {}
        for vn_id in self.virtual_networks:
            vn_dict[vn_id] = []

        li_set = self.logical_interfaces
        for pi_uuid in self.physical_interfaces:
            pi = PhysicalInterfaceDM.get(pi_uuid)
            if pi is None:
                continue
            li_set |= pi.logical_interfaces
        for li_uuid in li_set:
            li = LogicalInterfaceDM.get(li_uuid)
            if li is None:
                continue
            vmi_id = li.virtual_machine_interface
            vmi = VirtualMachineInterfaceDM.get(vmi_id)
            if vmi is None:
                continue
            vn_id = vmi.virtual_network
            if vn_id in vn_dict:
                vn_dict[vn_id].append(li.name)
            else:
                vn_dict[vn_id] = [li.name]

        #for now, assume service port ifls unit numbers are always starts with 0 and goes on
        service_port_id = 0
        for vn_id, interfaces in vn_dict.items():
            vn_obj = VirtualNetworkDM.get(vn_id)
            if vn_obj is None:
                continue
            export_set = None
            import_set = None
            for ri_id in vn_obj.routing_instances:
                # Find the primary RI by matching the name
                ri_obj = RoutingInstanceDM.get(ri_id)
                if ri_obj is None:
                    continue
                if ri_obj.fq_name[-1] == vn_obj.fq_name[-1]:
                    vrf_name = vn_obj.get_vrf_name()
                    vrf_name_l2 = vn_obj.get_vrf_name(vrf_type='l2')
                    vrf_name_l3 = vn_obj.get_vrf_name(vrf_type='l3')
                    export_set = copy.copy(ri_obj.export_targets)
                    import_set = copy.copy(ri_obj.import_targets)
                    for ri2_id in ri_obj.routing_instances:
                        ri2 = RoutingInstanceDM.get(ri2_id)
                        if ri2 is None:
                            continue
                        import_set |= ri2.export_targets

                    if vn_obj.router_external == False:
                        self.config_manager.add_routing_instance(vrf_name_l3,
                                                             import_set,
                                                             export_set,
                                                             vn_obj.prefixes,
                                                             vn_obj.gateways,
                                                             vn_obj.router_external,
                                                             ["irb" + "." + str(vn_obj.vxlan_vni)])
                        self.config_manager.add_routing_instance(vrf_name_l2,
                                                             import_set,
                                                             export_set,
                                                             vn_obj.prefixes,
                                                             vn_obj.gateways,
                                                             vn_obj.router_external,
                                                             interfaces,
                                                             vn_obj.vxlan_vni)
                    else:
                        self.config_manager.add_routing_instance(vrf_name_l3,
                                                             import_set,
                                                             export_set,
                                                             vn_obj.prefixes,
                                                             vn_obj.gateways,
                                                             vn_obj.router_external,
                                                             interfaces,
                                                             vn_obj.vxlan_vni)

                    break

            if export_set is not None and self.is_junos_service_ports_enabled() and len(vn_obj.instance_ip_map) > 0:
                vrf_name = vrf_name[:123] + '-nat'
                interfaces = []
                service_ports = self.junos_service_ports.get('service_port')
                interfaces.append(service_ports[0] + "." + str(service_port_id))
                service_port_id  = service_port_id + 1
                interfaces.append(service_ports[0] + "." + str(service_port_id))
                service_port_id  = service_port_id + 1
                self.config_manager.add_routing_instance(vrf_name,
                                                         import_set,
                                                         export_set,
                                                         None,
                                                         None,
                                                         False,
                                                         interfaces,
                                                         None,
                                                         vn_obj.instance_ip_map)

        self.config_manager.send_bgp_config()
        self.uve_send()
    # end push_config

    def uve_send(self, deleted=False):
        pr_trace = UvePhysicalRouterConfig(name=self.name,
                                           ip_address=self.management_ip,
                                           connected_bgp_router=self.bgp_router,
                                           auto_conf_enabled=self.vnc_managed,
                                           product_info=self.vendor + ':' + self.product)
        if deleted:
            pr_trace.deleted = True
            pr_msg = UvePhysicalRouterConfigTrace(data=pr_trace,
                                                  sandesh=PhysicalRouterDM._sandesh)
            pr_msg.send(sandesh=PhysicalRouterDM._sandesh)
            return

        commit_stats = self.config_manager.get_commit_stats()

        if commit_stats['netconf_enabled'] is True:
            pr_trace.last_commit_time = commit_stats['last_commit_time']
            pr_trace.last_commit_duration = commit_stats['last_commit_duration']
            pr_trace.commit_status_message = commit_stats['commit_status_message']
            pr_trace.total_commits_sent_since_up = commit_stats['total_commits_sent_since_up']
        else:
            pr_trace.netconf_enabled_status = commit_stats['netconf_enabled_status']

        pr_msg = UvePhysicalRouterConfigTrace(data=pr_trace, sandesh=PhysicalRouterDM._sandesh)
        pr_msg.send(sandesh=PhysicalRouterDM._sandesh)
Пример #6
0
class PhysicalRouterDM(DBBaseDM):
    _dict = {}
    obj_type = 'physical_router'
    _sandesh = None

    def __init__(self, uuid, obj_dict=None):
        self.uuid = uuid
        self.virtual_networks = set()
        self.bgp_router = None
        self.config_manager = None
        self.nc_q = queue.Queue(maxsize=1)
        self.nc_handler_gl = gevent.spawn(self.nc_handler)
        self.vn_ip_map = {}
        self.init_cs_state()
        self.update(obj_dict)
        self.config_manager = PhysicalRouterConfig(self.management_ip,
                                                   self.user_credentials,
                                                   self.vendor, self.product,
                                                   self.vnc_managed,
                                                   self._logger)
        self.uve_send()

    # end __init__

    def update(self, obj=None):
        if obj is None:
            obj = self.read_obj(self.uuid)
        self.name = obj['fq_name'][-1]
        self.management_ip = obj.get('physical_router_management_ip')
        self.dataplane_ip = obj.get('physical_router_dataplane_ip')
        self.vendor = obj.get('physical_router_vendor_name', '')
        self.product = obj.get('physical_router_product_name', '')
        self.vnc_managed = obj.get('physical_router_vnc_managed')
        self.user_credentials = obj.get('physical_router_user_credentials')
        self.junos_service_ports = obj.get(
            'physical_router_junos_service_ports')
        self.update_single_ref('bgp_router', obj)
        self.update_multiple_refs('virtual_network', obj)
        self.physical_interfaces = set(
            [pi['uuid'] for pi in obj.get('physical_interfaces', [])])
        self.logical_interfaces = set(
            [li['uuid'] for li in obj.get('logical_interfaces', [])])
        if self.config_manager is not None:
            self.config_manager.update(self.management_ip,
                                       self.user_credentials, self.vendor,
                                       self.product, self.vnc_managed)

    # end update

    @classmethod
    def delete(cls, uuid):
        if uuid not in cls._dict:
            return
        obj = cls._dict[uuid]
        obj._cassandra.delete_pr(uuid)
        obj.config_manager.delete_bgp_config()
        obj.uve_send(True)
        obj.update_single_ref('bgp_router', {})
        obj.update_multiple_refs('virtual_network', {})
        del cls._dict[uuid]

    # end delete

    def is_junos_service_ports_enabled(self):
        if self.junos_service_ports is not None and self.junos_service_ports.get(
                'service_port') is not None:
            return True
        return False

    #end is_junos_service_ports_enabled

    def set_config_state(self):
        try:
            self.nc_q.put_nowait(1)
        except queue.Full:
            pass

    #end

    def nc_handler(self):
        while self.nc_q.get() is not None:
            try:
                self.push_config()
            except Exception as e:
                self._logger.error("Exception: " + str(e))

    #end

    def is_valid_ip(self, ip_str):
        try:
            socket.inet_aton(ip_str)
            return True
        except socket.error:
            return False

    #end

    def init_cs_state(self):
        vn_subnet_set = self._cassandra.get_pr_vn_set(self.uuid)
        for vn_subnet in vn_subnet_set:
            ip = self._cassandra.get(self._cassandra._PR_VN_IP_CF,
                                     self.uuid + ':' + vn_subnet)
            if ip is not None:
                self.vn_ip_map[vn_subnet] = ip['ip_address']

    #end init_cs_state

    def reserve_ip(self, vn_uuid, subnet_prefix):
        try:
            vn = VirtualNetwork()
            vn.set_uuid(vn_uuid)
            ip_addr = self._manager._vnc_lib.virtual_network_ip_alloc(
                vn, subnet=subnet_prefix)
            if ip_addr:
                return ip_addr[0]  #ip_alloc default ip count is 1
        except Exception as e:
            self._logger.error("Exception: %s" % (str(e)))
            return None

    #end

    def free_ip(self, vn_uuid, subnet_prefix, ip_addr):
        try:
            vn = VirtualNetwork()
            vn.set_uuid(vn_uuid)
            self._manager._vnc_lib.virtual_network_ip_free(
                vn, [ip_addr], subnet=subnet_prefix)
            return True
        except Exception as e:
            self._logger.error("Exception: %s" % (str(e)))
            return False

    #end

    def get_vn_irb_ip_map(self):
        irb_ips = {}
        for vn_subnet, ip_addr in self.vn_ip_map.items():
            (vn_uuid, subnet_prefix) = vn_subnet.split(':')
            vn = VirtualNetworkDM.get(vn_uuid)
            if vn_uuid not in irb_ips:
                irb_ips[vn_uuid] = set()
            irb_ips[vn_uuid].add((ip_addr, vn.gateways[subnet_prefix]))
        return irb_ips

    #end get_vn_irb_ip_map

    def evaluate_vn_irb_ip_map(self, vn_set):
        new_vn_ip_set = set()
        for vn_uuid in vn_set:
            vn = VirtualNetworkDM.get(vn_uuid)
            if vn.router_external == True:  #dont need irb ip, gateway ip
                continue
            for subnet_prefix in vn.gateways.keys():
                new_vn_ip_set.add(vn_uuid + ':' + subnet_prefix)

        old_set = set(self.vn_ip_map.keys())
        delete_set = old_set.difference(new_vn_ip_set)
        create_set = new_vn_ip_set.difference(old_set)
        for vn_subnet in delete_set:
            (vn_uuid, subnet_prefix) = vn_subnet.split(':')
            ret = self.free_ip(vn_uuid, subnet_prefix,
                               self.vn_ip_map[vn_subnet])
            if ret == False:
                self._logger.error("Unable to free ip for vn/subnet/pr \
                                  (%s/%s/%s)" %
                                   (vn_uuid, subnet_prefix, self.uuid))
            ret = self._cassandra.delete(
                self._cassandra._PR_VN_IP_CF,
                self.uuid + ':' + vn_uuid + ':' + subnet_prefix)
            if ret == False:
                self._logger.error(
                    "Unable to free ip from db for vn/subnet/pr \
                                  (%s/%s/%s)" %
                    (vn_uuid, subnet_prefix, self.uuid))
                continue
            self._cassandra.delete_from_pr_map(self.uuid, vn_subnet)
            del self.vn_ip_map[vn_subnet]

        for vn_subnet in create_set:
            (vn_uuid, subnet_prefix) = vn_subnet.split(':')
            (sub, length) = subnet_prefix.split('/')
            ip_addr = self.reserve_ip(vn_uuid, subnet_prefix)
            if ip_addr is None:
                self._logger.error("Unable to allocate ip for vn/subnet/pr \
                               (%s/%s/%s)" %
                                   (vn_uuid, subnet_prefix, self.uuid))
                continue
            ret = self._cassandra.add(
                self._cassandra._PR_VN_IP_CF,
                self.uuid + ':' + vn_uuid + ':' + subnet_prefix,
                {'ip_address': ip_addr + '/' + length})
            if ret == False:
                self._logger.error("Unable to store ip for vn/subnet/pr \
                               (%s/%s/%s)" %
                                   (self.uuid, subnet_prefix, self.uuid))
                if self.free_ip(vn_uuid, subnet_prefix, ip_addr) == False:
                    self._logger.error("Unable to free ip for vn/subnet/pr \
                               (%s/%s/%s)" %
                                       (self.uuid, subnet_prefix, self.uuid))
                continue
            self._cassandra.add_to_pr_map(self.uuid, vn_subnet)
            self.vn_ip_map[vn_subnet] = ip_addr + '/' + length

    #end evaluate_vn_irb_ip_map

    def get_vn_li_map(self):
        vn_dict = {}
        for vn_id in self.virtual_networks:
            vn_dict[vn_id] = []

        li_set = self.logical_interfaces
        for pi_uuid in self.physical_interfaces:
            pi = PhysicalInterfaceDM.get(pi_uuid)
            if pi is None:
                continue
            li_set |= pi.logical_interfaces
        for li_uuid in li_set:
            li = LogicalInterfaceDM.get(li_uuid)
            if li is None:
                continue
            vmi_id = li.virtual_machine_interface
            vmi = VirtualMachineInterfaceDM.get(vmi_id)
            if vmi is None:
                continue
            vn_id = vmi.virtual_network
            if vn_id in vn_dict:
                vn_dict[vn_id].append(li.name)
            else:
                vn_dict[vn_id] = [li.name]

    #end

    def push_config(self):
        self.config_manager.reset_bgp_config()
        bgp_router = BgpRouterDM.get(self.bgp_router)
        if bgp_router:
            for peer_uuid, attr in bgp_router.bgp_routers.items():
                peer = BgpRouterDM.get(peer_uuid)
                if peer is None:
                    continue
                external = (bgp_router.params['autonomous_system'] !=
                            peer.params['autonomous_system'])
                self.config_manager.add_bgp_peer(peer.params['address'],
                                                 peer.params, attr, external)
            self.config_manager.set_bgp_config(bgp_router.params)
            self.config_manager.set_global_routing_options(bgp_router.params)
            bgp_router_ips = bgp_router.get_all_bgp_router_ips()
            if self.dataplane_ip is not None and self.is_valid_ip(
                    self.dataplane_ip):
                self.config_manager.add_dynamic_tunnels(
                    self.dataplane_ip, GlobalSystemConfigDM.ip_fabric_subnets,
                    bgp_router_ips)

        vn_dict = self.get_vn_li_map()
        self.evaluate_vn_irb_ip_map(set(vn_dict.keys()))
        vn_irb_ip_map = self.get_vn_irb_ip_map()

        for vn_id, interfaces in vn_dict.items():
            vn_obj = VirtualNetworkDM.get(vn_id)
            if vn_obj is None or vn_obj.vxlan_vni is None or vn_obj.vn_network_id is None:
                continue
            export_set = None
            import_set = None
            for ri_id in vn_obj.routing_instances:
                # Find the primary RI by matching the name
                ri_obj = RoutingInstanceDM.get(ri_id)
                if ri_obj is None:
                    continue
                if ri_obj.fq_name[-1] == vn_obj.fq_name[-1]:
                    vrf_name_l2 = vn_obj.get_vrf_name(vrf_type='l2')
                    vrf_name_l3 = vn_obj.get_vrf_name(vrf_type='l3')
                    export_set = copy.copy(ri_obj.export_targets)
                    import_set = copy.copy(ri_obj.import_targets)
                    for ri2_id in ri_obj.routing_instances:
                        ri2 = RoutingInstanceDM.get(ri2_id)
                        if ri2 is None:
                            continue
                        import_set |= ri2.export_targets

                    if vn_obj.router_external == False:
                        irb_ips = vn_irb_ip_map.get(vn_id, [])
                        self.config_manager.add_routing_instance(
                            vrf_name_l3, import_set, export_set,
                            vn_obj.get_prefixes(), irb_ips,
                            vn_obj.router_external,
                            ["irb" + "." + str(vn_obj.vn_network_id)])
                        self.config_manager.add_routing_instance(
                            vrf_name_l2, import_set, export_set,
                            vn_obj.get_prefixes(), irb_ips,
                            vn_obj.router_external, interfaces,
                            vn_obj.vxlan_vni, None, vn_obj.vn_network_id)
                    else:
                        self.config_manager.add_routing_instance(
                            vrf_name_l3, import_set, export_set,
                            vn_obj.get_prefixes(), None,
                            vn_obj.router_external, interfaces,
                            vn_obj.vxlan_vni, None, vn_obj.vn_network_id)

                    break

            if export_set is not None and self.is_junos_service_ports_enabled(
            ) and len(vn_obj.instance_ip_map) > 0:
                service_port_id = 2 * vn_obj.vn_network_id - 1
                if self.is_service_port_id_valid(service_port_id) == False:
                    self._logger.error(
                        "DM can't allocate service interfaces for \
                                          (vn, vn-id)=(%s,%s)" %
                        (vn_obj.fq_name, vn_obj.vn_network_id))
                else:
                    vrf_name = vrf_name_l3[:123] + '-nat'
                    interfaces = []
                    service_ports = self.junos_service_ports.get(
                        'service_port')
                    interfaces.append(service_ports[0] + "." +
                                      str(service_port_id))
                    interfaces.append(service_ports[0] + "." +
                                      str(service_port_id + 1))
                    self.config_manager.add_routing_instance(
                        vrf_name, import_set, set(), None, None, False,
                        interfaces, None, vn_obj.instance_ip_map,
                        vn_obj.vn_network_id)

        self.config_manager.send_bgp_config()
        self.uve_send()

    # end push_config

    def is_service_port_id_valid(self, service_port_id):
        #mx allowed ifl unit number range is (1, 16385) for service ports
        if service_port_id < 1 or service_port_id > 16384:
            return False
        return True

    #end is_service_port_id_valid

    def uve_send(self, deleted=False):
        pr_trace = UvePhysicalRouterConfig(
            name=self.name,
            ip_address=self.management_ip,
            connected_bgp_router=self.bgp_router,
            auto_conf_enabled=self.vnc_managed,
            product_info=self.vendor + ':' + self.product)
        if deleted:
            pr_trace.deleted = True
            pr_msg = UvePhysicalRouterConfigTrace(
                data=pr_trace, sandesh=PhysicalRouterDM._sandesh)
            pr_msg.send(sandesh=PhysicalRouterDM._sandesh)
            return

        commit_stats = self.config_manager.get_commit_stats()

        if commit_stats['netconf_enabled'] is True:
            pr_trace.last_commit_time = commit_stats['last_commit_time']
            pr_trace.last_commit_duration = commit_stats[
                'last_commit_duration']
            pr_trace.commit_status_message = commit_stats[
                'commit_status_message']
            pr_trace.total_commits_sent_since_up = commit_stats[
                'total_commits_sent_since_up']
        else:
            pr_trace.netconf_enabled_status = commit_stats[
                'netconf_enabled_status']

        pr_msg = UvePhysicalRouterConfigTrace(
            data=pr_trace, sandesh=PhysicalRouterDM._sandesh)
        pr_msg.send(sandesh=PhysicalRouterDM._sandesh)
Пример #7
0
class PhysicalRouterDM(DBBase):
    _dict = {}
    obj_type = 'physical_router'

    def __init__(self, uuid, obj_dict=None):
        self.uuid = uuid
        self.virtual_networks = set()
        self.bgp_router = None
        self.config_manager = None
        self.update(obj_dict)
        self.config_manager = PhysicalRouterConfig(self.management_ip,
                                                   self.user_credentials,
                                                   self.vendor, self.product,
                                                   self.vnc_managed,
                                                   self._logger)

    # end __init__

    def update(self, obj=None):
        if obj is None:
            obj = self.read_obj(self.uuid)
        self.management_ip = obj.get('physical_router_management_ip')
        self.vendor = obj.get('physical_router_vendor_name')
        self.product = obj.get('physical_router_product_name')
        self.vnc_managed = obj.get('physical_router_vnc_managed')
        self.user_credentials = obj.get('physical_router_user_credentials')
        self.update_single_ref('bgp_router', obj)
        self.update_multiple_refs('virtual_network', obj)
        self.physical_interfaces = set(
            [pi['uuid'] for pi in obj.get('physical_interfaces', [])])
        self.logical_interfaces = set(
            [li['uuid'] for li in obj.get('logical_interfaces', [])])
        if self.config_manager is not None:
            self.config_manager.update(self.management_ip,
                                       self.user_credentials, self.vendor,
                                       self.product, self.vnc_managed)

    # end update

    @classmethod
    def delete(cls, uuid):
        if uuid not in cls._dict:
            return
        obj = cls._dict[uuid]
        obj.config_manager.delete_bgp_config()
        obj.update_single_ref('bgp_router', {})
        obj.update_multiple_refs('virtual_network', {})
        del cls._dict[uuid]

    # end delete

    def push_config(self):
        self.config_manager.reset_bgp_config()
        bgp_router = BgpRouterDM.get(self.bgp_router)
        if bgp_router:
            for peer_uuid, params in bgp_router.bgp_routers.items():
                peer = BgpRouterDM.get(peer_uuid)
                if peer is None:
                    continue
                external = (bgp_router.params['autonomous_system'] !=
                            peer.params['autonomous_system'])
                self.config_manager.add_bgp_peer(peer.params['address'],
                                                 params, external)
            self.config_manager.set_bgp_config(bgp_router.params)

        vn_dict = {}
        for vn_id in self.virtual_networks:
            vn_dict[vn_id] = []

        li_set = self.logical_interfaces
        for pi_uuid in self.physical_interfaces:
            pi = PhysicalInterfaceDM.get(pi_uuid)
            if pi is None:
                continue
            li_set |= pi.logical_interfaces
        for li_uuid in li_set:
            li = LogicalInterfaceDM.get(li_uuid)
            if li is None:
                continue
            vmi_id = li.virtual_machine_interface
            vmi = VirtualMachineInterfaceDM.get(vmi_id)
            if vmi is None:
                continue
            vn_id = vmi.virtual_network
            if vn_id in vn_dict:
                vn_dict[vn_id].append(li.name)
            else:
                vn_dict[vn_id] = [li.name]

        for vn_id, interfaces in vn_dict.items():
            vn_obj = VirtualNetworkDM.get(vn_id)
            if vn_obj is None:
                continue
            for ri_id in vn_obj.routing_instances:
                # Find the primary RI by matching the name
                ri_obj = RoutingInstanceDM.get(ri_id)
                if ri_obj is None:
                    continue
                if ri_obj.fq_name[-1] == vn_obj.fq_name[-1]:
                    vrf_name = ':'.join(vn_obj.fq_name)
                    export_set = copy.copy(ri_obj.export_targets)
                    import_set = copy.copy(ri_obj.import_targets)
                    for ri2_id in ri_obj.routing_instances:
                        ri2 = RoutingInstanceDM.get(ri2_id)
                        if ri2 is None:
                            continue
                        import_set |= ri2.export_targets
                        export_set |= ri2.import_targets
                    self.config_manager.add_routing_instance(
                        vrf_name, import_set, export_set, vn_obj.prefixes,
                        vn_obj.gateways, vn_obj.router_external, interfaces,
                        vn_obj.vxlan_vni)
                    break
        self.config_manager.send_bgp_config()
Пример #8
0
class PhysicalRouterDM(DBBase):
    _dict = {}
    obj_type = 'physical_router'

    def __init__(self, uuid, obj_dict=None):
        self.uuid = uuid
        self.virtual_networks = set()
        self.bgp_router = None
        self.config_manager = None
        self.update(obj_dict)
        self.config_manager = PhysicalRouterConfig(
            self.management_ip, self.user_credentials, self.vendor,
            self.product, self.vnc_managed, self._logger)
    # end __init__

    def update(self, obj=None):
        if obj is None:
            obj = self.read_obj(self.uuid)
        self.management_ip = obj.get('physical_router_management_ip')
        self.vendor = obj.get('physical_router_vendor_name')
        self.product = obj.get('physical_router_product_name')
        self.vnc_managed = obj.get('physical_router_vnc_managed')
        self.user_credentials = obj.get('physical_router_user_credentials')
        self.update_single_ref('bgp_router', obj)
        self.update_multiple_refs('virtual_network', obj)
        self.physical_interfaces = set([pi['uuid'] for pi in
                                        obj.get('physical_interfaces', [])])
        self.logical_interfaces = set([li['uuid'] for li in
                                       obj.get('logical_interfaces', [])])
        if self.config_manager is not None:
            self.config_manager.update(
                self.management_ip, self.user_credentials, self.vendor,
                self.product, self.vnc_managed)
    # end update

    @classmethod
    def delete(cls, uuid):
        if uuid not in cls._dict:
            return
        obj = cls._dict[uuid]
        obj.config_manager.delete_bgp_config()
        obj.update_single_ref('bgp_router', {})
        obj.update_multiple_refs('virtual_network', {})
        del cls._dict[uuid]
    # end delete

    def push_config(self):
        self.config_manager.reset_bgp_config()
        bgp_router = BgpRouterDM.get(self.bgp_router)
        if bgp_router:
            for peer_uuid, params in bgp_router.bgp_routers.items():
                peer = BgpRouterDM.get(peer_uuid)
                if peer is None:
                    continue
                external = (bgp_router.params['autonomous_system'] !=
                            peer.params['autonomous_system'])
                self.config_manager.add_bgp_peer(peer.params['address'],
                                                 params, external)
            self.config_manager.set_bgp_config(bgp_router.params)

        vn_dict = {}
        for vn_id in self.virtual_networks:
            vn_dict[vn_id] = []

        li_set = self.logical_interfaces
        for pi_uuid in self.physical_interfaces:
            pi = PhysicalInterfaceDM.get(pi_uuid)
            if pi is None:
                continue
            li_set |= pi.logical_interfaces
        for li_uuid in li_set:
            li = LogicalInterfaceDM.get(li_uuid)
            if li is None:
                continue
            vmi_id = li.virtual_machine_interface
            vmi = VirtualMachineInterfaceDM.get(vmi_id)
            if vmi is None:
                continue
            vn_id = vmi.virtual_network
            if vn_id in vn_dict:
                vn_dict[vn_id].append(li.name)
            else:
                vn_dict[vn_id] = [li.name]

        for vn_id, interfaces in vn_dict.items():
            vn_obj = VirtualNetworkDM.get(vn_id)
            if vn_obj is None:
                continue
            for ri_id in vn_obj.routing_instances:
                # Find the primary RI by matching the name
                ri_obj = RoutingInstanceDM.get(ri_id)
                if ri_obj is None:
                    continue
                if ri_obj.fq_name[-1] == vn_obj.fq_name[-1]:
                    vrf_name = ':'.join(vn_obj.fq_name)
                    export_set = copy.copy(ri_obj.export_targets)
                    import_set = copy.copy(ri_obj.import_targets)
                    for ri2_id in ri_obj.routing_instances:
                        ri2 = RoutingInstanceDM.get(ri2_id)
                        if ri2 is None:
                            continue
                        import_set |= ri2.export_targets
                        export_set |= ri2.import_targets
                    self.config_manager.add_routing_instance(vrf_name,
                                                             import_set,
                                                             export_set,
                                                             vn_obj.prefixes,
                                                             vn_obj.gateways,
                                                             vn_obj.router_external,
                                                             interfaces,
                                                             vn_obj.vxlan_vni)
                    break
        self.config_manager.send_bgp_config()
Пример #9
0
class PhysicalRouterDM(DBBaseDM):
    _dict = {}
    obj_type = 'physical_router'
    _sandesh = None

    def __init__(self, uuid, obj_dict=None):
        self.uuid = uuid
        self.virtual_networks = set()
        self.bgp_router = None
        self.config_manager = None
        self.nc_q = queue.Queue(maxsize=1)
        self.nc_handler_gl = gevent.spawn(self.nc_handler)
        self.vn_ip_map = {}
        self.init_cs_state()
        self.update(obj_dict)
        self.config_manager = PhysicalRouterConfig(
            self.management_ip, self.user_credentials, self.vendor,
            self.product, self.vnc_managed, self._logger)
        self.uve_send()
    # end __init__

    def update(self, obj=None):
        if obj is None:
            obj = self.read_obj(self.uuid)
        self.name = obj['fq_name'][-1]
        self.management_ip = obj.get('physical_router_management_ip')
        self.dataplane_ip = obj.get('physical_router_dataplane_ip')
        self.vendor = obj.get('physical_router_vendor_name', '')
        self.product = obj.get('physical_router_product_name', '')
        self.vnc_managed = obj.get('physical_router_vnc_managed')
        self.user_credentials = obj.get('physical_router_user_credentials')
        self.junos_service_ports = obj.get('physical_router_junos_service_ports')
        self.update_single_ref('bgp_router', obj)
        self.update_multiple_refs('virtual_network', obj)
        self.physical_interfaces = set([pi['uuid'] for pi in
                                        obj.get('physical_interfaces', [])])
        self.logical_interfaces = set([li['uuid'] for li in
                                       obj.get('logical_interfaces', [])])
        if self.config_manager is not None:
            self.config_manager.update(
                self.management_ip, self.user_credentials, self.vendor,
                self.product, self.vnc_managed)
    # end update

    @classmethod
    def delete(cls, uuid):
        if uuid not in cls._dict:
            return
        obj = cls._dict[uuid]
        obj._cassandra.delete_pr(uuid) 
        obj.config_manager.delete_bgp_config()
        obj.uve_send(True)
        obj.update_single_ref('bgp_router', {})
        obj.update_multiple_refs('virtual_network', {})
        del cls._dict[uuid]
    # end delete

    def is_junos_service_ports_enabled(self):
        if self.junos_service_ports is not None and self.junos_service_ports.get('service_port') is not None:
            return True
        return False
    #end is_junos_service_ports_enabled

    def set_config_state(self):
        try:
            self.nc_q.put_nowait(1)
        except queue.Full:
            pass
    #end

    def nc_handler(self):
        while self.nc_q.get() is not None:
            try:
                self.push_config()
            except Exception as e:
                self._logger.error("Exception: " + str(e))
    #end

    def is_valid_ip(self, ip_str):
        try:
            socket.inet_aton(ip_str)
            return True
        except socket.error:
            return False
    #end

    def init_cs_state(self):
        vn_subnet_set = self._cassandra.get_pr_vn_set(self.uuid)
        for vn_subnet in vn_subnet_set:
            ip = self._cassandra.get(self._cassandra._PR_VN_IP_CF,
                                     self.uuid + ':' + vn_subnet)
            if ip is not None:
                self.vn_ip_map[vn_subnet] = ip['ip_address']
    #end init_cs_state

    def reserve_ip(self, vn_uuid, subnet_prefix):
        try:
            vn = VirtualNetwork()
            vn.set_uuid(vn_uuid)
            ip_addr = self._manager._vnc_lib.virtual_network_ip_alloc(
                                                       vn,
                                                       subnet=subnet_prefix)
            if ip_addr:
                return ip_addr[0] #ip_alloc default ip count is 1
        except Exception as e:
            self._logger.error("Exception: %s" %(str(e)))
            return None
    #end

    def free_ip(self, vn_uuid, subnet_prefix, ip_addr):
        try:
            vn = VirtualNetwork()
            vn.set_uuid(vn_uuid)
            self._manager._vnc_lib.virtual_network_ip_free(
                                                     vn,
                                                     [ip_addr],
                                                     subnet=subnet_prefix)
            return True
        except Exception as e:
            self._logger.error("Exception: %s" %(str(e)))
            return False
    #end

    def get_vn_irb_ip_map(self):
        irb_ips = {}
        for vn_subnet, ip_addr in self.vn_ip_map.items():
            (vn_uuid, subnet_prefix) = vn_subnet.split(':')
            vn = VirtualNetworkDM.get(vn_uuid)
            if vn_uuid not in irb_ips:
                irb_ips[vn_uuid] = set()
            irb_ips[vn_uuid].add((ip_addr, vn.gateways[subnet_prefix]))
        return irb_ips
    #end get_vn_irb_ip_map

    def evaluate_vn_irb_ip_map(self, vn_set):
        new_vn_ip_set = set()
        for vn_uuid in vn_set:
            vn = VirtualNetworkDM.get(vn_uuid)
            if vn.forwarding_mode != 'l2_l3': #dont need irb ip, gateway ip
                continue
            for subnet_prefix in vn.gateways.keys():
                new_vn_ip_set.add(vn_uuid + ':' + subnet_prefix)

        old_set = set(self.vn_ip_map.keys())
        delete_set = old_set.difference(new_vn_ip_set)
        create_set = new_vn_ip_set.difference(old_set)
        for vn_subnet in delete_set:
            (vn_uuid, subnet_prefix) = vn_subnet.split(':')
            ret = self.free_ip(vn_uuid, subnet_prefix, self.vn_ip_map[vn_subnet])
            if ret == False:
                self._logger.error("Unable to free ip for vn/subnet/pr \
                                  (%s/%s/%s)" %(vn_uuid, subnet_prefix, self.uuid))
            ret = self._cassandra.delete(self._cassandra._PR_VN_IP_CF,
                       self.uuid + ':' + vn_uuid + ':' + subnet_prefix)
            if ret == False:
                self._logger.error("Unable to free ip from db for vn/subnet/pr \
                                  (%s/%s/%s)" %(vn_uuid, subnet_prefix, self.uuid))
                continue
            self._cassandra.delete_from_pr_map(self.uuid, vn_subnet)
            del self.vn_ip_map[vn_subnet]

        for vn_subnet in create_set:
            (vn_uuid, subnet_prefix) = vn_subnet.split(':')
            (sub, length) = subnet_prefix.split('/')
            ip_addr = self.reserve_ip(vn_uuid, subnet_prefix)
            if ip_addr is None:
                self._logger.error("Unable to allocate ip for vn/subnet/pr \
                               (%s/%s/%s)" %(vn_uuid, subnet_prefix, self.uuid))
                continue
            ret = self._cassandra.add(self._cassandra._PR_VN_IP_CF,
                            self.uuid + ':' + vn_uuid + ':' + subnet_prefix,
                            {'ip_address': ip_addr + '/' + length})
            if ret == False:
                self._logger.error("Unable to store ip for vn/subnet/pr \
                               (%s/%s/%s)" %(self.uuid, subnet_prefix, self.uuid))
                if self.free_ip(vn_uuid, subnet_prefix, ip_addr) == False:
                    self._logger.error("Unable to free ip for vn/subnet/pr \
                               (%s/%s/%s)" %(self.uuid, subnet_prefix, self.uuid))
                continue
            self._cassandra.add_to_pr_map(self.uuid, vn_subnet)
            self.vn_ip_map[vn_subnet] = ip_addr + '/' + length
    #end evaluate_vn_irb_ip_map

    def get_vn_li_map(self):
        vn_dict = {}
        for vn_id in self.virtual_networks:
            vn_dict[vn_id] = []

        li_set = self.logical_interfaces
        for pi_uuid in self.physical_interfaces:
            pi = PhysicalInterfaceDM.get(pi_uuid)
            if pi is None:
                continue
            li_set |= pi.logical_interfaces
        for li_uuid in li_set:
            li = LogicalInterfaceDM.get(li_uuid)
            if li is None:
                continue
            vmi_id = li.virtual_machine_interface
            vmi = VirtualMachineInterfaceDM.get(vmi_id)
            if vmi is None:
                continue
            vn_id = vmi.virtual_network
            if vn_id in vn_dict:
                vn_dict[vn_id].append(li.name)
            else:
                vn_dict[vn_id] = [li.name]
        return vn_dict
    #end

    def push_config(self):
        self.config_manager.reset_bgp_config()
        bgp_router = BgpRouterDM.get(self.bgp_router)
        if bgp_router:
            for peer_uuid, attr in bgp_router.bgp_routers.items():
                peer = BgpRouterDM.get(peer_uuid)
                if peer is None:
                    continue
                external = (bgp_router.params['autonomous_system'] !=
                            peer.params['autonomous_system'])
                self.config_manager.add_bgp_peer(peer.params['address'],
                                                 peer.params, attr, external)
            self.config_manager.set_bgp_config(bgp_router.params)
            self.config_manager.set_global_routing_options(bgp_router.params)
            bgp_router_ips = bgp_router.get_all_bgp_router_ips()
            if self.dataplane_ip is not None and self.is_valid_ip(self.dataplane_ip):
                self.config_manager.add_dynamic_tunnels(self.dataplane_ip,
                              GlobalSystemConfigDM.ip_fabric_subnets, bgp_router_ips)

        vn_dict = self.get_vn_li_map()
        self.evaluate_vn_irb_ip_map(set(vn_dict.keys()))
        vn_irb_ip_map = self.get_vn_irb_ip_map()

        for vn_id, interfaces in vn_dict.items():
            vn_obj = VirtualNetworkDM.get(vn_id)
            if vn_obj is None or vn_obj.vxlan_vni is None or vn_obj.vn_network_id is None:
                continue
            export_set = None
            import_set = None
            for ri_id in vn_obj.routing_instances:
                # Find the primary RI by matching the name
                ri_obj = RoutingInstanceDM.get(ri_id)
                if ri_obj is None:
                    continue
                if ri_obj.fq_name[-1] == vn_obj.fq_name[-1]:
                    vrf_name_l2 = vn_obj.get_vrf_name(vrf_type='l2')
                    vrf_name_l3 = vn_obj.get_vrf_name(vrf_type='l3')
                    export_set = copy.copy(ri_obj.export_targets)
                    import_set = copy.copy(ri_obj.import_targets)
                    for ri2_id in ri_obj.routing_instances:
                        ri2 = RoutingInstanceDM.get(ri2_id)
                        if ri2 is None:
                            continue
                        import_set |= ri2.export_targets

                    if vn_obj.forwarding_mode in ['l2', 'l2_l3']:
                        irb_ips = None
                        if vn_obj.forwarding_mode == 'l2_l3':
                            irb_ips = vn_irb_ip_map.get(vn_id, [])
                        self.config_manager.add_routing_instance(vrf_name_l2,
                                                             True,
                                                             vn_obj.forwarding_mode == 'l2_l3',
                                                             import_set,
                                                             export_set,
                                                             vn_obj.get_prefixes(),
                                                             irb_ips,
                                                             vn_obj.router_external,
                                                             interfaces,
                                                             vn_obj.vxlan_vni,
                                                             None, vn_obj.vn_network_id)

                    if vn_obj.forwarding_mode in ['l3', 'l2_l3']:
                        self.config_manager.add_routing_instance(vrf_name_l3,
                                                             False,
                                                             vn_obj.forwarding_mode == 'l2_l3',
                                                             import_set,
                                                             export_set,
                                                             vn_obj.get_prefixes(),
                                                             None,
                                                             vn_obj.router_external,
                                                             ["irb" + "." + str(vn_obj.vn_network_id)])

                    break

            if export_set is not None and self.is_junos_service_ports_enabled() and len(vn_obj.instance_ip_map) > 0:
                service_port_id = 2*vn_obj.vn_network_id - 1
                if self.is_service_port_id_valid(service_port_id) == False:
                    self._logger.error("DM can't allocate service interfaces for \
                                          (vn, vn-id)=(%s,%s)" % (vn_obj.fq_name, vn_obj.vn_network_id))
                else:
                    vrf_name = vrf_name_l3[:123] + '-nat'
                    interfaces = []
                    service_ports = self.junos_service_ports.get('service_port')
                    interfaces.append(service_ports[0] + "." + str(service_port_id))
                    interfaces.append(service_ports[0] + "." + str(service_port_id + 1))
                    self.config_manager.add_routing_instance(vrf_name,
                                                         False,
                                                         False,
                                                         import_set,
                                                         set(),
                                                         None,
                                                         None,
                                                         False,
                                                         interfaces,
                                                         None,
                                                         vn_obj.instance_ip_map, vn_obj.vn_network_id)

        self.config_manager.send_bgp_config()
        self.uve_send()
    # end push_config

    def is_service_port_id_valid(self, service_port_id):
        #mx allowed ifl unit number range is (1, 16385) for service ports
        if service_port_id < 1 or service_port_id > 16384:
            return False
        return True  
    #end is_service_port_id_valid

    def uve_send(self, deleted=False):
        pr_trace = UvePhysicalRouterConfig(name=self.name,
                                           ip_address=self.management_ip,
                                           connected_bgp_router=self.bgp_router,
                                           auto_conf_enabled=self.vnc_managed,
                                           product_info=self.vendor + ':' + self.product)
        if deleted:
            pr_trace.deleted = True
            pr_msg = UvePhysicalRouterConfigTrace(data=pr_trace,
                                                  sandesh=PhysicalRouterDM._sandesh)
            pr_msg.send(sandesh=PhysicalRouterDM._sandesh)
            return

        commit_stats = self.config_manager.get_commit_stats()

        if commit_stats['netconf_enabled'] is True:
            pr_trace.last_commit_time = commit_stats['last_commit_time']
            pr_trace.last_commit_duration = commit_stats['last_commit_duration']
            pr_trace.commit_status_message = commit_stats['commit_status_message']
            pr_trace.total_commits_sent_since_up = commit_stats['total_commits_sent_since_up']
        else:
            pr_trace.netconf_enabled_status = commit_stats['netconf_enabled_status']

        pr_msg = UvePhysicalRouterConfigTrace(data=pr_trace, sandesh=PhysicalRouterDM._sandesh)
        pr_msg.send(sandesh=PhysicalRouterDM._sandesh)
Пример #10
0
class PhysicalRouterDM(DBBase):
    _dict = {}
    obj_type = 'physical_router'

    def __init__(self, uuid, obj_dict=None):
        self.uuid = uuid
        self.virtual_networks = set()
        self.bgp_router = None
        self.config_manager = None
        self.update(obj_dict)
        self.config_manager = PhysicalRouterConfig(
            self.management_ip, self.user_credentials, self.vendor,
            self.product, self.vnc_managed, self._logger)
    # end __init__

    def update(self, obj=None):
        if obj is None:
            obj = self.read_obj(self.uuid)
        self.management_ip = obj.get('physical_router_management_ip')
        self.dataplane_ip = obj.get('physical_router_dataplane_ip')
        self.vendor = obj.get('physical_router_vendor_name')
        self.product = obj.get('physical_router_product_name')
        self.vnc_managed = obj.get('physical_router_vnc_managed')
        self.user_credentials = obj.get('physical_router_user_credentials')
        self.junos_service_ports = obj.get('physical_router_junos_service_ports')
        self.update_single_ref('bgp_router', obj)
        self.update_multiple_refs('virtual_network', obj)
        self.physical_interfaces = set([pi['uuid'] for pi in
                                        obj.get('physical_interfaces', [])])
        self.logical_interfaces = set([li['uuid'] for li in
                                       obj.get('logical_interfaces', [])])
        if self.config_manager is not None:
            self.config_manager.update(
                self.management_ip, self.user_credentials, self.vendor,
                self.product, self.vnc_managed)
    # end update

    @classmethod
    def delete(cls, uuid):
        if uuid not in cls._dict:
            return
        obj = cls._dict[uuid]
        obj.config_manager.delete_bgp_config()
        obj.update_single_ref('bgp_router', {})
        obj.update_multiple_refs('virtual_network', {})
        del cls._dict[uuid]
    # end delete

    def is_junos_service_ports_enabled(self):
        if self.junos_service_ports is not None and self.junos_service_ports.get('service_port') is not None:
            return True
        return False
    #end is_junos_service_ports_enabled

    def push_config(self):
        self.config_manager.reset_bgp_config()
        bgp_router = BgpRouterDM.get(self.bgp_router)
        if bgp_router:
            for peer_uuid, params in bgp_router.bgp_routers.items():
                peer = BgpRouterDM.get(peer_uuid)
                if peer is None:
                    continue
                external = (bgp_router.params['autonomous_system'] !=
                            peer.params['autonomous_system'])
                self.config_manager.add_bgp_peer(peer.params['address'],
                                                 params, external)
            self.config_manager.set_bgp_config(bgp_router.params)
            bgp_router_ips = bgp_router.get_all_bgp_router_ips()
            if self.dataplane_ip is not None:
                self.config_manager.add_dynamic_tunnels(self.dataplane_ip,
                              GlobalSystemConfigDM.ip_fabric_subnets, bgp_router_ips)
        vn_dict = {}
        for vn_id in self.virtual_networks:
            vn_dict[vn_id] = []

        li_set = self.logical_interfaces
        for pi_uuid in self.physical_interfaces:
            pi = PhysicalInterfaceDM.get(pi_uuid)
            if pi is None:
                continue
            li_set |= pi.logical_interfaces
        for li_uuid in li_set:
            li = LogicalInterfaceDM.get(li_uuid)
            if li is None:
                continue
            vmi_id = li.virtual_machine_interface
            vmi = VirtualMachineInterfaceDM.get(vmi_id)
            if vmi is None:
                continue
            vn_id = vmi.virtual_network
            if vn_id in vn_dict:
                vn_dict[vn_id].append(li.name)
            else:
                vn_dict[vn_id] = [li.name]

        #for now, assume service port ifls unit numbers are always starts with 0 and goes on
        service_port_id = 0
        for vn_id, interfaces in vn_dict.items():
            vn_obj = VirtualNetworkDM.get(vn_id)
            if vn_obj is None:
                continue
            export_set = None
            import_set = None
            for ri_id in vn_obj.routing_instances:
                # Find the primary RI by matching the name
                ri_obj = RoutingInstanceDM.get(ri_id)
                if ri_obj is None:
                    continue
                if ri_obj.fq_name[-1] == vn_obj.fq_name[-1]:
                    vrf_name = vn_obj.get_vrf_name()
                    export_set = copy.copy(ri_obj.export_targets)
                    import_set = copy.copy(ri_obj.import_targets)
                    for ri2_id in ri_obj.routing_instances:
                        ri2 = RoutingInstanceDM.get(ri2_id)
                        if ri2 is None:
                            continue
                        import_set |= ri2.export_targets
                        export_set |= ri2.import_targets
                    self.config_manager.add_routing_instance(vrf_name,
                                                             import_set,
                                                             export_set,
                                                             vn_obj.prefixes,
                                                             vn_obj.gateways,
                                                             vn_obj.router_external,
                                                             interfaces,
                                                             vn_obj.vxlan_vni)

                    break

            if export_set is not None and self.is_junos_service_ports_enabled() and len(vn_obj.instance_ip_map) > 0:
                vrf_name = vrf_name[:123] + '-nat'
                interfaces = []
                service_ports = self.junos_service_ports.get('service_port')
                interfaces.append(service_ports[0] + "." + str(service_port_id))
                service_port_id  = service_port_id + 1
                interfaces.append(service_ports[0] + "." + str(service_port_id))
                service_port_id  = service_port_id + 1
                self.config_manager.add_routing_instance(vrf_name,
                                                         import_set,
                                                         export_set,
                                                         None,
                                                         None,
                                                         False,
                                                         interfaces,
                                                         None,
                                                         vn_obj.instance_ip_map)

        self.config_manager.send_bgp_config()
Пример #11
0
class PhysicalRouterDM(DBBase):
    _dict = {}
    obj_type = 'physical_router'

    def __init__(self, uuid, obj_dict=None):
        self.uuid = uuid
        self.virtual_networks = set()
        self.bgp_router = None
        self.update(obj_dict)
        self.config_manager = PhysicalRouterConfig(
            self.management_ip, self.user_credentials,
            self._device_manager._sandesh.logger())
    # end __init__

    def update(self, obj=None):
        if obj is None:
            obj = self.read_obj(self.uuid)
        self.management_ip = obj.get('physical_router_management_ip')
        self.vendor = obj.get('physical_router_vendor_name')
        self.user_credentials = obj.get('physical_router_user_credentials')
        self.update_single_ref('bgp_router', obj)
        self.update_multiple_refs('virtual_network', obj)
        self.physical_interfaces = set([pi['uuid'] for pi in
                                        obj.get('physical_interfaces', [])])
        self.logical_interfaces = set([li['uuid'] for li in
                                       obj.get('logical_interfaces', [])])
    # end update

    @classmethod
    def delete(cls, uuid):
        if uuid not in cls._dict:
            return
        obj = cls._dict[uuid]
        obj.config_manager.delete_bgp_config()
        obj.update_single_ref('bgp_router', {})
        obj.update_multiple_refs('virtual_network', {})
        del cls._dict[uuid]
    # end delete

    def push_config(self):
        self.config_manager.reset_bgp_config()
        bgp_router = BgpRouterDM.get(self.bgp_router)
        if bgp_router:
            for peer_uuid, params in bgp_router.bgp_routers.items():
                peer = BgpRouterDM.get(peer_uuid)
                if peer is None:
                    continue
                self.config_manager.add_bgp_peer(peer.params.address, params)
            self.config_manager.set_bgp_config(bgp_router.params)

        vn_dict = {}
        for vn_id in self.virtual_networks:
            vn_dict[vn_id] = []

        li_set = self.logical_interfaces
        for pi_uuid in self.physical_interfaces:
            pi = PhysicalInterfaceDM.get(pi_uuid)
            if pi is None:
                continue
            li_set |= pi.logical_interfaces
        for li_uuid in li_set:
            li = LogicalInterfaceDM.get(li_uuid)
            if li is None:
                continue
            vmi_id = li.virtual_machine_interface
            vmi = VirtualMachineInterfaceDM.get(vmi_id)
            if vmi is None:
                continue
            vn_id = vmi.virtual_network
            if vn_id in vn_dict:
                vn_dict[vn_id].append(li.name)
            else:
                vn_dict[vn_id] = [li.name]

        for vn_id, interfaces in vn_dict.items():
            vn_obj = VirtualNetworkDM.get(vn_id)
            if vn_obj is None:
                continue
            for ri in vn_obj.routing_instances:
                # Find the primary RI by matching the name
                ri_obj = self.read_obj(ri, 'routing_instance')
                if ri_obj['fq_name'][-1] == vn_obj.fq_name[-1]:
                    vrf_name = ':'.join(vn_obj.fq_name)
                    rt = ri_obj['route_target_refs'][0]['to'][0]
                    self.config_manager.add_routing_instance(vrf_name, rt,
                                                             vn_obj.prefixes,
                                                             interfaces)
                    break
        self.config_manager.send_bgp_config()
Пример #12
0
class PhysicalRouterDM(DBBaseDM):
    _dict = {}
    obj_type = "physical_router"
    _sandesh = None

    def __init__(self, uuid, obj_dict=None):
        self.uuid = uuid
        self.virtual_networks = set()
        self.bgp_router = None
        self.config_manager = None
        self.nc_q = queue.Queue(maxsize=1)
        self.nc_handler_gl = gevent.spawn(self.nc_handler)
        self.update(obj_dict)
        self.config_manager = PhysicalRouterConfig(
            self.management_ip, self.user_credentials, self.vendor, self.product, self.vnc_managed, self._logger
        )
        self.uve_send()

    # end __init__

    def update(self, obj=None):
        if obj is None:
            obj = self.read_obj(self.uuid)
        self.name = obj["fq_name"][-1]
        self.management_ip = obj.get("physical_router_management_ip")
        self.dataplane_ip = obj.get("physical_router_dataplane_ip")
        self.vendor = obj.get("physical_router_vendor_name", "")
        self.product = obj.get("physical_router_product_name", "")
        self.vnc_managed = obj.get("physical_router_vnc_managed")
        self.user_credentials = obj.get("physical_router_user_credentials")
        self.junos_service_ports = obj.get("physical_router_junos_service_ports")
        self.update_single_ref("bgp_router", obj)
        self.update_multiple_refs("virtual_network", obj)
        self.physical_interfaces = set([pi["uuid"] for pi in obj.get("physical_interfaces", [])])
        self.logical_interfaces = set([li["uuid"] for li in obj.get("logical_interfaces", [])])
        if self.config_manager is not None:
            self.config_manager.update(
                self.management_ip, self.user_credentials, self.vendor, self.product, self.vnc_managed
            )

    # end update

    @classmethod
    def delete(cls, uuid):
        if uuid not in cls._dict:
            return
        obj = cls._dict[uuid]
        obj.config_manager.delete_bgp_config()
        obj.uve_send(True)
        obj.update_single_ref("bgp_router", {})
        obj.update_multiple_refs("virtual_network", {})
        del cls._dict[uuid]

    # end delete

    def is_junos_service_ports_enabled(self):
        if self.junos_service_ports is not None and self.junos_service_ports.get("service_port") is not None:
            return True
        return False

    # end is_junos_service_ports_enabled

    def set_config_state(self):
        try:
            self.nc_q.put_nowait(1)
        except queue.Full:
            pass

    # end

    def nc_handler(self):
        while self.nc_q.get() is not None:
            try:
                self.push_config()
            except Exception as e:
                self._logger.error("Exception: " + str(e))

    # end

    def is_valid_ip(self, ip_str):
        try:
            socket.inet_aton(ip_str)
            return True
        except socket.error:
            return False

    # end

    def push_config(self):
        self.config_manager.reset_bgp_config()
        bgp_router = BgpRouterDM.get(self.bgp_router)
        if bgp_router:
            for peer_uuid, attr in bgp_router.bgp_routers.items():
                peer = BgpRouterDM.get(peer_uuid)
                if peer is None:
                    continue
                external = bgp_router.params["autonomous_system"] != peer.params["autonomous_system"]
                self.config_manager.add_bgp_peer(peer.params["address"], peer.params, attr, external)
            self.config_manager.set_bgp_config(bgp_router.params)
            self.config_manager.set_global_routing_options(bgp_router.params)
            bgp_router_ips = bgp_router.get_all_bgp_router_ips()
            if self.dataplane_ip is not None and self.is_valid_ip(self.dataplane_ip):
                self.config_manager.add_dynamic_tunnels(
                    self.dataplane_ip, GlobalSystemConfigDM.ip_fabric_subnets, bgp_router_ips
                )
        vn_dict = {}
        for vn_id in self.virtual_networks:
            vn_dict[vn_id] = []

        li_set = self.logical_interfaces
        for pi_uuid in self.physical_interfaces:
            pi = PhysicalInterfaceDM.get(pi_uuid)
            if pi is None:
                continue
            li_set |= pi.logical_interfaces
        for li_uuid in li_set:
            li = LogicalInterfaceDM.get(li_uuid)
            if li is None:
                continue
            vmi_id = li.virtual_machine_interface
            vmi = VirtualMachineInterfaceDM.get(vmi_id)
            if vmi is None:
                continue
            vn_id = vmi.virtual_network
            if vn_id in vn_dict:
                vn_dict[vn_id].append(li.name)
            else:
                vn_dict[vn_id] = [li.name]

        for vn_id, interfaces in vn_dict.items():
            vn_obj = VirtualNetworkDM.get(vn_id)
            if vn_obj is None or vn_obj.vxlan_vni is None or vn_obj.vn_network_id is None:
                continue
            export_set = None
            import_set = None
            for ri_id in vn_obj.routing_instances:
                # Find the primary RI by matching the name
                ri_obj = RoutingInstanceDM.get(ri_id)
                if ri_obj is None:
                    continue
                if ri_obj.fq_name[-1] == vn_obj.fq_name[-1]:
                    vrf_name_l2 = vn_obj.get_vrf_name(vrf_type="l2")
                    vrf_name_l3 = vn_obj.get_vrf_name(vrf_type="l3")
                    export_set = copy.copy(ri_obj.export_targets)
                    import_set = copy.copy(ri_obj.import_targets)
                    for ri2_id in ri_obj.routing_instances:
                        ri2 = RoutingInstanceDM.get(ri2_id)
                        if ri2 is None:
                            continue
                        import_set |= ri2.export_targets

                    if vn_obj.router_external == False:
                        self.config_manager.add_routing_instance(
                            vrf_name_l3,
                            import_set,
                            export_set,
                            vn_obj.prefixes,
                            vn_obj.gateways,
                            vn_obj.router_external,
                            ["irb" + "." + str(vn_obj.vn_network_id)],
                        )
                        self.config_manager.add_routing_instance(
                            vrf_name_l2,
                            import_set,
                            export_set,
                            vn_obj.prefixes,
                            vn_obj.gateways,
                            vn_obj.router_external,
                            interfaces,
                            vn_obj.vxlan_vni,
                            None,
                            vn_obj.vn_network_id,
                        )
                    else:
                        self.config_manager.add_routing_instance(
                            vrf_name_l3,
                            import_set,
                            export_set,
                            vn_obj.prefixes,
                            vn_obj.gateways,
                            vn_obj.router_external,
                            interfaces,
                            vn_obj.vxlan_vni,
                            None,
                            vn_obj.vn_network_id,
                        )

                    break

            if export_set is not None and self.is_junos_service_ports_enabled() and len(vn_obj.instance_ip_map) > 0:
                service_port_id = 2 * vn_obj.vn_network_id - 1
                if self.is_service_port_id_valid(service_port_id) == False:
                    self._logger.error(
                        "DM can't allocate service interfaces for \
                                          (vn, vn-id)=(%s,%s)"
                        % (vn_obj.fq_name, vn_obj.vn_network_id)
                    )
                else:
                    vrf_name = vrf_name_l3[:123] + "-nat"
                    interfaces = []
                    service_ports = self.junos_service_ports.get("service_port")
                    interfaces.append(service_ports[0] + "." + str(service_port_id))
                    interfaces.append(service_ports[0] + "." + str(service_port_id + 1))
                    self.config_manager.add_routing_instance(
                        vrf_name,
                        import_set,
                        set(),
                        None,
                        None,
                        False,
                        interfaces,
                        None,
                        vn_obj.instance_ip_map,
                        vn_obj.vn_network_id,
                    )

        self.config_manager.send_bgp_config()
        self.uve_send()

    # end push_config

    def is_service_port_id_valid(self, service_port_id):
        # mx allowed ifl unit number range is (1, 16385) for service ports
        if service_port_id < 1 or service_port_id > 16384:
            return False
        return True

    # end is_service_port_id_valid

    def uve_send(self, deleted=False):
        pr_trace = UvePhysicalRouterConfig(
            name=self.name,
            ip_address=self.management_ip,
            connected_bgp_router=self.bgp_router,
            auto_conf_enabled=self.vnc_managed,
            product_info=self.vendor + ":" + self.product,
        )
        if deleted:
            pr_trace.deleted = True
            pr_msg = UvePhysicalRouterConfigTrace(data=pr_trace, sandesh=PhysicalRouterDM._sandesh)
            pr_msg.send(sandesh=PhysicalRouterDM._sandesh)
            return

        commit_stats = self.config_manager.get_commit_stats()

        if commit_stats["netconf_enabled"] is True:
            pr_trace.last_commit_time = commit_stats["last_commit_time"]
            pr_trace.last_commit_duration = commit_stats["last_commit_duration"]
            pr_trace.commit_status_message = commit_stats["commit_status_message"]
            pr_trace.total_commits_sent_since_up = commit_stats["total_commits_sent_since_up"]
        else:
            pr_trace.netconf_enabled_status = commit_stats["netconf_enabled_status"]

        pr_msg = UvePhysicalRouterConfigTrace(data=pr_trace, sandesh=PhysicalRouterDM._sandesh)
        pr_msg.send(sandesh=PhysicalRouterDM._sandesh)