get_interfaces.py 9.47 KB
Newer Older
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
1
2
3
# ---------------------------------------------------------------------
# Eltex.LTE.get_interfaces
# ---------------------------------------------------------------------
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
4
# Copyright (C) 2007-2021 The NOC Project
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
5
6
# See LICENSE for details
# ---------------------------------------------------------------------
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
7

Dmitry Volodin's avatar
Dmitry Volodin committed
8
# Python modules
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
9
10
import re

Dmitry Volodin's avatar
Dmitry Volodin committed
11
12
13
# NOC modules
from noc.core.script.base import BaseScript
from noc.sa.interfaces.igetinterfaces import IGetInterfaces
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
14
from noc.core.text import parse_table
Dmitry Volodin's avatar
Dmitry Volodin committed
15

Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
16
17
18
19
20

class Script(BaseScript):
    name = "Eltex.LTE.get_interfaces"
    interface = IGetInterfaces

Dmitry Volodin's avatar
Dmitry Volodin committed
21
    rx_mac1 = re.compile(r"^Port (?P<port>\d+) MAC address: (?P<mac>\S+)", re.MULTILINE)
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
22
23
24
25
26
27
28
29
30
    rx_mac2 = re.compile(r"MAC address:\s*\n^\s+(?P<mac>\S+)", re.MULTILINE)
    rx_vlan = re.compile(
        r"^Interface (?P<port>\d+)\s*\n"
        r"^\s+PVID:\s+\d+\s*\n"
        r"^\s+Frame types:\s+\S+\s*\n"
        r"^\s+Ingress filtering:\s+\S+\s*\n"
        r"^\s+Member of VLANs:\s*\n"
        r"^\s+tagged:\s+(?P<tagged>.+)\n"
        r"^\s+untagged:\s+(?P<untagged>\d+|none)",
Dmitry Volodin's avatar
Dmitry Volodin committed
31
32
        re.MULTILINE | re.DOTALL,
    )
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
33
34
35

    rx_ip = re.compile(r"\n\nIP address:\s+(?P<ip>\d+\S+)")
    rx_mgmt_ip = re.compile(
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
36
        r"^Management interface:( \((?P<state>\S+)\))?\s*\n"
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
37
        r"^IP address:\s+(?P<ip>\d+\S+)\s*\n"
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
38
        r"(^Default gateway:.+\n)?"
Dmitry Volodin's avatar
Dmitry Volodin committed
39
40
41
        r"^VID:\s+(?P<vlan_id>\d+|none)",
        re.MULTILINE,
    )
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
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
    rx_mac = re.compile(r"^\s+MAC address: (?P<mac>\S+)", re.MULTILINE)
    rx_status = re.compile(r"^.+\d\s+(?P<oper_status>up|down|off)", re.MULTILINE)

    def normalize_ifname(self, port):
        port = port.strip()
        while port.find("  ") != -1:
            port = port.replace("  ", " ")
        while port.find("- ") != -1:
            port = port.replace("- ", "-")
        return port

    def create_iface(self, i, iftype):
        ifname = " ".join(i[0].split())
        if not ifname.startswith(iftype):
            return None
        pvid = i[1]
        if i[4] not in ["none", "N/S"]:
            tagged = self.expand_rangelist(i[4])
        else:
            tagged = []
        untagged = i[5]
        if untagged in ["none", "N/S"]:
            untagged = pvid
        iface = {
            "name": ifname,
            "type": "physical",
            "subinterfaces": [{"name": ifname, "enabled_afi": ["BRIDGE"]}],
        }
        if untagged != "N/S":
            iface["subinterfaces"][0]["untagged_vlan"] = int(untagged)
        if tagged:
            iface["subinterfaces"][0]["tagged_vlans"] = tagged
        return iface
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
75
76
77
78
79
80

    def execute(self):
        interfaces = []
        macs = {}
        tagged_vlans = {}
        untagged_vlans = {}
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
81
82
83
84
85
        try:
            with self.profile.switch(self):
                cmd = self.cli("show interfaces mac-address")
                for match in self.rx_mac1.finditer(cmd):
                    macs[match.group("port")] = match.group("mac")
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
86
87
                for line in self.cli("show interfaces vlans").split("\n\n"):
                    match = self.rx_vlan.search(line)
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
88
89
90
91
92
93
94
95
96
                    if match:
                        if match.group("tagged") != "none":
                            tagged_vlans[match.group("port")] = self.expand_rangelist(
                                match.group("tagged")
                            )
                        if match.group("untagged") != "none":
                            untagged_vlans[match.group("port")] = match.group("untagged")
                cmd = self.cli("show version")
                match = self.rx_mac2.search(cmd)
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
97
                if match:
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
                    ip_mac = match.group("mac")
                else:
                    ip_mac = ""
            ports = self.scripts.get_interface_status()
            for i in ports:
                iface = {
                    "name": i["interface"],
                    "type": "physical",
                    "oper_status": i["status"],
                    "mac": macs[i["interface"]],
                    "subinterfaces": [
                        {
                            "name": i["interface"],
                            "oper_status": i["status"],
                            "enabled_afi": ["BRIDGE"],
                            "mac": macs[i["interface"]],
                        }
                    ],
                }
                t = tagged_vlans.get(i["interface"])
                if t:
                    iface["subinterfaces"][0]["tagged_vlans"] = t
                u = untagged_vlans.get(i["interface"])
                if u:
                    iface["subinterfaces"][0]["untagged_vlan"] = u
                interfaces += [iface]
        except self.CLISyntaxError:
            # We are already in `switch` context
            c = self.cli("show vlan", cached=True)
            t = parse_table(c, allow_wrap=True, footer="dummy footer")
            for i in t:
                vlan_id = i[0]
                if i[2] != "none":
                    tagged = i[2].split(", ")
                    for port in tagged:
                        ifname = self.normalize_ifname(port)
                        found = False
                        for iface in interfaces:
                            if iface["name"] == ifname:
                                if "tagged_vlans" in iface["subinterfaces"][0]:
                                    iface["subinterfaces"][0]["tagged_vlans"] += [vlan_id]
                                else:
                                    iface["subinterfaces"][0]["tagged_vlans"] = [vlan_id]
                                found = True
                                break
                        if not found:
                            iface = {
                                "name": ifname,
                                "type": "physical",
                                "subinterfaces": [
                                    {
                                        "name": ifname,
                                        "enabled_afi": ["BRIDGE"],
                                        "tagged_vlans": [vlan_id],
                                    }
                                ],
                            }
                            interfaces += [iface]
                if i[3] != "none":
                    untagged = i[3].split(", ")
                    for port in untagged:
                        ifname = self.normalize_ifname(port)
                        found = False
                        for iface in interfaces:
                            if iface["name"] == ifname:
                                iface["subinterfaces"][0]["untagged_vlan"] = vlan_id
                                found = True
                                break
                        if not found:
                            iface = {
                                "name": ifname,
                                "type": "physical",
                                "subinterfaces": [
                                    {
                                        "name": ifname,
                                        "enabled_afi": ["BRIDGE"],
                                        "untagged_vlan": vlan_id,
                                    }
                                ],
                            }
                            interfaces += [iface]
            for i in interfaces:
                c = self.cli("show interfaces mac-address %s" % i["name"])
                match = self.rx_mac.search(c)
                if match:
                    i["mac"] = match.group("mac")
                    i["subinterfaces"][0]["mac"] = match.group("mac")
                try:
                    c = self.cli("show interfaces status %s" % i["name"])
                    match = self.rx_status.search(c)
                    i["oper_status"] = match.group("oper_status") == "up"
                    i["subinterfaces"][0]["oper_status"] = match.group("oper_status") == "up"
                    i["admin_status"] = match.group("oper_status") != "off"
                    i["subinterfaces"][0]["admin_status"] = match.group("oper_status") != "off"
                except self.CLISyntaxError:
                    pass
            c = self.cli("exit")  # manually exit from `switch` context
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
195
196
197
198
199
200
        cmd = self.cli("show system information")
        match = self.rx_ip.search(cmd)
        if match:
            iface = {
                "name": "ip",
                "type": "SVI",
Dmitry Volodin's avatar
Dmitry Volodin committed
201
202
203
                "subinterfaces": [
                    {"name": "ip", "enabled_afi": ["IPv4"], "ipv4_addresses": [match.group("ip")]}
                ],
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
204
205
206
207
208
209
210
211
212
213
214
215
            }
            if ip_mac:
                iface["mac"] = ip_mac
                iface["subinterfaces"][0]["mac"] = ip_mac
            interfaces += [iface]
        match = self.rx_mgmt_ip.search(cmd)
        if match:
            iface = {
                "name": "mgmt",
                "type": "management",
                "oper_status": match.group("state") == "enabled",
                "admin_status": match.group("state") == "enabled",
Dmitry Volodin's avatar
Dmitry Volodin committed
216
217
218
219
220
221
222
223
224
                "subinterfaces": [
                    {
                        "name": "mgmt",
                        "oper_status": match.group("state") == "enabled",
                        "admin_status": match.group("state") == "enabled",
                        "enabled_afi": ["IPv4"],
                        "ipv4_addresses": [match.group("ip")],
                    }
                ],
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
225
226
            }
            if match.group("vlan_id") != "none":
Maksim's avatar
Maksim committed
227
                iface["subinterfaces"][0]["vlan_id"] = [match.group("vlan_id")]
Dmitry Lukhtionov's avatar
Dmitry Lukhtionov committed
228
229
            interfaces += [iface]
        return [{"interfaces": interfaces}]