From 022b2947c74593707b2822ca1ded62efd251e3ea Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Mon, 26 Jul 2021 16:25:53 +0700 Subject: [PATCH 01/11] vak-1644 added wares data (pairs connected ports). --- services/web/apps/inv/inv/views.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index 28c619e868..1e9d98f17a 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -8,6 +8,7 @@ # Python modules import inspect import os +import re from typing import Optional, Dict, List, Any # NOC modules @@ -26,6 +27,9 @@ from noc.sa.interfaces.base import ( from noc.core.translation import ugettext as _ +pattern = re.compile(r"Wire\s\S:(.+)\s<->\s\S:(.+)") + + class InvApplication(ExtApplication): """ inv.inv application @@ -336,12 +340,20 @@ class InvApplication(ExtApplication): "disable_reason": disable_reason, } ] + wares = [] + if lcs and rcs: + for w in Object.objects.filter(container=lo.container.id): + result = pattern.fullmatch(w.name) + if result: + wares.append((result.group(1), result.group(2))) + print(wares) # Forming cable return { "left": {"connections": lcs}, "right": {"connections": rcs}, "cable": [{"name": c.name, "available": True} for c in cables], "valid": lcs and rcs and left_filter and right_filter, + "wares": wares, } @view( -- GitLab From 21a5c6ee65902f78cd025aa2857a04054ede8a8e Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Mon, 26 Jul 2021 16:38:52 +0700 Subject: [PATCH 02/11] vak-1644 renamed wares -> wires. --- services/web/apps/inv/inv/views.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index 1e9d98f17a..5af3188aac 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -340,20 +340,19 @@ class InvApplication(ExtApplication): "disable_reason": disable_reason, } ] - wares = [] + wires = [] if lcs and rcs: for w in Object.objects.filter(container=lo.container.id): result = pattern.fullmatch(w.name) if result: - wares.append((result.group(1), result.group(2))) - print(wares) + wires.append((result.group(1), result.group(2))) # Forming cable return { "left": {"connections": lcs}, "right": {"connections": rcs}, "cable": [{"name": c.name, "available": True} for c in cables], "valid": lcs and rcs and left_filter and right_filter, - "wares": wares, + "wires": wires, } @view( -- GitLab From 312cebf244a99746e5fe5c4fa93314119f9ec409 Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Mon, 26 Jul 2021 17:11:07 +0700 Subject: [PATCH 03/11] vak-1644 added devices_names field to json answer. --- services/web/apps/inv/inv/views.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index 5af3188aac..01a745cb2d 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -27,7 +27,7 @@ from noc.sa.interfaces.base import ( from noc.core.translation import ugettext as _ -pattern = re.compile(r"Wire\s\S:(.+)\s<->\s\S:(.+)") +pattern = re.compile(r"Wire\s\S+:(.+)\s<->\s\S+:(.+)") class InvApplication(ExtApplication): @@ -341,11 +341,13 @@ class InvApplication(ExtApplication): } ] wires = [] + devices_names = [] if lcs and rcs: for w in Object.objects.filter(container=lo.container.id): result = pattern.fullmatch(w.name) if result: wires.append((result.group(1), result.group(2))) + devices_names = (lo.name, ro.name) # Forming cable return { "left": {"connections": lcs}, @@ -353,6 +355,7 @@ class InvApplication(ExtApplication): "cable": [{"name": c.name, "available": True} for c in cables], "valid": lcs and rcs and left_filter and right_filter, "wires": wires, + "devices_names": devices_names, } @view( -- GitLab From e83998ea0bec90a4a30a1ff453c0bbf62610a58a Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Mon, 26 Jul 2021 20:30:55 +0700 Subject: [PATCH 04/11] vak-1644 added id ports and devices into json answer. --- services/web/apps/inv/inv/views.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index 01a745cb2d..43f7c6daa5 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -9,6 +9,7 @@ import inspect import os import re +import ctypes from typing import Optional, Dict, List, Any # NOC modules @@ -25,6 +26,7 @@ from noc.sa.interfaces.base import ( BooleanParameter, ) from noc.core.translation import ugettext as _ +from noc.core.comp import smart_text pattern = re.compile(r"Wire\s\S+:(.+)\s<->\s\S+:(.+)") @@ -267,6 +269,7 @@ class InvApplication(ExtApplication): ro: Optional[Object] = None if o2: ro = self.get_object_or_404(Object, id=o2) + id_ports_left = {} lcs: List[Dict[str, Any]] = [] cable: Optional[ObjectModel] = None # Getting cable @@ -291,8 +294,10 @@ class InvApplication(ExtApplication): [c for c in lo.model.get_connection_proposals(c.name) if c[0] == ro.model.id] ) oc, oo, _ = lo.get_p2p_connection(c.name) + left_id = ctypes.c_size_t(hash(c.name)).value lcs += [ { + "id": left_id, "name": c.name, "type": str(c.type.id), "type__label": c.type.name, @@ -304,6 +309,8 @@ class InvApplication(ExtApplication): "disable_reason": disable_reason, } ] + id_ports_left[c.name] = left_id + id_ports_right = {} rcs: List[Dict[str, Any]] = [] if ro: for c in ro.model.connections: @@ -327,8 +334,10 @@ class InvApplication(ExtApplication): ] ) oc, oo, _ = ro.get_p2p_connection(c.name) + right_id = ctypes.c_size_t(hash(c.name)).value rcs += [ { + "id": right_id, "name": c.name, "type": str(c.type.id), "type__label": c.type.name, @@ -340,14 +349,22 @@ class InvApplication(ExtApplication): "disable_reason": disable_reason, } ] + id_ports_right[c.name] = right_id wires = [] - devices_names = [] + devices_names = () if lcs and rcs: for w in Object.objects.filter(container=lo.container.id): result = pattern.fullmatch(w.name) if result: - wires.append((result.group(1), result.group(2))) - devices_names = (lo.name, ro.name) + name_left = result.group(1) + name_right = result.group(2) + wires.append( + ( + (id_ports_left.get(name_left, 0), name_left), + (id_ports_right.get(name_right, 0), name_right), + ) + ) + devices_names = ((smart_text(lo.id), lo.name), (smart_text(ro.id), ro.name)) # Forming cable return { "left": {"connections": lcs}, -- GitLab From 1de39a8fc87f12298a14f82759dbd6a8753fab42 Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Tue, 27 Jul 2021 13:47:31 +0700 Subject: [PATCH 05/11] vak-1644 fixed c.id --- services/web/apps/inv/inv/views.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index 43f7c6daa5..d3613f1370 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -9,7 +9,6 @@ import inspect import os import re -import ctypes from typing import Optional, Dict, List, Any # NOC modules @@ -294,7 +293,7 @@ class InvApplication(ExtApplication): [c for c in lo.model.get_connection_proposals(c.name) if c[0] == ro.model.id] ) oc, oo, _ = lo.get_p2p_connection(c.name) - left_id = ctypes.c_size_t(hash(c.name)).value + left_id = f"{smart_text(ro.id)}{c.name}" lcs += [ { "id": left_id, @@ -334,7 +333,7 @@ class InvApplication(ExtApplication): ] ) oc, oo, _ = ro.get_p2p_connection(c.name) - right_id = ctypes.c_size_t(hash(c.name)).value + right_id = f"{smart_text(ro.id)}{c.name}" rcs += [ { "id": right_id, @@ -351,7 +350,6 @@ class InvApplication(ExtApplication): ] id_ports_right[c.name] = right_id wires = [] - devices_names = () if lcs and rcs: for w in Object.objects.filter(container=lo.container.id): result = pattern.fullmatch(w.name) @@ -364,15 +362,13 @@ class InvApplication(ExtApplication): (id_ports_right.get(name_right, 0), name_right), ) ) - devices_names = ((smart_text(lo.id), lo.name), (smart_text(ro.id), ro.name)) # Forming cable return { - "left": {"connections": lcs}, - "right": {"connections": rcs}, + "left": {"connections": lcs, "device": {"id": smart_text(lo.id), "name": lo.name}}, + "right": {"connections": rcs, "device": {"id": smart_text(ro.id), "name": ro.name}}, "cable": [{"name": c.name, "available": True} for c in cables], "valid": lcs and rcs and left_filter and right_filter, "wires": wires, - "devices_names": devices_names, } @view( -- GitLab From 09e6b44ce3ac4e67ca25957ec721f998cf68153f Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Tue, 27 Jul 2021 14:43:23 +0700 Subject: [PATCH 06/11] vak-1644 fixed forming data of wires. --- services/web/apps/inv/inv/views.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index d3613f1370..51b12b7cc6 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -8,7 +8,6 @@ # Python modules import inspect import os -import re from typing import Optional, Dict, List, Any # NOC modules @@ -24,13 +23,11 @@ from noc.sa.interfaces.base import ( ListOfParameter, BooleanParameter, ) +from noc.core.inv.path import find_path from noc.core.translation import ugettext as _ from noc.core.comp import smart_text -pattern = re.compile(r"Wire\s\S+:(.+)\s<->\s\S+:(.+)") - - class InvApplication(ExtApplication): """ inv.inv application @@ -269,6 +266,7 @@ class InvApplication(ExtApplication): if o2: ro = self.get_object_or_404(Object, id=o2) id_ports_left = {} + checking_ports = [] lcs: List[Dict[str, Any]] = [] cable: Optional[ObjectModel] = None # Getting cable @@ -294,6 +292,8 @@ class InvApplication(ExtApplication): ) oc, oo, _ = lo.get_p2p_connection(c.name) left_id = f"{smart_text(ro.id)}{c.name}" + if bool(oc): + checking_ports.append(c) lcs += [ { "id": left_id, @@ -350,17 +350,18 @@ class InvApplication(ExtApplication): ] id_ports_right[c.name] = right_id wires = [] - if lcs and rcs: - for w in Object.objects.filter(container=lo.container.id): - result = pattern.fullmatch(w.name) - if result: - name_left = result.group(1) - name_right = result.group(2) + for p in checking_ports: + for path in find_path(lo, p.name, p.protocols): + if path.obj == ro: + print(path.obj, path.connection) wires.append( - ( - (id_ports_left.get(name_left, 0), name_left), - (id_ports_right.get(name_right, 0), name_right), - ) + { + "left": {"id": id_ports_left.get(p.name), "name": p.name}, + "right": { + "id": id_ports_right.get(path.connection), + "name": path.connection, + }, + } ) # Forming cable return { -- GitLab From 6b46168a32fd5b62f23f68ebf146edf756b08d11 Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Tue, 27 Jul 2021 15:42:02 +0700 Subject: [PATCH 07/11] vak-1644 created get_remote_slot method. --- services/web/apps/inv/inv/views.py | 32 +++++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index 51b12b7cc6..e42ae4def1 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -62,6 +62,15 @@ class InvApplication(ExtApplication): def get_plugin_data(self, name): return {"name": name, "xtype": self.plugins[name].js} + def get_remote_slot(self, left_slot, lo, ro): + """ + Determing right device's slot with find_path method + :return: + """ + for path in find_path(lo, left_slot.name, left_slot.protocols): + if path.obj == ro: + return path + @view("^node/$", method=["GET"], access="read", api=True) def api_node(self, request): children = [] @@ -351,18 +360,17 @@ class InvApplication(ExtApplication): id_ports_right[c.name] = right_id wires = [] for p in checking_ports: - for path in find_path(lo, p.name, p.protocols): - if path.obj == ro: - print(path.obj, path.connection) - wires.append( - { - "left": {"id": id_ports_left.get(p.name), "name": p.name}, - "right": { - "id": id_ports_right.get(path.connection), - "name": path.connection, - }, - } - ) + remote = self.get_remote_slot(p, lo, ro) + if remote: + wires.append( + { + "left": {"id": id_ports_left.get(p.name), "name": p.name}, + "right": { + "id": id_ports_right.get(remote.connection), + "name": remote.connection, + }, + } + ) # Forming cable return { "left": {"connections": lcs, "device": {"id": smart_text(lo.id), "name": lo.name}}, -- GitLab From 276b9c11a73e38671c9f0305349fcbf446fdc2e5 Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Tue, 27 Jul 2021 15:56:52 +0700 Subject: [PATCH 08/11] vak-1644 fixed checking lcs and rcs data. --- services/web/apps/inv/inv/views.py | 37 ++++++++++++++++++------------ 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index e42ae4def1..dfc7ba52c9 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -300,7 +300,7 @@ class InvApplication(ExtApplication): [c for c in lo.model.get_connection_proposals(c.name) if c[0] == ro.model.id] ) oc, oo, _ = lo.get_p2p_connection(c.name) - left_id = f"{smart_text(ro.id)}{c.name}" + left_id = f"{smart_text(lo.id)}{c.name}" if bool(oc): checking_ports.append(c) lcs += [ @@ -359,22 +359,29 @@ class InvApplication(ExtApplication): ] id_ports_right[c.name] = right_id wires = [] - for p in checking_ports: - remote = self.get_remote_slot(p, lo, ro) - if remote: - wires.append( - { - "left": {"id": id_ports_left.get(p.name), "name": p.name}, - "right": { - "id": id_ports_right.get(remote.connection), - "name": remote.connection, - }, - } - ) + device_left = {} + device_right = {} + if lcs and rcs: + device_left["id"] = smart_text(lo.id) + device_left["name"] = lo.name + device_right["id"] = smart_text(ro.id) + device_right["name"] = ro.name + for p in checking_ports: + remote = self.get_remote_slot(p, lo, ro) + if remote: + wires.append( + { + "left": {"id": id_ports_left.get(p.name), "name": p.name}, + "right": { + "id": id_ports_right.get(remote.connection), + "name": remote.connection, + }, + } + ) # Forming cable return { - "left": {"connections": lcs, "device": {"id": smart_text(lo.id), "name": lo.name}}, - "right": {"connections": rcs, "device": {"id": smart_text(ro.id), "name": ro.name}}, + "left": {"connections": lcs, "device": device_left}, + "right": {"connections": rcs, "device": device_right}, "cable": [{"name": c.name, "available": True} for c in cables], "valid": lcs and rcs and left_filter and right_filter, "wires": wires, -- GitLab From 00bd0232a2b94a89165513bf71b35bd2210fcae4 Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Tue, 27 Jul 2021 16:26:01 +0700 Subject: [PATCH 09/11] vak-1644 created var is_employed, set default value of id_ports_left and id_ports_right dicts. --- services/web/apps/inv/inv/views.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index dfc7ba52c9..382efe530b 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -301,7 +301,8 @@ class InvApplication(ExtApplication): ) oc, oo, _ = lo.get_p2p_connection(c.name) left_id = f"{smart_text(lo.id)}{c.name}" - if bool(oc): + is_employed = bool(oc) + if is_employed: checking_ports.append(c) lcs += [ { @@ -312,7 +313,7 @@ class InvApplication(ExtApplication): "gender": c.gender, "direction": c.direction, "protocols": c.protocols, - "free": not bool(oc), + "free": not is_employed, "valid": valid, "disable_reason": disable_reason, } @@ -371,9 +372,9 @@ class InvApplication(ExtApplication): if remote: wires.append( { - "left": {"id": id_ports_left.get(p.name), "name": p.name}, + "left": {"id": id_ports_left.get(p.name, 0), "name": p.name}, "right": { - "id": id_ports_right.get(remote.connection), + "id": id_ports_right.get(remote.connection, 0), "name": remote.connection, }, } -- GitLab From cd93bbf356023462b990bc7897775c4f822f80c9 Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Tue, 27 Jul 2021 21:06:39 +0700 Subject: [PATCH 10/11] vak-1644-devices created get_remote_device method. --- services/web/apps/inv/inv/views.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index 382efe530b..50d975b465 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -71,6 +71,15 @@ class InvApplication(ExtApplication): if path.obj == ro: return path + def get_remote_device(self, slot, o): + """ + Determing remote device with find_path method + :return: + """ + for path in find_path(o, slot.name, slot.protocols): + if path.obj != o and not path.obj.name.startswith("Wire"): + return path + @view("^node/$", method=["GET"], access="read", api=True) def api_node(self, request): children = [] -- GitLab From 9e0c65bf7c459b8a370525d7da077bbe5d79a785 Mon Sep 17 00:00:00 2001 From: Vladimir Komarov Date: Wed, 28 Jul 2021 13:49:16 +0700 Subject: [PATCH 11/11] vak-1644-devices created get_cs_item method. --- services/web/apps/inv/inv/views.py | 94 +++++++++++++++++++++--------- 1 file changed, 68 insertions(+), 26 deletions(-) diff --git a/services/web/apps/inv/inv/views.py b/services/web/apps/inv/inv/views.py index 50d975b465..7eb557de16 100644 --- a/services/web/apps/inv/inv/views.py +++ b/services/web/apps/inv/inv/views.py @@ -71,15 +71,55 @@ class InvApplication(ExtApplication): if path.obj == ro: return path - def get_remote_device(self, slot, o): + def get_remote_device(self, slot, protocols, o): """ Determing remote device with find_path method :return: """ - for path in find_path(o, slot.name, slot.protocols): + for path in find_path(o, slot, protocols): if path.obj != o and not path.obj.name.startswith("Wire"): return path + def get_cs_item( + self, + id, + name, + type, + type__label, + gender, + direction, + protocols, + free, + valid, + disable_reason, + o, + ): + """ + Creating member of cs dict + :return: + """ + cs = { + "id": id, + "name": name, + "type": type, + "type__label": type__label, + "gender": gender, + "direction": direction, + "protocols": protocols, + "free": free, + "valid": valid, + "disable_reason": disable_reason, + } + if not free: + rd = self.get_remote_device(name, protocols, o) + if rd: + cs["remote_device"] = { + "name": rd.obj.name, + "id": smart_text(rd.obj.id), + "slot": rd.connection, + } + return cs + @view("^node/$", method=["GET"], access="read", api=True) def api_node(self, request): children = [] @@ -314,18 +354,19 @@ class InvApplication(ExtApplication): if is_employed: checking_ports.append(c) lcs += [ - { - "id": left_id, - "name": c.name, - "type": str(c.type.id), - "type__label": c.type.name, - "gender": c.gender, - "direction": c.direction, - "protocols": c.protocols, - "free": not is_employed, - "valid": valid, - "disable_reason": disable_reason, - } + self.get_cs_item( + left_id, + c.name, + str(c.type.id), + c.type.name, + c.gender, + c.direction, + c.protocols, + not is_employed, + valid, + disable_reason, + lo, + ) ] id_ports_left[c.name] = left_id id_ports_right = {} @@ -354,18 +395,19 @@ class InvApplication(ExtApplication): oc, oo, _ = ro.get_p2p_connection(c.name) right_id = f"{smart_text(ro.id)}{c.name}" rcs += [ - { - "id": right_id, - "name": c.name, - "type": str(c.type.id), - "type__label": c.type.name, - "gender": c.gender, - "direction": c.direction, - "protocols": c.protocols, - "free": not bool(oc), - "valid": valid, - "disable_reason": disable_reason, - } + self.get_cs_item( + right_id, + c.name, + str(c.type.id), + c.type.name, + c.gender, + c.direction, + c.protocols, + not bool(oc), + valid, + disable_reason, + ro, + ) ] id_ports_right[c.name] = right_id wires = [] -- GitLab