def main(args_str=None):
    global _zookeeper_client

    if not args_str:
        args_str = ' '.join(sys.argv[1:])
    args = parse_args(args_str)
    if args.cluster_id:
        client_pfx = args.cluster_id + '-'
        zk_path_pfx = args.cluster_id + '/'
    else:
        client_pfx = ''
        zk_path_pfx = ''

    # randomize collector list
    args.random_collectors = args.collectors
    if args.collectors:
        args.random_collectors = random.sample(args.collectors,
                                               len(args.collectors))

    # Initialize logger without introspect thread
    dm_logger = DeviceManagerLogger(args, http_server_port=-1)

    # Initialize AMQP handler then close it to be sure remain queue of a
    # precedent run is cleaned
    vnc_amqp = DMAmqpHandle(dm_logger, DeviceManager.REACTION_MAP, args)
    vnc_amqp.establish()
    vnc_amqp.close()
    dm_logger.debug("Removed remained AMQP queue")

    _zookeeper_client = ZookeeperClient(client_pfx+"device-manager",
                                        args.zk_server_ip)
    dm_logger.notice("Waiting to be elected as master...")
    _zookeeper_client.master_election(zk_path_pfx+"/device-manager",
                                      os.getpid(), run_device_manager,
                                      dm_logger, args)
def main(args_str=None):
    global _zookeeper_client

    if not args_str:
        args_str = ' '.join(sys.argv[1:])
    args = parse_args(args_str)
    if args.cluster_id:
        client_pfx = args.cluster_id + '-'
        zk_path_pfx = args.cluster_id + '/'
    else:
        client_pfx = ''
        zk_path_pfx = ''

    # randomize collector list
    args.random_collectors = args.collectors
    if args.collectors:
        args.random_collectors = random.sample(args.collectors,
                                               len(args.collectors))

    # Initialize logger without introspect thread
    dm_logger = DeviceManagerLogger(args, http_server_port=-1)

    # Initialize AMQP handler then close it to be sure remain queue of a
    # precedent run is cleaned
    vnc_amqp = DMAmqpHandle(dm_logger, DeviceManager.REACTION_MAP, args)
    vnc_amqp.establish()
    vnc_amqp.close()
    dm_logger.debug("Removed remained AMQP queue")

    _zookeeper_client = ZookeeperClient(client_pfx + "device-manager",
                                        args.zk_server_ip)
    dm_logger.notice("Waiting to be elected as master...")
    _zookeeper_client.master_election(zk_path_pfx + "/device-manager",
                                      os.getpid(), run_device_manager,
                                      dm_logger, args)
Ejemplo n.º 3
0
def main(args_str=None):
    global _zookeeper_client

    if not args_str:
        args_str = ' '.join(sys.argv[1:])
    args = parse_args(args_str)
    if args.cluster_id:
        client_pfx = args.cluster_id + '-'
        zk_path_pfx = args.cluster_id + '/'
    else:
        client_pfx = ''
        zk_path_pfx = ''

    # Initialize discovery client
    discovery_client = None
    if args.disc_server_ip and args.disc_server_port:
        dss_kwargs = {}
        if args.disc_server_ssl:
            if args.disc_server_cert:
                dss_kwargs.update({'cert': args.disc_server_cert})
            if args.disc_server_key:
                dss_kwargs.update({'key': args.disc_server_key})
            if args.disc_server_cacert:
                dss_kwargs.update({'cacert': args.disc_server_cacert})
        discovery_client = client.DiscoveryClient(
            args.disc_server_ip, args.disc_server_port,
            ModuleNames[Module.DEVICE_MANAGER], **dss_kwargs)
    # Initialize logger
    module = Module.DEVICE_MANAGER
    module_pkg = "device_manager"
    dm_logger = ConfigServiceLogger(discovery_client,
                                    module,
                                    module_pkg,
                                    args,
                                    http_server_port=-1)

    # Initialize AMQP handler then close it to be sure remain queue of a
    # precedent run is cleaned
    vnc_amqp = DMAmqpHandle(dm_logger, DeviceManager.REACTION_MAP, args)
    vnc_amqp.establish()
    vnc_amqp.close()
    dm_logger.debug("Removed remained AMQP queue")

    _zookeeper_client = ZookeeperClient(client_pfx + "device-manager",
                                        args.zk_server_ip)
    dm_logger.notice("Waiting to be elected as master...")
    _zookeeper_client.master_election(zk_path_pfx + "/device-manager",
                                      os.getpid(), run_device_manager,
                                      dm_logger, args)
Ejemplo n.º 4
0
class DeviceManager(object):
    REACTION_MAP = {
        'physical_router': {
            'self': ['bgp_router',
                     'physical_interface',
                     'logical_interface'],
            'bgp_router': [],
            'physical_interface': [],
            'logical_interface': [],
            'virtual_network': [],
            'global_system_config': [],
        },
        'global_system_config': {
            'self': ['physical_router'],
            'physical_router': [],
        },
        'bgp_router': {
            'self': ['bgp_router', 'physical_router'],
            'bgp_router': ['physical_router'],
            'physical_router': [],
        },
        'physical_interface': {
            'self': ['physical_router',
                     'physical_interface',
                     'logical_interface'],
            'physical_router': ['logical_interface'],
            'logical_interface': ['physical_router'],
            'physical_interface': ['physical_router'],
            'virtual_machine_interface': ['physical_interface'],
        },
        'logical_interface': {
            'self': ['physical_router',
                     'physical_interface',
                     'virtual_machine_interface'],
            'physical_interface': ['virtual_machine_interface'],
            'virtual_machine_interface': ['physical_router',
                                          'physical_interface'],
            'physical_router': ['virtual_machine_interface']
        },
        'virtual_machine_interface': {
            'self': ['logical_interface',
                     'physical_interface',
                     'virtual_network',
                     'floating_ip',
                     'instance_ip',
                     'port_tuple'],
            'logical_interface': ['virtual_network'],
            'virtual_network': ['logical_interface'],
            'floating_ip': ['virtual_network'],
            'instance_ip': ['virtual_network'],
            'routing_instance': ['port_tuple','physical_interface'],
            'port_tuple': ['physical_interface']
        },
        'service_instance': {
            'self': ['port_tuple'],
            'port_tuple':[],
        },
        'port_tuple':{
            'self':['virtual_machine_interface','service_instance'],
            'service_instance':['virtual_machine_interface'],
            'virtual_machine_interface':['service_instance']
        },
        'virtual_network': {
            'self': ['physical_router',
                     'virtual_machine_interface'],
            'routing_instance': ['physical_router',
                                 'virtual_machine_interface'],
            'physical_router': [],
            'virtual_machine_interface': ['physical_router'],
        },
        'routing_instance': {
            'self': ['routing_instance',
                     'virtual_network',
                     'virtual_machine_interface'],
            'routing_instance': ['virtual_network',
                                 'virtual_machine_interface'],
            'virtual_network': []
        },
        'floating_ip': {
            'self': ['virtual_machine_interface'],
            'virtual_machine_interface': [],
        },
        'instance_ip': {
            'self': ['virtual_machine_interface'],
            'virtual_machine_interface': [],
        },
    }

    _device_manager = None

    def __init__(self, dm_logger=None, args=None):
        self._args = args
        PushConfigState.set_repush_interval(int(self._args.repush_interval))
        PushConfigState.set_repush_max_interval(int(self._args.repush_max_interval))
        PushConfigState.set_push_delay_per_kb(float(self._args.push_delay_per_kb))
        PushConfigState.set_push_delay_max(int(self._args.push_delay_max))
        PushConfigState.set_push_delay_enable(bool(self._args.push_delay_enable))

        self._chksum = "";
        if self._args.collectors:
            self._chksum = hashlib.md5(''.join(self._args.collectors)).hexdigest()

        # Initialize logger
        self.logger = dm_logger or DeviceManagerLogger(args)

        # Register Plugins
        try:
            DeviceConf.register_plugins()
        except DeviceConf.PluginsRegistrationFailed as e:
            self.logger.error("Exception: " + str(e))
        except Exception as e:
            tb = traceback.format_exc()
            self.logger.error("Internal error while registering plugins: " + str(e) + tb)

        # Retry till API server is up
        connected = False
        self.connection_state_update(ConnectionStatus.INIT)
        while not connected:
            try:
                self._vnc_lib = VncApi(
                    args.admin_user, args.admin_password,
                    args.admin_tenant_name, args.api_server_ip,
                    args.api_server_port,
                    api_server_use_ssl=args.api_server_use_ssl)
                connected = True
                self.connection_state_update(ConnectionStatus.UP)
            except requests.exceptions.ConnectionError as e:
                # Update connection info
                self.connection_state_update(ConnectionStatus.DOWN, str(e))
                time.sleep(3)
            except ResourceExhaustionError:  # haproxy throws 503
                time.sleep(3)

        """ @sighup
        Handle of SIGHUP for collector list config change
        """
        gevent.signal(signal.SIGHUP, self.sighup_handler)

        # Initialize amqp
        self._vnc_amqp = DMAmqpHandle(self.logger, self.REACTION_MAP,
                                      self._args)
        self._vnc_amqp.establish()

        # Initialize cassandra
        self._object_db = DMCassandraDB.get_instance(self, _zookeeper_client)
        DBBaseDM.init(self, self.logger, self._object_db)
        DBBaseDM._sandesh = self.logger._sandesh

        for obj in GlobalSystemConfigDM.list_obj():
            GlobalSystemConfigDM.locate(obj['uuid'], obj)

        for obj in GlobalVRouterConfigDM.list_obj():
            GlobalVRouterConfigDM.locate(obj['uuid'], obj)

        for obj in VirtualNetworkDM.list_obj():
            VirtualNetworkDM.locate(obj['uuid'], obj)

        for obj in RoutingInstanceDM.list_obj():
            RoutingInstanceDM.locate(obj['uuid'], obj)

        for obj in BgpRouterDM.list_obj():
            BgpRouterDM.locate(obj['uuid'], obj)

        pr_obj_list = PhysicalRouterDM.list_obj()
        for obj in pr_obj_list:
            PhysicalRouterDM.locate(obj['uuid'],obj)

        pr_uuid_set = set([pr_obj['uuid'] for pr_obj in pr_obj_list])
        self._object_db.handle_pr_deletes(pr_uuid_set)

        for obj in PortTupleDM.list_obj():
            PortTupleDM.locate(obj['uuid'],obj)

        for obj in PhysicalInterfaceDM.list_obj():
            PhysicalInterfaceDM.locate(obj['uuid'],obj)

        for obj in LogicalInterfaceDM.list_obj():
            LogicalInterfaceDM.locate(obj['uuid'],obj)

        for obj in VirtualMachineInterfaceDM.list_obj():
            VirtualMachineInterfaceDM.locate(obj['uuid'],obj)

        for obj in pr_obj_list:
            pr = PhysicalRouterDM.locate(obj['uuid'], obj)
            li_set = pr.logical_interfaces
            vmi_set = set()
            for pi_id in pr.physical_interfaces:
                pi = PhysicalInterfaceDM.locate(pi_id)
                if pi:
                    li_set |= pi.logical_interfaces
                    vmi_set |= pi.virtual_machine_interfaces
            for li_id in li_set:
                li = LogicalInterfaceDM.locate(li_id)
                if li and li.virtual_machine_interface:
                    vmi_set |= set([li.virtual_machine_interface])
            for vmi_id in vmi_set:
                vmi = VirtualMachineInterfaceDM.locate(vmi_id)

        si_obj_list = ServiceInstanceDM.list_obj()
        si_uuid_set = set([si_obj['uuid'] for si_obj in si_obj_list])
        self._object_db.handle_pnf_resource_deletes(si_uuid_set)

        for obj in si_obj_list:
            ServiceInstanceDM.locate(obj['uuid'], obj)

        for obj in InstanceIpDM.list_obj():
            InstanceIpDM.locate(obj['uuid'], obj)

        for obj in FloatingIpDM.list_obj():
            FloatingIpDM.locate(obj['uuid'], obj)

        for vn in VirtualNetworkDM.values():
            vn.update_instance_ip_map()

        for pr in PhysicalRouterDM.values():
            pr.set_config_state()

        DeviceManager._device_manager = self
        self._vnc_amqp._db_resync_done.set()
        try:
            gevent.joinall(self._vnc_amqp._vnc_kombu.greenlets())
        except KeyboardInterrupt:
            DeviceManager.destroy_instance()
            raise
    # end __init__

    @classmethod
    def get_instance(cls):
        return cls._device_manager

    @classmethod
    def destroy_instance(cls):
        inst = cls.get_instance()
        if not inst:
            return
        inst._vnc_amqp.close()
        for obj_cls in DBBaseDM.get_obj_type_map().values():
            obj_cls.reset()
        DBBase.clear()
        DMCassandraDB.clear_instance()
        inst._object_db = None
        cls._device_manager = None

    def connection_state_update(self, status, message=None):
        ConnectionState.update(
            conn_type=ConnType.APISERVER, name='ApiServer',
            status=status, message=message or '',
            server_addrs=['%s:%s' % (self._args.api_server_ip,
                                     self._args.api_server_port)])
    # end connection_state_update

    # sighup handler for applying new configs
    def sighup_handler(self):
        if self._args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.read(self._args.conf_file)
            if 'DEFAULTS' in config.sections():
               try:
                   collectors = config.get('DEFAULTS', 'collectors')
                   if type(collectors) is str:
                       collectors = collectors.split()
                       new_chksum = hashlib.md5("".join(collectors)).hexdigest()
                       if new_chksum != self._chksum:
                           self._chksum = new_chksum
                           config.random_collectors = random.sample(collectors, len(collectors))
                       # Reconnect to achieve load-balance irrespective of list
                       self.logger.sandesh_reconfig_collectors(config)
               except ConfigParser.NoOptionError as e:
                   pass
