confdbquery.py 5.02 KB
Newer Older
Dmitry Volodin's avatar
Dmitry Volodin committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------
# ConfDBQuery model
# ----------------------------------------------------------------------
# Copyright (C) 2007-2019 The NOC Project
# See LICENSE for details
# ----------------------------------------------------------------------

# Python modules
import threading
import operator
import os

# Third-party modules
Dmitry Volodin's avatar
Dmitry Volodin committed
15
16
17
18
19
20
21
22
from mongoengine.document import Document, EmbeddedDocument
from mongoengine.fields import (
    StringField,
    UUIDField,
    BooleanField,
    ListField,
    EmbeddedDocumentField,
)
Dmitry Volodin's avatar
Dmitry Volodin committed
23
24
25
26
27
28
29
import six
import cachetools

# NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.core.model.decorator import on_delete_check
Dmitry Volodin's avatar
Dmitry Volodin committed
30
from noc.sa.interfaces.base import StringParameter, IntParameter, BooleanParameter
Dmitry Volodin's avatar
Dmitry Volodin committed
31
32

id_lock = threading.Lock()
Dmitry Volodin's avatar
Dmitry Volodin committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
TYPE_MAP = {"str": StringParameter(), "int": IntParameter(), "bool": BooleanParameter()}


@six.python_2_unicode_compatible
class ConfDBQueryParam(EmbeddedDocument):
    meta = {"strict": True}
    name = StringField()
    type = StringField(choices=["str", "int", "bool"])
    default = StringField()
    description = StringField()

    def __str__(self):
        return self.name

    def to_json(self):
        return {
            "name": self.name,
            "type": self.type,
            "description": self.description,
            "default": self.default,
        }

    def get_parameter(self):
        return TYPE_MAP[self.type]
Dmitry Volodin's avatar
Dmitry Volodin committed
57
58


59
60
@on_delete_check(
    check=[
Andrey Vertiprahov's avatar
Andrey Vertiprahov committed
61
        ("cm.ObjectValidationPolicy", "filter_query"),
Dmitry Volodin's avatar
Dmitry Volodin committed
62
63
        # ("cm.ObjectValidationPolicy", "rules.query"),
        # ("cm.ObjectValidationPolicy", "rules.filer_query"),
64
65
    ]
)
Dmitry Volodin's avatar
Dmitry Volodin committed
66
67
68
69
@six.python_2_unicode_compatible
class ConfDBQuery(Document):
    meta = {
        "collection": "confdbqueries",
Dmitry Volodin's avatar
Dmitry Volodin committed
70
        "strict": False,
Dmitry Volodin's avatar
Dmitry Volodin committed
71
72
73
74
75
76
77
78
79
        "auto_create_index": False,
        "json_collection": "cm.confdbqueries",
        "json_unique_fields": ["name"],
    }

    name = StringField(unique=True)
    uuid = UUIDField(binary=True)
    description = StringField()
    source = StringField()
Dmitry Volodin's avatar
Dmitry Volodin committed
80
    params = ListField(EmbeddedDocumentField(ConfDBQueryParam))
81
82
    allow_object_filter = BooleanField(default=False)
    allow_interface_filter = BooleanField(default=False)
Dmitry Volodin's avatar
Dmitry Volodin committed
83
84
85
86
    allow_object_validation = BooleanField(default=False)
    allow_interface_validation = BooleanField(default=False)
    allow_object_classification = BooleanField(default=False)
    allow_interface_classification = BooleanField(default=False)
87
    require_raw = BooleanField(default=False)
Dmitry Volodin's avatar
Dmitry Volodin committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

    _id_cache = cachetools.TTLCache(maxsize=100, ttl=60)

    def __str__(self):
        return self.name

    @classmethod
    @cachetools.cachedmethod(operator.attrgetter("_id_cache"), lock=lambda _: id_lock)
    def get_by_id(cls, id):
        return ConfDBQuery.objects.filter(id=id).first()

    def get_json_path(self):
        p = [quote_safe_path(n.strip()) for n in self.name.split("|")]
        return os.path.join(*p) + ".json"

    def query(self, engine, **kwargs):
        """
        Run query against ConfDB engine
        :param engine: ConfDB engine
        :param kwargs: Optional arguments
        :return:
        """
Dmitry Volodin's avatar
Dmitry Volodin committed
110
111
112
113
        params = kwargs.copy()
        for p in self.params:
            params[p.name] = p.get_parameter().clean(params.get(p.name, p.default))
        for ctx in engine.query(self.source, **params):
Dmitry Volodin's avatar
Dmitry Volodin committed
114
115
            yield ctx

116
117
118
119
120
121
122
123
124
    def any(self, engine, **kwargs):
        """
        Run query agains ConfDB engine and return True if any result found
        :param engine: ConfDB engine
        :param kwargs: Optional arguments
        :return: True if any result found
        """
        return engine.any(self.source, **kwargs)

Dmitry Volodin's avatar
Dmitry Volodin committed
125
126
127
128
129
130
    def to_json(self):
        r = {
            "name": self.name,
            "$collection": self._meta["json_collection"],
            "uuid": self.uuid,
            "source": self.source,
Dmitry Volodin's avatar
Dmitry Volodin committed
131
            "params": [p.to_json() for p in self.params],
132
133
            "allow_object_filter": self.allow_object_filter,
            "allow_interface_filter": self.allow_interface_filter,
Dmitry Volodin's avatar
Dmitry Volodin committed
134
135
136
137
            "allow_object_validation": self.allow_object_validation,
            "allow_interface_validation": self.allow_interface_validation,
            "allow_object_classification": self.allow_object_classification,
            "allow_interface_classification": self.allow_interface_classification,
138
            "require_raw": self.require_raw,
Dmitry Volodin's avatar
Dmitry Volodin committed
139
140
141
142
143
144
145
146
147
148
149
        }
        if self.description:
            r["description"] = self.description
        return to_json(
            r,
            order=[
                "name",
                "$collection",
                "uuid",
                "description",
                "source",
Dmitry Volodin's avatar
Dmitry Volodin committed
150
                "params",
151
152
                "allow_object_filter",
                "allow_interface_filter",
Dmitry Volodin's avatar
Dmitry Volodin committed
153
154
155
156
                "allow_object_validation",
                "allow_interface_validation",
                "allow_object_classification",
                "allow_interface_classification",
157
                "require_raw",
Dmitry Volodin's avatar
Dmitry Volodin committed
158
159
            ],
        )