Verified Commit 3e3e3ffa authored by Dmitry Volodin's avatar Dmitry Volodin
Browse files

Object Validation Policy: Filter queries

parent 8a44672a
......@@ -25,7 +25,13 @@ from noc.core.model.decorator import on_delete_check
id_lock = threading.Lock()
@on_delete_check(check=[("cm.ObjectValidationPolicy", "rules.query")])
@on_delete_check(
check=[
("cm.ObjectValidationPolicy", "filer_query"),
("cm.ObjectValidationPolicy", "rules.query"),
("cm.ObjectValidationPolicy", "rules.filer_query"),
]
)
@six.python_2_unicode_compatible
class ConfDBQuery(Document):
meta = {
......@@ -40,6 +46,8 @@ class ConfDBQuery(Document):
uuid = UUIDField(binary=True)
description = StringField()
source = StringField()
allow_object_filter = BooleanField(default=False)
allow_interface_filter = BooleanField(default=False)
allow_object_validation = BooleanField(default=False)
allow_interface_validation = BooleanField(default=False)
allow_object_classification = BooleanField(default=False)
......@@ -69,12 +77,23 @@ class ConfDBQuery(Document):
for ctx in engine.query(self.source, **kwargs):
yield ctx
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)
def to_json(self):
r = {
"name": self.name,
"$collection": self._meta["json_collection"],
"uuid": self.uuid,
"source": self.source,
"allow_object_filter": self.allow_object_filter,
"allow_interface_filter": self.allow_interface_filter,
"allow_object_validation": self.allow_object_validation,
"allow_interface_validation": self.allow_interface_validation,
"allow_object_classification": self.allow_object_classification,
......@@ -90,6 +109,8 @@ class ConfDBQuery(Document):
"uuid",
"description",
"source",
"allow_object_filter",
"allow_interface_filter",
"allow_object_validation",
"allow_interface_validation",
"allow_object_classification",
......
......@@ -30,6 +30,7 @@ id_lock = threading.Lock()
@six.python_2_unicode_compatible
class ObjectValidationRule(EmbeddedDocument):
query = PlainReferenceField(ConfDBQuery)
filter_query = PlainReferenceField(ConfDBQuery)
is_active = BooleanField(default=True)
error_code = StringField()
error_text_template = StringField(default="{{error}}")
......@@ -47,6 +48,7 @@ class ObjectValidationPolicy(Document):
name = StringField(unique=True)
description = StringField()
filter_query = PlainReferenceField(ConfDBQuery)
rules = ListField(EmbeddedDocumentField(ObjectValidationRule))
_id_cache = cachetools.TTLCache(maxsize=100, ttl=60)
......@@ -66,9 +68,17 @@ class ObjectValidationPolicy(Document):
:param engine: ConfDB Engine instance
:return: List of problems
"""
# Check filter query, if any
if self.filter_query:
if not self.filter_query.any(engine):
raise StopIteration
# Process rules
for rule in self.rules:
if not rule.is_active:
continue
if rule.filter_query:
if not rule.filter_query.any(engine):
continue
for ctx in rule.query.query(engine):
if "error" in ctx:
tpl = Template(rule.error_text_template)
......
......@@ -17,6 +17,6 @@ class ObjectValidationPolicyApplication(ExtDocApplication):
ObjectValidationPolicy application
"""
title = "ObjectValidationPolicy"
title = "Object Validation Policy"
menu = [_("Setup"), _("Object Validation Policies")]
model = ObjectValidationPolicy
......@@ -70,11 +70,21 @@ Ext.define("NOC.cm.confdbquery.Application", {
padding: 4
},
items: [
{
name: "allow_object_filter",
xtype: "checkbox",
boxLabel: __("Object Filter")
},
{
name: "allow_object_validation",
xtype: "checkbox",
boxLabel: __("Object Validation")
},
{
name: "allow_interface_filter",
xtype: "checkbox",
boxLabel: __("Interface Filter")
},
{
name: "allow_interface_validation",
xtype: "checkbox",
......
......@@ -43,6 +43,14 @@ Ext.define("NOC.cm.confdbquery.Model", {
name: "allow_object_validation",
type: "boolean"
},
{
name: "allow_object_filter",
type: "boolean"
},
{
name: "allow_interface_filter",
type: "boolean"
},
{
name: "uuid",
type: "string"
......
......@@ -41,6 +41,15 @@ Ext.define("NOC.cm.objectvalidationpolicy.Application", {
fieldLabel: __("Description"),
allowBlank: true
},
{
name: "filter_query",
xtype: "cm.confdbquery.LookupField",
fieldLabel: __("Filter Query"),
query: {
allow_object_filter: true
},
allowBlank: true
},
{
name: "rules",
xtype: "gridfield",
......@@ -59,6 +68,19 @@ Ext.define("NOC.cm.objectvalidationpolicy.Application", {
},
renderer: NOC.render.Lookup("query")
},
{
text: __("Filter Query"),
dataIndex: "filter_query",
width: 200,
editor: {
xtype: "cm.confdbquery.LookupField",
allowBlank: false,
query: {
allow_object_filter: true
}
},
renderer: NOC.render.Lookup("filter_query")
},
{
text: __("Active"),
dataIndex: "is_active",
......
......@@ -26,6 +26,15 @@ Ext.define("NOC.cm.objectvalidationpolicy.Model", {
{
name: "description",
type: "string"
},
{
name: "filter_query",
type: "string"
},
{
name: "filter_query__label",
type: "string",
persist: false
}
]
});
\ No newline at end of file
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