Commit c2b29de6 authored by Andrey Vertiprahov's avatar Andrey Vertiprahov
Browse files

Add update effective_labels on update_if_changed.

parent 65bc616f
...@@ -85,7 +85,7 @@ class Script(BaseScript): ...@@ -85,7 +85,7 @@ class Script(BaseScript):
"name": "FE:0/0/1", "name": "FE:0/0/1",
"type": "physical", "type": "physical",
"mac": mac, "mac": mac,
"hints": ["uplink"], "hints": ["noc::interface::role::uplink"],
"subinterfaces": [], "subinterfaces": [],
} }
] ]
...@@ -101,7 +101,7 @@ class Script(BaseScript): ...@@ -101,7 +101,7 @@ class Script(BaseScript):
vlans += [v["vlan_id"]] vlans += [v["vlan_id"]]
iface = { iface = {
"name": "FE:0/0/1", "name": "FE:0/0/1",
"hints": ["nni"], "hints": ["noc::topology::direction::nni"],
"type": "physical", "type": "physical",
"subinterfaces": [ "subinterfaces": [
{"name": "FE:0/0/1", "enabled_afi": ["BRIDGE"], "tagged_vlans": vlans} {"name": "FE:0/0/1", "enabled_afi": ["BRIDGE"], "tagged_vlans": vlans}
...@@ -110,7 +110,7 @@ class Script(BaseScript): ...@@ -110,7 +110,7 @@ class Script(BaseScript):
interfaces += [iface] interfaces += [iface]
iface = { iface = {
"name": "FE:0/0/2", "name": "FE:0/0/2",
"hints": ["nni"], "hints": ["noc::topology::direction::nni"],
"type": "physical", "type": "physical",
"subinterfaces": [ "subinterfaces": [
{"name": "FE:0/0/2", "enabled_afi": ["BRIDGE"], "tagged_vlans": vlans} {"name": "FE:0/0/2", "enabled_afi": ["BRIDGE"], "tagged_vlans": vlans}
......
...@@ -27,6 +27,7 @@ from builtins import str, object ...@@ -27,6 +27,7 @@ from builtins import str, object
# NOC modules # NOC modules
from noc.core.scheduler.periodicjob import PeriodicJob from noc.core.scheduler.periodicjob import PeriodicJob
from noc.core.models.problem import ProblemItem from noc.core.models.problem import ProblemItem
from noc.main.models.label import Label, MATCH_OPS
from noc.sa.models.managedobject import ManagedObject from noc.sa.models.managedobject import ManagedObject
from noc.inv.models.subinterface import SubInterface from noc.inv.models.subinterface import SubInterface
from noc.inv.models.interfaceprofile import InterfaceProfile from noc.inv.models.interfaceprofile import InterfaceProfile
...@@ -562,6 +563,19 @@ class DiscoveryCheck(object): ...@@ -562,6 +563,19 @@ class DiscoveryCheck(object):
def handler(self): def handler(self):
pass pass
@staticmethod
def build_effective_labels(obj) -> List[str]:
"""
Build object effective labels
:param obj:
:return:
"""
return [
ll
for ll in Label.merge_labels(obj.iter_effective_labels(obj))
if obj.can_set_label(ll) or ll[-1] in MATCH_OPS
]
def update_if_changed( def update_if_changed(
self, self,
obj, obj,
...@@ -569,6 +583,7 @@ class DiscoveryCheck(object): ...@@ -569,6 +583,7 @@ class DiscoveryCheck(object):
ignore_empty: List[str] = None, ignore_empty: List[str] = None,
wait: bool = True, wait: bool = True,
bulk: Optional[List[str]] = None, bulk: Optional[List[str]] = None,
update_effective_labels: bool = False,
): ):
""" """
Update fields if changed. Update fields if changed.
...@@ -579,6 +594,7 @@ class DiscoveryCheck(object): ...@@ -579,6 +594,7 @@ class DiscoveryCheck(object):
:param ignore_empty: List of fields which may be ignored if empty :param ignore_empty: List of fields which may be ignored if empty
:param wait: Wait for operation to complete. set write concern to 0 if False :param wait: Wait for operation to complete. set write concern to 0 if False
:param bulk: Execute as the bulk op instead :param bulk: Execute as the bulk op instead
:param update_effective_labels:
:returns: List of changed (key, value) :returns: List of changed (key, value)
:rtype: list :rtype: list
""" """
...@@ -603,6 +619,10 @@ class DiscoveryCheck(object): ...@@ -603,6 +619,10 @@ class DiscoveryCheck(object):
continue continue
setattr(obj, k, v) setattr(obj, k, v)
changes += [(k, v)] changes += [(k, v)]
if update_effective_labels and hasattr(obj, "effective_labels"):
el = self.build_effective_labels(obj)
if set(el) != set(getattr(obj, "effective_labels", [])):
changes += [("effective_labels", el)]
if changes: if changes:
if bulk is not None: if bulk is not None:
op = {"$set": dict(changes)} op = {"$set": dict(changes)}
......
...@@ -279,6 +279,7 @@ class InterfaceCheck(PolicyDiscoveryCheck): ...@@ -279,6 +279,7 @@ class InterfaceCheck(PolicyDiscoveryCheck):
"extra_labels": [ll for ll in labels if Interface.can_set_label(ll)], "extra_labels": [ll for ll in labels if Interface.can_set_label(ll)],
}, },
ignore_empty=ignore_empty, ignore_empty=ignore_empty,
update_effective_labels=True,
) )
self.log_changes(f"Interface '{name}' has been changed", changes) self.log_changes(f"Interface '{name}' has been changed", changes)
else: else:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment