self.conf = config
self.common_db = CommonDbClient(config)
self.vim_account = self.common_db.get_vim_account(vim_account_id)
- self.keystone = self._build_keystone_client(self.vim_account)
+ # self.keystone = self._build_keystone_client(self.vim_account)
+ self.vim_session = None
self.nova = self._build_nova_client(self.vim_account)
self.cinder = self._build_cinder_client(self.vim_account)
self.neutron, self.tenant_id = self._build_neutron_client(self.vim_account)
def _build_nova_client(self, vim_account: dict) -> nova_client.Client:
sess = OpenstackUtils.get_session(vim_account)
+ self.vim_session = sess
return nova_client.Client("2", session=sess, timeout=10)
def _build_cinder_client(self, vim_account: dict) -> cinder_client.Client:
- sess = OpenstackUtils.get_session(vim_account)
- return cinder_client.Client("3", session=sess, timeout=10)
+ # sess = OpenstackUtils.get_session(vim_account)
+ return cinder_client.Client("3", session=self.vim_session, timeout=10)
def _build_neutron_client(self, vim_account: dict) -> tuple:
- sess = OpenstackUtils.get_session(vim_account)
- tenant_id = sess.get_project_id()
- return neutron_client.Client("2", session=sess, timeout=10), tenant_id
+ # sess = OpenstackUtils.get_session(vim_account)
+ tenant_id = self.vim_session.get_project_id()
+ return neutron_client.Client("2", session=self.vim_session, timeout=10), tenant_id
}
SDN_INFRA_COLLECTORS = {"onosof": OnosInfraCollector, "onos_vpls": OnosInfraCollector}
+# Map to store vim ids and corresponding vim session objects
+vim_sess_map = {}
+
+
+# Invoked from process executor to initialize the vim session map
+def init_session(session_map: dict):
+ global vim_sess_map
+ vim_sess_map = session_map
+
class CollectorService:
def __init__(self, config: Config):
vim_type = CollectorService._get_vim_type(conf, vim_account_id)
log.debug("vim type.....{}".format(vim_type))
if vim_type in VIM_COLLECTORS:
- collector = VIM_COLLECTORS[vim_type](conf, vim_account_id)
+ collector = VIM_COLLECTORS[vim_type](conf, vim_account_id, vim_sess_map[vim_account_id])
metrics = collector.collect(vnfr)
log.debug("Collecting vim metrics.....{}".format(metrics))
else:
vnfrs = self.common_db.get_vnfrs()
metrics = []
+ # Get all vim ids regiestered in osm and create their corresponding vim session objects
+ # Vim ids and their corresponding session objects are stored in vim-session-map
+ # It optimizes the number of authentication tokens created in vim for metric colleciton
+ vim_sess_map.clear()
+ vims = self.common_db.get_vim_accounts()
+ for vim in vims:
+ vim_type = CollectorService._get_vim_type(self.conf, vim["_id"])
+ if vim_type in VIM_INFRA_COLLECTORS:
+ collector = VIM_INFRA_COLLECTORS[vim_type](self.conf, vim["_id"])
+ vim_sess = collector.vim_session if vim_type == "openstack" else None
+ # Populate the vim session map with vim ids and corresponding session objects
+ # vim session objects are stopred only for vim type openstack
+ if vim_sess:
+ vim_sess_map[vim["_id"]] = vim_sess
+
start_time = time.time()
# Starting executor pool with pool size process_pool_size. Default process_pool_size is 20
+ # init_session is called to assign the session map to the gloabal vim session map variable
with concurrent.futures.ProcessPoolExecutor(
- self.conf.get("collector", "process_pool_size")
+ self.conf.get("collector", "process_pool_size"), initializer=init_session, initargs=(vim_sess_map,)
) as executor:
log.info(
"Started metric collector process pool with pool size %s"
)
)
- vims = self.common_db.get_vim_accounts()
for vim in vims:
futures.append(
executor.submit(
class OpenstackCollector(BaseVimCollector):
- def __init__(self, config: Config, vim_account_id: str):
+ def __init__(self, config: Config, vim_account_id: str, vim_session: object):
super().__init__(config, vim_account_id)
self.common_db = CommonDbClient(config)
vim_account = self.common_db.get_vim_account(vim_account_id)
- self.backend = self._get_backend(vim_account)
+ self.backend = self._get_backend(vim_account, vim_session)
def _build_keystone_client(self, vim_account: dict) -> keystone_client.Client:
sess = OpenstackUtils.get_session(vim_account)
log.info("Error in metric collection: %s" % e)
return metrics
- def _get_backend(self, vim_account: dict):
+ def _get_backend(self, vim_account: dict, vim_session: object):
try:
- gnocchi = GnocchiBackend(vim_account)
+ gnocchi = GnocchiBackend(vim_account, vim_session)
gnocchi.client.metric.list(limit=1)
log.info("Using gnocchi backend to collect metric")
return gnocchi
except (HTTPException, EndpointNotFound):
- ceilometer = CeilometerBackend(vim_account)
+ ceilometer = CeilometerBackend(vim_account, vim_session)
ceilometer.client.capabilities.get()
log.info("Using ceilometer backend to collect metric")
return ceilometer
class GnocchiBackend(OpenstackBackend):
- def __init__(self, vim_account: dict):
- self.client = self._build_gnocchi_client(vim_account)
- self.neutron = self._build_neutron_client(vim_account)
+ def __init__(self, vim_account: dict, vim_session: object):
+ self.client = self._build_gnocchi_client(vim_account, vim_session)
+ self.neutron = self._build_neutron_client(vim_account, vim_session)
- def _build_gnocchi_client(self, vim_account: dict) -> gnocchi_client.Client:
- sess = OpenstackUtils.get_session(vim_account)
- return gnocchi_client.Client(session=sess)
+ def _build_gnocchi_client(self, vim_account: dict, vim_session: object) -> gnocchi_client.Client:
+ return gnocchi_client.Client(session=vim_session)
- def _build_neutron_client(self, vim_account: dict) -> neutron_client.Client:
- sess = OpenstackUtils.get_session(vim_account)
- return neutron_client.Client(session=sess)
+ def _build_neutron_client(self, vim_account: dict, vim_session: object) -> neutron_client.Client:
+ return neutron_client.Client(session=vim_session)
def collect_metric(
self, metric_type: MetricType, metric_name: str, resource_id: str
class CeilometerBackend(OpenstackBackend):
- def __init__(self, vim_account: dict):
- self.client = self._build_ceilometer_client(vim_account)
+ def __init__(self, vim_account: dict, vim_session: object):
+ self.client = self._build_ceilometer_client(vim_account, vim_session)
- def _build_ceilometer_client(self, vim_account: dict) -> ceilometer_client.Client:
- sess = OpenstackUtils.get_session(vim_account)
- return ceilometer_client.Client("2", session=sess)
+ def _build_ceilometer_client(self, vim_account: dict, vim_session: object) -> ceilometer_client.Client:
+ return ceilometer_client.Client("2", session=vim_session)
def collect_metric(
self, metric_type: MetricType, metric_name: str, resource_id: str
class VIOCollector(BaseVimCollector):
- def __init__(self, config: Config, vim_account_id: str):
+ def __init__(self, config: Config, vim_account_id: str, vim_session: object):
super().__init__(config, vim_account_id)
self.common_db = CommonDbClient(config)
cfg = self.get_vim_account(vim_account_id)
class VMwareCollector(BaseVimCollector):
- def __init__(self, config: Config, vim_account_id: str):
+ def __init__(self, config: Config, vim_account_id: str, vim_session: object):
super().__init__(config, vim_account_id)
self.common_db = CommonDbClient(config)
vim_account = self.get_vim_account(vim_account_id)
from osm_mon.collector.vnf_collectors.vio import VIOCollector
from osm_mon.core.common_db import CommonDbClient
from osm_mon.core.config import Config
+from osm_mon.collector.service import init_session
@mock.patch.object(CommonDbClient, "__init__", lambda *args, **kwargs: None)
def setUp(self):
super().setUp()
self.config = Config()
+ mock_vim_session = mock.MagicMock()
+ init_session(mock_vim_session)
@mock.patch.object(OpenstackCollector, "__init__", lambda *args, **kwargs: None)
@mock.patch.object(OpenstackCollector, "collect")
def test_collect_gnocchi_rate_instance(self, build_gnocchi_client, _):
mock_gnocchi_client = mock.Mock()
mock_gnocchi_client.metric = mock.Mock()
+ mock_vim_session = mock.Mock()
mock_gnocchi_client.metric.get_measures.return_value = [
(
datetime.datetime(
]
build_gnocchi_client.return_value = mock_gnocchi_client
- backend = GnocchiBackend({"_id": "test_uuid"})
+ backend = GnocchiBackend({"_id": "test_uuid"}, mock_vim_session)
value = backend._collect_instance_metric("cpu", "test_resource_id")
self.assertEqual(value, 1.0)
mock_gnocchi_client.metric.get_measures.assert_called_once_with(
@mock.patch.object(GnocchiBackend, "_build_gnocchi_client")
def test_collect_gnocchi_non_rate_instance(self, build_gnocchi_client, _):
mock_gnocchi_client = mock.Mock()
+ mock_vim_session = mock.Mock()
mock_gnocchi_client.metric.get_measures.return_value = [
(
datetime.datetime(
]
build_gnocchi_client.return_value = mock_gnocchi_client
- backend = GnocchiBackend({"_id": "test_uuid"})
+ backend = GnocchiBackend({"_id": "test_uuid"}, mock_vim_session)
value = backend._collect_instance_metric("memory.usage", "test_resource_id")
self.assertEqual(value, 128)
mock_gnocchi_client.metric.get_measures.assert_called_once_with(
@mock.patch.object(GnocchiBackend, "_build_gnocchi_client")
def test_collect_gnocchi_no_metric(self, build_gnocchi_client, _):
mock_gnocchi_client = mock.Mock()
+ mock_vim_session = mock.Mock()
mock_gnocchi_client.metric.get_measures.side_effect = (
gnocchiclient.exceptions.NotFound()
)
build_gnocchi_client.return_value = mock_gnocchi_client
- backend = GnocchiBackend({"_id": "test_uuid"})
+ backend = GnocchiBackend({"_id": "test_uuid"}, mock_vim_session)
value = backend._collect_instance_metric("memory.usage", "test_resource_id")
self.assertIsNone(value)
mock_gnocchi_client.metric.get_measures.assert_called_once_with(
self, build_gnocchi_client, build_neutron_client
):
mock_gnocchi_client = mock.Mock()
+ mock_vim_session = mock.Mock()
mock_gnocchi_client.resource.search.return_value = [
{"id": "test_id_1"},
{"id": "test_id_2"},
build_gnocchi_client.return_value = mock_gnocchi_client
- backend = GnocchiBackend({"_id": "test_uuid"})
+ backend = GnocchiBackend({"_id": "test_uuid"}, mock_vim_session)
value = backend._collect_interface_all_metric(
"packets_received", "test_resource_id"
)
@mock.patch("osm_mon.collector.vnf_collectors.vmware.CommonDbClient")
def setUp(self, mock_db, mock_get_vim_account):
super().setUp()
+ mock_vim_session = mock.Mock()
mock_get_vim_account.return_value = VIM_ACCOUNT
self.collector = VMwareCollector(
- Config(), "9de6df67-b820-48c3-bcae-ee4838c5c5f4"
+ Config(), "9de6df67-b820-48c3-bcae-ee4838c5c5f4", mock_vim_session
)
self.mock_db = mock_db
with open(
def setUp(self, mock_db, mock_get_vim_account):
super().setUp()
self.mock_db = mock_db
+ mock_vim_session = mock.Mock()
mock_get_vim_account.return_value = VIM_ACCOUNT
self.collector = VMwareCollector(
- Config(), "9de6df67-b820-48c3-bcae-ee4838c5c5f4"
+ Config(), "9de6df67-b820-48c3-bcae-ee4838c5c5f4", mock_vim_session
)
def tearDown(self):
def setUp(self, mock_db, mock_get_vim_account):
super().setUp()
self.mock_db = mock_db
+ mock_vim_session = mock.Mock()
mock_get_vim_account.return_value = VIM_ACCOUNT
self.collector = VMwareCollector(
- Config(), "9de6df67-b820-48c3-bcae-ee4838c5c5f4"
+ Config(), "9de6df67-b820-48c3-bcae-ee4838c5c5f4", mock_vim_session
)
def tearDown(self):
def setUp(self, mock_db, mock_get_vim_account):
super().setUp()
self.mock_db = mock_db
+ mock_vim_session = mock.Mock()
mock_get_vim_account.return_value = VIM_ACCOUNT
- self.collector = VIOCollector(Config(), "9de6df67-b820-48c3-bcae-ee4838c5c5f4")
+ self.collector = VIOCollector(Config(), "9de6df67-b820-48c3-bcae-ee4838c5c5f4", mock_vim_session)
with open(
os.path.join(os.path.dirname(__file__), "osm_mocks", "VNFR.json"), "r"
) as f: