text: # -*- coding: utf-8 -*-
  # ----------------------------------------------------------------------
  # AllocationGroup
  # ----------------------------------------------------------------------
  # Copyright (C) 2007-2019 The NOC Project
  # See LICENSE for details
  # ----------------------------------------------------------------------
  
  # Python modules
  from __future__ import absolute_import
  from threading import Lock
  import operator
  
  # Third-party modules
  import six
  from mongoengine.document import Document
  from mongoengine.fields import StringField, LongField, ListField
  import cachetools
  
  # NOC modules
  from noc.main.models.remotesystem import RemoteSystem
  from noc.core.mongo.fields import PlainReferenceField
  from noc.core.bi.decorator import bi_sync
  from noc.core.model.decorator import on_delete_check
  
  id_lock = Lock()
  
  
  @bi_sync
  @on_delete_check(check=[("inv.NetworkSegment", "allocation_group")])
  @six.python_2_unicode_compatible
  class AllocationGroup(Document):
      meta = {"collection": "allocationgroups", "strict": False, "auto_create_index": False}
  
      name = StringField(unique=True)
      description = StringField()
      tags = ListField(StringField())
      # Integration with external NRI and TT systems
      # Reference to remote system object has been imported from
      remote_system = PlainReferenceField(RemoteSystem)
      # Object id in remote system
      remote_id = StringField()
      # Object id in BI
      bi_id = LongField(unique=True)
  
      _id_cache = cachetools.TTLCache(maxsize=100, ttl=60)
      _bi_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 AllocationGroup.objects.filter(id=id).first()
  
      @classmethod
      @cachetools.cachedmethod(operator.attrgetter("_bi_id_cache"), lock=lambda _: id_lock)
      def get_by_bi_id(cls, id):
          return AllocationGroup.objects.filter(bi_id=id).first()
  