def setUp(self):
        self.vnc_lib = mock.Mock()
        self.cassandra = mock.Mock()
        self.logger = mock.Mock()
        self.svc = mock.Mock()

        mocked_gsc = mock.MagicMock()
        mocked_gsc.uuid = 'fake-gsc-uuid'
        self.vnc_lib.service_appliance_set_create.return_value = "opencontrail"
        self.vnc_lib.global_system_config_read.return_value = mocked_gsc
        def no_id_side_effect(fq_name):
            raise NoIdError("xxx")
        self.vnc_lib.service_appliance_set_read = mock.Mock(side_effect=no_id_side_effect)
        conf_parser = argparse.ArgumentParser(add_help=False)
        config = ConfigParser.SafeConfigParser({'admin_token': None})
        self._args, remaining_argv = conf_parser.parse_known_args()
        self._args.config_sections = config

        def sas_read_side_effect(obj_type, uuids):
            if obj_type == 'service_appliance_set':
                return (True, [{
                   'fq_name': ['default-global-system-config', 'opencontrail'],
                   'service_appliance_driver': 'svc_monitor.services.loadbalancer.drivers.ha_proxy.driver.OpencontrailLoadbalancerDriver'
                   }])
            return (False, None)
        self.cassandra.read = mock.Mock(
            side_effect=sas_read_side_effect)
        DBBase.init(self.svc, None, self.cassandra)
        self.lb_agent = loadbalancer_agent.LoadbalancerAgent(self.svc, self.vnc_lib,
                                               self.cassandra, self._args)
        self.svc.loadbalancer_agent = self.lb_agent
        sas = config_db.ServiceApplianceSetSM.get('opencontrail')
        self.assertEqual(sas.driver, "svc_monitor.services.loadbalancer.drivers.ha_proxy.driver.OpencontrailLoadbalancerDriver")
        sas.add()
        self.assertIsNotNone(self.lb_agent._loadbalancer_driver['opencontrail'])
    def setUp(self):
        self.vnc_lib = mock.Mock()

        self.cassandra = mock.Mock()
        self.logger = mock.Mock()

        self.svc = mock.Mock()
        self.svc.netns_manager = instance_manager.NetworkNamespaceManager(
            self.vnc_lib, self.cassandra, self.logger, None, None, None
        )

        self.snat_agent = snat_agent.SNATAgent(self.svc, self.vnc_lib, self.cassandra, None)
        DBBase.init(self, None, self.cassandra)

        # register the project
        proj_fq_name = ["default-domain", "demo"]
        config_db.ProjectSM.locate(ROUTER_1["parent_uuid"], {"fq_name": proj_fq_name})

        project = Project(name=proj_fq_name[-1])
        self.vnc_lib.project_read = mock.Mock(return_value=project)

        # register the public network
        config_db.VirtualNetworkSM.locate(
            ROUTER_1["virtual_network_refs"][0]["uuid"], {"fq_name": ROUTER_1["virtual_network_refs"][0]["to"]}
        )

        # register interfaces
        config_db.VirtualMachineInterfaceSM.locate(
            ROUTER_1["virtual_machine_interface_refs"][0]["uuid"],
            {
                "fq_name": ROUTER_1["virtual_machine_interface_refs"][0]["to"],
                "virtual_network_refs": [{"uuid": "private1-uuid"}],
            },
        )
    def setUp(self):
        self.vnc_lib = mock.Mock()

        self.cassandra = mock.Mock()
        self.logger = mock.Mock()

        self.svc = mock.Mock()
        self.svc.netns_manager = instance_manager.NetworkNamespaceManager(
            self.vnc_lib, self.cassandra, self.logger, None, None, None)

        self.snat_agent = snat_agent.SNATAgent(self.svc, self.vnc_lib,
                                               self.cassandra, None)
        DBBase.init(self, self.logger, self.cassandra)

        # register the project
        proj_fq_name = ['default-domain', 'demo']
        config_db.ProjectSM.locate(
            ROUTER_1['parent_uuid'],
            {'fq_name': proj_fq_name})

        project = Project(name=proj_fq_name[-1])
        self.vnc_lib.project_read = mock.Mock(
            return_value=project)

        # register the public network
        config_db.VirtualNetworkSM.locate(
            ROUTER_1['virtual_network_refs'][0]['uuid'],
            {'fq_name': ROUTER_1['virtual_network_refs'][0]['to']})

        # register interfaces
        config_db.VirtualMachineInterfaceSM.locate(
            ROUTER_1['virtual_machine_interface_refs'][0]['uuid'],
            {'fq_name': ROUTER_1['virtual_machine_interface_refs'][0]['to'],
             'virtual_network_refs': [{'uuid': 'private1-uuid'}]})