Ejemplo n.º 5
0
def main(args_str=None):
    global _amqp_client
    global _zookeeper_client

    if not args_str:
        args_str = ' '.join(sys.argv[1:])
    args = parse_args(args_str)
    if args.cluster_id:
        client_pfx = args.cluster_id + '-'
        zk_path_pfx = args.cluster_id + '/'
    else:
        client_pfx = ''
        zk_path_pfx = ''

    # randomize collector list
    args.random_collectors = args.collectors
    if args.collectors:
        args.random_collectors = random.sample(args.collectors,
                                               len(args.collectors))

    # Initialize logger without introspect thread
    dm_logger = DeviceManagerLogger(args, http_server_port=-1)

    # Initialize AMQP handler then close it to be sure remain queue of a
    # precedent run is cleaned
    vnc_amqp = DMAmqpHandle(dm_logger, {}, args)
    vnc_amqp.establish()
    vnc_amqp.close()
    dm_logger.debug("Removed remaining AMQP queue from previous run")

    if 'host_ip' not in args:
        args.host_ip = socket.gethostbyname(socket.getfqdn())

    _amqp_client = initialize_amqp_client(dm_logger, args)
    _zookeeper_client = ZookeeperClient(client_pfx + "device-manager",
                                        args.zk_server_ip, args.host_ip)

    try:
        # Initialize the device job manager
        DeviceJobManager(_amqp_client, _zookeeper_client, args, dm_logger)
        # Allow kombu client to connect consumers
        gevent.sleep(0.5)
    except Exception as e:
        dm_logger.error("Error while initializing the device job "
                        "manager %s" % str(e))
        raise e

    try:
        # Initialize the device ztp manager
        DeviceZtpManager(_amqp_client, args, dm_logger)
        # Allow kombu client to connect consumers
        gevent.sleep(0.5)
    except Exception as e:
        dm_logger.error("Error while initializing the device ztp "
                        "manager %s" % str(e))
        raise e

    gevent.signal(signal.SIGHUP, sighup_handler)
    gevent.signal(signal.SIGTERM, sigterm_handler)
    gevent.signal(signal.SIGINT, sigterm_handler)

    dm_logger.notice("Waiting to be elected as master...")
    _zookeeper_client.master_election(zk_path_pfx + "/device-manager",
                                      os.getpid(), run_device_manager,
                                      dm_logger, args)
Ejemplo n.º 6
0
class DeviceManager(object):
    REACTION_MAP = {
        'physical_router': {
            'self': [
                'bgp_router', 'physical_interface', 'logical_interface',
                'e2_service_provider', 'service_endpoint'
            ],
            'bgp_router': [],
            'physical_interface': [],
            'logical_interface': [],
            'virtual_network': [],
            'logical_router': [],
            'global_system_config': [],
            'network_device_config': [],
            'service_endpoint': [],
            'service_connection_module': [],
            'service_object': [],
            'e2_service_provider': [],
            'node_profile': [],
            'role_config': [],
            'fabric': [],
            'fabric_namespace': [],
            'virtual_port_group': [],
            'service_instance': [],
            'service_appliance': [],
        },
        'global_system_config': {
            'self': ['physical_router', 'data_center_interconnect'],
            'physical_router': [],
        },
        'node_profile': {
            'self': ['physical_router'],
            'role_config': ['physical_router'],
        },
        'data_center_interconnect': {
            'self': ['logical_router', 'virtual_network'],
            'logical_router': [],
            'virtual_network': ['logical_router'],
            'global_system_config': ['logical_router'],
        },
        'role_config': {
            'self': ['node_profile'],
            'node_profile': [],
        },
        'virtual_port_group': {
            'self': ['physical_interface'],
            'virtual_machine_interface': ['physical_interface'],
            'physical_interface': ['physical_interface'],
        },
        'fabric': {
            'self': ['physical_router'],
            'fabric_namespace': ['physical_router'],
        },
        'fabric_namespace': {
            'self': ['fabric'],
        },
        'bgp_router': {
            'self': ['bgp_router', 'physical_router'],
            'bgp_router': ['physical_router'],
            'physical_router': [],
        },
        'physical_interface': {
            'self': [
                'physical_router', 'physical_interface', 'logical_interface',
                'virtual_port_group'
            ],
            'physical_router': ['logical_interface'],
            'logical_interface': ['physical_interface', 'physical_router'],
            'physical_interface': ['physical_router'],
            'virtual_port_group': ['physical_router'],
            'virtual_machine_interface':
            ['physical_interface', 'physical_router'],
            'service_appliance': ['physical_router'],
            'port': ['physical_router'],
        },
        'logical_interface': {
            'self': [
                'physical_router', 'physical_interface',
                'virtual_machine_interface', 'service_endpoint'
            ],
            'physical_interface': ['virtual_machine_interface'],
            'virtual_machine_interface':
            ['physical_router', 'physical_interface'],
            'physical_router': ['virtual_machine_interface'],
            'instance_ip': ['physical_interface'],
        },
        'service_appliance': {
            'self': ['physical_interface'],
            'service_appliance_set': ['physical_interface'],
        },
        'virtual_machine_interface': {
            'self': [
                'logical_interface', 'physical_interface', 'virtual_network',
                'logical_router', 'floating_ip', 'instance_ip', 'port_tuple',
                'service_endpoint', 'virtual_port_group'
            ],
            'logical_interface': ['virtual_network'],
            'virtual_network': ['logical_interface', 'logical_router'],
            'logical_router': [],
            'floating_ip': ['virtual_network'],
            'instance_ip': ['virtual_network'],
            'routing_instance': ['port_tuple', 'physical_interface'],
            'port_tuple': ['physical_interface'],
            'service_endpoint': ['physical_router'],
            'security_group': ['logical_interface', 'virtual_port_group'],
            'port_profile': ['virtual_port_group'],
        },
        'security_group': {
            'self': [],
            'access_control_list': ['virtual_machine_interface'],
        },
        'access_control_list': {
            'self': ['security_group'],
            'security_group': [],
        },
        'port_profile': {
            'self': ['virtual_machine_interface'],
            'storm_control_profile': ['virtual_machine_interface'],
        },
        'storm_control_profile': {
            'self': ['port_profile'],
        },
        'service_appliance_set': {
            'self': [],
            'service_template': ['service_appliance'],
        },
        'service_template': {
            'self': [],
            'service_instance': ['service_appliance_set'],
        },
        'service_instance': {
            'self': ['port_tuple'],
            'port_tuple': ['service_template'],
        },
        'port_tuple': {
            'self': ['virtual_machine_interface', 'service_instance'],
            'logical_router': ['service_instance'],
            'service_instance':
            ['virtual_machine_interface', 'service_template'],
            'virtual_machine_interface': ['service_instance']
        },
        'virtual_network': {
            'self': [
                'physical_router', 'data_center_interconnect',
                'virtual_machine_interface', 'logical_router', 'fabric',
                'floating_ip_pool', 'tag', 'network_ipam'
            ],
            'routing_instance':
            ['physical_router', 'logical_router', 'virtual_machine_interface'],
            'physical_router': [],
            'logical_router': ['physical_router'],
            'data_center_interconnect': ['physical_router'],
            'virtual_machine_interface': ['physical_router'],
            'floating_ip_pool': ['physical_router'],
            'network_ipam': ['tag']
        },
        'logical_router': {
            'self': ['physical_router', 'virtual_network', 'port_tuple'],
            'physical_router': [],
            'data_center_interconnect': ['physical_router'],
            'virtual_network': ['physical_router'],
            'routing_instance': ['physical_router'],
            'virtual_machine_interface': ['physical_router']
        },
        'routing_instance': {
            'self': [
                'routing_instance', 'virtual_network',
                'virtual_machine_interface'
            ],
            'routing_instance':
            ['virtual_network', 'virtual_machine_interface'],
            'virtual_network': []
        },
        'floating_ip': {
            'self': ['virtual_machine_interface', 'floating_ip_pool'],
            'virtual_machine_interface': ['floating_ip_pool'],
            'floating_ip_pool': ['virtual_machine_interface']
        },
        'floating_ip_pool': {
            'self': ['virtual_network'],
            'virtual_network': ['floating_ip'],
            'floating_ip': ['virtual_network']
        },
        'instance_ip': {
            'self': ['virtual_machine_interface', 'logical_interface'],
            'virtual_machine_interface': [],
        },
        'service_endpoint': {
            'self': [
                'physical_router', 'virtual_machine_interface',
                'service_connection_module'
            ],
            'physical_router': ['service_connection_module'],
            'logical_interface': ['service_connection_module'],
            'virtual_machine_interface': ['service_connection_module'],
            'service_connection_module': [],
        },
        'service_connection_module': {
            'self': ['service_endpoint'],
            'service_endpoint': [],
        },
        'service_object': {
            'self': [],
        },
        'network_device_config': {
            'self': [],
        },
        'e2_service_provider': {
            'self': ['physical_router', 'peering_policy'],
            'physical_router': [],
            'peering_policy': [],
        },
        'peering_policy': {
            'self': ['e2_service_provider'],
            'e2_service_provider': [],
        },
        'tag': {
            'self': ['port'],
            'virtual_network': ['port']
        },
        'network_ipam': {
            'self': ['virtual_network'],
            'virtual_network': ['tag']
        },
        'port': {
            'self': ['physical_interface'],
            'tag': ['physical_interface'],
        },
    }

    _instance = None

    def __init__(self,
                 dm_logger=None,
                 args=None,
                 zookeeper_client=None,
                 amqp_client=None):
        """Physical Router init routine."""
        DeviceManager._instance = self
        self._args = args
        self._amqp_client = amqp_client
        self.logger = dm_logger or DeviceManagerLogger(args)
        self._vnc_amqp = DMAmqpHandle(self.logger, self.REACTION_MAP,
                                      self._args)

        PushConfigState.set_push_mode(int(self._args.push_mode))
        PushConfigState.set_repush_interval(int(self._args.repush_interval))
        PushConfigState.set_repush_max_interval(
            int(self._args.repush_max_interval))
        PushConfigState.set_push_delay_per_kb(
            float(self._args.push_delay_per_kb))
        PushConfigState.set_push_delay_max(int(self._args.push_delay_max))
        PushConfigState.set_push_delay_enable(
            bool(self._args.push_delay_enable))

        self._chksum = ""
        if self._args.collectors:
            self._chksum = hashlib.md5(''.join(
                self._args.collectors)).hexdigest()

        # Register Plugins
        try:
            DeviceConf.register_plugins()
        except DeviceConf.PluginsRegistrationFailed as e:
            self.logger.error("Exception: " + str(e))
        except Exception as e:
            tb = traceback.format_exc()
            self.logger.error("Internal error while registering plugins: " +
                              str(e) + tb)

        # Register Ansible Plugins
        try:
            AnsibleBase.register_plugins()
        except AnsibleBase.PluginsRegistrationFailed as e:
            self.logger.error("Exception: " + str(e))
        except Exception as e:
            tb = traceback.format_exc()
            self.logger.error(
                "Internal error while registering ansible plugins: " + str(e) +
                tb)

        # Register Feature Plugins
        try:
            FeatureBase.register_plugins()
        except FeatureBase.PluginRegistrationFailed as e:
            self.logger.error("Exception: " + str(e))
        except Exception as e:
            tb = traceback.format_exc()
            self.logger.error(
                "Internal error while registering feature plugins: " + str(e) +
                tb)
            raise e

        # Retry till API server is up
        connected = False
        self.connection_state_update(ConnectionStatus.INIT)
        api_server_list = args.api_server_ip.split(',')
        while not connected:
            try:
                self._vnc_lib = VncApi(
                    args.admin_user,
                    args.admin_password,
                    args.admin_tenant_name,
                    api_server_list,
                    args.api_server_port,
                    api_server_use_ssl=args.api_server_use_ssl)
                connected = True
                self.connection_state_update(ConnectionStatus.UP)
            except requests.exceptions.ConnectionError as e:
                # Update connection info
                self.connection_state_update(ConnectionStatus.DOWN, str(e))
                time.sleep(3)
            except ResourceExhaustionError:  # haproxy throws 503
                time.sleep(3)

        if PushConfigState.is_push_mode_ansible():
            FabricManager.initialize(args, dm_logger, self._vnc_lib)
        # Initialize amqp
        self._vnc_amqp.establish()

        # Initialize cassandra
        self._object_db = DMCassandraDB.get_instance(zookeeper_client,
                                                     self._args, self.logger)
        DBBaseDM.init(self, self.logger, self._object_db)
        DBBaseDM._sandesh = self.logger._sandesh

        GlobalSystemConfigDM.locate_all()
        FeatureDM.locate_all()
        PhysicalRoleDM.locate_all()
        OverlayRoleDM.locate_all()
        RoleDefinitionDM.locate_all()
        FeatureConfigDM.locate_all()
        NodeProfileDM.locate_all()
        RoleConfigDM.locate_all()
        GlobalVRouterConfigDM.locate_all()
        VirtualNetworkDM.locate_all()
        DataCenterInterconnectDM.locate_all()
        FabricDM.locate_all()
        FabricNamespaceDM.locate_all()
        LogicalRouterDM.locate_all()
        RoutingInstanceDM.locate_all()
        FloatingIpPoolDM.locate_all()
        BgpRouterDM.locate_all()
        PhysicalInterfaceDM.locate_all()
        LogicalInterfaceDM.locate_all()
        PhysicalRouterDM.locate_all()
        LinkAggregationGroupDM.locate_all()
        VirtualPortGroupDM.locate_all()
        PortDM.locate_all()
        TagDM.locate_all()
        NetworkIpamDM.locate_all()
        VirtualMachineInterfaceDM.locate_all()
        SecurityGroupDM.locate_all()
        AccessControlListDM.locate_all()
        PortProfileDM.locate_all()
        StormControlProfileDM.locate_all()
        ServiceInstanceDM.locate_all()
        ServiceApplianceSetDM.locate_all()
        ServiceApplianceDM.locate_all()
        ServiceTemplateDM.locate_all()
        PortTupleDM.locate_all()
        InstanceIpDM.locate_all()
        FloatingIpDM.locate_all()

        for vn in VirtualNetworkDM.values():
            vn.update_instance_ip_map()

        ServiceEndpointDM.locate_all()
        ServiceConnectionModuleDM.locate_all()
        ServiceObjectDM.locate_all()
        NetworkDeviceConfigDM.locate_all()
        E2ServiceProviderDM.locate_all()
        PeeringPolicyDM.locate_all()

        pr_obj_list = PhysicalRouterDM.list_obj()
        pr_uuid_set = set([pr_obj['uuid'] for pr_obj in pr_obj_list])
        self._object_db.handle_pr_deletes(pr_uuid_set)

        dci_obj_list = DataCenterInterconnectDM.list_obj()
        dci_uuid_set = set([dci_obj['uuid'] for dci_obj in dci_obj_list])
        self._object_db.handle_dci_deletes(dci_uuid_set)

        si_obj_list = ServiceInstanceDM.list_obj()
        si_uuid_set = set([si_obj['uuid'] for si_obj in si_obj_list])
        self._object_db.handle_pnf_resource_deletes(si_uuid_set)

        for pr in PhysicalRouterDM.values():
            pr.set_config_state()
            pr.uve_send()

        self._vnc_amqp._db_resync_done.set()

        gevent.joinall(self._vnc_amqp._vnc_kombu.greenlets())

    # end __init__

    def get_analytics_config(self):
        return {
            'ips': self._args.analytics_server_ip.split(','),
            'port': self._args.analytics_server_port,
            'username': self._args.analytics_username,
            'password': self._args.analytics_password
        }

    # end get_analytics_config

    def get_api_server_config(self):
        return {
            'ips': self._args.api_server_ip.split(','),
            'port': self._args.api_server_port,
            'username': self._args.admin_user,
            'password': self._args.admin_password,
            'tenant': self._args.admin_tenant_name,
            'use_ssl': self._args.api_server_use_ssl
        }

    # end get_api_server_config

    def get_job_status_config(self):
        return {
            'timeout': int(self._args.job_status_retry_timeout),
            'max_retries': int(self._args.job_status_max_retries)
        }

    # end get_job_status_config

    @classmethod
    def get_instance(cls):
        return cls._instance

    # end get_instance

    @classmethod
    def destroy_instance(cls):
        inst = cls.get_instance()
        if not inst:
            return
        inst._vnc_amqp.close()
        if PushConfigState.is_push_mode_ansible():
            FabricManager.destroy_instance()
        for obj_cls in DBBaseDM.get_obj_type_map().values():
            obj_cls.reset()
        DBBase.clear()
        DMCassandraDB.clear_instance()
        cls._instance = None

    # end destroy_instance

    def connection_state_update(self, status, message=None):
        ConnectionState.update(
            conn_type=ConnType.APISERVER,
            name='ApiServer',
            status=status,
            message=message or 'ApiServer Connection State updated',
            server_addrs=[
                '%s:%s' %
                (self._args.api_server_ip, self._args.api_server_port)
            ])

    # end connection_state_update

    # sighup handler for applying new configs
    def sighup_handler(self):
        if self._args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.read(self._args.conf_file)
            if 'DEFAULTS' in config.sections():
                try:
                    collectors = config.get('DEFAULTS', 'collectors')
                    if type(collectors) is str:
                        collectors = collectors.split()
                        new_chksum = hashlib.md5(
                            "".join(collectors)).hexdigest()
                        if new_chksum != self._chksum:
                            self._chksum = new_chksum
                            config.random_collectors = random.sample(
                                collectors, len(collectors))
                        # Reconnect to achieve loadbalance irrespective of list
                        self.logger.sandesh_reconfig_collectors(config)
                except ConfigParser.NoOptionError:
                    pass
