From 7e83d0876ddb84a45e130eeba28bc40ef53c074b Mon Sep 17 00:00:00 2001 From: Yaron Yogev Date: Thu, 27 Jul 2017 09:02:54 +0300 Subject: Calipso initial release for OPNFV Change-Id: I7210c244b0c10fa80bfa8c77cb86c9d6ddf8bc88 Signed-off-by: Yaron Yogev --- .../fetchers/cli/cli_fetch_host_vservice.py | 80 ++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 app/discover/fetchers/cli/cli_fetch_host_vservice.py (limited to 'app/discover/fetchers/cli/cli_fetch_host_vservice.py') diff --git a/app/discover/fetchers/cli/cli_fetch_host_vservice.py b/app/discover/fetchers/cli/cli_fetch_host_vservice.py new file mode 100644 index 0000000..9f8173f --- /dev/null +++ b/app/discover/fetchers/cli/cli_fetch_host_vservice.py @@ -0,0 +1,80 @@ +############################################################################### +# Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) # +# and others # +# # +# All rights reserved. This program and the accompanying materials # +# are made available under the terms of the Apache License, Version 2.0 # +# which accompanies this distribution, and is available at # +# http://www.apache.org/licenses/LICENSE-2.0 # +############################################################################### +import re + +from discover.fetchers.cli.cli_access import CliAccess +from discover.fetchers.db.db_access import DbAccess +from discover.network_agents_list import NetworkAgentsList +from utils.inventory_mgr import InventoryMgr + + +class CliFetchHostVservice(CliAccess, DbAccess): + def __init__(self): + super(CliFetchHostVservice, self).__init__() + # match only DHCP agent and router (L3 agent) + self.type_re = re.compile("^q(dhcp|router)-") + self.inv = InventoryMgr() + self.agents_list = NetworkAgentsList() + + def get_vservice(self, host_id, name_space): + result = {"local_service_id": name_space} + self.set_details(host_id, result) + return result + + def set_details(self, host_id, r): + # keep the index without prefix + id_full = r["local_service_id"].strip() + prefix = id_full[1:id_full.index('-')] + id_clean = id_full[id_full.index('-') + 1:] + r["service_type"] = prefix + name = self.get_router_name(r, id_clean) if prefix == "router" \ + else self.get_network_name(id_clean) + r["name"] = prefix + "-" + name + r["host"] = host_id + r["id"] = host_id + "-" + id_full + self.set_agent_type(r) + + def get_network_name(self, id): + query = """ + SELECT name + FROM {}.networks + WHERE id = %s + """.format(self.neutron_db) + results = self.get_objects_list_for_id(query, "router", id) + if not list(results): + return id + for db_row in results: + return db_row["name"] + + def get_router_name(self, r, id): + query = """ + SELECT * + FROM {}.routers + WHERE id = %s + """.format(self.neutron_db) + results = self.get_objects_list_for_id(query, "router", id.strip()) + for db_row in results: + r.update(db_row) + return r["name"] + + # dynamically create sub-folder for vService by type + def set_agent_type(self, o): + o["master_parent_id"] = o["host"] + "-vservices" + o["master_parent_type"] = "vservices_folder" + atype = o["service_type"] + agent = self.agents_list.get_type(atype) + try: + o["parent_id"] = o["master_parent_id"] + "-" + agent["type"] + "s" + o["parent_type"] = "vservice_" + agent["type"] + "s_folder" + o["parent_text"] = agent["folder_text"] + except KeyError: + o["parent_id"] = o["master_parent_id"] + "-" + "miscellenaous" + o["parent_type"] = "vservice_miscellenaous_folder" + o["parent_text"] = "Misc. services" -- cgit 1.2.3-korg