Exemplo n.º 4
0
 def setUp(self):
     DBBase.init(self, None, None)
     DBBase._OBJ_TYPE_MAP['red'] = RedSM
     DBBase._OBJ_TYPE_MAP['blue'] = BlueSM
     DBBase._OBJ_TYPE_MAP['green'] = GreenSM
     DBBase._OBJ_TYPE_MAP['white'] = WhiteSM
     DBBase._OBJ_TYPE_MAP['purple'] = PurpleSM
     BlueSM._object_db = mock.MagicMock()
     BlueSM._object_db.object_read = self.blue_read
     RedSM._object_db = mock.MagicMock()
     RedSM._object_db.object_read = self.red_read
     GreenSM._object_db = mock.MagicMock()
     GreenSM._object_db.object_read = self.green_read
     WhiteSM._object_db = mock.MagicMock()
     WhiteSM._object_db.object_read = self.white_read
     PurpleSM._object_db = mock.MagicMock()
     PurpleSM._object_db.object_read = self.purple_read
    def setUp(self):
        self.vnc_lib = mock.Mock()

        self.cassandra = mock.Mock()
        self.logger = mock.Mock()

        self.svc = mock.Mock()
        self.svc.netns_manager = instance_manager.NetworkNamespaceManager(
            self.vnc_lib, self.cassandra, self.logger, None, None, None)

        # Mock service module logger.
        self.logger = mock.MagicMock()
        self.module_logger = ServiceMonitorModuleLogger(self.logger)

        self.snat_agent = snat_agent.SNATAgent(self.svc, self.vnc_lib,
                                               self.cassandra, None,
                                               logger = self.module_logger)
        DBBase.init(self, self.logger, self.cassandra)

        # register the project
        config_db.ProjectSM.locate(PROJECT_UUID, {'fq_name': PROJECT_FQ_NAME})

        project = Project(name=PROJECT_FQ_NAME[-1])
        self.vnc_lib.project_read = mock.Mock(return_value=project)

        for router in ROUTERS:
            # register the public network
            config_db.VirtualNetworkSM.locate(
                router['virtual_network_refs'][0]['uuid'],
                {'fq_name': router['virtual_network_refs'][0]['to'], 'parent_type': 'project'})

            for vmi_ref in router['virtual_machine_interface_refs']:
                # register interfaces
                config_db.VirtualMachineInterfaceSM.locate(
                    vmi_ref['uuid'],
                    {
                        'fq_name': vmi_ref['to'],
                        'virtual_network_refs': [{'uuid': 'private1-uuid'}],
                        'parent_type': 'project',
                    },
                )
Exemplo n.º 6
0
    def _connect_rabbit(self):
        rabbit_server = self._args.rabbit_server
        rabbit_port = self._args.rabbit_port
        rabbit_user = self._args.rabbit_user
        rabbit_password = self._args.rabbit_password
        rabbit_vhost = self._args.rabbit_vhost

        self._db_resync_done = gevent.event.Event()

        q_name = 'svc_mon.%s' % (socket.gethostname())
        self._vnc_kombu = VncKombuClient(rabbit_server, rabbit_port,
                                         rabbit_user, rabbit_password,
                                         rabbit_vhost, q_name,
                                         self._vnc_subscribe_callback,
                                         self.config_log)

        cass_server_list = self._args.cassandra_server_list
        reset_config = self._args.reset_config
        self._cassandra = VncCassandraClient(cass_server_list, reset_config,
                                             self._args.cluster_id, None,
                                             self.config_log)
        DBBase.init(self, self.logger, self._cassandra)
Exemplo n.º 7
0
 def setUp(self):
     self.vnc_lib = mock.Mock()
     self.cassandra = mock.Mock()
     self.logger = mock.Mock()
     self.svc = mock.Mock()
     DBBase.init(self.svc, None, self.cassandra)