Ejemplo n.º 7
0
class DeviceManager(object):
    _REACTION_MAP = {
        'physical_router': {
            'self': ['bgp_router', 'physical_interface', 'logical_interface'],
            'bgp_router': [],
            'physical_interface': [],
            'logical_interface': [],
            'virtual_network': [],
            'global_system_config': [],
        },
        'global_system_config': {
            'self': ['physical_router'],
            'physical_router': [],
        },
        'bgp_router': {
            'self': ['bgp_router', 'physical_router'],
            'bgp_router': ['physical_router'],
            'physical_router': [],
        },
        'physical_interface': {
            'self':
            ['physical_router', 'physical_interface', 'logical_interface'],
            'physical_router': ['logical_interface'],
            'logical_interface': ['physical_router'],
            'physical_interface': ['physical_router'],
            'virtual_machine_interface': ['physical_interface'],
        },
        'logical_interface': {
            'self': [
                'physical_router', 'physical_interface',
                'virtual_machine_interface'
            ],
            'physical_interface': ['virtual_machine_interface'],
            'virtual_machine_interface':
            ['physical_router', 'physical_interface'],
            'physical_router': ['virtual_machine_interface']
        },
        'virtual_machine_interface': {
            'self': [
                'logical_interface', 'physical_interface', 'virtual_network',
                'floating_ip', 'instance_ip', 'port_tuple'
            ],
            'logical_interface': ['virtual_network'],
            'virtual_network': ['logical_interface'],
            'floating_ip': ['virtual_network'],
            'instance_ip': ['virtual_network'],
            'routing_instance': ['port_tuple', 'physical_interface'],
            'port_tuple': ['physical_interface']
        },
        'service_instance': {
            'self': ['port_tuple'],
            'port_tuple': [],
        },
        'port_tuple': {
            'self': ['virtual_machine_interface', 'service_instance'],
            'service_instance': ['virtual_machine_interface'],
            'virtual_machine_interface': ['service_instance']
        },
        'virtual_network': {
            'self': ['physical_router', 'virtual_machine_interface'],
            'routing_instance':
            ['physical_router', 'virtual_machine_interface'],
            'physical_router': [],
            'virtual_machine_interface': ['physical_router'],
        },
        'routing_instance': {
            'self': [
                'routing_instance', 'virtual_network',
                'virtual_machine_interface'
            ],
            'routing_instance':
            ['virtual_network', 'virtual_machine_interface'],
            'virtual_network': []
        },
        'floating_ip': {
            'self': ['virtual_machine_interface'],
            'virtual_machine_interface': [],
        },
        'instance_ip': {
            'self': ['virtual_machine_interface'],
            'virtual_machine_interface': [],
        },
    }

    def __init__(self, args=None):
        self._args = args

        # Initialize discovery client
        self._disc = None
        if self._args.disc_server_ip and self._args.disc_server_port:
            self._disc = client.DiscoveryClient(
                self._args.disc_server_ip, self._args.disc_server_port,
                ModuleNames[Module.DEVICE_MANAGER])

        PushConfigState.set_repush_interval(int(self._args.repush_interval))
        PushConfigState.set_repush_max_interval(
            int(self._args.repush_max_interval))
        PushConfigState.set_push_delay_per_kb(
            float(self._args.push_delay_per_kb))
        PushConfigState.set_push_delay_max(int(self._args.push_delay_max))
        PushConfigState.set_push_delay_enable(
            bool(self._args.push_delay_enable))

        # Initialize logger
        module = Module.DEVICE_MANAGER
        module_pkg = "device_manager"
        self.logger = ConfigServiceLogger(self._disc, module, module_pkg, args)

        # Retry till API server is up
        connected = False
        self.connection_state_update(ConnectionStatus.INIT)
        while not connected:
            try:
                self._vnc_lib = VncApi(
                    args.admin_user,
                    args.admin_password,
                    args.admin_tenant_name,
                    args.api_server_ip,
                    args.api_server_port,
                    api_server_use_ssl=args.api_server_use_ssl)
                connected = True
                self.connection_state_update(ConnectionStatus.UP)
            except requests.exceptions.ConnectionError as e:
                # Update connection info
                self.connection_state_update(ConnectionStatus.DOWN, str(e))
                time.sleep(3)
            except ResourceExhaustionError:  # haproxy throws 503
                time.sleep(3)

        # Initialize amqp
        self._vnc_amqp = DMAmqpHandle(self.logger, self._REACTION_MAP,
                                      self._args)
        self._vnc_amqp.establish()

        # Initialize cassandra
        self._object_db = DMCassandraDB.getInstance(self, _zookeeper_client)
        DBBaseDM.init(self, self.logger, self._object_db)
        DBBaseDM._sandesh = self.logger._sandesh

        for obj in GlobalSystemConfigDM.list_obj():
            GlobalSystemConfigDM.locate(obj['uuid'], obj)

        for obj in GlobalVRouterConfigDM.list_obj():
            GlobalVRouterConfigDM.locate(obj['uuid'], obj)

        for obj in VirtualNetworkDM.list_obj():
            vn = VirtualNetworkDM.locate(obj['uuid'], obj)
            if vn is not None and vn.routing_instances is not None:
                for ri_id in vn.routing_instances:
                    ri_obj = RoutingInstanceDM.locate(ri_id)

        for obj in BgpRouterDM.list_obj():
            BgpRouterDM.locate(obj['uuid'], obj)

        pr_obj_list = PhysicalRouterDM.list_obj()
        pr_uuid_set = set([pr_obj['uuid'] for pr_obj in pr_obj_list])
        self._object_db.handle_pr_deletes(pr_uuid_set)

        for obj in PortTupleDM.list_obj():
            PortTupleDM.locate(obj['uuid'], obj)

        for obj in pr_obj_list:
            pr = PhysicalRouterDM.locate(obj['uuid'], obj)
            li_set = pr.logical_interfaces
            vmi_set = set()
            for pi_id in pr.physical_interfaces:
                pi = PhysicalInterfaceDM.locate(pi_id)
                if pi:
                    li_set |= pi.logical_interfaces
                    vmi_set |= pi.virtual_machine_interfaces
            for li_id in li_set:
                li = LogicalInterfaceDM.locate(li_id)
                if li and li.virtual_machine_interface:
                    vmi_set |= set([li.virtual_machine_interface])
            for vmi_id in vmi_set:
                vmi = VirtualMachineInterfaceDM.locate(vmi_id)

        si_obj_list = ServiceInstanceDM.list_obj()
        si_uuid_set = set([si_obj['uuid'] for si_obj in si_obj_list])
        self._object_db.handle_pnf_resource_deletes(si_uuid_set)

        for obj in si_obj_list:
            ServiceInstanceDM.locate(obj['uuid'], obj)

        for obj in InstanceIpDM.list_obj():
            InstanceIpDM.locate(obj['uuid'], obj)

        for obj in FloatingIpDM.list_obj():
            FloatingIpDM.locate(obj['uuid'], obj)

        for vn in VirtualNetworkDM.values():
            vn.update_instance_ip_map()

        for pr in PhysicalRouterDM.values():
            pr.set_config_state()

        self._vnc_amqp._db_resync_done.set()
        gevent.joinall(self._vnc_amqp._vnc_kombu.greenlets())

    # end __init__

    def connection_state_update(self, status, message=None):
        ConnectionState.update(
            conn_type=ConnType.APISERVER,
            name='ApiServer',
            status=status,
            message=message or '',
            server_addrs=[
                '%s:%s' %
                (self._args.api_server_ip, self._args.api_server_port)
            ])
