Пример #1
0
    def post(self):

        try:
            request_body = request.get_json()
            snapshot_name = request_body['snapshot_name']
            id = request_body['id']

            if not self.snapshot_validate(snapshot_name):
                return JsonRes(
                    info={},
                    usr_err_mes=
                    "No Snapshot named %s is found! Please import snapshot first!"
                    % snapshot_name,
                    err_code=0,
                    status=False,
                    data=[])

            filter_dict = {"snapshot_name": snapshot_name, "id": id}
            if check_duplicated(current_app.mongo.db.policy, **filter_dict):
                return JsonRes(
                    info={},
                    usr_err_mes=
                    "Duplicated Policy name in same Snapshot, please change another one!",
                    status=False,
                    data=[],
                    code=400)

            result = current_app.mongo.db.policy.insert_one(request_body)

            if not result:
                return JsonRes(info={},
                               usr_err_mes="MongoDB Insert Exception!",
                               status=False,
                               data=[],
                               code=400)
            else:
                return JsonRes(data={}, info={}, code=201)
        except Exception as e:
            LOG.debug(e)
            return JsonRes(info=e,
                           usr_err_mes="MongoDB Insert Exception!",
                           status=False,
                           data=[],
                           code=400)
Пример #2
0
    def post(self):
        try:
            args = self.parser.parse(pasta_reopt, request)
            if "congested_link" in args.keys():
                result = reopt_congest_dispatch(args)
            else:
                result = reopt_global(args)
            LOG.debug(result)

            if result.get("info"):
                LOG.debug(result.get("info"))
                LOG.debug(type(result.get("info")))
                return JsonRes(info=result.get("info"), usr_err_mes="Reoptimize Exception!",
                               status=False, data=[], code=400)
            else:
                return JsonRes(data=result.get("data"), info=result.get("info"), code=200)
        except Exception as e:
            LOG.debug(e)
            return JsonRes(info=e, usr_err_mes="Reoptimize Exception!", status=False, data=[], code=400)
Пример #3
0
 def get(self):
     try:
         args = self.parser.parse(self.get_params_check, request)
         async_func = getattr(core, constants.TASK_TYPE_DICT[args['type']])
         data = check_async_task_status(async_func, args['task_id'])
         return JsonRes(data=data, info=[], code=200)
     except Exception as e:
         LOG.error("Params parser failed!")
         try:
             usr_err_mes = '\n'.join(
                 [i for item in e.messages.values() for i in item])
         except Exception as f:
             LOG.error(str(f))
             usr_err_mes = constants.DEFAULT_USER_ERR_MES
         return JsonRes(info=e,
                        data={},
                        usr_err_mes=usr_err_mes,
                        status=False,
                        code=400)
Пример #4
0
 def wrapper(*args, **kwargs):
     try:
         data = func(*args, **kwargs)
         return JsonRes(data=data, info={}, status=True, code=201)
     except ValidationError as err:
         info = {
             'exception': 'ValidationError',
             'message': json.dumps(err.messages)
         }
         return JsonRes(data={},
                        info=info,
                        status=False,
                        usr_err_mes='Invalid Input',
                        err_code=0,
                        code=400)
     except Exception as err:
         return JsonRes(data={},
                        info=err,
                        status=False,
                        usr_err_mes='Pasta Error',
                        err_code=0,
                        code=400)
