Exemple #1
0
    def get(self, lean_obj):
        """Retrieve a model instance from the database. This function uses
           lean_obj to deduce ID and model type

           >>> nb_api.get(Chassis(id="one"))
           Chassis(id="One", ip="192.168.121.22", tunnel_types=["vxlan"])

        """
        if mproxy.is_model_proxy(lean_obj):
            lean_obj = lean_obj.get_proxied_model()(id=lean_obj.id)
        model = type(lean_obj)
        try:
            serialized_obj = self.driver.get_key(
                model.table_name,
                lean_obj.id,
                _get_topic(lean_obj),
            )
        except df_exceptions.DBKeyNotFound:
            exception_tb = traceback.format_exc()
            LOG.debug('Could not get object %(id)s from table %(table)s', {
                'id': lean_obj.id,
                'table': model.table_name
            })
            LOG.debug('%s', (exception_tb, ))
        else:
            return model.from_json(serialized_obj)
    def iter_model_references_deep(self, obj):
        """
        Return an iterator on all model instances referenced by the given model
        instance, including indirect references. e.g. if an lport references
        a network, and that network references a QoS policy, then both are
        returned in the iterator.

        Raises a ReferencedObjectNotFound exception upon a referenced model
        cannot be found in the db_store or NB DB. The exception is thrown
        when that object is reached by the iterator, allowing other objects
        to be processed. If you need to verify all objects can be referenced,
        use 'get_model_references_deep'.

        :param obj: Model instance
        :type obj:  model_framework.ModelBase
        :return:    iterator
        :raises:    exceptions.ReferencedObjectNotFound
        """
        seen = set()
        queue = collections.deque((obj, ))
        while queue:
            item = queue.pop()
            if item.id in seen:
                continue
            seen.add(item.id)
            if model_proxy.is_model_proxy(item):
                # NOTE(oanson) _dereference raises an exception for unknown
                # objectes, i.e. objects not found in the NB DB.
                item = self._dereference(item)
                yield item
            for submodel in item.iter_submodels():
                queue.append(submodel)
Exemple #3
0
 def _is_physical_chassis(self, chassis):
     if not chassis:
         return False
     if chassis.id == constants.DRAGONFLOW_VIRTUAL_PORT:
         return False
     if model_proxy.is_model_proxy(chassis) and not chassis.get_object():
         return False
     return True
Exemple #4
0
 def _get(obj):
     if model_proxy.is_model_proxy(obj):
         model = obj.get_proxied_model()
     else:
         model = type(obj)
     objs = _get_all(model)
     for o in objs:
         if obj.id == o.id:
             return o
Exemple #5
0
 def _add_edge_message(self, edges, instance, field):
     if model_proxy.is_model_proxy(field):
         field = self.nb_api.get(field)
     if not hasattr(field, 'id'):
         return
     result = self._build_edge_message(
         type(instance).__name__, instance.id,
         type(field).__name__, field.id)
     edges.append(result)
Exemple #6
0
 def test_is_model_proxy(self):
     model_instance = ModelTest(id='id1', topic='topic1')
     self.assertFalse(model_proxy.is_model_proxy(model_instance))
     model_ref = model_proxy.create_reference(ModelTest, id='id1')
     self.assertTrue(model_proxy.is_model_proxy(model_ref))
 def _get_model(self, obj):
     if model_proxy.is_model_proxy(obj):
         return obj.get_proxied_model()
     return type(obj)
 def _is_physical_chassis(self, chassis):
     if not chassis:
         return False
     if model_proxy.is_model_proxy(chassis) and not chassis.get_object():
         return False
     return True