Ejemplo n.º 8
0
class DeviceManager(object):
    REACTION_MAP = {
        'physical_router': {
            'self': ['bgp_router',
                     'physical_interface',
                     'logical_interface',
                     'e2_service_provider',
                     'service_endpoint'],
            'bgp_router': [],
            'physical_interface': [],
            'logical_interface': [],
            'virtual_network': [],
            'logical_router': [],
            'global_system_config': [],
            'network_device_config': [],
            'service_endpoint': [],
            'service_connection_module': [],
            'service_object': [],
            'e2_service_provider': [],
            'node_profile': [],
            'role_config': [],
            'fabric': [],
            'fabric_namespace': [],
            'virtual_port_group': [],
            'service_instance': [],
            'service_appliance': [],
        },
        'global_system_config': {
            'self': ['physical_router', 'data_center_interconnect'],
            'physical_router': [],
        },
        'node_profile': {
            'self': ['physical_router'],
            'role_config': ['physical_router'],
        },
        'data_center_interconnect': {
            'self': ['logical_router', 'virtual_network'],
            'logical_router': [],
            'virtual_network': ['logical_router'],
            'global_system_config': ['logical_router'],
        },
        'role_config': {
            'self': ['node_profile'],
            'node_profile': [],
        },
        'virtual_port_group': {
            'self': ['physical_interface'],
            'virtual_machine_interface': ['physical_interface'],
            'physical_interface': ['physical_interface'],
        },
        'fabric': {
            'self': ['physical_router'],
            'fabric_namespace': ['physical_router'],
        },
        'fabric_namespace': {
            'self': ['fabric'],
        },
        'bgp_router': {
            'self': ['bgp_router', 'physical_router'],
            'bgp_router': ['physical_router'],
            'physical_router': [],
        },
        'physical_interface': {
            'self': ['physical_router',
                     'physical_interface',
                     'logical_interface',
                     'virtual_port_group'
                     ],
            'physical_router': ['logical_interface'],
            'logical_interface': ['physical_interface', 'physical_router'],
            'physical_interface': ['physical_router'],
            'virtual_port_group': ['physical_router'],
            'virtual_machine_interface': ['physical_interface', 'physical_router'],
            'service_appliance': ['physical_router'],
            'port': ['physical_router'],
        },
        'logical_interface': {
            'self': ['physical_router',
                     'physical_interface',
                     'virtual_machine_interface',
                     'service_endpoint'],
            'physical_interface': ['virtual_machine_interface'],
            'virtual_machine_interface': ['physical_router',
                                          'physical_interface'],
            'physical_router': ['virtual_machine_interface'],
            'instance_ip': ['physical_interface'],
        },
        'service_appliance': {
            'self': ['physical_interface'],
            'service_appliance_set': ['physical_interface'],
        },
        'virtual_machine_interface': {
            'self': ['logical_interface',
                     'physical_interface',
                     'virtual_network',
                     'logical_router',
                     'floating_ip',
                     'instance_ip',
                     'port_tuple',
                     'service_endpoint',
                     'virtual_port_group'],
            'logical_interface': ['virtual_network'],
            'virtual_network': ['logical_interface', 'logical_router'],
            'logical_router': [],
            'floating_ip': ['virtual_network'],
            'instance_ip': ['virtual_network'],
            'routing_instance': ['port_tuple', 'physical_interface'],
            'port_tuple': ['physical_interface'],
            'service_endpoint': ['physical_router'],
            'security_group': ['logical_interface', 'virtual_port_group'],
        },
        'security_group': {
            'self': [],
            'access_control_list': ['virtual_machine_interface'],
        },
        'access_control_list': {
            'self': ['security_group'],
            'security_group': [],
        },
        'service_appliance_set': {
            'self': [],
            'service_template': ['service_appliance'],
        },
        'service_template': {
            'self': [],
            'service_instance': ['service_appliance_set'],
        },
        'service_instance': {
            'self': ['port_tuple'],
            'port_tuple': ['service_template'],
        },
        'port_tuple': {
            'self': ['virtual_machine_interface', 'service_instance'],
            'logical_router': ['service_instance'],
            'service_instance': ['virtual_machine_interface', 'service_template'],
            'virtual_machine_interface': ['service_instance']
        },
        'virtual_network': {
            'self': ['physical_router', 'data_center_interconnect',
                     'virtual_machine_interface', 'logical_router',
                     'fabric', 'floating_ip_pool', 'tag', 'network_ipam'],
            'routing_instance': ['physical_router', 'logical_router',
                                 'virtual_machine_interface'],
            'physical_router': [],
            'logical_router': ['physical_router'],
            'data_center_interconnect': ['physical_router'],
            'virtual_machine_interface': ['physical_router'],
            'floating_ip_pool': ['physical_router'],
            'network_ipam': ['tag']
        },
        'logical_router': {
            'self': ['physical_router', 'virtual_network', 'port_tuple'],
            'physical_router': [],
            'data_center_interconnect': ['physical_router'],
            'virtual_network': ['physical_router'],
            'routing_instance': ['physical_router'],
            'virtual_machine_interface': ['physical_router']
        },
        'routing_instance': {
            'self': ['routing_instance',
                     'virtual_network',
                     'virtual_machine_interface'],
            'routing_instance': ['virtual_network',
                                 'virtual_machine_interface'],
            'virtual_network': []
        },
        'floating_ip': {
            'self': ['virtual_machine_interface', 'floating_ip_pool'],
            'virtual_machine_interface': ['floating_ip_pool'],
            'floating_ip_pool': ['virtual_machine_interface']
        },
        'floating_ip_pool': {
            'self': ['virtual_network'],
            'virtual_network': ['floating_ip'],
            'floating_ip': ['virtual_network']
        },
        'instance_ip': {
            'self': ['virtual_machine_interface', 'logical_interface'],
            'virtual_machine_interface': [],
        },
        'service_endpoint': {
            'self': ['physical_router',
                     'virtual_machine_interface',
                     'service_connection_module'],
            'physical_router': ['service_connection_module'],
            'logical_interface': ['service_connection_module'],
            'virtual_machine_interface': ['service_connection_module'],
            'service_connection_module': [],
        },
        'service_connection_module': {
            'self': ['service_endpoint'],
            'service_endpoint': [],
        },
        'service_object': {
            'self': [],
        },
        'network_device_config': {
            'self': [],
        },
        'e2_service_provider': {
            'self': ['physical_router',
                     'peering_policy'],
            'physical_router': [],
            'peering_policy': [],
        },
        'peering_policy': {
            'self': ['e2_service_provider'],
            'e2_service_provider': [],
        },
        'tag': {
            'self': ['port'],
            'virtual_network': ['port']
        },
        'network_ipam': {
            'self': ['virtual_network'],
            'virtual_network': ['tag']
        },
        'port': {
            'self': ['physical_interface'],
            'tag': ['physical_interface'],
        },
    }

    _instance = None

    def __init__(self, dm_logger=None, args=None, zookeeper_client=None,
                 amqp_client=None):
        DeviceManager._instance = self
        self._args = args
        self._amqp_client = amqp_client
        self.logger = dm_logger or DeviceManagerLogger(args)
        self._vnc_amqp = DMAmqpHandle(self.logger, self.REACTION_MAP,
                                      self._args)

        PushConfigState.set_push_mode(int(self._args.push_mode))
        PushConfigState.set_repush_interval(int(self._args.repush_interval))
        PushConfigState.set_repush_max_interval(
            int(self._args.repush_max_interval))
        PushConfigState.set_push_delay_per_kb(
            float(self._args.push_delay_per_kb))
        PushConfigState.set_push_delay_max(int(self._args.push_delay_max))
        PushConfigState.set_push_delay_enable(
            bool(self._args.push_delay_enable))

        self._chksum = ""
        if self._args.collectors:
            self._chksum = hashlib.md5(
                ''.join(self._args.collectors)).hexdigest()

        # Register Plugins
        try:
            DeviceConf.register_plugins()
        except DeviceConf.PluginsRegistrationFailed as e:
            self.logger.error("Exception: " + str(e))
        except Exception as e:
            tb = traceback.format_exc()
            self.logger.error(
                "Internal error while registering plugins: " + str(e) + tb)

        # Register Ansible Plugins
        try:
            AnsibleBase.register_plugins()
        except AnsibleBase.PluginsRegistrationFailed as e:
            self.logger.error("Exception: " + str(e))
        except Exception as e:
            tb = traceback.format_exc()
            self.logger.error(
                "Internal error while registering ansible plugins: " +
                str(e) + tb)

        # Register Feature Plugins
        try:
            FeatureBase.register_plugins()
        except FeatureBase.PluginRegistrationFailed as e:
            self.logger.error("Exception: " + str(e))
        except Exception as e:
            tb = traceback.format_exc()
            self.logger.error(
                "Internal error while registering feature plugins: " +
                str(e) + tb)
            raise e

        # Retry till API server is up
        connected = False
        self.connection_state_update(ConnectionStatus.INIT)
        api_server_list = args.api_server_ip.split(',')
        while not connected:
            try:
                self._vnc_lib = VncApi(
                    args.admin_user, args.admin_password,
                    args.admin_tenant_name, api_server_list,
                    args.api_server_port,
                    api_server_use_ssl=args.api_server_use_ssl)
                connected = True
                self.connection_state_update(ConnectionStatus.UP)
            except requests.exceptions.ConnectionError as e:
                # Update connection info
                self.connection_state_update(ConnectionStatus.DOWN, str(e))
                time.sleep(3)
            except ResourceExhaustionError:  # haproxy throws 503
                time.sleep(3)

        if PushConfigState.is_push_mode_ansible():
            FabricManager.initialize(args, dm_logger, self._vnc_lib)
        # Initialize amqp
        self._vnc_amqp.establish()

        # Initialize cassandra
        self._object_db = DMCassandraDB.get_instance(zookeeper_client, self._args, self.logger)
        DBBaseDM.init(self, self.logger, self._object_db)
        DBBaseDM._sandesh = self.logger._sandesh

        GlobalSystemConfigDM.locate_all()
        FeatureDM.locate_all()
        PhysicalRoleDM.locate_all()
        OverlayRoleDM.locate_all()
        RoleDefinitionDM.locate_all()
        FeatureConfigDM.locate_all()
        NodeProfileDM.locate_all()
        RoleConfigDM.locate_all()
        GlobalVRouterConfigDM.locate_all()
        VirtualNetworkDM.locate_all()
        DataCenterInterconnectDM.locate_all()
        FabricDM.locate_all()
        FabricNamespaceDM.locate_all()
        LogicalRouterDM.locate_all()
        RoutingInstanceDM.locate_all()
        FloatingIpPoolDM.locate_all()
        BgpRouterDM.locate_all()
        PhysicalInterfaceDM.locate_all()
        LogicalInterfaceDM.locate_all()
        PhysicalRouterDM.locate_all()
        LinkAggregationGroupDM.locate_all()
        VirtualPortGroupDM.locate_all()
        PortDM.locate_all()
        TagDM.locate_all()
        NetworkIpamDM.locate_all()
        VirtualMachineInterfaceDM.locate_all()
        SecurityGroupDM.locate_all()
        AccessControlListDM.locate_all()
        ServiceInstanceDM.locate_all()
        ServiceApplianceSetDM.locate_all()
        ServiceApplianceDM.locate_all()
        ServiceTemplateDM.locate_all()
        PortTupleDM.locate_all()
        InstanceIpDM.locate_all()
        FloatingIpDM.locate_all()

        for vn in VirtualNetworkDM.values():
            vn.update_instance_ip_map()

        ServiceEndpointDM.locate_all()
        ServiceConnectionModuleDM.locate_all()
        ServiceObjectDM.locate_all()
        NetworkDeviceConfigDM.locate_all()
        E2ServiceProviderDM.locate_all()
        PeeringPolicyDM.locate_all()

        pr_obj_list = PhysicalRouterDM.list_obj()
        pr_uuid_set = set([pr_obj['uuid'] for pr_obj in pr_obj_list])
        self._object_db.handle_pr_deletes(pr_uuid_set)

        dci_obj_list = DataCenterInterconnectDM.list_obj()
        dci_uuid_set = set([dci_obj['uuid'] for dci_obj in dci_obj_list])
        self._object_db.handle_dci_deletes(dci_uuid_set)

        si_obj_list = ServiceInstanceDM.list_obj()
        si_uuid_set = set([si_obj['uuid'] for si_obj in si_obj_list])
        self._object_db.handle_pnf_resource_deletes(si_uuid_set)

        for pr in PhysicalRouterDM.values():
            pr.set_config_state()
            pr.uve_send()

        self._vnc_amqp._db_resync_done.set()

        gevent.joinall(self._vnc_amqp._vnc_kombu.greenlets())
    # end __init__

    def get_analytics_config(self):
        return {
            'ips': self._args.analytics_server_ip.split(','),
            'port': self._args.analytics_server_port,
            'username': self._args.analytics_username,
            'password': self._args.analytics_password
        }
    # end get_analytics_config

    def get_api_server_config(self):
        return {
            'ips': self._args.api_server_ip.split(','),
            'port': self._args.api_server_port,
            'username': self._args.admin_user,
            'password': self._args.admin_password,
            'tenant': self._args.admin_tenant_name,
            'use_ssl': self._args.api_server_use_ssl
        }
    # end get_api_server_config

    def get_job_status_config(self):
        return {
            'timeout': int(self._args.job_status_retry_timeout),
            'max_retries': int(self._args.job_status_max_retries)
        }
    # end get_job_status_config

    @classmethod
    def get_instance(cls):
        return cls._instance
     # end get_instance

    @classmethod
    def destroy_instance(cls):
        inst = cls.get_instance()
        if not inst:
            return
        inst._vnc_amqp.close()
        if PushConfigState.is_push_mode_ansible():
            FabricManager.destroy_instance()
        for obj_cls in DBBaseDM.get_obj_type_map().values():
            obj_cls.reset()
        DBBase.clear()
        DMCassandraDB.clear_instance()
        cls._instance = None
    # end destroy_instance

    def connection_state_update(self, status, message=None):
        ConnectionState.update(
            conn_type=ConnType.APISERVER, name='ApiServer',
            status=status,
            message=message or 'ApiServer Connection State updated',
            server_addrs=['%s:%s' % (self._args.api_server_ip,
                                     self._args.api_server_port)])
    # end connection_state_update

    # sighup handler for applying new configs
    def sighup_handler(self):
        if self._args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.read(self._args.conf_file)
            if 'DEFAULTS' in config.sections():
               try:
                   collectors = config.get('DEFAULTS', 'collectors')
                   if type(collectors) is str:
                       collectors = collectors.split()
                       new_chksum = hashlib.md5("".join(collectors)).hexdigest()
                       if new_chksum != self._chksum:
                           self._chksum = new_chksum
                           config.random_collectors = random.sample(collectors, len(collectors))
                       # Reconnect to achieve load-balance irrespective of list
                       self.logger.sandesh_reconfig_collectors(config)
               except ConfigParser.NoOptionError as _:
                   pass