Пример #5
0
    def delete(self):

        try:
            args = self.parser.parse(self.del_params_check, request)
            snapshot_name = args['snapshot_name']

            if not self.snapshot_validate(snapshot_name):
                return JsonRes(
                    info={},
                    usr_err_mes=
                    "No Snapshot named %s is found! Please import snapshot first!"
                    % snapshot_name,
                    err_code=0,
                    status=False,
                    data=[])

            # snapshot_res = current_app.mongo.db.snapshot.remove({
            current_app.mongo.db.snapshot.remove(
                {'snapshot_name': snapshot_name})
            # topo_res = current_app.mongo.db.topo.remove({
            current_app.mongo.db.topo.remove({'snapshot_name': snapshot_name})
            # policy_res = current_app.mongo.db.policy.remove({
            current_app.mongo.db.policy.remove(
                {"snapshot_name": snapshot_name})
            # params_res = current_app.mongo.db.params.remove({
            current_app.mongo.db.params.remove(
                {'snapshot_name': snapshot_name})

            return JsonRes(data={}, info={}, code=201)

        except Exception as e:
            LOG.debug(e)
            return JsonRes(info=e,
                           usr_err_mes="MongoDB Operation Exception!",
                           status=False,
                           data=[],
                           code=400)
Пример #6
0
    def delete(self):

        try:
            args = self.parser.parse(self.get_params_check, request)
            snapshot_name = args['snapshot_name']
            id = args['id']

            if not self.snapshot_validate(snapshot_name):
                return JsonRes(
                    info={},
                    usr_err_mes=
                    "No Snapshot named %s is found! Please import snapshot first!"
                    % snapshot_name,
                    err_code=0,
                    status=False,
                    data=[])

            filter_dict = {"snapshot_name": snapshot_name, "id": id}
            result = current_app.mongo.db.policy.remove(filter_dict)

            if result['n'] == 0:
                return JsonRes(
                    info={},
                    usr_err_mes="In Snapshot %s, No Policy named %s is found!"
                    % (snapshot_name, id),
                    err_code=0,
                    status=False,
                    data=[])
            else:
                return JsonRes(data={}, info={}, code=201)
        except Exception as e:
            LOG.debug(e)
            return JsonRes(info=e,
                           usr_err_mes="MongoDB Delete Exception!",
                           status=False,
                           data=[],
                           code=400)
Пример #7
0
    def get(self):
        try:
            args = self.parser.parse(self.get_params_check, request)
            snapshot_name = args['snapshot_name']

            result = current_app.mongo.db.topo.find_one(
                {'snapshot_name': snapshot_name})

            if not result:
                return JsonRes(info={},
                               usr_err_mes="No Snapshot named %s is found!" %
                               snapshot_name,
                               err_code=0,
                               status=False,
                               data=[])
            else:
                return JsonRes(data=result, info={}, code=200)
        except Exception as e:
            LOG.debug(e)
            return JsonRes(info=e,
                           usr_err_mes="data error!",
                           status=False,
                           data=[],
                           code=400)
Пример #8
0
    def delete(self):
        try:
            args = self.parser.parse(self.get_params_check, request)
            snapshot_name = args['snapshot_name']

            if not self.snapshot_validate(snapshot_name):
                return JsonRes(
                    info={},
                    usr_err_mes=
                    "No Snapshot named %s is found! Please import snapshot first!"
                    % snapshot_name,
                    err_code=0,
                    status=False,
                    data=[])

            result = current_app.mongo.db.params.remove(
                {'snapshot_name': snapshot_name})

            if result['n'] == 0:
                return JsonRes(
                    info={},
                    usr_err_mes=
                    "In Snapshot %s, GlobalParams is Empty, Nothing to Delete!"
                    % snapshot_name,
                    err_code=0,
                    status=False,
                    data=[])
            else:
                return JsonRes(data={}, info={}, code=201)
        except Exception as e:
            LOG.debug(e)
            return JsonRes(info=e,
                           usr_err_mes="MongoDB Delete Exception!",
                           status=False,
                           data=[],
                           code=400)
Пример #9
0
    def delete(self):
        try:
            args = self.parser.parse(self.get_params_check, request)
            snapshot_name = args['snapshot_name']

            result = current_app.mongo.db.topo.remove(
                {'snapshot_name': snapshot_name})

            if result['n'] == 0:
                return JsonRes(info={},
                               usr_err_mes="No Topology named %s is found!" %
                               snapshot_name,
                               err_code=0,
                               status=False,
                               data=[])
            else:
                return JsonRes(data={}, info={}, code=201)
        except Exception as e:
            LOG.debug(e)
            return JsonRes(info=e,
                           usr_err_mes="MongoDB Delete Exception!",
                           status=False,
                           data=[],
                           code=400)
