summaryrefslogtreecommitdiffstats
path: root/app/discover/find_links_for_vedges.py
blob: 1235074bedc549671f77f45e2ea7c7c1f31a967d (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
###############################################################################
# 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                                  #
###############################################################################
from discover.find_links import FindLinks


class FindLinksForVedges(FindLinks):
    def __init__(self):
        super().__init__()

    def add_links(self):
        self.log.info("adding link types: " +
                      "vnic-vedge, vconnector-vedge, vedge-pnic")
        vedges = self.inv.find_items({
            "environment": self.get_env(),
            "type": "vedge"
        })
        for vedge in vedges:
            ports = vedge["ports"]
            for p in ports.values():
                self.add_link_for_vedge(vedge, p)

    def add_link_for_vedge(self, vedge, port):
        vnic = self.inv.get_by_id(self.get_env(),
                                  vedge['host'] + '-' + port["name"])
        if not vnic:
            self.find_matching_vconnector(vedge, port)
            self.find_matching_pnic(vedge, port)
            return
        source = vnic["_id"]
        source_id = vnic["id"]
        target = vedge["_id"]
        target_id = vedge["id"]
        link_type = "vnic-vedge"
        link_name = vnic["name"] + "-" + vedge["name"]
        if "tag" in port:
            link_name += "-" + port["tag"]
        state = "up"  # TBD
        link_weight = 0  # TBD
        source_label = vnic["mac_address"]
        target_label = port["id"]
        self.create_link(self.get_env(), vedge["host"],
                         source, source_id, target, target_id,
                         link_type, link_name, state, link_weight,
                         source_label, target_label)

    def find_matching_vconnector(self, vedge, port):
        if self.configuration.has_network_plugin('VPP'):
            vconnector_interface_name = port['name']
        else:
            if not port["name"].startswith("qv"):
                return
            base_id = port["name"][3:]
            vconnector_interface_name = "qvb" + base_id
        vconnector = self.inv.find_items({
            "environment": self.get_env(),
            "type": "vconnector",
            "host": vedge['host'],
            'interfaces_names': vconnector_interface_name},
            get_single=True)
        if not vconnector:
            return
        source = vconnector["_id"]
        source_id = vconnector["id"]
        target = vedge["_id"]
        target_id = vedge["id"]
        link_type = "vconnector-vedge"
        link_name = "port-" + port["id"]
        if "tag" in port:
            link_name += "-" + port["tag"]
        state = "up"  # TBD
        link_weight = 0  # TBD
        source_label = vconnector_interface_name
        target_label = port["name"]
        mac_address = "Unknown"
        attributes = {'mac_address': mac_address}
        for interface in vconnector['interfaces'].values():
            if vconnector_interface_name != interface['name']:
                continue
            if 'mac_address' not in interface:
                continue
            mac_address = interface['mac_address']
            attributes['mac_address'] = mac_address
            break
        if 'network' in vconnector:
            attributes['network'] = vconnector['network']
        self.create_link(self.get_env(), vedge["host"],
                         source, source_id, target, target_id,
                         link_type, link_name, state, link_weight,
                         source_label, target_label,
                         attributes)

    def find_matching_pnic(self, vedge, port):
        pname = port["name"]
        if "pnic" in vedge:
            if pname != vedge["pnic"]:
                return
        elif self.configuration.has_network_plugin('VPP'):
            pass
        pnic = self.inv.find_items({
            "environment": self.get_env(),
            "type": "pnic",
            "host": vedge["host"],
            "name": pname
        }, get_single=True)
        if not pnic:
            return
        source = vedge["_id"]
        source_id = vedge["id"]
        target = pnic["_id"]
        target_id = pnic["id"]
        link_type = "vedge-pnic"
        link_name = "Port-" + port["id"]
        state = "up" if pnic["Link detected"] == "yes" else "down"
        link_weight = 0  # TBD
        self.create_link(self.get_env(), vedge["host"],
                         source, source_id, target, target_id,
                         link_type, link_name, state, link_weight)