Exemplo n.º 8
0
    def setUp(self):
        self.vnc_lib = mock.Mock()
        self.cassandra = mock.Mock()
        self.logger = mock.Mock()
        self.svc = mock.Mock()
        self._si_pool = {}

        mocked_gsc = mock.MagicMock()
        mocked_gsc.uuid = "fake-gsc-uuid"
        self.vnc_lib.global_system_config_read.return_value = mocked_gsc

        def no_id_side_effect(fq_name):
            raise NoIdError("xxx")

        # Return NoIdError while si is read for first time
        self.vnc_lib.service_instance_read = mock.Mock(side_effect=no_id_side_effect)
        self.vnc_lib.kv_retrieve.return_value = "fake-pool-vn 40.1.1.0/24"
        self.vnc_lib.service_appliance_set_create.return_value = "opencontrail"
        self.vnc_lib.service_appliance_set_read = mock.Mock(side_effect=no_id_side_effect)

        self._store_si = {}

        def read_si(obj_type, uuid):
            return (True, [self.obj_to_dict(self._store_si[uuid[0]])])

        def store_si_create(obj):
            config_db.ServiceInstanceSM._cassandra.object_read = mock.Mock(side_effect=read_si)
            obj.uuid = "pool-si"
            self._store_si[obj.uuid] = obj

        def update_si_side_effect(obj):
            self._store_si[obj.uuid] = obj

        self.vnc_lib.service_instance_create = mock.Mock(side_effect=store_si_create)

        self.vnc_lib.service_instance_update = mock.Mock(side_effect=update_si_side_effect)

        self._db = {}

        def read_db(id):
            if id in self._db:
                return self._db[id]

        def put_db(id, data):
            self._db[id] = data

        def remove_db(id, data=None):
            if data is None:
                del self._db[id]
                return
            if self._db[id][data[0]]:
                del self._db[id][data[0]]

        self.cassandra.pool_driver_info_get = mock.Mock(side_effect=read_db)
        self.cassandra.pool_driver_info_insert = mock.Mock(side_effect=put_db)
        self.cassandra.pool_remove = mock.Mock(side_effect=remove_db)

        def validate_pool_update(obj_type, obj_uuid, ref_type, ref_uuid, ref_fq_name, operation):
            self.assertEqual(obj_type, "loadbalancer-pool")
            self.assertEqual(ref_type, "service_instance_refs")
            pool = config_db.LoadbalancerPoolSM.get(obj_uuid)
            if operation is "ADD":
                si = config_db.ServiceInstanceSM.get(ref_uuid)
                self.assertIsNotNone(si)
                pool.service_instance = si.uuid
                si.loadbalancer_pool = pool.uuid
                self._si_pool[pool.uuid] = si.uuid
                self.assertEqual(si.uuid, "pool-si")
            elif operation is "DELETE":
                pool.service_instance = None
                del self._si_pool[pool.uuid]
            else:
                self.assertTrue(False)
            return

        self.vnc_lib.ref_update = mock.Mock(side_effect=validate_pool_update)

        conf_parser = argparse.ArgumentParser(add_help=False)
        config = ConfigParser.SafeConfigParser({"admin_token": None})
        self._args, remaining_argv = conf_parser.parse_known_args()
        self._args.config_sections = config

        def sas_read_side_effect(obj_type, uuids):
            if obj_type == "service_appliance_set":
                return (
                    True,
                    [
                        {
                            "fq_name": ["default-global-system-config", "opencontrail"],
                            "service_appliance_driver": "svc_monitor.services.loadbalancer\
.drivers.ha_proxy.driver.OpencontrailLoadbalancerDriver",
                        }
                    ],
                )
            return (False, None)

        DBBase.init(self.svc, None, self.cassandra)
        config_db.ServiceApplianceSetSM._cassandra.object_read = mock.Mock(side_effect=sas_read_side_effect)

        self.lb_agent = loadbalancer_agent.LoadbalancerAgent(self.svc, self.vnc_lib, self.cassandra, self._args)
        self.svc.loadbalancer_agent = self.lb_agent
        sas = config_db.ServiceApplianceSetSM.get("opencontrail")
        self.assertEqual(
            sas.driver,
            "svc_monitor.services.loadbalancer.drivers.ha_proxy.driver.\
OpencontrailLoadbalancerDriver",
        )
        sas.add()
        self.assertIsNotNone(self.lb_agent._loadbalancer_driver["opencontrail"])
        mock_st_obj = self.create_lb_st()
