Commit cb3ac1ca authored by Dmitry Volodin's avatar Dmitry Volodin
Browse files

Fix collections loading

--HG--
branch : feature/microservices
parent 76780bf2
......@@ -14,10 +14,8 @@ from mongoengine.fields import StringField, UUIDField
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
@collection
class ErrorType(Document):
meta = {
"collection": "noc.errortypes",
......
......@@ -22,10 +22,8 @@ from alarmclassjob import AlarmClassJob
from alarmplugin import AlarmPlugin
from noc.lib.escape import json_escape as q
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
@collection
class AlarmClass(nosql.Document):
"""
Alarm class
......
......@@ -15,10 +15,8 @@ from noc.main.models.style import Style
from noc.lib.nosql import ForeignKeyField
from noc.lib.text import quote_safe_path
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class AlarmSeverity(Document):
"""
Alarm severities
......
......@@ -14,10 +14,8 @@ from mongoengine import fields
## NOC modules
from noc.lib.text import quote_safe_path
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class CloneClassificationRule(Document):
"""
Classification rules cloning
......
......@@ -12,10 +12,8 @@ from mongoengine.fields import StringField, DictField, UUIDField
## Python modules
from noc.lib.text import quote_safe_path
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class Enumeration(Document):
meta = {
"collection": "noc.enumerations",
......
......@@ -17,7 +17,6 @@ from noc.lib import nosql
from alarmclass import AlarmClass
from noc.lib.escape import json_escape as q
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
class EventClassVar(EmbeddedDocument):
......@@ -192,7 +191,6 @@ class EventClassCategory(nosql.Document):
super(EventClassCategory, self).save(*args, **kwargs)
@collection
class EventClass(Document):
"""
Event class
......
......@@ -17,7 +17,6 @@ from datasource import DataSource
from noc.lib.nosql import PlainReferenceField
from noc.lib.escape import json_escape as jq
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
class EventClassificationRuleVar(EmbeddedDocument):
......@@ -73,7 +72,6 @@ class EventClassificationPattern(EmbeddedDocument):
return self.key_re == other.key_re and self.value_re == other.value_re
@collection
class EventClassificationRule(Document):
"""
Classification rules
......
......@@ -11,10 +11,8 @@ from mongoengine.document import Document
from mongoengine.fields import StringField, UUIDField
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class MIBAlias(Document):
"""
MIB Aliases
......
......@@ -11,10 +11,8 @@ from mongoengine.document import Document
from mongoengine.fields import StringField, UUIDField, IntField
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class MIBPreference(Document):
meta = {
"collection": "noc.mibpreferences",
......
......@@ -11,10 +11,8 @@ from mongoengine.document import Document
from mongoengine.fields import StringField, UUIDField
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class OIDAlias(Document):
meta = {
"collection": "noc.oidaliases",
......
......@@ -11,10 +11,8 @@ from mongoengine.document import Document
from mongoengine.fields import (StringField, UUIDField, DictField)
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class SyntaxAlias(Document):
meta = {
"collection": "noc.syntaxaliases",
......
......@@ -15,10 +15,8 @@ from mongoengine.fields import (StringField, UUIDField, IntField,
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
@collection
class Layer(Document):
meta = {
"collection": "noc.layers",
......
......@@ -15,10 +15,8 @@ from mongoengine.fields import (StringField, UUIDField, ObjectIdField)
from noc.main.models.doccategory import category
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
@collection
@category
class Capability(Document):
meta = {
......
......@@ -15,7 +15,6 @@ from mongoengine.fields import (StringField, UUIDField,
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
class Context(EmbeddedDocument):
......@@ -85,7 +84,6 @@ class Rule(EmbeddedDocument):
}
@collection
class ConnectionRule(Document):
"""
Equipment vendor
......
......@@ -17,10 +17,8 @@ from noc.lib.nosql import PlainReferenceField
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.main.models.doccategory import category
from noc.lib.collection import collection
@collection
@category
class ConnectionType(Document):
"""
......
......@@ -19,7 +19,6 @@ from noc.lib.escape import json_escape as q
from noc.sa.interfaces.base import (StringParameter, BooleanParameter,
FloatParameter, IntParameter,
StringListParameter)
from noc.lib.collection import collection
T_MAP = {
......@@ -83,7 +82,6 @@ class ModelInterfaceAttr(EmbeddedDocument):
return False
@collection
class ModelInterface(Document):
"""
Equipment vendor
......
......@@ -24,7 +24,6 @@ from noc.main.models.doccategory import category
from noc.lib.nosql import PlainReferenceField
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
class ObjectModelConnection(EmbeddedDocument):
......@@ -83,7 +82,6 @@ class ObjectModelConnection(EmbeddedDocument):
return r
@collection
@category
class ObjectModel(Document):
"""
......
......@@ -14,10 +14,8 @@ from mongoengine.fields import StringField, UUIDField
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.text import quote_safe_path
from noc.lib.collection import collection
@collection
class Technology(Document):
"""
Equipment vendor
......
......@@ -12,10 +12,8 @@ from mongoengine.fields import (StringField, BooleanField, URLField,
UUIDField)
## NOC modules
from noc.lib.prettyjson import to_json
from noc.lib.collection import collection
@collection
class Vendor(Document):
"""
Equipment vendor
......
......@@ -21,6 +21,7 @@ from mongoengine.fields import ListField, EmbeddedDocumentField
from noc.lib.fileutils import safe_rewrite
from noc.lib.serialize import json_decode
from noc.lib.log import PrefixLoggerAdapter
from noc.models import COLLECTIONS, get_model
logger = logging.getLogger(__name__)
......@@ -506,6 +507,13 @@ class Collection(object):
@classmethod
def setup(cls):
from noc.settings import config
cls.COLLECTIONS = {}
cls.COLLECTION_ORDER = []
for c in COLLECTIONS:
cc = get_model(c)
cn = cc._meta["json_collection"]
cls.COLLECTIONS[cn] = cc
cls.COLLECTION_ORDER += [cn]
for opt in config.options("i18n"):
if opt.startswith("collections."):
cn = opt[12:]
......@@ -527,51 +535,10 @@ class Collection(object):
"""
yield (collection name, collection class)
"""
if not cls.COLLECTION_ORDER:
# Order collections on json_depends_on meta property
pending = []
for c in cls.COLLECTIONS:
d = cls.COLLECTIONS[c]
depends_on = d._meta.get("json_depends_on", [])
if depends_on:
pending += [(c, depends_on)]
else:
cls.COLLECTION_ORDER += [c]
while pending:
new_pending = []
for c, d in pending:
if sum(1 for n in cls.COLLECTION_ORDER if n in d) == len(d):
# All requirements match
cls.COLLECTION_ORDER += [c]
else:
new_pending += [(c, d)]
if len(new_pending) == len(pending):
raise RuntimeError("Cannot resolve collection dependencies")
else:
pending = new_pending
for o in cls.COLLECTION_ORDER:
yield o
Collection.setup()
def collection(cls):
"""
Decorator to denote JSON-synchronizable collections
@collection
class MyDocument(Document):
meta = {
"json_collection": ...
}
"""
assert "json_collection" in cls._meta, "Class %s must have json_collection" % cls.__name__
cn = cls._meta["json_collection"]
logger.debug("Registering collection %s", cn)
Collection.COLLECTIONS[cn] = cls
return cls
##
from noc.main.models.collectioncache import CollectionCache
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