class DeviceManager(object):
    REACTION_MAP = {
        'physical_router': {
            'self': ['bgp_router',
                     'physical_interface',
                     'logical_interface',
                     'e2_service_provider',
                     'service_endpoint'],
            'bgp_router': [],
            'physical_interface': [],
            'logical_interface': [],
            'virtual_network': [],
            'logical_router': [],
            'global_system_config': [],
            'network_device_config': [],
            'service_endpoint': [],
            'service_connection_module': [],
            'service_object': [],
            'e2_service_provider': [],
        },
        'global_system_config': {
            'self': ['physical_router'],
            'physical_router': [],
        },
        'bgp_router': {
            'self': ['bgp_router', 'physical_router'],
            'bgp_router': ['physical_router'],
            'physical_router': [],
        },
        'physical_interface': {
            'self': ['physical_router',
                     'physical_interface',
                     'logical_interface'],
            'physical_router': ['logical_interface'],
            'logical_interface': ['physical_router'],
            'physical_interface': ['physical_router'],
            'virtual_machine_interface': ['physical_interface'],
        },
        'logical_interface': {
            'self': ['physical_router',
                     'physical_interface',
                     'virtual_machine_interface',
                     'service_endpoint'],
            'physical_interface': ['virtual_machine_interface'],
            'virtual_machine_interface': ['physical_router',
                                          'physical_interface'],
            'physical_router': ['virtual_machine_interface']
        },
        'virtual_machine_interface': {
            'self': ['logical_interface',
                     'physical_interface',
                     'virtual_network',
                     'logical_router',
                     'floating_ip',
                     'instance_ip',
                     'port_tuple',
                     'service_endpoint'],
            'logical_interface': ['virtual_network'],
            'virtual_network': ['logical_interface', 'logical_router'],
            'logical_router': [],
            'floating_ip': ['virtual_network'],
            'instance_ip': ['virtual_network'],
            'routing_instance': ['port_tuple','physical_interface'],
            'port_tuple': ['physical_interface'],
            'service_endpoint': ['physical_router'],
        },
        'service_instance': {
            'self': ['port_tuple'],
            'port_tuple':[],
        },
        'port_tuple':{
            'self':['virtual_machine_interface','service_instance'],
            'service_instance':['virtual_machine_interface'],
            'virtual_machine_interface':['service_instance']
        },
        'virtual_network': {
            'self': ['physical_router',
                     'virtual_machine_interface', 'logical_router'],
            'routing_instance': ['physical_router',
                                 'virtual_machine_interface'],
            'physical_router': [],
            'logical_router': ['physical_router'],
            'virtual_machine_interface': ['physical_router'],
        },
        'logical_router': {
            'self': ['physical_router', 'virtual_network'],
            'physical_router': [],
            'virtual_network': ['physical_router'],
            'virtual_machine_interface': ['physical_router']
        },
        'routing_instance': {
            'self': ['routing_instance',
                     'virtual_network',
                     'virtual_machine_interface'],
            'routing_instance': ['virtual_network',
                                 'virtual_machine_interface'],
            'virtual_network': []
        },
        'floating_ip': {
            'self': ['virtual_machine_interface'],
            'virtual_machine_interface': [],
        },
        'instance_ip': {
            'self': ['virtual_machine_interface'],
            'virtual_machine_interface': [],
        },
        'service_endpoint': {
            'self': ['physical_router',
                     'virtual_machine_interface',
                     'service_connection_module'],
            'physical_router': ['service_connection_module'],
            'logical_interface': ['service_connection_module'],
            'virtual_machine_interface': ['service_connection_module'],
            'service_connection_module': [],
        },
        'service_connection_module': {
            'self': ['service_endpoint'],
            'service_endpoint': [],
        },
        'service_object': {
            'self': [],
        },
        'network_device_config': {
            'self': [],
        },
        'e2_service_provider': {
            'self': ['physical_router',
                     'peering_policy'],
            'physical_router': [],
            'peering_policy': [],
        },
        'peering_policy': {
            'self': ['e2_service_provider'],
            'e2_service_provider': [],
        },
    }

    _device_manager = None

    def __init__(self, dm_logger=None, args=None):
        self._args = args
        PushConfigState.set_repush_interval(int(self._args.repush_interval))
        PushConfigState.set_repush_max_interval(int(self._args.repush_max_interval))
        PushConfigState.set_push_delay_per_kb(float(self._args.push_delay_per_kb))
        PushConfigState.set_push_delay_max(int(self._args.push_delay_max))
        PushConfigState.set_push_delay_enable(bool(self._args.push_delay_enable))

        self._chksum = "";
        if self._args.collectors:
            self._chksum = hashlib.md5(''.join(self._args.collectors)).hexdigest()

        # Initialize logger
        self.logger = dm_logger or DeviceManagerLogger(args)

        # Register Plugins
        try:
            DeviceConf.register_plugins()
        except DeviceConf.PluginsRegistrationFailed as e:
            self.logger.error("Exception: " + str(e))
        except Exception as e:
            tb = traceback.format_exc()
            self.logger.error("Internal error while registering plugins: " + str(e) + tb)

        # Retry till API server is up
        connected = False
        self.connection_state_update(ConnectionStatus.INIT)
        while not connected:
            try:
                self._vnc_lib = VncApi(
                    args.admin_user, args.admin_password,
                    args.admin_tenant_name, args.api_server_ip,
                    args.api_server_port,
                    api_server_use_ssl=args.api_server_use_ssl)
                connected = True
                self.connection_state_update(ConnectionStatus.UP)
            except requests.exceptions.ConnectionError as e:
                # Update connection info
                self.connection_state_update(ConnectionStatus.DOWN, str(e))
                time.sleep(3)
            except ResourceExhaustionError:  # haproxy throws 503
                time.sleep(3)

        """ @sighup
        Handle of SIGHUP for collector list config change
        """
        gevent.signal(signal.SIGHUP, self.sighup_handler)

        # Initialize amqp
        self._vnc_amqp = DMAmqpHandle(self.logger, self.REACTION_MAP,
                                      self._args)
        self._vnc_amqp.establish()

        # Initialize cassandra
        self._object_db = DMCassandraDB.get_instance(self, _zookeeper_client)
        DBBaseDM.init(self, self.logger, self._object_db)
        DBBaseDM._sandesh = self.logger._sandesh

        for obj in GlobalSystemConfigDM.list_obj():
            GlobalSystemConfigDM.locate(obj['uuid'], obj)

        for obj in GlobalVRouterConfigDM.list_obj():
            GlobalVRouterConfigDM.locate(obj['uuid'], obj)

        for obj in VirtualNetworkDM.list_obj():
            VirtualNetworkDM.locate(obj['uuid'], obj)

        for obj in LogicalRouterDM.list_obj():
            LogicalRouterDM.locate(obj['uuid'], obj)

        for obj in RoutingInstanceDM.list_obj():
            RoutingInstanceDM.locate(obj['uuid'], obj)

        for obj in BgpRouterDM.list_obj():
            BgpRouterDM.locate(obj['uuid'], obj)

        pr_obj_list = PhysicalRouterDM.list_obj()
        for obj in pr_obj_list:
            PhysicalRouterDM.locate(obj['uuid'],obj)

        pr_uuid_set = set([pr_obj['uuid'] for pr_obj in pr_obj_list])
        self._object_db.handle_pr_deletes(pr_uuid_set)

        for obj in PortTupleDM.list_obj():
            PortTupleDM.locate(obj['uuid'],obj)

        for obj in PhysicalInterfaceDM.list_obj():
            PhysicalInterfaceDM.locate(obj['uuid'],obj)

        for obj in LogicalInterfaceDM.list_obj():
            LogicalInterfaceDM.locate(obj['uuid'],obj)

        for obj in VirtualMachineInterfaceDM.list_obj():
            VirtualMachineInterfaceDM.locate(obj['uuid'],obj)

        for obj in pr_obj_list:
            pr = PhysicalRouterDM.locate(obj['uuid'], obj)
            li_set = pr.logical_interfaces
            vmi_set = set()
            for pi_id in pr.physical_interfaces:
                pi = PhysicalInterfaceDM.locate(pi_id)
                if pi:
                    li_set |= pi.logical_interfaces
                    vmi_set |= pi.virtual_machine_interfaces
            for li_id in li_set:
                li = LogicalInterfaceDM.locate(li_id)
                if li and li.virtual_machine_interface:
                    vmi_set |= set([li.virtual_machine_interface])
            for vmi_id in vmi_set:
                vmi = VirtualMachineInterfaceDM.locate(vmi_id)

        si_obj_list = ServiceInstanceDM.list_obj()
        si_uuid_set = set([si_obj['uuid'] for si_obj in si_obj_list])
        self._object_db.handle_pnf_resource_deletes(si_uuid_set)

        for obj in si_obj_list:
            ServiceInstanceDM.locate(obj['uuid'], obj)

        for obj in InstanceIpDM.list_obj():
            InstanceIpDM.locate(obj['uuid'], obj)

        for obj in FloatingIpDM.list_obj():
            FloatingIpDM.locate(obj['uuid'], obj)

        for vn in VirtualNetworkDM.values():
            vn.update_instance_ip_map()

        for obj in ServiceEndpointDM.list_obj():
            ServiceEndpointDM.locate(obj['uuid'], obj)

        for obj in ServiceConnectionModuleDM.list_obj():
            ServiceConnectionModuleDM.locate(obj['uuid'], obj)

        for obj in ServiceObjectDM.list_obj():
            ServiceObjectDM.locate(obj['uuid'], obj)

        for obj in NetworkDeviceConfigDM.list_obj():
            NetworkDeviceConfigDM.locate(obj['uuid'], obj)

        for obj in E2ServiceProviderDM.list_obj():
            E2ServiceProviderDM.locate(obj['uuid'], obj)

        for obj in PeeringPolicyDM.list_obj():
            PeeringPolicyDM.locate(obj['uuid'], obj)

        for pr in PhysicalRouterDM.values():
            pr.set_config_state()

        DeviceManager._device_manager = self
        self._vnc_amqp._db_resync_done.set()
        try:
            gevent.joinall(self._vnc_amqp._vnc_kombu.greenlets())
        except KeyboardInterrupt:
            DeviceManager.destroy_instance()
            raise
    # end __init__

    @classmethod
    def get_instance(cls):
        return cls._device_manager

    @classmethod
    def destroy_instance(cls):
        inst = cls.get_instance()
        if not inst:
            return
        inst._vnc_amqp.close()
        for obj_cls in DBBaseDM.get_obj_type_map().values():
            obj_cls.reset()
        DBBase.clear()
        DMCassandraDB.clear_instance()
        inst._object_db = None
        cls._device_manager = None

    def connection_state_update(self, status, message=None):
        ConnectionState.update(
            conn_type=ConnType.APISERVER, name='ApiServer',
            status=status, message=message or '',
            server_addrs=['%s:%s' % (self._args.api_server_ip,
                                     self._args.api_server_port)])
    # end connection_state_update

    # sighup handler for applying new configs
    def sighup_handler(self):
        if self._args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.read(self._args.conf_file)
            if 'DEFAULTS' in config.sections():
               try:
                   collectors = config.get('DEFAULTS', 'collectors')
                   if type(collectors) is str:
                       collectors = collectors.split()
                       new_chksum = hashlib.md5("".join(collectors)).hexdigest()
                       if new_chksum != self._chksum:
                           self._chksum = new_chksum
                           config.random_collectors = random.sample(collectors, len(collectors))
                       # Reconnect to achieve load-balance irrespective of list
                       self.logger.sandesh_reconfig_collectors(config)
               except ConfigParser.NoOptionError as e:
                   pass