Exemplo n.º 9
0
    def setUp(self):
        self.vnc_lib = mock.Mock()
        self.object_db = mock.Mock()
        self.logger = mock.Mock()
        self.svc = mock.Mock()

        mocked_gsc = mock.MagicMock()
        mocked_gsc.uuid = 'fake-gsc-uuid'
        self.vnc_lib.global_system_config_read.return_value = mocked_gsc
        def mock_kv_retrieve(subnet_id):
            if subnet_id == "pool_subnet_id":
                return "fake-pool-vn 40.1.1.0/24"
            elif subnet_id == "vip_subnet_id":
                return "fake-vip-vn 1.1.1.0/24"
            self.assertTrue(False)
        #end
        self.vnc_lib.kv_retrieve = mock.Mock(side_effect=mock_kv_retrieve)
        self.vnc_lib.service_appliance_set_create.return_value = "opencontrail"

        self._mock_bigip_interfaces = None
        self._mock_BigIp = None

        self._db = {}
        def read_db(id):
            if id in self._db:
                return self._db[id].get('driver_info', None)

        def put_db(id, data):
            from copy import deepcopy
            self._db[id] = {'driver_info': deepcopy(data)}

        def remove_db(id, data=None):
            if data is None:
                del self._db[id]
                return
            if self._db[id][data[0]]:
                del self._db[id][data[0]]
        self.object_db.pool_driver_info_get = mock.Mock(side_effect=read_db)
        self.object_db.pool_driver_info_insert = mock.Mock(side_effect=put_db)
        self.object_db.pool_remove = mock.Mock(side_effect=remove_db)

        conf_parser = argparse.ArgumentParser(add_help=False)
        config = ConfigParser.SafeConfigParser({'admin_token': None})
        self._args, remaining_argv = conf_parser.parse_known_args()
        self._args.config_sections = config

        def sas_read_side_effect(obj_type, uuids, **kwargs):
            if obj_type == 'service_appliance_set':
                return (True, [{
                 'fq_name': ['default-global-system-config', 'opencontrail'],
                 'service_appliance_driver': 'svc_monitor.services.loadbalancer\
.drivers.ha_proxy.driver.OpencontrailLoadbalancerDriver'
                   }])
            return (False, None)
        DBBase.init(self.svc, None, self.object_db)
        config_db.ServiceApplianceSetSM._object_db.object_read = \
                         mock.Mock(side_effect=sas_read_side_effect)

        # return NoIdError exception for first query
        def no_id_side_effect(fq_name):
            raise NoIdError("xxx")
        self.vnc_lib.service_appliance_set_read = \
            mock.Mock(side_effect=no_id_side_effect)

        self.lb_agent = loadbalancer_agent.LoadbalancerAgent(self.svc,
            self.vnc_lib, self.object_db, self._args)
        self.svc.loadbalancer_agent = self.lb_agent
        sas = config_db.ServiceApplianceSetSM.get('opencontrail')
        self.assertEqual(sas.driver,
"svc_monitor.services.loadbalancer.drivers.ha_proxy.driver.\
OpencontrailLoadbalancerDriver")
        sas.add()
        DBBase.init(self.svc, None, self.object_db)
        config_db.ServiceApplianceSetSM._object_db.object_read = \
                         mock.Mock(side_effect=sas_read_side_effect)

        import sys
        sys.modules['f5'] = mock.Mock()
        sys.modules['f5.bigip'] = mock.Mock()
        self.create_f5_service_appliance_set()
    def setUp(self):
        self.vnc_lib = mock.Mock()
        self.cassandra = mock.Mock()
        self.logger = mock.Mock()
        self.svc = mock.Mock()
        self._db = {}

        def read_db(id):
            if id in self._db:
                return self._db[id]

        def put_db_config(id, data):
            if id not in self._db:
                self._db[id] = {}
            self._db[id]['config_info'] = data

        def put_db_driver(id, data):
            if id not in self._db:
                self._db[id] = {}
            self._db[id]['driver_info'] = data

        def remove_db(id, data=None):
            if data is None:
                del self._db[id]
                return
            if self._db[id]['driver_info'][data[0]]:
                del self._db[id]['driver_info'][data[0]]

        def list_pools():
            ret_list = []
            for each_entry_id, each_entry_data in self._db.iteritems() or []:
                config_info_obj_dict = each_entry_data['config_info']
                driver_info_obj_dict = None
                if 'driver_info' in each_entry_data:
                    driver_info_obj_dict = each_entry_data['driver_info']
                ret_list.append((each_entry_id, config_info_obj_dict,
                                 driver_info_obj_dict))
            return ret_list

        def list_loadbalancers():
            res_list = []
            return res_list

        def list_health_monitors():
            res_list = []
            return res_list

        self.cassandra.loadbalancer_list = mock.Mock(
            side_effect=list_loadbalancers)
        self.cassandra.pool_list = mock.Mock(side_effect=list_pools)
        self.cassandra.pool_remove = mock.Mock(side_effect=remove_db)
        self.cassandra.pool_driver_info_get = mock.Mock(side_effect=read_db)
        self.cassandra.pool_driver_info_insert = mock.Mock(
            side_effect=put_db_driver)
        self.cassandra.pool_config_insert = mock.Mock(
            side_effect=put_db_config)
        self.cassandra.health_monitor_list = mock.Mock(
            side_effect=list_health_monitors)

        mocked_gsc = mock.MagicMock()
        mocked_gsc.uuid = 'fake-gsc-uuid'
        self.vnc_lib.service_appliance_set_create.return_value = "opencontrail"
        self.vnc_lib.global_system_config_read.return_value = mocked_gsc

        def no_id_side_effect(fq_name):
            raise NoIdError("xxx")

        self.vnc_lib.service_appliance_set_read = mock.Mock(
            side_effect=no_id_side_effect)
        conf_parser = argparse.ArgumentParser(add_help=False)
        config = ConfigParser.SafeConfigParser({'admin_token': None})
        self._args, remaining_argv = conf_parser.parse_known_args()
        self._args.config_sections = config

        def sas_read_side_effect(obj_type, uuids, **kwargs):
            if obj_type == 'service_appliance_set':
                return (True, [{
                    'fq_name':
                    ['default-global-system-config', 'opencontrail'],
                    'service_appliance_driver':
                    'svc_monitor.services.loadbalancer.drivers.ha_proxy.driver.OpencontrailLoadbalancerDriver'
                }])
            return (False, None)

        self.cassandra.object_read = mock.Mock(
            side_effect=sas_read_side_effect)
        DBBase.init(self.svc, None, self.cassandra)
        self.lb_agent = loadbalancer_agent.LoadbalancerAgent(
            self.svc, self.vnc_lib, self.cassandra, self._args)
        self.svc.loadbalancer_agent = self.lb_agent
        sas = config_db.ServiceApplianceSetSM.get('opencontrail')
        self.assertEqual(
            sas.driver,
            "svc_monitor.services.loadbalancer.drivers.ha_proxy.driver.OpencontrailLoadbalancerDriver"
        )
        sas.add()
        self.assertIsNotNone(
            self.lb_agent._loadbalancer_driver['opencontrail'])
 def setUp(self):
     self.vnc_lib = mock.Mock()
     self.object_db = mock.Mock()
     self.logger = mock.Mock()
     self.svc = mock.Mock()
     DBBase.init(self.svc, None, self.object_db)
