Пример #1
0
def add_object_from_json(json_str, table):
    """add a new object that described by json
     string to dragonflow db.

    :param json_str: json string that describes the object to be added
    :param table: table name where object should be added
    :return: None
    """
    nb_api = api_nb.NbApi.get_instance(False, True)
    try:
        model = model_framework.get_model(table)
    except KeyError:
        print("Model {} is not found in models list".format(table))
        return

    try:
        obj = model.from_json(json_str)
    except ValueError:
        print("Json {} is not valid".format(json_str))
        return
    except TypeError:
        print("Json {} is not applicable to {}".format(json_str, table))
        return

    try:
        nb_api.create(obj)
    except errors.ValidationError:
        print("Json {} is not applicable to {}".format(json_str, table))
Пример #2
0
 def _handle_db_change(self, update):
     action = update.action
     if action == ctrl_const.CONTROLLER_REINITIALIZE:
         self.db_store.clear()
         self.vswitch_api.initialize(self.nb_api)
         self.sync()
     elif action == ctrl_const.CONTROLLER_SYNC:
         self.sync()
     elif action == ctrl_const.CONTROLLER_DBRESTART:
         self.nb_api.db_recover_callback()
     elif action == ctrl_const.CONTROLLER_OVS_SYNC_FINISHED:
         self.ovs_sync_finished()
     elif action == ctrl_const.CONTROLLER_OVS_SYNC_STARTED:
         self.ovs_sync_started()
     elif action == ctrl_const.CONTROLLER_LOG:
         LOG.info('Log event: %s', str(update))
     elif update.table is not None:
         try:
             model_class = model_framework.get_model(update.table)
         except KeyError:
             # Model class not found, possibly update was not about a model
             LOG.warning('Unknown table %s', update.table)
         else:
             if action == 'delete':
                 self.delete_by_id(model_class, update.key)
             else:
                 obj = model_class.from_json(update.value)
                 self.update(obj)
     else:
         LOG.warning('Unfamiliar update: %s', str(update))
Пример #3
0
 def _output_table_node(self, nodes, edges, instance):
     metadata = {
         'ID': "DF-{}".format(instance.id),
         'Type': WSClientDragonflowProtocol._get_instance_type(instance),
         'source': 'dragonflow',
         'data': instance.to_struct(),
         'Name': getattr(instance, 'name', None) or instance.id
     }
     result = {
         'Metadata': metadata,
         'ID': "DF-{}".format(instance.id),
         'Host': 'dragonflow'
     }
     nodes.append(result)
     self._output_table_node_edges(edges, instance)
     # If we have an owner, add the edge from it to this instance
     if WSClientDragonflowProtocol._has_owner(instance):
         return
         # TODO(snapiri) Fix this code as it is not working correctly
         owner_class = mf.get_model(instance.device_owner)
         if not owner_class:
             return
         edge = self._build_edge_message(owner_class.__name__,
                                         instance.device_id,
                                         type(instance).__name__,
                                         instance.id)
         edges.append(edge)
Пример #4
0
 def wrapper(*args, **kwargs):
     name = kwargs['name']
     try:
         model = mf.get_model(name)
     except KeyError:
         bottle.abort(HTTPStatus.NOT_FOUND.value,
                      "Model '%s' not found" % (name, ))
     return f(model, *args, **kwargs)
Пример #5
0
def _get_model_or_exit(table):
    """
    Return the model by table name. If no such model is found, raise
    SystemExit, which (in general should) exit the process.
    """
    try:
        return model_framework.get_model(table)
    except KeyError:
        print('Table not found: ' + table)
        raise SystemExit(1)