Ejemplo n.º 10
0
class DeviceManager(object):
    _REACTION_MAP = {
        'physical_router': {
            'self': ['bgp_router',
                     'physical_interface',
                     'logical_interface'],
            'bgp_router': [],
            'physical_interface': [],
            'logical_interface': [],
            'virtual_network': [],
            'global_system_config': [],
        },
        'global_system_config': {
            'self': ['physical_router'],
            'physical_router': [],
        },
        'bgp_router': {
            'self': ['bgp_router', 'physical_router'],
            'bgp_router': ['physical_router'],
            'physical_router': [],
        },
        'physical_interface': {
            'self': ['physical_router',
                     'physical_interface',
                     'logical_interface'],
            'physical_router': ['logical_interface'],
            'logical_interface': ['physical_router'],
            'physical_interface': ['physical_router'],
            'virtual_machine_interface': ['physical_interface'],
        },
        'logical_interface': {
            'self': ['physical_router',
                     'physical_interface',
                     'virtual_machine_interface'],
            'physical_interface': ['virtual_machine_interface'],
            'virtual_machine_interface': ['physical_router',
                                          'physical_interface'],
            'physical_router': ['virtual_machine_interface']
        },
        'virtual_machine_interface': {
            'self': ['logical_interface',
                     'physical_interface',
                     'virtual_network',
                     'floating_ip',
                     'instance_ip',
                     'port_tuple'],
            'logical_interface': ['virtual_network'],
            'virtual_network': ['logical_interface'],
            'floating_ip': ['virtual_network'],
            'instance_ip': ['virtual_network'],
            'routing_instance': ['port_tuple','physical_interface'],
            'port_tuple': ['physical_interface']
        },
        'service_instance': {
            'self': ['port_tuple'],
            'port_tuple':[],
        },
        'port_tuple':{
            'self':['virtual_machine_interface','service_instance'],
            'service_instance':['virtual_machine_interface'],
            'virtual_machine_interface':['service_instance']
        },
        'virtual_network': {
            'self': ['physical_router',
                     'virtual_machine_interface'],
            'routing_instance': ['physical_router',
                                 'virtual_machine_interface'],
            'physical_router': [],
            'virtual_machine_interface': ['physical_router'],
        },
        'routing_instance': {
            'self': ['routing_instance',
                     'virtual_network',
                     'virtual_machine_interface'],
            'routing_instance': ['virtual_network',
                                 'virtual_machine_interface'],
            'virtual_network': []
        },
        'floating_ip': {
            'self': ['virtual_machine_interface'],
            'virtual_machine_interface': [],
        },
        'instance_ip': {
            'self': ['virtual_machine_interface'],
            'virtual_machine_interface': [],
        },
    }

    def __init__(self, args=None):
        self._args = args

        # Initialize discovery client
        self._disc = None
        if self._args.disc_server_ip and self._args.disc_server_port:
            self._disc = client.DiscoveryClient(
                self._args.disc_server_ip,
                self._args.disc_server_port,
                ModuleNames[Module.DEVICE_MANAGER])
        
        PushConfigState.set_repush_interval(int(self._args.repush_interval))
        PushConfigState.set_repush_max_interval(int(self._args.repush_max_interval))
        PushConfigState.set_push_delay_per_kb(float(self._args.push_delay_per_kb))
        PushConfigState.set_push_delay_max(int(self._args.push_delay_max))
        PushConfigState.set_push_delay_enable(bool(self._args.push_delay_enable))

        # Initialize logger
        module = Module.DEVICE_MANAGER
        module_pkg = "device_manager"
        self.logger = ConfigServiceLogger(self._disc, module, module_pkg, args)

        # Retry till API server is up
        connected = False
        self.connection_state_update(ConnectionStatus.INIT)
        while not connected:
            try:
                self._vnc_lib = VncApi(
                    args.admin_user, args.admin_password,
                    args.admin_tenant_name, args.api_server_ip,
                    args.api_server_port,
                    api_server_use_ssl=args.api_server_use_ssl)
                connected = True
                self.connection_state_update(ConnectionStatus.UP)
            except requests.exceptions.ConnectionError as e:
                # Update connection info
                self.connection_state_update(ConnectionStatus.DOWN, str(e))
                time.sleep(3)
            except ResourceExhaustionError:  # haproxy throws 503
                time.sleep(3)

        # Initialize amqp
        self._vnc_amqp = DMAmqpHandle(self.logger,
                self._REACTION_MAP, self._args)
        self._vnc_amqp.establish()

        # Initialize cassandra
        self._cassandra = DMCassandraDB.getInstance(self, _zookeeper_client)
        DBBaseDM.init(self, self.logger, self._cassandra)
        DBBaseDM._sandesh = self.logger._sandesh

        for obj in GlobalSystemConfigDM.list_obj():
            GlobalSystemConfigDM.locate(obj['uuid'], obj)

        for obj in GlobalVRouterConfigDM.list_obj():
            GlobalVRouterConfigDM.locate(obj['uuid'], obj)

        for obj in VirtualNetworkDM.list_obj():
            vn = VirtualNetworkDM.locate(obj['uuid'], obj)
            if vn is not None and vn.routing_instances is not None:
                for ri_id in vn.routing_instances:
                    ri_obj = RoutingInstanceDM.locate(ri_id)

        for obj in BgpRouterDM.list_obj():
            BgpRouterDM.locate(obj['uuid'], obj)

        pr_obj_list = PhysicalRouterDM.list_obj()
        pr_uuid_set = set([pr_obj['uuid'] for pr_obj in pr_obj_list])
        self._cassandra.handle_pr_deletes(pr_uuid_set)

        for obj in PortTupleDM.list_obj():
            PortTupleDM.locate(obj['uuid'],obj)

        for obj in pr_obj_list:
            pr = PhysicalRouterDM.locate(obj['uuid'], obj)
            li_set = pr.logical_interfaces
            vmi_set = set()
            for pi_id in pr.physical_interfaces:
                pi = PhysicalInterfaceDM.locate(pi_id)
                if pi:
                    li_set |= pi.logical_interfaces
                    vmi_set |= pi.virtual_machine_interfaces
            for li_id in li_set:
                li = LogicalInterfaceDM.locate(li_id)
                if li and li.virtual_machine_interface:
                    vmi_set |= set([li.virtual_machine_interface])
            for vmi_id in vmi_set:
                vmi = VirtualMachineInterfaceDM.locate(vmi_id)

        si_obj_list = ServiceInstanceDM.list_obj()
        si_uuid_set = set([si_obj['uuid'] for si_obj in si_obj_list])
        self._cassandra.handle_pnf_resource_deletes(si_uuid_set)

        for obj in si_obj_list:
            ServiceInstanceDM.locate(obj['uuid'], obj)

        for obj in InstanceIpDM.list_obj():
            InstanceIpDM.locate(obj['uuid'], obj)

        for obj in FloatingIpDM.list_obj():
            FloatingIpDM.locate(obj['uuid'], obj)

        for vn in VirtualNetworkDM.values():
            vn.update_instance_ip_map()

        for pr in PhysicalRouterDM.values():
            pr.set_config_state()

        self._vnc_amqp._db_resync_done.set()
        gevent.joinall(self._vnc_amqp._vnc_kombu.greenlets())
    # end __init__

    def connection_state_update(self, status, message=None):
        ConnectionState.update(
            conn_type=ConnType.APISERVER, name='ApiServer',
            status=status, message=message or '',
            server_addrs=['%s:%s' % (self._args.api_server_ip,
                                     self._args.api_server_port)])
Ejemplo n.º 11
0
def main(args_str=None):
    global _amqp_client
    global _zookeeper_client
    global _object_db

    if not args_str:
        args_str = ' '.join(sys.argv[1:])
    args = parse_args(args_str)
    if args.cluster_id:
        client_pfx = args.cluster_id + '-'
        zk_path_pfx = args.cluster_id + '/'
    else:
        client_pfx = ''
        zk_path_pfx = ''

    # randomize collector list
    args.random_collectors = args.collectors
    if args.collectors:
        args.random_collectors = random.sample(args.collectors,
                                               len(args.collectors))

    # Initialize logger without introspect thread
    dm_logger = DeviceManagerLogger(args, http_server_port=-1)

    # Initialize AMQP handler then close it to be sure remain queue of a
    # precedent run is cleaned
    vnc_amqp = DMAmqpHandle(dm_logger, {}, args)
    vnc_amqp.establish()
    vnc_amqp.close()
    dm_logger.debug("Removed remaining AMQP queue from previous run")

    if 'host_ip' in args:
        host_ip = args.host_ip
    else:
        host_ip = socket.gethostbyname(socket.getfqdn())

    _amqp_client = initialize_amqp_client(dm_logger, args)
    _zookeeper_client = ZookeeperClient(client_pfx+"device-manager",
                                        args.zk_server_ip, host_ip)
    _object_db = DMCassandraDB.get_instance(_zookeeper_client, args, dm_logger)

    try:
        # Initialize the device job manager
        DeviceJobManager(_object_db, _amqp_client, _zookeeper_client, args,
                         dm_logger)
    except Exception as e:
        dm_logger.error("Error while initializing the device job "
                        "manager %s" % repr(e))

    try:
        # Initialize the device ztp manager
        DeviceZtpManager(_amqp_client, args, dm_logger)
    except Exception as e:
        dm_logger.error("Error while initializing the device ztp "
                        "manager %s" % repr(e))

    gevent.signal(signal.SIGHUP, sighup_handler)
    gevent.signal(signal.SIGTERM, sigterm_handler)
    gevent.signal(signal.SIGINT, sigterm_handler)

    dm_logger.notice("Waiting to be elected as master...")
    _zookeeper_client.master_election(zk_path_pfx+"/device-manager",
                                      os.getpid(), run_device_manager,
                                      dm_logger, args)
Ejemplo n.º 12
0
class DeviceManager(object):
    _REACTION_MAP = {
        "physical_router": {
            "self": ["bgp_router", "physical_interface", "logical_interface"],
            "bgp_router": [],
            "physical_interface": [],
            "logical_interface": [],
            "virtual_network": [],
            "global_system_config": [],
        },
        "global_system_config": {"self": ["physical_router"], "physical_router": []},
        "bgp_router": {
            "self": ["bgp_router", "physical_router"],
            "bgp_router": ["physical_router"],
            "physical_router": [],
        },
        "physical_interface": {
            "self": ["physical_router", "physical_interface", "logical_interface"],
            "physical_router": ["logical_interface"],
            "logical_interface": ["physical_router"],
            "physical_interface": ["physical_router"],
            "virtual_machine_interface": ["physical_interface"],
        },
        "logical_interface": {
            "self": ["physical_router", "physical_interface", "virtual_machine_interface"],
            "physical_interface": ["virtual_machine_interface"],
            "virtual_machine_interface": ["physical_router", "physical_interface"],
            "physical_router": ["virtual_machine_interface"],
        },
        "virtual_machine_interface": {
            "self": [
                "logical_interface",
                "physical_interface",
                "virtual_network",
                "floating_ip",
                "instance_ip",
                "port_tuple",
            ],
            "logical_interface": ["virtual_network"],
            "virtual_network": ["logical_interface"],
            "floating_ip": ["virtual_network"],
            "instance_ip": ["virtual_network"],
            "routing_instance": ["port_tuple", "physical_interface"],
            "port_tuple": ["physical_interface"],
        },
        "service_instance": {"self": ["port_tuple"], "port_tuple": []},
        "port_tuple": {
            "self": ["virtual_machine_interface", "service_instance"],
            "service_instance": ["virtual_machine_interface"],
            "virtual_machine_interface": ["service_instance"],
        },
        "virtual_network": {
            "self": ["physical_router", "virtual_machine_interface"],
            "routing_instance": ["physical_router", "virtual_machine_interface"],
            "physical_router": [],
            "virtual_machine_interface": ["physical_router"],
        },
        "routing_instance": {
            "self": ["routing_instance", "virtual_network", "virtual_machine_interface"],
            "routing_instance": ["virtual_network", "virtual_machine_interface"],
            "virtual_network": [],
        },
        "floating_ip": {"self": ["virtual_machine_interface"], "virtual_machine_interface": []},
        "instance_ip": {"self": ["virtual_machine_interface"], "virtual_machine_interface": []},
    }

    def __init__(self, args=None):
        self._args = args

        # Initialize discovery client
        self._disc = None
        if self._args.disc_server_ip and self._args.disc_server_port:
            self._disc = client.DiscoveryClient(
                self._args.disc_server_ip, self._args.disc_server_port, ModuleNames[Module.DEVICE_MANAGER]
            )

        PushConfigState.set_repush_interval(int(self._args.repush_interval))
        PushConfigState.set_repush_max_interval(int(self._args.repush_max_interval))
        PushConfigState.set_push_delay_per_kb(float(self._args.push_delay_per_kb))
        PushConfigState.set_push_delay_max(int(self._args.push_delay_max))
        PushConfigState.set_push_delay_enable(bool(self._args.push_delay_enable))

        # randomize collector list
        self._args.random_collectors = self._args.collectors
        self._chksum = ""
        if self._args.collectors:
            self._chksum = hashlib.md5("".join(self._args.collectors)).hexdigest()
            self._args.random_collectors = random.sample(self._args.collectors, len(self._args.collectors))

        # Initialize logger
        module = Module.DEVICE_MANAGER
        module_pkg = "device_manager"
        self.logger = ConfigServiceLogger(self._disc, module, module_pkg, args)

        # Retry till API server is up
        connected = False
        self.connection_state_update(ConnectionStatus.INIT)
        while not connected:
            try:
                self._vnc_lib = VncApi(
                    args.admin_user,
                    args.admin_password,
                    args.admin_tenant_name,
                    args.api_server_ip,
                    args.api_server_port,
                    api_server_use_ssl=args.api_server_use_ssl,
                )
                connected = True
                self.connection_state_update(ConnectionStatus.UP)
            except requests.exceptions.ConnectionError as e:
                # Update connection info
                self.connection_state_update(ConnectionStatus.DOWN, str(e))
                time.sleep(3)
            except ResourceExhaustionError:  # haproxy throws 503
                time.sleep(3)

        """ @sighup
        Handle of SIGHUP for collector list config change
        """
        gevent.signal(signal.SIGHUP, self.sighup_handler)

        # Initialize amqp
        self._vnc_amqp = DMAmqpHandle(self.logger, self._REACTION_MAP, self._args)
        self._vnc_amqp.establish()

        # Initialize cassandra
        self._object_db = DMCassandraDB.getInstance(self, _zookeeper_client)
        DBBaseDM.init(self, self.logger, self._object_db)
        DBBaseDM._sandesh = self.logger._sandesh

        for obj in GlobalSystemConfigDM.list_obj():
            GlobalSystemConfigDM.locate(obj["uuid"], obj)

        for obj in GlobalVRouterConfigDM.list_obj():
            GlobalVRouterConfigDM.locate(obj["uuid"], obj)

        for obj in VirtualNetworkDM.list_obj():
            vn = VirtualNetworkDM.locate(obj["uuid"], obj)
            if vn is not None and vn.routing_instances is not None:
                for ri_id in vn.routing_instances:
                    ri_obj = RoutingInstanceDM.locate(ri_id)

        for obj in BgpRouterDM.list_obj():
            BgpRouterDM.locate(obj["uuid"], obj)

        pr_obj_list = PhysicalRouterDM.list_obj()
        pr_uuid_set = set([pr_obj["uuid"] for pr_obj in pr_obj_list])
        self._object_db.handle_pr_deletes(pr_uuid_set)

        for obj in PortTupleDM.list_obj():
            PortTupleDM.locate(obj["uuid"], obj)

        for obj in pr_obj_list:
            pr = PhysicalRouterDM.locate(obj["uuid"], obj)
            li_set = pr.logical_interfaces
            vmi_set = set()
            for pi_id in pr.physical_interfaces:
                pi = PhysicalInterfaceDM.locate(pi_id)
                if pi:
                    li_set |= pi.logical_interfaces
                    vmi_set |= pi.virtual_machine_interfaces
            for li_id in li_set:
                li = LogicalInterfaceDM.locate(li_id)
                if li and li.virtual_machine_interface:
                    vmi_set |= set([li.virtual_machine_interface])
            for vmi_id in vmi_set:
                vmi = VirtualMachineInterfaceDM.locate(vmi_id)

        si_obj_list = ServiceInstanceDM.list_obj()
        si_uuid_set = set([si_obj["uuid"] for si_obj in si_obj_list])
        self._object_db.handle_pnf_resource_deletes(si_uuid_set)

        for obj in si_obj_list:
            ServiceInstanceDM.locate(obj["uuid"], obj)

        for obj in InstanceIpDM.list_obj():
            InstanceIpDM.locate(obj["uuid"], obj)

        for obj in FloatingIpDM.list_obj():
            FloatingIpDM.locate(obj["uuid"], obj)

        for vn in VirtualNetworkDM.values():
            vn.update_instance_ip_map()

        for pr in PhysicalRouterDM.values():
            pr.set_config_state()

        self._vnc_amqp._db_resync_done.set()
        gevent.joinall(self._vnc_amqp._vnc_kombu.greenlets())

    # end __init__

    def connection_state_update(self, status, message=None):
        ConnectionState.update(
            conn_type=ConnType.APISERVER,
            name="ApiServer",
            status=status,
            message=message or "",
            server_addrs=["%s:%s" % (self._args.api_server_ip, self._args.api_server_port)],
        )

    # end connection_state_update

    # sighup handler for applying new configs
    def sighup_handler(self):
        if self._args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.read(self._args.conf_file)
            if "DEFAULTS" in config.sections():
                try:
                    collectors = config.get("DEFAULTS", "collectors")
                    if type(collectors) is str:
                        collectors = collectors.split()
                        new_chksum = hashlib.md5("".join(collectors)).hexdigest()
                        if new_chksum != self._chksum:
                            self._chksum = new_chksum
                            config.random_collectors = random.sample(collectors, len(collectors))
                            self.logger.sandesh_reconfig_collectors(config)
                except ConfigParser.NoOptionError as e:
                    pass