Exemplo n.º 12
0
 def setUp(self):
     self.vnc_lib = mock.Mock()
     self.cassandra = mock.Mock()
     self.logger = mock.Mock()
     self.svc = mock.Mock()
     DBBase.init(self.svc, None, self.cassandra)
Exemplo n.º 13
0
    def setUp(self):
        self.vnc_lib = mock.Mock()
        self.cassandra = mock.Mock()
        self.logger = mock.Mock()
        self.svc = mock.Mock()
        self._si_pool = {}

        mocked_gsc = mock.MagicMock()
        mocked_gsc.uuid = 'fake-gsc-uuid'
        self.vnc_lib.global_system_config_read.return_value = mocked_gsc

        def no_id_side_effect(fq_name):
            raise NoIdError("xxx")

        # Return NoIdError while si is read for first time
        self.vnc_lib.service_instance_read = \
            mock.Mock(side_effect=no_id_side_effect)
        self.vnc_lib.kv_retrieve.return_value = "fake-pool-vn 40.1.1.0/24"
        self.vnc_lib.service_appliance_set_create.return_value = "opencontrail"
        self.vnc_lib.service_appliance_set_read = \
            mock.Mock(side_effect=no_id_side_effect)

        self._store_si = {}

        def read_si(obj_type, uuid, **kwargs):
            return (True, [self.obj_to_dict(self._store_si[uuid[0]])])

        def store_si_create(obj):
            config_db.ServiceInstanceSM._cassandra.object_read = \
                mock.Mock(side_effect=read_si)
            obj.uuid = 'pool-si'
            self._store_si[obj.uuid] = obj

        def update_si_side_effect(obj):
            self._store_si[obj.uuid] = obj

        def delete_si_side_effect(id):
            del self._store_si[id]

        self.vnc_lib.service_instance_create = \
            mock.Mock(side_effect=store_si_create)

        self.vnc_lib.service_instance_update = \
            mock.Mock(side_effect=update_si_side_effect)

        self.vnc_lib.service_instance_delete = \
            mock.Mock(side_effect=delete_si_side_effect)

        self._db = {}

        def read_db(id, **kwargs):
            if id in self._db:
                return self._db[id]

        def put_db(id, data):
            self._db[id] = data

        def remove_db(id, data=None):
            if data is None:
                del self._db[id]
                return
            if self._db[id][data[0]]:
                del self._db[id][data[0]]

        self.cassandra.pool_driver_info_get = mock.Mock(side_effect=read_db)
        self.cassandra.pool_driver_info_insert = mock.Mock(side_effect=put_db)
        self.cassandra.pool_remove = mock.Mock(side_effect=remove_db)

        def validate_pool_update(obj_type, obj_uuid, ref_type, ref_uuid,
                                 ref_fq_name, operation):
            self.assertEqual(obj_type, "loadbalancer-pool")
            self.assertEqual(ref_type, "service-instance")
            pool = config_db.LoadbalancerPoolSM.get(obj_uuid)
            if operation is "ADD":
                si = config_db.ServiceInstanceSM.get(ref_uuid)
                self.assertIsNotNone(si)
                pool.service_instance = si.uuid
                si.loadbalancer_pool = pool.uuid
                self._si_pool[pool.uuid] = si.uuid
                self.assertEqual(si.uuid, "pool-si")
            elif operation is "DELETE":
                pool.service_instance = None
                del self._si_pool[pool.uuid]
            else:
                self.assertTrue(False)
            return

        self.vnc_lib.ref_update = mock.Mock(side_effect=validate_pool_update)

        conf_parser = argparse.ArgumentParser(add_help=False)
        config = ConfigParser.SafeConfigParser({'admin_token': None})
        self._args, remaining_argv = conf_parser.parse_known_args()
        self._args.config_sections = config

        def sas_read_side_effect(obj_type, uuids, **kwargs):
            if obj_type == 'service_appliance_set':
                return (True, [{
                    'fq_name':
                    ['default-global-system-config', 'opencontrail'],
                    'service_appliance_driver':
                    'svc_monitor.services.loadbalancer\
.drivers.ha_proxy.driver.OpencontrailLoadbalancerDriver'
                }])
            return (False, None)

        DBBase.init(self.svc, None, self.cassandra)
        config_db.ServiceApplianceSetSM._cassandra.object_read = \
                         mock.Mock(side_effect=sas_read_side_effect)

        self.lb_agent = loadbalancer_agent.LoadbalancerAgent(
            self.svc, self.vnc_lib, self.cassandra, self._args)
        self.svc.loadbalancer_agent = self.lb_agent
        sas = config_db.ServiceApplianceSetSM.get('opencontrail')
        self.assertEqual(
            sas.driver,
            "svc_monitor.services.loadbalancer.drivers.ha_proxy.driver.\
OpencontrailLoadbalancerDriver")
        sas.add()
        self.assertIsNotNone(
            self.lb_agent._loadbalancer_driver['opencontrail'])
        mock_st_obj = self.create_lb_st()
