aboutsummaryrefslogtreecommitdiffstats
path: root/app/discover/fetchers/cli/cli_fetch_host_vservice.py
blob: ae7c65649c374a7f6b7228c759717bcaf0d4098e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
###############################################################################
# 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[:id_full.index('-')]
        id_clean = id_full[len(prefix)+1:]
        r["service_type"] = prefix[1:]
        name = self.get_router_name(r, id_clean) \
            if r["service_type"] == "router" \
            else self.get_network_name(id_clean)
        r["name"] = prefix + "-" + name
        r["host"] = host_id
        r["id"] = "{}-{}".format(host_id, id_full)
        self.set_agent_type(r)

    def get_network_name(self, network_id):
        query = """
                SELECT name
                FROM {}.networks
                WHERE id = %s
                """.format(self.neutron_db)
        results = self.get_objects_list_for_id(query, "router", network_id)
        if not list(results):
            return network_id
        for db_row in results:
            return db_row["name"]

    def get_router_name(self, r, router_id):
        query = """
                SELECT *
                FROM {}.routers
                WHERE id = %s
                """.format(self.neutron_db)
        results = self.get_objects_list_for_id(query, "router",
                                               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"