Пример #6
0
    def apply_db_change(self, table, key, action, value):
        # determine if the action is allowed or not
        if action not in DB_ACTION_LIST:
            LOG.warning('Unknown action %(action)s for table '
                        '%(table)s', {
                            'action': action,
                            'table': table
                        })
            return

        if action == 'sync':
            self.controller.run_sync(value)
        elif action == 'dbrestart':
            self.db_recover_callback()
        elif action == 'db_sync':
            self.db_consistency_manager.process(direct=False)
        elif action == 'ovs_sync_finished':
            self.controller.ovs_sync_finished()
        elif action == 'ovs_sync_started':
            self.controller.ovs_sync_started()
        elif 'log' == action:
            message = ('Log event (Info): table: %(table)s key: %(key)s '
                       'action: %(action)s value: %(value)s')

            LOG.info(
                message, {
                    'table': str(table),
                    'key': str(key),
                    'action': str(action),
                    'value': str(value),
                })
        elif table == 'lport_migration' and action == 'migrate':
            lport = db_models.LogicalPort(value)
            self.controller.update_migration_flows(lport)
        elif table is not None:
            try:
                model_class = mf.get_model(table)
            except KeyError:
                # Model class not found, possibly update was not about a model
                # Added lport migration for VM migration flag
                LOG.warning('Unknown table %s', table)
            else:
                if action == 'delete':
                    self.controller.delete_by_id(model_class, key)
                else:
                    obj = model_class.from_json(value)
                    self.controller.update(obj)
Пример #7
0
def model_object_from_json(json_str, model):
    """Constructs a model object that described by json
     string to dragonflow db.

    :param json_str: json string that describes the object to be updated
    :param model: The object model name (table name) to be constructed
    :raises ValueError: exception raised from model.from_json
    :raises TypeError: exception raised from model.from_json
    :return: None
    """
    try:
        model = model_framework.get_model(model)
    except KeyError:
        print("Model {} is not found in models list".format(model))
        return

    obj = model.from_json(json_str)
    return obj
Пример #8
0
 def _handle_db_change(self, update):
     action = update.action
     if action == ctrl_const.CONTROLLER_REINITIALIZE:
         #重新初始化controller
         self.db_store.clear()
         self.switch_backend.initialize(self.db_change_callback,
                                        self.neutron_notifier)
         self.sync()
     elif action == ctrl_const.CONTROLLER_SYNC:
         #执行同步
         self.sync()
     elif action == ctrl_const.CONTROLLER_DBRESTART:
         #db重启事件
         self.nb_api.db_recover_callback()
     elif action == ctrl_const.CONTROLLER_SWITCH_SYNC_FINISHED:
         #switch同步结束
         self.switch_sync_finished()
     elif action == ctrl_const.CONTROLLER_SWITCH_SYNC_STARTED:
         #switch同步开始
         self.switch_sync_started()
     elif action == ctrl_const.CONTROLLER_LOG:
         #显示contrller log
         LOG.info('Log event: %s', str(update))
     elif update.table is not None:
         #收到module 更新event
         try:
             model_class = model_framework.get_model(update.table)
         except KeyError:
             # Model class not found, possibly update was not about a model
             LOG.warning('Unknown table %s', update.table)
         else:
             if action == 'delete':
                 #执行obj删除
                 self.delete_by_id(model_class, update.key)
             else:
                 #执行obj更新
                 obj = model_class.from_json(update.value)
                 self._send_updates_for_object(obj)
     else:
         LOG.warning('Unfamiliar update: %s', str(update))
Пример #9
0
 def types(self):
     if self._types is None:
         self._types = (model_proxy.create_model_proxy(
             model_framework.get_model(self._model), ), )
     return self._types
Пример #10
0
 def test_lookup(self):
     self.assertEqual(ModelTest, mf.get_model('ModelTest'))
     self.assertEqual(ModelTest, mf.get_model('table1'))
     self.assertEqual(ModelTest, mf.get_model(ModelTest))
Пример #11
0
 def _output_table(self, nodes, edges, table_name):
     model = mf.get_model(table_name)
     instances = self.nb_api.get_all(model)
     for instance in instances:
         self._output_table_node(nodes, edges, instance)