Exemplo n.º 14
0
    def setUp(self):
        self.vnc_lib = mock.Mock()
        self.object_db = mock.Mock()
        self.logger = mock.Mock()
        self.svc = mock.Mock()

        mocked_gsc = mock.MagicMock()
        mocked_gsc.uuid = 'fake-gsc-uuid'
        self.vnc_lib.global_system_config_read.return_value = mocked_gsc
        def mock_kv_retrieve(subnet_id):
            if subnet_id == "pool_subnet_id":
                return "fake-pool-vn 40.1.1.0/24"
            elif subnet_id == "vip_subnet_id":
                return "fake-vip-vn 1.1.1.0/24"
            self.assertTrue(False)
        #end
        self.vnc_lib.kv_retrieve = mock.Mock(side_effect=mock_kv_retrieve)
        self.vnc_lib.service_appliance_set_create.return_value = "opencontrail"

        self._mock_bigip_interfaces = None
        self._mock_BigIp = None

        self._db = {}
        def read_db(id):
            if id in self._db:
                return self._db[id].get('driver_info', None)

        def put_db(id, data):
            from copy import deepcopy
            self._db[id] = {'driver_info': deepcopy(data)}

        def remove_db(id, data=None):
            if data is None:
                del self._db[id]
                return
            if self._db[id][data[0]]:
                del self._db[id][data[0]]
        self.object_db.pool_driver_info_get = mock.Mock(side_effect=read_db)
        self.object_db.pool_driver_info_insert = mock.Mock(side_effect=put_db)
        self.object_db.pool_remove = mock.Mock(side_effect=remove_db)

        conf_parser = argparse.ArgumentParser(add_help=False)
        config = ConfigParser.SafeConfigParser({'admin_token': None})
        self._args, remaining_argv = conf_parser.parse_known_args()
        self._args.config_sections = config

        def sas_read_side_effect(obj_type, uuids, **kwargs):
            if obj_type == 'service_appliance_set':
                return (True, [{
                 'fq_name': ['default-global-system-config', 'opencontrail'],
                 'service_appliance_driver': 'svc_monitor.services.loadbalancer\
.drivers.ha_proxy.driver.OpencontrailLoadbalancerDriver'
                   }])
            return (False, None)
        DBBase.init(self.svc, None, self.object_db)
        config_db.ServiceApplianceSetSM._object_db.object_read = \
                         mock.Mock(side_effect=sas_read_side_effect)

        # return NoIdError exception for first query
        def no_id_side_effect(fq_name):
            raise NoIdError("xxx")
        self.vnc_lib.service_appliance_set_read = \
            mock.Mock(side_effect=no_id_side_effect)

        self.lb_agent = loadbalancer_agent.LoadbalancerAgent(self.svc,
            self.vnc_lib, self.object_db, self._args)
        self.svc.loadbalancer_agent = self.lb_agent
        sas = config_db.ServiceApplianceSetSM.get('opencontrail')
        self.assertEqual(sas.driver,
"svc_monitor.services.loadbalancer.drivers.ha_proxy.driver.\
OpencontrailLoadbalancerDriver")
        sas.add()
        DBBase.init(self.svc, None, self.object_db)
        config_db.ServiceApplianceSetSM._object_db.object_read = \
                         mock.Mock(side_effect=sas_read_side_effect)

        import sys
        sys.modules['f5'] = mock.Mock()
        sys.modules['f5.bigip'] = mock.Mock()
        self.create_f5_service_appliance_set()
Exemplo n.º 15
0
 def setUp(self):
     self.vnc_lib = mock.Mock()
     self.object_db = mock.Mock()
     self.logger = mock.Mock()
     self.svc = mock.Mock()
     DBBase.init(self.svc, None, self.object_db)
