Commit 621dc995 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

QKD App:

- Fixed error codes
parent 2ef6f8eb
Loading
Loading
Loading
Loading
+19 −9
Original line number Diff line number Diff line
@@ -12,16 +12,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import uuid
import json
import json, logging
from flask import request
from flask.json import jsonify
from flask_restful import Resource
from common.proto.context_pb2 import Empty
from common.proto.qkd_app_pb2 import App, QKDAppTypesEnum
from common.Constants import DEFAULT_CONTEXT_NAME
from context.client.ContextClient import ContextClient
from nbi.service._tools.HttpStatusCodes import HTTP_OK, HTTP_SERVERERROR
from qkd_app.client.QKDAppClient import QKDAppClient

LOGGER = logging.getLogger(__name__)

class _Resource(Resource):
    def __init__(self) -> None:
        super().__init__()
@@ -30,7 +33,7 @@ class _Resource(Resource):

class Index(_Resource):
    def get(self):
        return {'hello': 'world'}
        return {}

class ListDevices(_Resource):
    def get(self):
@@ -81,18 +84,24 @@ class CreateQKDApp(_Resource):
        devices = self.context_client.ListDevices(Empty()).devices
        local_device = None

        local_qkdn_id = app['local_qkdn_id']

        # This for-loop won't be necessary if Device ID is guaranteed to be the same as QKDN Id
        for device in devices:
            for config_rule in device.device_config.config_rules:
                if config_rule.custom.resource_key == '__node__':
                    value = json.loads(config_rule.custom.resource_value)
                    qkdn_id = value['qkdn_id']
                    if app['local_qkdn_id'] == qkdn_id:
                    if local_qkdn_id != value['qkdn_id']: continue
                    local_device = device
                    break

        if local_device is None:
            return {"status": "fail"}
            MSG = 'Unable to find local_device for local_qkdn_id({:s})'
            msg = MSG.format(str(local_qkdn_id))
            LOGGER.exception(msg)
            response = jsonify({'error': msg})
            response.status_code = HTTP_SERVERERROR
            return response

        external_app_src_dst = {
            'app_id': {'context_id': {'context_uuid': {'uuid': DEFAULT_CONTEXT_NAME}}, 'app_uuid': {'uuid': ''}},
@@ -107,5 +116,6 @@ class CreateQKDApp(_Resource):

        self.qkd_app_client.RegisterApp(App(**external_app_src_dst))

        return {"status": "success"}
        response = jsonify({'status': 'success'})
        response.status_code = HTTP_OK
        return response