Commit 759a6ec4 authored by Andrey Vertiprahov's avatar Andrey Vertiprahov
Browse files

Merge branch 'issue-1822-clear-ObjectCapabilities' into 'master'

Remove ObjectCapabilities model

See merge request noc/noc!6385
parents e3f444b1 65acb89d
......@@ -204,7 +204,6 @@ _MODELS = {
"sa.ManagedObjectProfile": "noc.sa.models.managedobjectprofile.ManagedObjectProfile",
"sa.ManagedObjectSelector": "noc.sa.models.managedobjectselector.ManagedObjectSelector",
"sa.ManagedObjectSelectorByAttribute": "noc.sa.models.managedobjectselector.ManagedObjectSelectorByAttribute",
"sa.ObjectCapabilities": "noc.sa.models.objectcapabilities.ObjectCapabilities",
"sa.ObjectNotification": "noc.sa.models.objectnotification.ObjectNotification",
"sa.ObjectStatus": "noc.sa.models.objectstatus.ObjectStatus",
"sa.Profile": "noc.sa.models.profile.Profile",
......
......@@ -170,7 +170,6 @@ logger = logging.getLogger(__name__)
("sa.ServiceSummary", "managed_object"),
("inv.DiscoveryID", "object"),
("inv.Sensor", "managed_object"),
("sa.ObjectCapabilities", "object"),
],
clean=[
("ip.Address", "managed_object"),
......
# ----------------------------------------------------------------------
# Object Capabilities
# ----------------------------------------------------------------------
# Copyright (C) 2007-2020 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------
# Python modules
import logging
from typing import Optional
# Third-party modules
from mongoengine.document import Document
from mongoengine.fields import ListField, EmbeddedDocumentField
# NOC modules
from noc.inv.models.capability import Capability
from .managedobject import ManagedObject, CREDENTIAL_CACHE_VERSION
from noc.core.mongo.fields import ForeignKeyField
from noc.core.model.decorator import on_save
from noc.core.cache.base import cache
from noc.core.change.decorator import change
from noc.inv.models.capsitem import CapsItem
logger = logging.getLogger(__name__)
@on_save
@change
class ObjectCapabilities(Document):
meta = {"collection": "noc.sa.objectcapabilities", "strict": False, "auto_create_index": False}
object = ForeignKeyField(ManagedObject, primary_key=True)
caps = ListField(EmbeddedDocumentField(CapsItem))
def __str__(self):
return "%s caps" % self.object.name
@classmethod
def get_by_id(cls, id) -> Optional["ObjectCapabilities"]:
return ObjectCapabilities.objects.filter(object=id).first()
def on_save(self):
cache.delete("cred-%s" % self.object.id, version=CREDENTIAL_CACHE_VERSION)
def iter_changed_datastream(self, changed_fields=None):
yield "managedobject", self.object.id
@classmethod
def get_capabilities(cls, object):
"""
Resolve object capabilities
:param object: ManagedObject instance or id
:return: dict of capability name -> current value
"""
if hasattr(object, "id"):
object = object.id
caps = {}
oc = ObjectCapabilities._get_collection().find_one({"_id": object})
if oc:
for c in oc["caps"]:
cc = Capability.get_by_id(c["capability"])
if cc:
caps[cc.name] = c.get("value")
return caps
@classmethod
def update_capabilities(cls, object, caps, source):
"""
Update stored capabilities
:param object:
:param caps:
:param source:
:return:
"""
o_label = object
if hasattr(object, "id"):
o_label = object.name
object = object.id
o_label += "|%s" % source
oc = ObjectCapabilities._get_collection().find_one({"_id": object}) or {}
# Update existing capabilities
new_caps = []
seen = set()
changed = False
for ci in oc.get("caps", []):
c = Capability.get_by_id(ci["capability"])
cs = ci.get("source")
cv = ci.get("value")
if not c:
logger.info("[%s] Removing unknown capability id %s", o_label, ci["capability"])
continue
cn = c.name
seen.add(cn)
if cs == source:
if cn in caps:
if caps[cn] != cv:
logger.info(
"[%s] Changing capability %s: %s -> %s", o_label, cn, cv, caps[cn]
)
ci["value"] = caps[cn]
changed = True
else:
logger.info("[%s] Removing capability %s", o_label, cn)
changed = True
continue
elif cn in caps:
logger.info(
"[%s] Not changing capability %s: " "Already set with source '%s'",
o_label,
cn,
cs,
)
new_caps += [ci]
# Add new capabilities
for cn in set(caps) - seen:
c = Capability.get_by_name(cn)
if not c:
logger.info("[%s] Unknown capability %s, ignoring", o_label, cn)
continue
logger.info("[%s] Adding capability %s = %s", o_label, cn, caps[cn])
new_caps += [{"capability": c.id, "value": caps[cn], "source": source}]
changed = True
if changed:
logger.info("[%s] Saving changes", o_label)
ObjectCapabilities._get_collection().update(
{"_id": object}, {"$set": {"caps": new_caps}}, upsert=True
)
cache.delete("cred-%s" % object, version=CREDENTIAL_CACHE_VERSION)
caps = {}
for ci in new_caps:
cn = Capability.get_by_id(ci["capability"])
if cn:
caps[cn.name] = ci.get("value")
return caps
......@@ -17,7 +17,6 @@ from noc.inv.models.subinterface import SubInterface
from noc.inv.models.link import Link
from noc.inv.models.macdb import MACDB
from noc.inv.models.discoveryid import DiscoveryID
from noc.sa.models.objectcapabilities import ObjectCapabilities
from noc.fm.models.failedevent import FailedEvent
from noc.fm.models.activeevent import ActiveEvent
from noc.fm.models.archivedevent import ArchivedEvent
......@@ -97,9 +96,6 @@ def wipe(o):
# Wipe reboots
log.debug("Wiping reboots")
Reboot.objects.filter(object=o.id).delete()
# Delete Managed Object's capabilities
log.debug("Wiping capabilitites")
ObjectCapabilities.objects.filter(object=o.id).delete()
# Delete Managed Object's attributes
log.debug("Wiping attributes")
ManagedObjectAttribute.objects.filter(managed_object=o).delete()
......
......@@ -188,7 +188,6 @@ class SAEAPI(API):
metrics["error", ("type", "object_not_managed")] += 1
raise APIError("Object is not managed")
# Build capabilities
# capabilities = ObjectCapabilities.get_capabilities(object_id)
capabilities = {}
if caps:
for c in caps:
......
......@@ -71,7 +71,6 @@ class ObjectListApplication(ExtApplication):
caps0=!CapsID - caps is not exists
caps0=CapsID:true - caps value equal True
caps0=CapsID:2~50 - caps value many then 2 and less then 50
c_ids = set(ObjectCapabilities.objects(cq).distinct('object'))
"""
c = nq.pop(cc)
......
......@@ -30,7 +30,6 @@ from noc.sa.models.profile import Profile
from noc.sa.models.managedobjectprofile import ManagedObjectProfile
from noc.sa.models.managedobject import ManagedObject
from noc.services.discovery.jobs.box.job import BoxDiscoveryJob
from noc.sa.models.objectcapabilities import ObjectCapabilities
from noc.inv.models.discoveryid import DiscoveryID
_root_ad_cache = {}
......@@ -89,7 +88,7 @@ class BeefCallWrapper(object):
scr = scls(
service=get_service(self.object.pool.name),
credentials=credentials,
capabilities=ObjectCapabilities.get_capabilities(self.object.id),
capabilities=self.object.get_caps(),
version=version,
timeout=3600,
name=script_name,
......
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