Exemplo n.º 16
0
    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])

        self._sandesh = Sandesh()
        module = Module.DEVICE_MANAGER
        module_name = ModuleNames[module]
        node_type = Module2NodeType[module]
        node_type_name = NodeTypeNames[node_type]
        instance_id = INSTANCE_ID_DEFAULT
        hostname = socket.gethostname()
        self._sandesh.init_generator(
            module_name, hostname, node_type_name, instance_id,
            self._args.collectors, 'to_bgp_context',
            int(args.http_server_port),
            ['cfgm_common', 'device_manager.sandesh'], self._disc)
        self._sandesh.set_logging_params(enable_local_log=args.log_local,
                                         category=args.log_category,
                                         level=args.log_level,
                                         file=args.log_file,
                                         enable_syslog=args.use_syslog,
                                         syslog_facility=args.syslog_facility)
        ConnectionState.init(
            self._sandesh, hostname, module_name, instance_id,
            staticmethod(ConnectionState.get_process_state_cb),
            NodeStatusUVE, NodeStatus)

        # 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)
                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)

        rabbit_servers = self._args.rabbit_server
        rabbit_port = self._args.rabbit_port
        rabbit_user = self._args.rabbit_user
        rabbit_password = self._args.rabbit_password
        rabbit_vhost = self._args.rabbit_vhost
        rabbit_ha_mode = self._args.rabbit_ha_mode

        self._db_resync_done = gevent.event.Event()

        q_name = 'device_manager.%s' % (socket.gethostname())
        self._vnc_kombu = VncKombuClient(rabbit_servers, rabbit_port,
                                         rabbit_user, rabbit_password,
                                         rabbit_vhost, rabbit_ha_mode,
                                         q_name, self._vnc_subscribe_callback,
                                         self.config_log)

        cass_server_list = self._args.cassandra_server_list
        reset_config = self._args.reset_config
        self._cassandra = VncCassandraClient(cass_server_list, reset_config,
                                             self._args.cluster_id, None,
                                             self.config_log)

        DBBase.init(self, self._sandesh.logger(), self._cassandra)
        ok, global_system_config_list = self._cassandra._cassandra_global_system_config_list()
        if not ok:
            self.config_log('global system config list returned error: %s' %
                            global_system_config_list)
        else:
            for fq_name, uuid in global_system_config_list:
                GlobalSystemConfigDM.locate(uuid)

        ok, vn_list = self._cassandra._cassandra_virtual_network_list()
        if not ok:
            self.config_log('virtual network list returned error: %s' %
                            vn_list)
        else:
            for fq_name, uuid in vn_list:
                vn = VirtualNetworkDM.locate(uuid)
                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)

        ok, bgp_list = self._cassandra._cassandra_bgp_router_list()
        if not ok:
            self.config_log('bgp router list returned error: %s' %
                            bgp_list)
        else:
            for fq_name, uuid in bgp_list:
                BgpRouterDM.locate(uuid)

        ok, pr_list = self._cassandra._cassandra_physical_router_list()
        if not ok:
            self.config_log('physical router list returned error: %s' %
                            pr_list)
        else:
            for fq_name, uuid in pr_list:
                pr = PhysicalRouterDM.locate(uuid)
                if pr.bgp_router:
                    BgpRouterDM.locate(pr.bgp_router)
                li_set = pr.logical_interfaces
                for pi_id in pr.physical_interfaces:
                    pi = PhysicalInterfaceDM.locate(pi_id)
                    if pi:
                        li_set |= pi.logical_interfaces
                vmi_set = set()
                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)

            ok, ip_list = self._cassandra._cassandra_instance_ip_list()
            if not ok:
                self.config_log('instance ip list returned error: %s' %
                            ip_list)
            else:
                for fq_name, uuid in ip_list:
                    InstanceIpDM.locate(uuid)

            ok, fip_list = self._cassandra._cassandra_floating_ip_list()
            if not ok:
                self.config_log('floating ip list returned error: %s' %
                            fip_list)
            else:
                for fq_name, uuid in fip_list:
                    FloatingIpDM.locate(uuid)

            for fq_name, uuid in vn_list:
                vn = VirtualNetworkDM.locate(uuid)
                if vn is not None:
                    vn.update_instance_ip_map()

            for pr in PhysicalRouterDM.values():
                pr.push_config()
        self._db_resync_done.set()
        while 1:
            # Just wait indefinitely
            time.sleep(5)
    def setUp(self):
        self.vnc_lib = mock.Mock()
        self.cassandra = mock.Mock()
        self.logger = mock.Mock()
        self.svc = mock.Mock()
        self._db = {}

        def read_db(id):
            if id in self._db:
                return self._db[id]

        def put_db_config(id, data):
            if id not in self._db:
                self._db[id] = {}
            self._db[id]['config_info'] = data

        def put_db_driver(id, data):
            if id not in self._db:
                self._db[id] = {}
            self._db[id]['driver_info'] = data

        def remove_db(id, data=None):
            if data is None:
                del self._db[id]
                return
            if self._db[id]['driver_info'][data[0]]:
                del self._db[id]['driver_info'][data[0]]

        def list_pools():
            ret_list = []
            for each_entry_id, each_entry_data in self._db.iteritems() or []:
                config_info_obj_dict = each_entry_data['config_info']
                driver_info_obj_dict = None
                if 'driver_info' in each_entry_data:
                    driver_info_obj_dict = each_entry_data['driver_info']
                ret_list.append((each_entry_id, config_info_obj_dict, driver_info_obj_dict))
            return ret_list

        def list_loadbalancers():
            res_list = []
            return res_list

        def list_health_monitors():
            res_list = []
            return res_list

        self.cassandra.loadbalancer_list = mock.Mock(side_effect=list_loadbalancers)
        self.cassandra.pool_list = mock.Mock(side_effect=list_pools)
        self.cassandra.pool_remove = mock.Mock(side_effect=remove_db)
        self.cassandra.pool_driver_info_get = mock.Mock(side_effect=read_db)
        self.cassandra.pool_driver_info_insert = mock.Mock(side_effect=put_db_driver)
        self.cassandra.pool_config_insert = mock.Mock(side_effect=put_db_config)
        self.cassandra.health_monitor_list = mock.Mock(side_effect=list_health_monitors)

        mocked_gsc = mock.MagicMock()
        mocked_gsc.uuid = 'fake-gsc-uuid'
        self.vnc_lib.service_appliance_set_create.return_value = "opencontrail"
        self.vnc_lib.global_system_config_read.return_value = mocked_gsc
        def no_id_side_effect(fq_name):
            raise NoIdError("xxx")
        self.vnc_lib.service_appliance_set_read = mock.Mock(side_effect=no_id_side_effect)
        conf_parser = argparse.ArgumentParser(add_help=False)
        config = ConfigParser.SafeConfigParser({'admin_token': None})
        self._args, remaining_argv = conf_parser.parse_known_args()
        self._args.config_sections = config

        def sas_read_side_effect(obj_type, uuids, **kwargs):
            if obj_type == 'service_appliance_set':
                return (True, [{
                   'fq_name': ['default-global-system-config', 'opencontrail'],
                   'service_appliance_driver': 'svc_monitor.services.loadbalancer.drivers.ha_proxy.driver.OpencontrailLoadbalancerDriver'
                   }])
            return (False, None)
        self.cassandra.object_read = mock.Mock(
            side_effect=sas_read_side_effect)
        DBBase.init(self.svc, None, self.cassandra)
        self.lb_agent = loadbalancer_agent.LoadbalancerAgent(self.svc, self.vnc_lib,
                                               self.cassandra, self._args)
        self.svc.loadbalancer_agent = self.lb_agent
        sas = config_db.ServiceApplianceSetSM.get('opencontrail')
        self.assertEqual(sas.driver, "svc_monitor.services.loadbalancer.drivers.ha_proxy.driver.OpencontrailLoadbalancerDriver")
        sas.add()
        self.assertIsNotNone(self.lb_agent._loadbalancer_driver['opencontrail'])