Пример #10
0
    def post(self):

        try:
            args = self.parser.parse(self.params_check, request)
            request_body = request.get_json()

            snapshot_name = args['snapshot_name']
            operation = args['operation']

            if operation == "import":
                filter_dict = {"snapshot_name": snapshot_name}
                if check_duplicated(current_app.mongo.db.snapshot,
                                    **filter_dict):
                    return JsonRes(
                        info={},
                        usr_err_mes=
                        "Duplicated Snapshot name, please change another name!",
                        status=False,
                        data=[],
                        code=400)

                # create snapshot instance
                snapshot_instance = {'snapshot_name': snapshot_name}
                # snapshot_res = current_app.mongo.db.snapshot.insert_one(snapshot_instance)
                current_app.mongo.db.snapshot.insert_one(snapshot_instance)

                # create topo instances
                topo_instance = {'snapshot_name': snapshot_name}
                if 'nodes' in request_body.keys():
                    topo_instance['nodes'] = request_body['nodes']
                    # topo_instance.update({
                    #     'nodes': request_body['nodes']
                    # })
                if 'links' in request_body.keys():
                    topo_instance['links'] = request_body['links']
                    # topo_instance.update({
                    #     'links': request_body['links']
                    # })
                # topo_res = current_app.mongo.db.topo.insert_one(topo_instance)
                current_app.mongo.db.topo.insert_one(topo_instance)

                # create policy instances
                if 'sr_policy' in request_body.keys():
                    sr_policies = request_body['sr_policy']
                    req_list = []
                    for policy in sr_policies:
                        policy_instance = {'snapshot_name': snapshot_name}
                        policy_instance.update(policy)
                        req_list.append(InsertOne(policy_instance))
                    # policies_res = current_app.mongo.db.policy.bulk_write(req_list)
                    current_app.mongo.db.policy.bulk_write(req_list)
                #
                # create global_params instances
                if 'global_params' in request_body.keys():
                    params_instance = {'snapshot_name': snapshot_name}
                    params_instance.update(request_body['global_params'])
                    # params_res = current_app.mongo.db.params.insert_one(params_instance)
                    current_app.mongo.db.params.insert_one(params_instance)

                return JsonRes(data={}, info={}, code=201)

            if operation == "export":

                if not self.snapshot_validate(snapshot_name):
                    return JsonRes(
                        info={},
                        usr_err_mes=
                        "No Snapshot named %s is found! Please import snapshot first!"
                        % snapshot_name,
                        err_code=0,
                        status=False,
                        data=[])

                topo_res = current_app.mongo.db.topo.find_one(
                    {'snapshot_name': snapshot_name})
                del topo_res['snapshot_name']

                policy_res = current_app.mongo.db.policy.find(
                    {'snapshot_name': snapshot_name})
                policy_list = []
                for policy in policy_res:
                    del policy['snapshot_name']
                    del policy['_id']
                    policy_list.append(policy)

                res = {
                    'nodes': topo_res['nodes'],
                    'links': topo_res['links'],
                    'sr_policy': policy_list
                }

                if 'with_params' in args.keys() and args['with_params']:
                    params_res = current_app.mongo.db.params.find_one(
                        {'snapshot_name': snapshot_name})
                    del params_res['snapshot_name']
                    del params_res['_id']

                    res.update({'global_params': params_res})

                return JsonRes(data=res, info={}, code=200)

        except Exception as e:
            LOG.debug(e)
            return JsonRes(info=e,
                           usr_err_mes="MongoDB Update Exception!",
                           status=False,
                           data=[],
                           code=400)
Пример #11
0
 def get(self):
     data = {"version": "v1", "updated": datetime.datetime.now()}
     return JsonRes(success=True, data=data)