Ejemplo n.º 13
0
class DeviceManager(object):
    _REACTION_MAP = {
        'physical_router': {
            'self': ['bgp_router',
                     'physical_interface',
                     'logical_interface'],
            'bgp_router': [],
            'physical_interface': [],
            'logical_interface': [],
            'virtual_network': [],
            'global_system_config': [],
        },
        'global_system_config': {
            'self': ['physical_router'],
            'physical_router': [],
        },
        'bgp_router': {
            'self': ['bgp_router', 'physical_router'],
            'bgp_router': ['physical_router'],
            'physical_router': [],
        },
        'physical_interface': {
            'self': ['physical_router',
                     'physical_interface',
                     'logical_interface'],
            'physical_router': ['logical_interface'],
            'logical_interface': ['physical_router'],
            'physical_interface': ['physical_router'],
            'virtual_machine_interface': ['physical_interface'],
        },
        'logical_interface': {
            'self': ['physical_router',
                     'physical_interface',
                     'virtual_machine_interface'],
            'physical_interface': ['virtual_machine_interface'],
            'virtual_machine_interface': ['physical_router',
                                          'physical_interface'],
            'physical_router': ['virtual_machine_interface']
        },
        'virtual_machine_interface': {
            'self': ['logical_interface',
                     'physical_interface',
                     'virtual_network',
                     'floating_ip',
                     'instance_ip',
                     'port_tuple'],
            'logical_interface': ['virtual_network'],
            'virtual_network': ['logical_interface'],
            'floating_ip': ['virtual_network'],
            'instance_ip': ['virtual_network'],
            'routing_instance': ['port_tuple','physical_interface'],
            'port_tuple': ['physical_interface']
        },
        'service_instance': {
            'self': ['port_tuple'],
            'port_tuple':[],
        },
        'port_tuple':{
            'self':['virtual_machine_interface','service_instance'],
            'service_instance':['virtual_machine_interface'],
            'virtual_machine_interface':['service_instance']
        },
        'virtual_network': {
            'self': ['physical_router',
                     'virtual_machine_interface'],
            'routing_instance': ['physical_router',
                                 'virtual_machine_interface'],
            'physical_router': [],
            'virtual_machine_interface': ['physical_router'],
        },
        'routing_instance': {
            'self': ['routing_instance',
                     'virtual_network',
                     'virtual_machine_interface'],
            'routing_instance': ['virtual_network',
                                 'virtual_machine_interface'],
            'virtual_network': []
        },
        'floating_ip': {
            'self': ['virtual_machine_interface'],
            'virtual_machine_interface': [],
        },
        'instance_ip': {
            'self': ['virtual_machine_interface'],
            'virtual_machine_interface': [],
        },
    }

    def __init__(self, args=None):
        self._args = args
        PushConfigState.set_repush_interval(int(self._args.repush_interval))
        PushConfigState.set_repush_max_interval(int(self._args.repush_max_interval))
        PushConfigState.set_push_delay_per_kb(float(self._args.push_delay_per_kb))
        PushConfigState.set_push_delay_max(int(self._args.push_delay_max))
        PushConfigState.set_push_delay_enable(bool(self._args.push_delay_enable))
  
        # randomize collector list
        self._args.random_collectors = self._args.collectors
        self._chksum = "";
        if self._args.collectors:
            self._chksum = hashlib.md5(''.join(self._args.collectors)).hexdigest()
            self._args.random_collectors = random.sample(self._args.collectors, \
                                                      len(self._args.collectors))
    
        # Initialize logger
        module = Module.DEVICE_MANAGER
        module_pkg = "device_manager"
        self.logger = ConfigServiceLogger(module, module_pkg, args)

        # Retry till API server is up
        connected = False
        self.connection_state_update(ConnectionStatus.INIT)
        while not connected:
            try:
                self._vnc_lib = VncApi(
                    args.admin_user, args.admin_password,
                    args.admin_tenant_name, args.api_server_ip,
                    args.api_server_port,
                    api_server_use_ssl=args.api_server_use_ssl)
                connected = True
                self.connection_state_update(ConnectionStatus.UP)
            except requests.exceptions.ConnectionError as e:
                # Update connection info
                self.connection_state_update(ConnectionStatus.DOWN, str(e))
                time.sleep(3)
            except ResourceExhaustionError:  # haproxy throws 503
                time.sleep(3)

        """ @sighup
        Handle of SIGHUP for collector list config change
        """
        gevent.signal(signal.SIGHUP, self.sighup_handler)

        # Initialize amqp
        self._vnc_amqp = DMAmqpHandle(self.logger,
                self._REACTION_MAP, self._args)
        self._vnc_amqp.establish()

        # Initialize cassandra
        self._object_db = DMCassandraDB.getInstance(self, _zookeeper_client)
        DBBaseDM.init(self, self.logger, self._object_db)
        DBBaseDM._sandesh = self.logger._sandesh

        for obj in GlobalSystemConfigDM.list_obj():
            GlobalSystemConfigDM.locate(obj['uuid'], obj)

        for obj in GlobalVRouterConfigDM.list_obj():
            GlobalVRouterConfigDM.locate(obj['uuid'], obj)

        for obj in VirtualNetworkDM.list_obj():
            vn = VirtualNetworkDM.locate(obj['uuid'], obj)
            if vn is not None and vn.routing_instances is not None:
                for ri_id in vn.routing_instances:
                    ri_obj = RoutingInstanceDM.locate(ri_id)

        for obj in BgpRouterDM.list_obj():
            BgpRouterDM.locate(obj['uuid'], obj)

        pr_obj_list = PhysicalRouterDM.list_obj()
        pr_uuid_set = set([pr_obj['uuid'] for pr_obj in pr_obj_list])
        self._object_db.handle_pr_deletes(pr_uuid_set)

        for obj in PortTupleDM.list_obj():
            PortTupleDM.locate(obj['uuid'],obj)

        for obj in pr_obj_list:
            pr = PhysicalRouterDM.locate(obj['uuid'], obj)
            li_set = pr.logical_interfaces
            vmi_set = set()
            for pi_id in pr.physical_interfaces:
                pi = PhysicalInterfaceDM.locate(pi_id)
                if pi:
                    li_set |= pi.logical_interfaces
                    vmi_set |= pi.virtual_machine_interfaces
            for li_id in li_set:
                li = LogicalInterfaceDM.locate(li_id)
                if li and li.virtual_machine_interface:
                    vmi_set |= set([li.virtual_machine_interface])
            for vmi_id in vmi_set:
                vmi = VirtualMachineInterfaceDM.locate(vmi_id)

        si_obj_list = ServiceInstanceDM.list_obj()
        si_uuid_set = set([si_obj['uuid'] for si_obj in si_obj_list])
        self._object_db.handle_pnf_resource_deletes(si_uuid_set)

        for obj in si_obj_list:
            ServiceInstanceDM.locate(obj['uuid'], obj)

        for obj in InstanceIpDM.list_obj():
            InstanceIpDM.locate(obj['uuid'], obj)

        for obj in FloatingIpDM.list_obj():
            FloatingIpDM.locate(obj['uuid'], obj)

        for vn in VirtualNetworkDM.values():
            vn.update_instance_ip_map()

        for pr in PhysicalRouterDM.values():
            pr.set_config_state()

        self._vnc_amqp._db_resync_done.set()
        gevent.joinall(self._vnc_amqp._vnc_kombu.greenlets())
    # end __init__

    def connection_state_update(self, status, message=None):
        ConnectionState.update(
            conn_type=ConnType.APISERVER, name='ApiServer',
            status=status, message=message or '',
            server_addrs=['%s:%s' % (self._args.api_server_ip,
                                     self._args.api_server_port)])
    # end connection_state_update

    # sighup handler for applying new configs
    def sighup_handler(self):
        if self._args.conf_file:
            config = ConfigParser.SafeConfigParser()
            config.read(self._args.conf_file)
            if 'DEFAULTS' in config.sections():
               try:
                   collectors = config.get('DEFAULTS', 'collectors')
                   if type(collectors) is str:
                       collectors = collectors.split()
                       new_chksum = hashlib.md5("".join(collectors)).hexdigest()
                       if new_chksum != self._chksum:
                           self._chksum = new_chksum
                           config.random_collectors = random.sample(collectors, len(collectors))
                           self.logger.sandesh_reconfig_collectors(config)
               except ConfigParser.NoOptionError as e:
                   pass