Exemplo n.º 18
0
    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])

        self._sandesh = Sandesh()
        module = Module.DEVICE_MANAGER
        module_name = ModuleNames[module]
        node_type = Module2NodeType[module]
        node_type_name = NodeTypeNames[node_type]
        instance_id = INSTANCE_ID_DEFAULT
        hostname = socket.gethostname()
        self._sandesh.init_generator(module_name, hostname, node_type_name,
                                     instance_id, self._args.collectors,
                                     'to_bgp_context',
                                     int(args.http_server_port),
                                     ['cfgm_common', 'device_manager.sandesh'],
                                     self._disc)
        self._sandesh.set_logging_params(enable_local_log=args.log_local,
                                         category=args.log_category,
                                         level=args.log_level,
                                         file=args.log_file,
                                         enable_syslog=args.use_syslog,
                                         syslog_facility=args.syslog_facility)
        ConnectionState.init(
            self._sandesh, hostname, module_name, instance_id,
            staticmethod(ConnectionState.get_process_state_cb), NodeStatusUVE,
            NodeStatus)

        # 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)
                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)

        rabbit_servers = self._args.rabbit_server
        rabbit_port = self._args.rabbit_port
        rabbit_user = self._args.rabbit_user
        rabbit_password = self._args.rabbit_password
        rabbit_vhost = self._args.rabbit_vhost
        rabbit_ha_mode = self._args.rabbit_ha_mode

        self._db_resync_done = gevent.event.Event()

        q_name = 'device_manager.%s' % (socket.gethostname())
        self._vnc_kombu = VncKombuClient(rabbit_servers, rabbit_port,
                                         rabbit_user, rabbit_password,
                                         rabbit_vhost, rabbit_ha_mode, q_name,
                                         self._vnc_subscribe_callback,
                                         self.config_log)

        cass_server_list = self._args.cassandra_server_list
        reset_config = self._args.reset_config
        self._cassandra = VncCassandraClient(cass_server_list, reset_config,
                                             self._args.cluster_id, None,
                                             self.config_log)

        DBBase.init(self, self._sandesh.logger(), self._cassandra)
        ok, pr_list = self._cassandra._cassandra_physical_router_list()
        if not ok:
            self.config_log('physical router list returned error: %s' %
                            pr_list)
        else:
            vn_set = set()
            for fq_name, uuid in pr_list:
                pr = PhysicalRouterDM.locate(uuid)
                if pr.bgp_router:
                    BgpRouterDM.locate(pr.bgp_router)
                vn_set |= pr.virtual_networks
                li_set = pr.logical_interfaces
                for pi_id in pr.physical_interfaces:
                    pi = PhysicalInterfaceDM.locate(pi_id)
                    if pi:
                        li_set |= pi.logical_interfaces
                vmi_set = set()
                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)
                    if vmi:
                        vn_set |= set([vmi.virtual_network])

            for vn_id in vn_set:
                VirtualNetworkDM.locate(vn_id)

            for pr in PhysicalRouterDM.values():
                pr.push_config()
        self._db_resync_done.set()
        while 1:
            # Just wait indefinitely
            time.sleep(5)