diff --git a/ChangeLog b/ChangeLog index c562012f..cae56db8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,7 +3,8 @@ CHANGES 1.5.0 -FEATURE: Support keystone groups in a Service, including role assignments +FEATURE: Metrics API [#58] +FEATURE: Support keystone groups in a Service, including role assignments [#56] 1.4.2 diff --git a/README.md b/README.md index 3d9b15db..4c849084 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ A tipical scenario for IoT Platform can be [scenario_test](https://pdihub.hi.ine - Assign/unassign roles to users in a service - Create/List Trust Tokens - Activate / deactivate IoT Modules -- Retrieve statistics about API usage +- Retrieve statistics and metrics about API usage Orchestrator is based mainly on: - Python diff --git a/apiary.apib b/apiary.apib index 9d6a4ac7..ff451e1b 100644 --- a/apiary.apib +++ b/apiary.apib @@ -977,3 +977,83 @@ In the cases where "serviceId" is part of URL "SERVICE_NAME" field at body can b { "level": "DEBUG" } + +## Metrics [/v1.0/admin/metrics] + +The following metrics are collected by the component: ++ incomingTransactions: number of requests consumed by the component. ++ incomingTransactionRequestSize: total size (bytes) in requests associated to incoming transactions ++ incomingTransactionResponseSize: total size (bytes) in responses associated to incoming transactions ++ incomingTransacionError: number of incoming transactions resulting in error. ++ serviceTime: average time to serve a transaction. + +### Get current metrics [GET] + ++ Parameters + + reset (optional, `string`) ... ["true", "false"] ++ Response 200 (application/json) + + Body + { + "service": { + "service1": { + "subservs": { + "subserv1": {}, + "subserv2": {} + }, + "sum": {} + }, + "service2": { + "subservs": { + "subserv1": {}, + "subserv2": {} + }, + "sum": {} + } + }, + "sum": { + "incomingTransactions": 25, + "incomingTransactionRequestSize": 3456, + "incomingTransactionResponseSize": 435, + "incomingTransacionError": 2, + "serviceTime": 0.48, + "outgoingTransactions": 46, + "outgoingTransactionRequestSize": 323361, + "outgoingTransactionResponseSize": 2343, + "outgoingTransacionError": 10 + } + } + + +### Reset current metrics [DELETE] ++ Parameters ++ Response 204 (application/json) + + Body + { + "service": { + "service1": { + "subservs": { + "subserv1": {}, + "subserv2": {} + }, + "sum": {} + }, + "service2": { + "subservs": { + "subserv1": {}, + "subserv2": {} + }, + "sum": {} + } + }, + "sum": { + "incomingTransactions": 25, + "incomingTransactionRequestSize": 3456, + "incomingTransactionResponseSize": 435, + "incomingTransacionError": 2, + "serviceTime": 0.48, + "outgoingTransactions": 46, + "outgoingTransactionRequestSize": 323361, + "outgoingTransactionResponseSize": 2343, + "outgoingTransacionError": 10 + } + } diff --git a/src/orchestrator/api/urls.py b/src/orchestrator/api/urls.py index d8e61737..00443fec 100644 --- a/src/orchestrator/api/urls.py +++ b/src/orchestrator/api/urls.py @@ -42,7 +42,8 @@ SubServiceIoTAService_RESTView, IOTModuleActivation_RESTView, OrchVersion_RESTView, - OrchLogLevel_RESTView + OrchLogLevel_RESTView, + OrchMetrics_RESTView ) @@ -69,4 +70,5 @@ url(r'^service/(?P\w+)/trust[/]?$', Trust_RESTView.as_view(), name='new_trust_rest_view'), url(r'^version[/]?$', OrchVersion_RESTView.as_view(), name='orch_version_rest_view'), url(r'^admin/log?$', OrchLogLevel_RESTView.as_view(), name='orch_loglevel_rest_view'), + url(r'^admin/metrics?$', OrchMetrics_RESTView.as_view(), name='orch_metrics_rest_view'), ) diff --git a/src/orchestrator/api/views.py b/src/orchestrator/api/views.py index 1e63e498..7e176a29 100644 --- a/src/orchestrator/api/views.py +++ b/src/orchestrator/api/views.py @@ -23,6 +23,7 @@ # import logging import json +import time from rest_framework.views import APIView from rest_framework.response import Response @@ -85,6 +86,14 @@ class Stats(object): num_get_userlist = 0 num_post_userlist = 0 + num_delete_group = 0 + num_put_group = 0 + num_get_group = 0 + num_post_group = 0 + + num_get_grouplist = 0 + num_post_grouplist = 0 + num_delete_role = 0 num_post_role = 0 num_get_role = 0 @@ -115,6 +124,171 @@ class Stats(object): num_api_errors = 0 num_flow_errors = 0 + service = {} + sum = { + "incomingTransactions": 0, + "incomingTransactionRequestSize": 0, + "incomingTransactionResponseSize": 0, + "incomingTransactionErrors": 0, + "serviceTime": 0, + "serviceTimeTotal": 0, + "outgoingTransactions": 0, + "outgoingTransactionRequestSize": 0, + "outgoingTransactionResponseSize": 0, + "outgoingTransactionErrors": 0, + } + + def collectMetrics(self, service_start, service_name, subservice_name, + request, response, flow): + + service_stop = time.time() + transactionError = False + if flow: + flow_metrics = flow.getFlowMetrics() + else: + flow_metrics = { + "serviceTime": 0, + "serviceTimeTotal": 0, + "outgoingTransactions": 0, + "outgoingTransactionRequestSize": 0, + "outgoingTransactionResponseSize": 0, + "outgoingTransactionErrors": 0, + } + + if service_name and not service_name in self.service: + self.service[service_name] = { + "sum": { + "incomingTransactions": 0, + "incomingTransactionRequestSize": 0, + "incomingTransactionResponseSize": 0, + "incomingTransactionErrors": 0, + "serviceTime": 0, + "serviceTimeTotal": 0, + "outgoingTransactions": 0, + "outgoingTransactionRequestSize": 0, + "outgoingTransactionResponseSize": 0, + "outgoingTransactionErrors": 0, + }, + "subservs": {} + } + + if (service_name and subservice_name and + not subservice_name in self.service[service_name]["subservs"]): + self.service[service_name]["subservs"][subservice_name] = { + "incomingTransactions": 0, + "incomingTransactionRequestSize": 0, + "incomingTransactionResponseSize": 0, + "incomingTransactionErrors": 0, + "serviceTime": 0, + "serviceTimeTotal": 0, + "outgoingTransactions": 0, + "outgoingTransactionRequestSize": 0, + "outgoingTransactionResponseSize": 0, + "outgoingTransactionErrors": 0, + } + + # Analize "response"" to know if is a Response about an error or not + if response.status_code not in [200, 201, 204]: + # API error + transactionError = True + + if service_name: + if subservice_name: + # Service and Subservice + if not transactionError: + self.service[service_name]["subservs"][subservice_name]["incomingTransactions"] += 1 + else: + self.service[service_name]["subservs"][subservice_name]["incomingTransactionErrors"] += 1 + self.service[service_name]["subservs"][subservice_name]["incomingTransactionRequestSize"] += len(json.dumps(request.data)) + self.service[service_name]["subservs"][subservice_name]["incomingTransactionResponseSize"] += len(json.dumps(response.data)) + self.service[service_name]["subservs"][subservice_name]["serviceTimeTotal"] += (service_stop - service_start) + + self.service[service_name]["subservs"][subservice_name]["outgoingTransactions"] += flow_metrics["outgoingTransactions"] + self.service[service_name]["subservs"][subservice_name]["outgoingTransactionRequestSize"] += flow_metrics["outgoingTransactionRequestSize"] + self.service[service_name]["subservs"][subservice_name]["outgoingTransactionResponseSize"] += flow_metrics["outgoingTransactionResponseSize"] + self.service[service_name]["subservs"][subservice_name]["outgoingTransactionErrors"] += flow_metrics["outgoingTransactionErrors"] + self.service[service_name]["subservs"][subservice_name]["serviceTimeTotal"] += flow_metrics["serviceTimeTotal"] + + + + # Service + if not transactionError: + self.service[service_name]["sum"]["incomingTransactions"] += 1 + else: + self.service[service_name]["sum"]["incomingTransactionErrors"] += 1 + self.service[service_name]["sum"]["incomingTransactionRequestSize"] += len(json.dumps(request.data)) + self.service[service_name]["sum"]["incomingTransactionResponseSize"] += len(json.dumps(response.data)) + self.service[service_name]["sum"]["serviceTimeTotal"] += (service_stop - service_start) + self.service[service_name]["sum"]["outgoingTransactions"] += flow_metrics["outgoingTransactions"] + self.service[service_name]["sum"]["outgoingTransactionRequestSize"] += flow_metrics["outgoingTransactionRequestSize"] + self.service[service_name]["sum"]["outgoingTransactionResponseSize"] += flow_metrics["outgoingTransactionResponseSize"] + self.service[service_name]["sum"]["outgoingTransactionErrors"] += flow_metrics["outgoingTransactionErrors"] + self.service[service_name]["sum"]["serviceTimeTotal"] += flow_metrics["serviceTimeTotal"] + + # Sum + if not transactionError: + self.sum["incomingTransactions"] += 1 + else: + self.sum["incomingTransactionErrors"] += 1 + self.sum["incomingTransactionRequestSize"] += len(json.dumps(request.data)) + self.sum["incomingTransactionResponseSize"] += len(json.dumps(response.data)) + self.sum["serviceTimeTotal"] += (service_stop - service_start) + self.sum["outgoingTransactions"] += flow_metrics["outgoingTransactions"] + self.sum["outgoingTransactionRequestSize"] += flow_metrics["outgoingTransactionRequestSize"] + self.sum["outgoingTransactionResponseSize"] += flow_metrics["outgoingTransactionResponseSize"] + self.sum["outgoingTransactionErrors"] += flow_metrics["outgoingTransactionErrors"] + self.sum["serviceTimeTotal"] += flow_metrics["serviceTimeTotal"] + + + def resetMetrics(self): + self.service = {} + self.sum = { + "incomingTransactions": 0, + "incomingTransactionRequestSize": 0, + "incomingTransactionResponseSize": 0, + "incomingTransactionErrors": 0, + "serviceTime": 0, + "serviceTimeTotal": 0, + "outgoingTransactions": 0, + "outgoingTransactionRequestSize": 0, + "outgoingTransactionResponseSize": 0, + "outgoingTransactionErrors": 0, + } + + + def composeMetrics(self): + + result = { + "service": self.service, + "sum": self.sum + } + + for serv in result["service"]: + if result["service"][serv]["sum"]["serviceTimeTotal"] > 0: + result["service"][serv]["sum"]["serviceTime"] = result["service"][serv]["sum"]["serviceTimeTotal"] / ( + result["service"][serv]["sum"]["incomingTransactions"] + + result["service"][serv]["sum"]["incomingTransactionErrors"] + + result["service"][serv]["sum"]["outgoingTransactions"] + + result["service"][serv]["sum"]["outgoingTransactionErrors"] + ) + + for subserv in result["service"][serv]["subservs"]: + if result["service"][serv]["subservs"][subserv]["serviceTimeTotal"] > 0: + result["service"][serv]["subservs"][subserv]["serviceTime"] = result["service"][serv]["subservs"][subserv]["serviceTimeTotal"] / ( + result["service"][serv]["subservs"][subserv]["incomingTransactions"] + + result["service"][serv]["subservs"][subserv]["incomingTransactionErrors"] + + result["service"][serv]["subservs"][subserv]["outgoingTransactions"] + + result["service"][serv]["subservs"][subserv]["outgoingTransactionErrors"] + ) + + if result["sum"]["serviceTimeTotal"] > 0: + result["sum"]["serviceTime"] = result["sum"]["serviceTimeTotal"] / ( + result["sum"]["incomingTransactions"] + + result["sum"]["incomingTransactionErrors"] + + result["sum"]["outgoingTransactions"] + + result["sum"]["outgoingTransactionErrors"] + ) + return result class IoTConf(Stats): @@ -231,6 +405,8 @@ def __init__(self): def get(self, request, service_id=None): self.schema_name = "ServiceList" + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -242,7 +418,7 @@ def get(self, request, service_id=None): CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) if not service_id: # Get all domains - result = flow.domains( + result, service_name, subservice_name = flow.domains( request.DATA.get("DOMAIN_NAME", None), request.DATA.get("SERVICE_ADMIN_USER", None), request.DATA.get("SERVICE_ADMIN_PASSWORD", None), @@ -250,7 +426,7 @@ def get(self, request, service_id=None): HTTP_X_AUTH_TOKEN)) else: # Get detail of one domains - result = flow.get_domain( + result, service_name, subservice_name = flow.get_domain( request.DATA.get("DOMAIN_ID", service_id), request.DATA.get("DOMAIN_NAME", None), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -259,24 +435,30 @@ def get(self, request, service_id=None): HTTP_X_AUTH_TOKEN)) if 'error' not in result: Stats.num_get_service += 1 - return Response(result, status=status.HTTP_200_OK, - headers={"Fiware-Correlator": CORRELATOR_ID}) + response = Response(result, status=status.HTTP_200_OK, + headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], - status=self.getStatusFromCode(result['code']), - headers={"Fiware-Correlator": CORRELATOR_ID}) + response = Response(result['error'], + status=self.getStatusFromCode(result['code']), + headers={"Fiware-Correlator": CORRELATOR_ID}) + except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response + def put(self, request, service_id=None): + service_start = time.time() + response = service_name = subservice_name = flow = None self.schema_name = "ServiceList" HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) @@ -287,7 +469,7 @@ def put(self, request, service_id=None): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.update_domain( + result, service_name, subservice_name = flow.update_domain( request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -296,23 +478,27 @@ def put(self, request, service_id=None): request.DATA.get("NEW_SERVICE_DESCRIPTION", None)) if 'error' not in result: Stats.num_put_service += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def delete(self, request, service_id=None): + service_start = time.time() + response = service_name = subservice_name = flow = None self.schema_name = "ServiceList" HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) @@ -338,7 +524,7 @@ def delete(self, request, service_id=None): self.PERSEO_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.delete_domain( + result, service_name, subservice_name = flow.delete_domain( request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -346,22 +532,23 @@ def delete(self, request, service_id=None): request.DATA.get("SERVICE_ADMIN_TOKEN", HTTP_X_AUTH_TOKEN)) if 'error' not in result: Stats.num_delete_service += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) - + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class ServiceCreate_RESTView(ServiceList_RESTView): """ @@ -375,6 +562,8 @@ def __init__(self): ServiceList_RESTView.__init__(self) def post(self, request, *args, **kw): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -387,7 +576,7 @@ def post(self, request, *args, **kw): self.KEYPASS_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.createNewService( + result, service_name, subservice_name = flow.createNewService( request.DATA.get("DOMAIN_NAME", None), request.DATA.get("DOMAIN_ADMIN_USER", None), request.DATA.get("DOMAIN_ADMIN_PASSWORD", None), @@ -398,24 +587,25 @@ def post(self, request, *args, **kw): request.DATA.get("NEW_SERVICE_ADMIN_USER"), request.DATA.get("NEW_SERVICE_ADMIN_PASSWORD"), request.DATA.get("NEW_SERVICE_ADMIN_EMAIL", None)) - if 'token' in result: Stats.num_post_service += 1 - return Response(result, status=status.HTTP_201_CREATED, + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class SubServiceList_RESTView(APIView, IoTConf): @@ -430,8 +620,9 @@ def __init__(self): IoTConf.__init__(self) def get(self, request, service_id=None, subservice_id=None): - self.schema_name = "SubServiceList" + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -443,7 +634,7 @@ def get(self, request, service_id=None, subservice_id=None): CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) if service_id: if not subservice_id: - result = flow.projects( + result, service_name, subservice_name = flow.projects( service_id, request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -452,8 +643,9 @@ def get(self, request, service_id=None, subservice_id=None): HTTP_X_AUTH_TOKEN)) else: # Get detail of subservice - result = flow.get_project( + result, service_name, subservice_name = flow.get_project( request.DATA.get("SERVICE_ID", service_id), + request.DATA.get("SERVICE_NAME", None), request.DATA.get("SUBSERVICE_ID", subservice_id), request.DATA.get("SERVICE_ADMIN_USER", None), request.DATA.get("SERVICE_ADMIN_PASSWORD", None), @@ -466,26 +658,29 @@ def get(self, request, service_id=None, subservice_id=None): if 'error' not in result: Stats.num_get_subservice += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def put(self, request, service_id=None, subservice_id=None): - self.schema_name = "SubServiceList" + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -497,7 +692,7 @@ def put(self, request, service_id=None, subservice_id=None): CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) if service_id: if subservice_id: - result = flow.update_project( + result, service_name, subservice_name = flow.update_project( request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_NAME", None), request.DATA.get("SUBSERVICE_ID", subservice_id), @@ -513,25 +708,29 @@ def put(self, request, service_id=None, subservice_id=None): if 'error' not in result: Stats.num_put_subservice += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def delete(self, request, service_id=None, subservice_id=None): self.schema_name = "SubServiceList" + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -557,7 +756,7 @@ def delete(self, request, service_id=None, subservice_id=None): CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) if service_id: - result = flow.delete_project( + result, service_name, subservice_name = flow.delete_project( request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_NAME", None), request.DATA.get("SUBSERVICE_ID", subservice_id), @@ -572,21 +771,23 @@ def delete(self, request, service_id=None, subservice_id=None): if 'error' not in result: Stats.num_delete_subservice += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class SubServiceCreate_RESTView(SubServiceList_RESTView): @@ -599,6 +800,8 @@ def __init__(self): SubServiceList_RESTView.__init__(self) def post(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -618,7 +821,7 @@ def post(self, request, service_id): CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.createNewSubService( + result, service_name, subservice_name = flow.createNewSubService( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -723,23 +926,24 @@ def post(self, request, service_id): if 'id' in result and ('error' not in result): Stats.num_post_subservice += 1 - return Response(result, status=status.HTTP_201_CREATED, + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) - + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class User_RESTView(APIView, IoTConf): """ @@ -753,6 +957,8 @@ def __init__(self): IoTConf.__init__(self) def delete(self, request, service_id, user_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -762,7 +968,7 @@ def delete(self, request, service_id, user_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.removeUser( + result, service_name, subservice_name = flow.removeUser( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -772,23 +978,27 @@ def delete(self, request, service_id, user_id): request.DATA.get("USER_ID", user_id)) if 'error' not in result: Stats.num_delete_user += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def put(self, request, service_id, user_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -798,33 +1008,37 @@ def put(self, request, service_id, user_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.updateUser( - request.DATA.get("SERVICE_NAME"), + result, service_name, subservice_name = flow.updateUser( + request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), request.DATA.get("SERVICE_ADMIN_PASSWORD", None), request.DATA.get("SERVICE_ADMIN_TOKEN", HTTP_X_AUTH_TOKEN), - request.DATA.get("USER_NAME"), + request.DATA.get("USER_NAME", None), request.DATA.get("USER_ID", user_id), request.DATA.get("USER_DATA_VALUE")) if 'error' not in result: Stats.num_put_user += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0}'.format(error.message), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def get(self, request, service_id, user_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -834,7 +1048,9 @@ def get(self, request, service_id, user_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.user(request.DATA.get("SERVICE_ID", service_id), + result, service_name, subservice_name = flow.user( + request.DATA.get("SERVICE_ID", service_id), + request.DATA.get("SERVICE_NAME", None), request.DATA.get("USER_ID", user_id), request.DATA.get("SERVICE_ADMIN_USER", None), request.DATA.get("SERVICE_ADMIN_PASSWORD", None), @@ -842,23 +1058,27 @@ def get(self, request, service_id, user_id): HTTP_X_AUTH_TOKEN)) if 'error' not in result: Stats.num_get_user += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def post(self, request, service_id, user_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -868,7 +1088,7 @@ def post(self, request, service_id, user_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.changeUserPassword( + result, service_name, subservice_name = flow.changeUserPassword( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("USER_ID", user_id), @@ -880,21 +1100,23 @@ def post(self, request, service_id, user_id): ) if 'error' not in result: Stats.num_post_user += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class UserList_RESTView(APIView, IoTConf): @@ -909,6 +1131,8 @@ def __init__(self): IoTConf.__init__(self) def get(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) index = request.GET.get('index', None) @@ -921,7 +1145,7 @@ def get(self, request, service_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.users( + result, service_name, subservice_name = flow.users( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -932,23 +1156,27 @@ def get(self, request, service_id): if 'error' not in result: Stats.num_get_userlist += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def post(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -958,7 +1186,7 @@ def post(self, request, service_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.createNewServiceUser( + result, service_name, subservice_name = flow.createNewServiceUser( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -971,21 +1199,23 @@ def post(self, request, service_id): request.DATA.get("NEW_SERVICE_USER_DESCRIPTION", None)) if 'id' in result: Stats.num_post_userlist += 1 - return Response(result, status=status.HTTP_201_CREATED, + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class Group_RESTView(APIView, IoTConf): @@ -1000,6 +1230,8 @@ def __init__(self): IoTConf.__init__(self) def delete(self, request, service_id, group_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -1009,7 +1241,7 @@ def delete(self, request, service_id, group_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.removeGroup( + result, service_name, subservice_name = flow.removeGroup( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1018,24 +1250,28 @@ def delete(self, request, service_id, group_id): request.DATA.get("GROUP_NAME", None), request.DATA.get("GROUP_ID", group_id)) if 'error' not in result: - Stats.num_delete_user += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + Stats.num_delete_group += 1 + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def put(self, request, service_id, group_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -1045,32 +1281,36 @@ def put(self, request, service_id, group_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.updateGroup( - request.DATA.get("SERVICE_NAME"), + result, service_name, subservice_name = flow.updateGroup( + request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), request.DATA.get("SERVICE_ADMIN_PASSWORD", None), request.DATA.get("SERVICE_ADMIN_TOKEN", HTTP_X_AUTH_TOKEN), - request.DATA.get("GROUP_NAME"), + request.DATA.get("GROUP_NAME", None), request.DATA.get("GROUP_ID", group_id)) if 'error' not in result: - Stats.num_put_user += 1 - return Response(result, status=status.HTTP_200_OK, + Stats.num_put_group += 1 + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0}'.format(error.message), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def get(self, request, service_id, group_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -1080,29 +1320,33 @@ def get(self, request, service_id, group_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.group(request.DATA.get("SERVICE_ID", service_id), + result, service_name, subservice_name = flow.group( + request.DATA.get("SERVICE_ID", service_id), + request.DATA.get("SERVICE_NAME", None), request.DATA.get("GROUP_ID", group_id), request.DATA.get("SERVICE_ADMIN_USER", None), request.DATA.get("SERVICE_ADMIN_PASSWORD", None), request.DATA.get("SERVICE_ADMIN_TOKEN", HTTP_X_AUTH_TOKEN)) if 'error' not in result: - Stats.num_get_user += 1 - return Response(result, status=status.HTTP_200_OK, + Stats.num_get_group += 1 + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class GroupList_RESTView(APIView, IoTConf): @@ -1117,6 +1361,8 @@ def __init__(self): IoTConf.__init__(self) def get(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) index = request.GET.get('index', None) @@ -1129,7 +1375,7 @@ def get(self, request, service_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.users( + result, service_name, subservice_name = flow.groups( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1139,34 +1385,38 @@ def get(self, request, service_id): request.DATA.get("COUNT", count)) if 'error' not in result: - Stats.num_get_userlist += 1 - return Response(result, status=status.HTTP_200_OK, + Stats.num_get_grouplist += 1 + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def post(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: request.DATA # json validation - flow = CreateNewServiceUser(self.KEYSTONE_PROTOCOL, - self.KEYSTONE_HOST, - self.KEYSTONE_PORT, - CORRELATOR_ID=CORRELATOR_ID) + flow = Groups(self.KEYSTONE_PROTOCOL, + self.KEYSTONE_HOST, + self.KEYSTONE_PORT, + CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.createNewServiceUser( + result, service_name, subservice_name = flow.createNewServiceGroup( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1176,22 +1426,24 @@ def post(self, request, service_id): request.DATA.get("NEW_SERVICE_GROUP_NAME", None), request.DATA.get("NEW_SERVICE_GROUP_DESCRIPTION", None)) if 'id' in result: - Stats.num_post_userlist += 1 - return Response(result, status=status.HTTP_201_CREATED, + Stats.num_post_grouplist += 1 + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class Role_RESTView(APIView, IoTConf): @@ -1206,6 +1458,8 @@ def __init__(self): IoTConf.__init__(self) def get(self, request, service_id, role_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -1219,7 +1473,7 @@ def get(self, request, service_id, role_id): self.KEYPASS_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.getDomainRolePolicies( + result, service_name, subservice_name = flow.getDomainRolePolicies( request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1230,24 +1484,29 @@ def get(self, request, service_id, role_id): if 'error' not in result: Stats.num_get_role_policies += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response + def post(self, request, service_id, role_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -1262,7 +1521,7 @@ def post(self, request, service_id, role_id): CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.setPolicyRole( + result, service_name, subservice_name = flow.setPolicyRole( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1275,24 +1534,28 @@ def post(self, request, service_id, role_id): if 'error' not in result: Stats.num_post_role_policies += 1 - return Response(result, status=status.HTTP_201_CREATED, + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def delete(self, request, service_id, role_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -1306,7 +1569,7 @@ def delete(self, request, service_id, role_id): CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.removeRole( + result, service_name, subservice_name = flow.removeRole( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1317,21 +1580,23 @@ def delete(self, request, service_id, role_id): if 'error' not in result: Stats.num_delete_role += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class RolePolicy_RESTView(APIView, IoTConf): @@ -1347,6 +1612,8 @@ def __init__(self): def get(self, request, service_id, role_id, policy_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -1360,7 +1627,7 @@ def get(self, request, service_id, role_id, policy_id): CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.getPolicyFromRole( + result, service_name, subservice_name = flow.getPolicyFromRole( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1372,23 +1639,27 @@ def get(self, request, service_id, role_id, policy_id): if 'error' not in result: Stats.num_get_policy_from_role += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def delete(self, request, service_id, role_id, policy_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -1402,7 +1673,7 @@ def delete(self, request, service_id, role_id, policy_id): CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.removePolicyFromRole( + result, service_name, subservice_name = flow.removePolicyFromRole( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1414,21 +1685,23 @@ def delete(self, request, service_id, role_id, policy_id): if 'error' not in result: Stats.num_delete_policy_from_role += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class RoleList_RESTView(APIView, IoTConf): @@ -1443,6 +1716,8 @@ def __init__(self): IoTConf.__init__(self) def post(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None self.schema_name = "RoleList" HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) @@ -1453,7 +1728,7 @@ def post(self, request, service_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.createNewServiceRole( + result, service_name, subservice_name = flow.createNewServiceRole( request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1463,24 +1738,28 @@ def post(self, request, service_id): request.DATA.get("XACML_POLICY", None)) if 'error' not in result: Stats.num_post_role += 1 - return Response(result, status=status.HTTP_201_CREATED, + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def get(self, request, service_id=None): + service_start = time.time() + response = service_name = subservice_name = flow = None self.schema_name = "RoleAssignmentList" # Like that scheme! HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) @@ -1493,7 +1772,7 @@ def get(self, request, service_id=None): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.roles( + result, service_name, subservice_name = flow.roles( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1504,21 +1783,23 @@ def get(self, request, service_id=None): if 'error' not in result: Stats.num_get_role += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class AssignRoleUser_RESTView(APIView, IoTConf): @@ -1530,6 +1811,8 @@ def __init__(self): IoTConf.__init__(self) def get(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None self.schema_name = "RoleAssignmentList" user_id = request.GET.get('user_id', None) subservice_id = request.GET.get('subservice_id', None) @@ -1544,7 +1827,7 @@ def get(self, request, service_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.roles_assignments( + result, service_name, subservice_name = flow.roles_assignments( request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_NAME",None), request.DATA.get("SUBSERVICE_ID", subservice_id), @@ -1560,23 +1843,27 @@ def get(self, request, service_id): if 'error' not in result: Stats.num_get_roleassignment += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def post(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None self.schema_name = "AssignRole" HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) @@ -1595,7 +1882,7 @@ def post(self, request, service_id): if not (request.DATA.get("SUBSERVICE_NAME", None) or request.DATA.get("SUBSERVICE_ID", subservice_id)): if inherit: - result = flow.assignInheritRoleServiceUser( + result, service_name, subservice_name = flow.assignInheritRoleServiceUser( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1607,7 +1894,7 @@ def post(self, request, service_id): request.DATA.get("SERVICE_USER_NAME", None), request.DATA.get("SERVICE_USER_ID", user_id)) else: - result = flow.assignRoleServiceUser( + result, service_name, subservice_name = flow.assignRoleServiceUser( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1619,7 +1906,7 @@ def post(self, request, service_id): request.DATA.get("SERVICE_USER_NAME", None), request.DATA.get("SERVICE_USER_ID", user_id)) else: - result = flow.assignRoleSubServiceUser( + result, service_name, subservice_name = flow.assignRoleSubServiceUser( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME", None), @@ -1633,23 +1920,27 @@ def post(self, request, service_id): request.DATA.get("SERVICE_USER_ID", user_id)) if 'error' not in result: Stats.num_post_roleassignment += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def delete(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None self.schema_name = "AssignRole" HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) @@ -1669,7 +1960,7 @@ def delete(self, request, service_id): if not (request.DATA.get("SUBSERVICE_NAME", None) or request.DATA.get("SUBSERVICE_ID", subservice_id)): if inherit: - result = flow.revokeInheritRoleServiceUser( + result, service_name, subservice_name = flow.revokeInheritRoleServiceUser( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1681,7 +1972,7 @@ def delete(self, request, service_id): request.DATA.get("SERVICE_USER_NAME", None), request.DATA.get("SERVICE_USER_ID", user_id)) else: - result = flow.revokeRoleServiceUser( + result, service_name, subservice_name = flow.revokeRoleServiceUser( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1693,7 +1984,7 @@ def delete(self, request, service_id): request.DATA.get("SERVICE_USER_NAME", None), request.DATA.get("SERVICE_USER_ID", user_id)) else: - result = flow.revokeRoleSubServiceUser( + result, service_name, subservice_name = flow.revokeRoleSubServiceUser( request.DATA.get("SERVICE_NAME"), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME"), @@ -1707,21 +1998,23 @@ def delete(self, request, service_id): request.DATA.get("SERVICE_USER_ID", user_id)) if 'error' not in result: Stats.num_delete_roleassignment += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class AssignRoleGroup_RESTView(APIView, IoTConf): @@ -1733,6 +2026,8 @@ def __init__(self): IoTConf.__init__(self) def get(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None self.schema_name = "RoleAssignmentList" group_id = request.GET.get('group_id', None) subservice_id = request.GET.get('subservice_id', None) @@ -1747,7 +2042,7 @@ def get(self, request, service_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.roles_assignments_groups( + result, service_name, subservice_name = flow.roles_assignments_groups( request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_NAME",None), request.DATA.get("SUBSERVICE_ID", subservice_id), @@ -1763,23 +2058,27 @@ def get(self, request, service_id): if 'error' not in result: Stats.num_get_roleassignment += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def post(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None self.schema_name = "AssignRole" HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) @@ -1798,7 +2097,7 @@ def post(self, request, service_id): if not (request.DATA.get("SUBSERVICE_NAME", None) or request.DATA.get("SUBSERVICE_ID", subservice_id)): if inherit: - result = flow.assignInheritRoleServiceGroup( + result, service_name, subservice_name = flow.assignInheritRoleServiceGroup( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1810,7 +2109,7 @@ def post(self, request, service_id): request.DATA.get("SERVICE_GROUP_NAME", None), request.DATA.get("SERVICE_GROUP_ID", group_id)) else: - result = flow.assignRoleServiceGroup( + result, service_name, subservice_name = flow.assignRoleServiceGroup( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1822,7 +2121,7 @@ def post(self, request, service_id): request.DATA.get("SERVICE_GROUP_NAME", None), request.DATA.get("SERVICE_GROUP_ID", group_id)) else: - result = flow.assignRoleSubServiceGroup( + result, service_name, subservice_name = flow.assignRoleSubServiceGroup( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME", None), @@ -1836,23 +2135,27 @@ def post(self, request, service_id): request.DATA.get("SERVICE_GROUP_ID", group_id)) if 'error' not in result: Stats.num_post_roleassignment += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def delete(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None self.schema_name = "AssignRole" HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) @@ -1872,7 +2175,7 @@ def delete(self, request, service_id): if not (request.DATA.get("SUBSERVICE_NAME", None) or request.DATA.get("SUBSERVICE_ID", subservice_id)): if inherit: - result = flow.revokeInheritRoleServiceUser( + result, service_name, subservice_name = flow.revokeInheritRoleServiceGroup( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1884,7 +2187,7 @@ def delete(self, request, service_id): request.DATA.get("SERVICE_GROUP_NAME", None), request.DATA.get("SERVICE_GROUP_ID", group_id)) else: - result = flow.revokeRoleServiceGroup( + result, service_name, subservice_name = flow.revokeRoleServiceGroup( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_ADMIN_USER", None), @@ -1896,7 +2199,7 @@ def delete(self, request, service_id): request.DATA.get("SERVICE_GROUP_NAME", None), request.DATA.get("SERVICE_GROUP_ID", group_id)) else: - result = flow.revokeRoleSubServiceGroup( + result, service_name, subservice_name = flow.revokeRoleSubServiceGroup( request.DATA.get("SERVICE_NAME"), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME"), @@ -1910,21 +2213,23 @@ def delete(self, request, service_id): request.DATA.get("SERVICE_GROUP_ID", group_id)) if 'error' not in result: Stats.num_delete_roleassignment += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class Trust_RESTView(APIView, IoTConf): @@ -1939,6 +2244,8 @@ def __init__(self): IoTConf.__init__(self) def post(self, request, service_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -1948,7 +2255,7 @@ def post(self, request, service_id): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.createTrustToken( + result, service_name, subservice_name = flow.createTrustToken( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME", None), @@ -1965,22 +2272,24 @@ def post(self, request, service_id): ) if 'error' not in result: Stats.num_post_trust += 1 - return Response(result, status=status.HTTP_201_CREATED, + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class SubServiceIoTADevice_RESTView(APIView, IoTConf): @@ -1995,6 +2304,8 @@ def __init__(self): IoTConf.__init__(self) def post(self, request, service_id, subservice_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -2016,7 +2327,7 @@ def post(self, request, service_id, subservice_id): self.CA_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.register_device( + result, service_name, subservice_name = flow.register_device( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME", None), @@ -2026,7 +2337,8 @@ def post(self, request, service_id, subservice_id): request.DATA.get("SERVICE_USER_TOKEN", HTTP_X_AUTH_TOKEN), request.DATA.get("DEVICE_ID", None), request.DATA.get("ENTITY_TYPE", None), - request.DATA.get("ENTITY_NAME", request.DATA.get("DEVICE_ID", None)), + request.DATA.get("ENTITY_NAME", + request.DATA.get("DEVICE_ID", None)), request.DATA.get("PROTOCOL", None), request.DATA.get("ATT_ICCID", None), request.DATA.get("ATT_IMEI", None), @@ -2037,24 +2349,28 @@ def post(self, request, service_id, subservice_id): ) if 'error' not in result: Stats.num_post_device += 1 - return Response(result, status=status.HTTP_201_CREATED, + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def delete(self, request, service_id, subservice_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -2079,7 +2395,7 @@ def delete(self, request, service_id, subservice_id): self.PERSEO_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.unregister_device( + result, service_name, subservice_name = flow.unregister_device( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME", None), @@ -2091,22 +2407,24 @@ def delete(self, request, service_id, subservice_id): ) if 'error' not in result: Stats.num_delete_device += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class SubServiceIoTADevices_RESTView(APIView, IoTConf): @@ -2121,6 +2439,8 @@ def __init__(self): IoTConf.__init__(self) def post(self, request, service_id, subservice_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -2145,7 +2465,7 @@ def post(self, request, service_id, subservice_id): self.PERSEO_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.register_devices( + result, service_name, subservice_name = flow.register_devices( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME", None), @@ -2157,22 +2477,24 @@ def post(self, request, service_id, subservice_id): ) if 'error' not in result: Stats.num_post_devices += 1 - return Response(result, status=status.HTTP_201_CREATED, + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class SubServiceIoTAService_RESTView(APIView, IoTConf): @@ -2187,6 +2509,8 @@ def __init__(self): IoTConf.__init__(self) def post(self, request, service_id, subservice_id): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -2211,7 +2535,7 @@ def post(self, request, service_id, subservice_id): self.PERSEO_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.register_service( + result, service_name, subservice_name = flow.register_service( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME", None), @@ -2233,22 +2557,24 @@ def post(self, request, service_id, subservice_id): ) if 'error' not in result: Stats.num_post_entity_service += 1 - return Response(result, status=status.HTTP_201_CREATED, + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class IOTModuleActivation_RESTView(APIView, IoTConf): @@ -2263,6 +2589,8 @@ def __init__(self): IoTConf.__init__(self) def get(self, request, service_id, subservice_id=None): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -2288,7 +2616,7 @@ def get(self, request, service_id, subservice_id=None): self.PERSEO_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - modules = flow.list_activated_modules( + modules, service_name, subservice_name = flow.list_activated_modules( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_USER_NAME", None), @@ -2316,7 +2644,7 @@ def get(self, request, service_id, subservice_id=None): self.PERSEO_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - modules = flow.list_activated_modules( + modules, service_name, subservice_name = flow.list_activated_modules( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME", None), @@ -2329,25 +2657,29 @@ def get(self, request, service_id, subservice_id=None): if 'error' not in modules: result['actived_modules'] = modules Stats.num_get_module_activation += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) else: result = modules Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def post(self, request, service_id, subservice_id=None, iot_module=None): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -2373,7 +2705,7 @@ def post(self, request, service_id, subservice_id=None, iot_module=None): self.PERSEO_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - sub = flow.activate_module( + sub, service_name, subservice_name = flow.activate_module( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_USER_NAME", None), @@ -2402,7 +2734,7 @@ def post(self, request, service_id, subservice_id=None, iot_module=None): self.PERSEO_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - sub = flow.activate_module( + sub, service_name, subservice_name = flow.activate_module( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME", None), @@ -2416,25 +2748,29 @@ def post(self, request, service_id, subservice_id=None, iot_module=None): result['subscriptionid'] = sub if 'error' not in result: Stats.num_post_module_activation += 1 - return Response(result, status=status.HTTP_201_CREATED, + response = Response(result, status=status.HTTP_201_CREATED, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def delete(self, request, service_id, subservice_id=None, iot_module=None): + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -2460,7 +2796,7 @@ def delete(self, request, service_id, subservice_id=None, iot_module=None): self.PERSEO_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - flow.deactivate_module( + result, service_name, subservice_name = flow.deactivate_module( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SERVICE_USER_NAME", None), @@ -2489,7 +2825,7 @@ def delete(self, request, service_id, subservice_id=None, iot_module=None): self.PERSEO_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - flow.deactivate_module( + result, service_name, subservice_name = flow.deactivate_module( request.DATA.get("SERVICE_NAME", None), request.DATA.get("SERVICE_ID", service_id), request.DATA.get("SUBSERVICE_NAME", None), @@ -2499,26 +2835,27 @@ def delete(self, request, service_id, subservice_id=None, iot_module=None): request.DATA.get("SERVICE_USER_TOKEN", HTTP_X_AUTH_TOKEN), request.DATA.get("IOTMODULE", iot_module), ) - result = {} if 'error' not in result: Stats.num_delete_module_activation += 1 - return Response(result, status=status.HTTP_204_NO_CONTENT, + response = Response(result, status=status.HTTP_204_NO_CONTENT, headers={"Fiware-Correlator": CORRELATOR_ID}) else: Stats.num_flow_errors += 1 - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code']), headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: Stats.num_api_errors += 1 - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class OrchVersion_RESTView(APIView, IoTConf): @@ -2530,7 +2867,8 @@ def __init__(self): IoTConf.__init__(self) def get(self, request): - + service_start = time.time() + response = service_name = subservice_name = flow = None #HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) try: # Extract version and stats data @@ -2595,17 +2933,19 @@ def get(self, request): result, indent=3)) if 'error' not in result: - return Response(result, status=status.HTTP_200_OK) + response = Response(result, status=status.HTTP_200_OK) else: - return Response(result['error'], + response = Response(result['error'], status=self.getStatusFromCode(result['code'])) except ParseError as error: - return Response( + response = Response( 'Input validation error - {0} {1}'.format(error.message, error.detail), status=status.HTTP_400_BAD_REQUEST ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response class OrchLogLevel_RESTView(APIView, IoTConf): @@ -2617,7 +2957,8 @@ def __init__(self): IoTConf.__init__(self) def get(self, request): - + service_start = time.time() + response = service_name = subservice_name = flow = None CORRELATOR_ID = self.getCorrelatorIdHeader(request) try: @@ -2626,21 +2967,24 @@ def get(self, request): logging.getLevelName( logging.getLogger('orchestrator_api').getEffectiveLevel()) } - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: body = { "error": "%s" % error.detail } - return Response( + response = Response( json.dumps(body), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response def put(self, request): - + service_start = time.time() + response = service_name = subservice_name = flow = None HTTP_X_AUTH_TOKEN = self.getXAuthToken(request) CORRELATOR_ID = self.getCorrelatorIdHeader(request) logLevel = request.GET.get('level', None) @@ -2652,7 +2996,7 @@ def put(self, request): self.KEYSTONE_PORT, CORRELATOR_ID=CORRELATOR_ID) CORRELATOR_ID = self.getCorrelatorId(flow, CORRELATOR_ID) - result = flow.domains( + result, service_name, subservice_name = flow.domains( "admin_domain", request.DATA.get("SERVICE_ADMIN_USER", None), request.DATA.get("SERVICE_ADMIN_PASSWORD", None), @@ -2696,16 +3040,76 @@ def put(self, request): logLevel, indent=3)) Stats.num_update_loglevel += 1 - return Response(result, status=status.HTTP_200_OK, + response = Response(result, status=status.HTTP_200_OK, + headers={"Fiware-Correlator": CORRELATOR_ID}) + + + except ParseError as error: + body = { + "error": "%s" % error.detail + } + response = Response( + json.dumps(body), + status=status.HTTP_400_BAD_REQUEST, + headers={"Fiware-Correlator": CORRELATOR_ID} + ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response + +class OrchMetrics_RESTView(APIView, IoTConf): + """ + { Read, Update } Orchestrator Common Metrics + """ + + def __init__(self): + IoTConf.__init__(self) + + def get(self, request): + service_start = time.time() + response = service_name = subservice_name = flow = None + CORRELATOR_ID = self.getCorrelatorIdHeader(request) + reset = request.GET.get('reset', False) == "true" + + try: + result = self.composeMetrics() + + if reset: + self.resetMetrics() + + response = Response(result, status=status.HTTP_200_OK, headers={"Fiware-Correlator": CORRELATOR_ID}) + except ParseError as error: + body = { + "error": "%s" % error.detail + } + response = Response( + json.dumps(body), + status=status.HTTP_400_BAD_REQUEST, + headers={"Fiware-Correlator": CORRELATOR_ID} + ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response + + def delete(self, request): + service_start = time.time() + response = service_name = subservice_name = flow = None + CORRELATOR_ID = self.getCorrelatorIdHeader(request) + + try: + result = self.composeMetrics() + self.resetMetrics() + response = Response(result, status=status.HTTP_204_OK, + headers={"Fiware-Correlator": CORRELATOR_ID}) except ParseError as error: body = { "error": "%s" % error.detail } - return Response( + response = Response( json.dumps(body), status=status.HTTP_400_BAD_REQUEST, headers={"Fiware-Correlator": CORRELATOR_ID} ) + self.collectMetrics(service_start, service_name, subservice_name, request, response, flow) + return response diff --git a/src/orchestrator/commands/changeUserPassword.py b/src/orchestrator/commands/changeUserPassword.py index d852769f..83e49326 100755 --- a/src/orchestrator/commands/changeUserPassword.py +++ b/src/orchestrator/commands/changeUserPassword.py @@ -99,7 +99,7 @@ def main(): USER_DATA_VALUE = {"password": NEW_USER_PASSWORD} - res = flow.updateUser( + res, service_name, subservice_name = flow.updateUser( SERVICE_NAME, None, SERVICE_ADMIN_USER, diff --git a/src/orchestrator/commands/changeUserPasswordByHimself.py b/src/orchestrator/commands/changeUserPasswordByHimself.py index a2a9bfb8..17fd780a 100755 --- a/src/orchestrator/commands/changeUserPasswordByHimself.py +++ b/src/orchestrator/commands/changeUserPasswordByHimself.py @@ -93,7 +93,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - res = flow.changeUserPassword( + res, service_name, subservice_name = flow.changeUserPassword( SERVICE_NAME, None, None, diff --git a/src/orchestrator/commands/createNewService.py b/src/orchestrator/commands/createNewService.py index 82166bc2..d4138802 100755 --- a/src/orchestrator/commands/createNewService.py +++ b/src/orchestrator/commands/createNewService.py @@ -122,7 +122,7 @@ def main(): KEYPASS_HOST, KEYPASS_PORT) - res = flow.createNewService( + res, service_name, subservice_name = flow.createNewService( DOMAIN_NAME, DOMAIN_ADMIN_USER, DOMAIN_ADMIN_PASSWORD, diff --git a/src/orchestrator/commands/createNewServiceGroup.py b/src/orchestrator/commands/createNewServiceGroup.py index b33bb811..96c73eff 100644 --- a/src/orchestrator/commands/createNewServiceGroup.py +++ b/src/orchestrator/commands/createNewServiceGroup.py @@ -33,7 +33,7 @@ sys.path.append("/var/env-orchestrator/lib/python2.6/site-packages/iotp-orchestrator") from settings.common import LOGGING -from orchestrator.core.flow.createNewServiceGroup import CreateNewServiceGroup +from orchestrator.core.flow import Groups from orchestrator.api import schemas try: logging.config.dictConfig(LOGGING) @@ -90,11 +90,11 @@ def main(): }, schemas.json["GroupList"]) - flow = CreateNewServiceGroup(KEYSTONE_PROTOCOL, + flow = Groups(KEYSTONE_PROTOCOL, KEYSTONE_HOST, KEYSTONE_PORT) - res = flow.createNewServiceGroup( + res, service_name, subservice_name = flow.createNewServiceGroup( SERVICE_NAME, None, SERVICE_ADMIN_USER, diff --git a/src/orchestrator/commands/createNewServiceRole.py b/src/orchestrator/commands/createNewServiceRole.py index 576a7230..9e4dc67f 100755 --- a/src/orchestrator/commands/createNewServiceRole.py +++ b/src/orchestrator/commands/createNewServiceRole.py @@ -105,7 +105,7 @@ def main(): KEYPASS_HOST, KEYPASS_PORT) - res = flow.createNewServiceRole( + res, service_name, subservice_name = flow.createNewServiceRole( None, SERVICE_NAME, SERVICE_ADMIN_USER, diff --git a/src/orchestrator/commands/createNewServiceUser.py b/src/orchestrator/commands/createNewServiceUser.py index c27b2f5e..828eaedb 100755 --- a/src/orchestrator/commands/createNewServiceUser.py +++ b/src/orchestrator/commands/createNewServiceUser.py @@ -97,7 +97,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - res = flow.createNewServiceUser( + res, service_name, subservice_name = flow.createNewServiceUser( SERVICE_NAME, None, SERVICE_ADMIN_USER, diff --git a/src/orchestrator/commands/createNewSubService.py b/src/orchestrator/commands/createNewSubService.py index 8ad61dba..77355002 100755 --- a/src/orchestrator/commands/createNewSubService.py +++ b/src/orchestrator/commands/createNewSubService.py @@ -97,7 +97,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - res = flow.createNewSubService( + res, service_name, subservice_name = flow.createNewSubService( SERVICE_NAME, None, SERVICE_ADMIN_USER, diff --git a/src/orchestrator/commands/createTrustToken.py b/src/orchestrator/commands/createTrustToken.py index 4e259485..629a33f1 100755 --- a/src/orchestrator/commands/createTrustToken.py +++ b/src/orchestrator/commands/createTrustToken.py @@ -105,7 +105,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - res = flow.createTrustToken( + res, service_name, subservice_name = flow.createTrustToken( SERVICE_NAME, None, SUBSERVICE_NAME, diff --git a/src/orchestrator/commands/editService.py b/src/orchestrator/commands/editService.py index d84e102a..b0b9e0c7 100755 --- a/src/orchestrator/commands/editService.py +++ b/src/orchestrator/commands/editService.py @@ -83,7 +83,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - domain_detail = flow.update_domain( + domain_detail, service_name, subservice_name = flow.update_domain( None, SERVICE_NAME, DOMAIN_ADMIN_USER, diff --git a/src/orchestrator/commands/editSubService.py b/src/orchestrator/commands/editSubService.py index d6c8bbc9..38c2cbc7 100755 --- a/src/orchestrator/commands/editSubService.py +++ b/src/orchestrator/commands/editSubService.py @@ -85,7 +85,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - project_detail = flow.update_project( + project_detail, service_name, subservice_name = flow.update_project( None, SERVICE_NAME, None, diff --git a/src/orchestrator/commands/listAllRoleAssignments.py b/src/orchestrator/commands/listAllRoleAssignments.py index c08b0f15..7eeed409 100755 --- a/src/orchestrator/commands/listAllRoleAssignments.py +++ b/src/orchestrator/commands/listAllRoleAssignments.py @@ -91,7 +91,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - roles = flow.roles_assignments( + roles, service_name, subservice_name = flow.roles_assignments( None, SERVICE_NAME, None, diff --git a/src/orchestrator/commands/listGroupRoleAssignments.py b/src/orchestrator/commands/listGroupRoleAssignments.py index 4ed5cee5..b279bec8 100644 --- a/src/orchestrator/commands/listGroupRoleAssignments.py +++ b/src/orchestrator/commands/listGroupRoleAssignments.py @@ -91,7 +91,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - roles = flow.roles_assignments_groups( + roles, service_name, subservice_name = flow.roles_assignments_groups( None, SERVICE_NAME, None, diff --git a/src/orchestrator/commands/listRoleAssignments.py b/src/orchestrator/commands/listRoleAssignments.py index fa6fd355..594c55ee 100755 --- a/src/orchestrator/commands/listRoleAssignments.py +++ b/src/orchestrator/commands/listRoleAssignments.py @@ -91,7 +91,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - roles = flow.roles_assignments( + roles, service_name, subservice_name = flow.roles_assignments( None, SERVICE_NAME, None, diff --git a/src/orchestrator/commands/listSubServiceRoleAssignments.py b/src/orchestrator/commands/listSubServiceRoleAssignments.py index 61db612e..996f8f73 100755 --- a/src/orchestrator/commands/listSubServiceRoleAssignments.py +++ b/src/orchestrator/commands/listSubServiceRoleAssignments.py @@ -91,7 +91,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - roles = flow.roles_assignments( + roles, service_name, subservice_name = flow.roles_assignments( None, SERVICE_NAME, None, diff --git a/src/orchestrator/commands/listUserRoleAssignments.py b/src/orchestrator/commands/listUserRoleAssignments.py index 36fa4c74..2574c36b 100755 --- a/src/orchestrator/commands/listUserRoleAssignments.py +++ b/src/orchestrator/commands/listUserRoleAssignments.py @@ -91,7 +91,7 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - roles = flow.roles_assignments( + roles, service_name, subservice_name = flow.roles_assignments( None, SERVICE_NAME, None, diff --git a/src/orchestrator/commands/printServiceGroups.py b/src/orchestrator/commands/printServiceGroups.py index d479f54d..695f8ef1 100644 --- a/src/orchestrator/commands/printServiceGroups.py +++ b/src/orchestrator/commands/printServiceGroups.py @@ -80,7 +80,8 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - groups = flow.groups(SERVICE_NAME, + groups, service_name, subservice_name = flow.groups( + SERVICE_NAME, None, SERVICE_ADMIN_USER, SERVICE_ADMIN_PASSWORD, diff --git a/src/orchestrator/commands/printServiceIotModules.py b/src/orchestrator/commands/printServiceIotModules.py index 10f0e455..eacbce8a 100755 --- a/src/orchestrator/commands/printServiceIotModules.py +++ b/src/orchestrator/commands/printServiceIotModules.py @@ -100,7 +100,8 @@ def main(): ORION_HOST, ORION_PORT) - roles = flow.list_activated_modules(SERVICE_NAME, + roles, service_name, subservice_name = flow.list_activated_modules( + SERVICE_NAME, None, SUBSERVICE_NAME, None, diff --git a/src/orchestrator/commands/printServiceRolePolicies.py b/src/orchestrator/commands/printServiceRolePolicies.py index c21ccb0c..25fe5c14 100755 --- a/src/orchestrator/commands/printServiceRolePolicies.py +++ b/src/orchestrator/commands/printServiceRolePolicies.py @@ -96,7 +96,8 @@ def main(): KEYPASS_HOST, KEYPASS_PORT) - policies = flow.getDomainRolePolicies(None, + policies, service_name, subservice_name = flow.getDomainRolePolicies( + None, SERVICE_NAME, SERVICE_ADMIN_USER, SERVICE_ADMIN_PASSWORD, diff --git a/src/orchestrator/commands/printServiceRoles.py b/src/orchestrator/commands/printServiceRoles.py index f4175476..0275f663 100755 --- a/src/orchestrator/commands/printServiceRoles.py +++ b/src/orchestrator/commands/printServiceRoles.py @@ -79,7 +79,8 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - roles = flow.roles(SERVICE_NAME, + roles, service_name, subservice_name = flow.roles( + SERVICE_NAME, None, SERVICE_ADMIN_USER, SERVICE_ADMIN_PASSWORD, diff --git a/src/orchestrator/commands/printServiceUsers.py b/src/orchestrator/commands/printServiceUsers.py index 4eb21453..b1ff7275 100755 --- a/src/orchestrator/commands/printServiceUsers.py +++ b/src/orchestrator/commands/printServiceUsers.py @@ -80,7 +80,8 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - users = flow.users(SERVICE_NAME, + users, service_name, subservice_name = flow.users( + SERVICE_NAME, None, SERVICE_ADMIN_USER, SERVICE_ADMIN_PASSWORD, diff --git a/src/orchestrator/commands/printServices.py b/src/orchestrator/commands/printServices.py index 75bc36de..afc7b5ee 100755 --- a/src/orchestrator/commands/printServices.py +++ b/src/orchestrator/commands/printServices.py @@ -79,7 +79,8 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - domain_detail = flow.domains(DOMAIN_NAME, + domain_detail, service_name, subservice_name = flow.domains( + DOMAIN_NAME, DOMAIN_ADMIN_USER, DOMAIN_ADMIN_PASSWORD, None) diff --git a/src/orchestrator/commands/printSubServices.py b/src/orchestrator/commands/printSubServices.py index 17d50925..4343d4d1 100755 --- a/src/orchestrator/commands/printSubServices.py +++ b/src/orchestrator/commands/printSubServices.py @@ -80,7 +80,8 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - subservices = flow.projects(None, + subservices, service_name, subservice_name = flow.projects( + None, SERVICE_NAME, SERVICE_ADMIN_USER, SERVICE_ADMIN_PASSWORD, diff --git a/src/orchestrator/commands/registerSubServiceDevice.py b/src/orchestrator/commands/registerSubServiceDevice.py index 8d8ff3e9..f74cdd07 100644 --- a/src/orchestrator/commands/registerSubServiceDevice.py +++ b/src/orchestrator/commands/registerSubServiceDevice.py @@ -158,7 +158,7 @@ def main(): ORION_HOST, ORION_PORT) - res = flow.register_device( + res, service_name, subservice_name = flow.register_device( SERVICE_NAME, None, SUBSERVICE_NAME, diff --git a/src/orchestrator/commands/registerSubServiceEntity.py b/src/orchestrator/commands/registerSubServiceEntity.py index 1159f114..737cb2d6 100644 --- a/src/orchestrator/commands/registerSubServiceEntity.py +++ b/src/orchestrator/commands/registerSubServiceEntity.py @@ -158,7 +158,7 @@ def main(): ORION_HOST, ORION_PORT) - res = flow.register_service( + res, service_name, subservice_name = flow.register_service( SERVICE_NAME, None, SUBSERVICE_NAME, diff --git a/src/orchestrator/commands/removeService.py b/src/orchestrator/commands/removeService.py index 4534c432..872ab1af 100755 --- a/src/orchestrator/commands/removeService.py +++ b/src/orchestrator/commands/removeService.py @@ -92,7 +92,8 @@ def main(): KEYPASS_HOST, KEYPASS_PORT) - domain_detail = flow.delete_domain(None, + domain_detail, service_name, subservice_name = flow.delete_domain( + None, SERVICE_NAME, DOMAIN_ADMIN_USER, DOMAIN_ADMIN_PASSWORD, diff --git a/src/orchestrator/commands/removeServiceGroup.py b/src/orchestrator/commands/removeServiceGroup.py index 6b9994d3..bd9944a8 100644 --- a/src/orchestrator/commands/removeServiceGroup.py +++ b/src/orchestrator/commands/removeServiceGroup.py @@ -83,7 +83,8 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - group_detail = flow.removeUser(SERVICE_NAME, + group_detail, service_name, subservice_name = flow.removeUser( + SERVICE_NAME, None, SERVICE_ADMIN_USER, SERVICE_ADMIN_PASSWORD, diff --git a/src/orchestrator/commands/removeServiceRole.py b/src/orchestrator/commands/removeServiceRole.py index 3de6c986..c279a918 100755 --- a/src/orchestrator/commands/removeServiceRole.py +++ b/src/orchestrator/commands/removeServiceRole.py @@ -83,7 +83,8 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - role_detail = flow.removeRole(SERVICE_NAME, + role_detail, service_name, subservice_name = flow.removeRole( + SERVICE_NAME, None, SERVICE_ADMIN_USER, SERVICE_ADMIN_PASSWORD, diff --git a/src/orchestrator/commands/removeServiceRolePolicy.py b/src/orchestrator/commands/removeServiceRolePolicy.py index 31d4e3af..1638ed4c 100644 --- a/src/orchestrator/commands/removeServiceRolePolicy.py +++ b/src/orchestrator/commands/removeServiceRolePolicy.py @@ -100,7 +100,7 @@ def main(): KEYPASS_HOST, KEYPASS_PORT) - role_detail = flow.removePolicyFromRole( + role_detail, service_name, subservice_name = flow.removePolicyFromRole( SERVICE_NAME, None, SERVICE_ADMIN_USER, diff --git a/src/orchestrator/commands/removeServiceUser.py b/src/orchestrator/commands/removeServiceUser.py index d446a12e..151f06ba 100755 --- a/src/orchestrator/commands/removeServiceUser.py +++ b/src/orchestrator/commands/removeServiceUser.py @@ -83,7 +83,8 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - user_detail = flow.removeUser(SERVICE_NAME, + user_detail, service_name, subservice_name = flow.removeUser( + SERVICE_NAME, None, SERVICE_ADMIN_USER, SERVICE_ADMIN_PASSWORD, diff --git a/src/orchestrator/commands/removeSubService.py b/src/orchestrator/commands/removeSubService.py index dc3f04b6..bbb0b63e 100755 --- a/src/orchestrator/commands/removeSubService.py +++ b/src/orchestrator/commands/removeSubService.py @@ -83,7 +83,8 @@ def main(): KEYSTONE_HOST, KEYSTONE_PORT) - project_detail = flow.delete_project(None, + project_detail, service_name, subservice_name = flow.delete_project( + None, SERVICE_NAME, None, SUBSERVICE_NAME, diff --git a/src/orchestrator/common/util.py b/src/orchestrator/common/util.py index c8b2d3ce..33b40309 100644 --- a/src/orchestrator/common/util.py +++ b/src/orchestrator/common/util.py @@ -28,6 +28,8 @@ import StringIO import requests import logging +import time + class RestOperations(object): ''' @@ -61,6 +63,21 @@ def __init__(self, else: self.CORRELATOR_ID = None + self.logger = logging.getLogger('orchestrator_core') + self.logger.addFilter(ContextFilterCorrelatorId(self.CORRELATOR_ID)) + self.logger.addFilter(ContextFilterTransactionId(self.TRANSACTION_ID)) + + self.service = {} + self.sum = { + "serviceTime": 0, + "serviceTimeTotal": 0, + "outgoingTransactions": 0, + "outgoingTransactionRequestSize": 0, + "outgoingTransactionResponseSize": 0, + "outgoingTransactionErrors": 0, + } + + def rest_request(self, url, method, user=None, password=None, data=None, json_data=True, relative_url=True, @@ -71,6 +88,7 @@ def rest_request(self, url, method, user=None, password=None, In case of HTTP error, the exception is returned normally instead of raised and, if JSON error data is present in the response, .msg will contain the error detail.''' + service_start = time.time() user = user or None password = password or None @@ -149,10 +167,12 @@ def rest_request(self, url, method, user=None, password=None, except Exception, e: print e except urllib2.URLError, e: + data = None res = e res.code = 500 res.msg = self.ENDPOINT_NAME + " endpoint ERROR: " + res.args[0][1] + self.collectOutgoingMetrics(service_start, request.data, request.headers, res) return res @@ -168,6 +188,7 @@ def rest_request2(self, url, method, user=None, password=None, Without SSL security ''' + service_start = time.time() user = user or None password = password or None auth = None @@ -234,9 +255,31 @@ def rest_request2(self, url, method, user=None, password=None, except Exception, e: print e + self.collectOutgoingMetrics(service_start, rdata, headers, res) return res + def collectOutgoingMetrics(self, service_start, data_request, headers_request, response): + try: + service_stop = time.time() + transactionError = False + if response.code not in [200, 201, 204]: + transactionError = True + data_response = response.msg + if transactionError: + self.sum["outgoingTransactions"] += 1 + else: + self.sum["outgoingTransactionErrors"] += 1 + self.sum["outgoingTransactionRequestSize"] += len(json.dumps(data_request)) + len(str(headers_request)) + self.sum["outgoingTransactionResponseSize"] += len(json.dumps(data_response)) + len(str(response.headers.headers)) + self.sum["serviceTimeTotal"] += (service_stop - service_start) + except Exception, ex: + self.logger.error("ERROR collecting outgoing metrics %s", ex) + + def getOutgoingMetrics(self): + return self.sum + + class CSVOperations(object): ''' @@ -298,13 +341,13 @@ def __init__(self, subservice): def filter(self, record): record.subservice = self.subservice - return True + return True class ContextFilterCorrelatorId(logging.Filter): """ This is a filter which injects contextual information into the log. - """ + """ def __init__(self, CORRELATOR_ID): self.CORRELATOR_ID = CORRELATOR_ID diff --git a/src/orchestrator/core/flow/Domains.py b/src/orchestrator/core/flow/Domains.py index fc1dfcba..27b62a5e 100644 --- a/src/orchestrator/core/flow/Domains.py +++ b/src/orchestrator/core/flow/Domains.py @@ -81,7 +81,11 @@ def domains(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return DOMAINS + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return DOMAINS, None, None def get_domain(self, DOMAIN_ID, @@ -143,7 +147,11 @@ def get_domain(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return DOMAIN + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return DOMAIN, DOMAIN_NAME, None def update_domain(self, DOMAIN_ID, @@ -210,7 +218,11 @@ def update_domain(self, "DOMAIN": DOMAIN } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return DOMAIN + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return DOMAIN, DOMAIN_NAME, None def delete_domain(self, DOMAIN_ID, @@ -351,7 +363,11 @@ def delete_domain(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return DOMAIN + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return DOMAIN, DOMAIN_NAME, None def getDomainRolePolicies(self, @@ -408,6 +424,13 @@ def getDomainRolePolicies(self, self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + # Get Role ID if not ROLE_ID and ROLE_NAME: if ROLE_NAME == "Admin": @@ -458,7 +481,11 @@ def getDomainRolePolicies(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return policies + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return policies, SERVICE_NAME, None def activate_module(self, DOMAIN_NAME, @@ -571,7 +598,11 @@ def activate_module(self, "subscriptionid": subscriptionid } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return subscriptionid + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return subscriptionid, DOMAIN_NAME, None def deactivate_module(self, DOMAIN_NAME, @@ -665,7 +696,10 @@ def deactivate_module(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return subscriptionid + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return subscriptionid, DOMAIN_NAME, None def list_activated_modules(self, @@ -742,4 +776,7 @@ def list_activated_modules(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return modules + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return modules, DOMAIN_NAME, None diff --git a/src/orchestrator/core/flow/Groups.py b/src/orchestrator/core/flow/Groups.py index e08f29e1..b370f61c 100644 --- a/src/orchestrator/core/flow/Groups.py +++ b/src/orchestrator/core/flow/Groups.py @@ -24,7 +24,7 @@ import json from orchestrator.core.flow.base import FlowBase - +from orchestrator.common.util import ContextFilterService class Groups(FlowBase): @@ -79,6 +79,13 @@ def groups(self, SERVICE_ADMIN_PASSWORD) self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + SERVICE_GROUPS = self.idm.getDomainGroups(SERVICE_ADMIN_TOKEN, SERVICE_ID, START_INDEX, @@ -95,11 +102,16 @@ def groups(self, "SERVICE_GROUPS": SERVICE_GROUPS, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return SERVICE_GROUPS + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return SERVICE_GROUPS, SERVICE_NAME, None def group(self, SERVICE_ID, + SERVICE_NAME, GROUP_ID, SERVICE_ADMIN_USER, SERVICE_ADMIN_PASSWORD, @@ -111,6 +123,7 @@ def group(self, Params: - SERVICE_ID: Service ID + - SERVICE_NAME: Service NAME - GROUP_ID: Group ID - SERVICE_ADMIN_USER: Service admin username - SERVICE_ADMIN_PASSWORD: Service admin password @@ -119,6 +132,7 @@ def group(self, ''' data_log = { "SERVICE_ID": "%s" % SERVICE_ID, + "SERVICE_NAME": "%s" % SERVICE_NAME, "GROUP_ID": "%s" % GROUP_ID, "SERVICE_ADMIN_USER": "%s" % SERVICE_ADMIN_USER, "SERVICE_ADMIN_PASSWORD": "%s" % SERVICE_ADMIN_PASSWORD, @@ -136,6 +150,13 @@ def group(self, SERVICE_ADMIN_PASSWORD) self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + DETAIL_GROUP = self.idm.detailUser(SERVICE_ADMIN_TOKEN, GROUP_ID) self.logger.debug("DETAIL_GROUP=%s" % json.dumps(DETAIL_GROUP, indent=3)) @@ -148,7 +169,11 @@ def group(self, "DETAIL_GROUP": DETAIL_GROUP, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return DETAIL_GROUP + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return DETAIL_GROUP, SERVICE_NAME, None def updateGroup(self, @@ -202,6 +227,13 @@ def updateGroup(self, SERVICE_ADMIN_PASSWORD) self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + # # 2. Get group ID # @@ -226,7 +258,10 @@ def updateGroup(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {"id": GROUP_ID} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {"id": GROUP_ID}, SERVICE_NAME, None def removeGroup(self, @@ -280,6 +315,13 @@ def removeGroup(self, SERVICE_ADMIN_PASSWORD) self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + # # 2. Get group ID # @@ -304,4 +346,92 @@ def removeGroup(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None + + def createNewServiceGroup(self, + SERVICE_NAME, + SERVICE_ID, + SERVICE_ADMIN_USER, + SERVICE_ADMIN_PASSWORD, + SERVICE_ADMIN_TOKEN, + NEW_SERVICE_GROUP_NAME, + NEW_SERVICE_GROUP_DESCRIPTION): + '''Creates a new group Service (aka domain group keystone). + + In case of HTTP error, return HTTP error + + Params: + - SERVICE_NAME: Service name + - SERVICE_ID: Service Id + - SERVICE_ADMIN_USER: Service admin username + - SERVICE_ADMIN_PASSWORD: Service admin password + - SERVICE_ADMIN_TOKEN: Service admin token + - NEW_SERVICE_GROUP_NAME: New group name (required) + - NEW_SERVICE_GROUP_DESCRIPTION: New group description (optional) + Return: + - id: New group Id + ''' + data_log = { + "SERVICE_NAME": "%s" % SERVICE_NAME, + "SERVICE_ID": "%s" % SERVICE_ID, + "SERVICE_ADMIN_USER": "%s" % SERVICE_ADMIN_USER, + "SERVICE_ADMIN_PASSWORD": "%s" % SERVICE_ADMIN_PASSWORD, + "SERVICE_ADMIN_TOKEN": self.get_extended_token(SERVICE_ADMIN_TOKEN), + "NEW_SERVICE_GROUP_NAME": "%s" % NEW_SERVICE_GROUP_NAME, + "NEW_SERVICE_GROUP_DESCRIPTION": "%s" % NEW_SERVICE_GROUP_DESCRIPTION + } + self.logger.debug("FLOW createNewServiceGroup invoked with: %s" % json.dumps( + data_log, + indent=3) + ) + try: + if not SERVICE_ADMIN_TOKEN: + SERVICE_ADMIN_TOKEN = self.idm.getToken(SERVICE_NAME, + SERVICE_ADMIN_USER, + SERVICE_ADMIN_PASSWORD) + self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + + # + # 1. Get service (aka domain) + # + if not SERVICE_ID: + SERVICE_ID = self.idm.getDomainId(SERVICE_ADMIN_TOKEN, + SERVICE_NAME) + + self.logger.debug("ID of your service %s:%s" % (SERVICE_NAME, + SERVICE_ID)) + + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + + # + # 2. Create group + # + ID_GROUP = self.idm.createGroupDomain(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME, + NEW_SERVICE_GROUP_NAME, + NEW_SERVICE_GROUP_DESCRIPTION) + self.logger.debug("ID of group %s: %s" % (NEW_SERVICE_GROUP_NAME, ID_GROUP)) + + except Exception, ex: + self.logger.error(ex) + return self.composeErrorCode(ex) + + data_log = { + "SERVICE_ID": "%s" % SERVICE_ID, + "ID_GROUP": "%s" % ID_GROUP, + } + self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {"id": ID_GROUP}, SERVICE_NAME, None diff --git a/src/orchestrator/core/flow/Projects.py b/src/orchestrator/core/flow/Projects.py index 0bb57be9..4665f072 100644 --- a/src/orchestrator/core/flow/Projects.py +++ b/src/orchestrator/core/flow/Projects.py @@ -77,6 +77,12 @@ def projects(self, ADMIN_PASSWORD) self.logger.debug("ADMIN_TOKEN=%s" % ADMIN_TOKEN) + # Ensure DOMAIN_NAME and PROJECT_NAME + DOMAIN_NAME = self.ensure_service_name(ADMIN_TOKEN, + DOMAIN_ID, + DOMAIN_NAME) + self.logger.addFilter(ContextFilterService(DOMAIN_NAME)) + PROJECTS = self.idm.getDomainProjects(ADMIN_TOKEN, DOMAIN_ID) @@ -91,10 +97,14 @@ def projects(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return PROJECTS + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return PROJECTS, DOMAIN_NAME, None def get_project(self, DOMAIN_ID, + DOMAIN_NAME, PROJECT_ID, ADMIN_USER, ADMIN_PASSWORD, @@ -106,6 +116,7 @@ def get_project(self, Params: - DOMAIN_ID: id of domain + - DOMAIN_NAME: name of domain - PROJECT_ID: id of project - SERVICE_ADMIN_USER: Service admin username - SERVICE_ADMIN_PASSWORD: Service admin password @@ -115,6 +126,7 @@ def get_project(self, ''' data_log = { "DOMAIN_ID": "%s" % DOMAIN_ID, + "DOMAIN_NAME": "%s" % DOMAIN_NAME, "PROJECT_ID": "%s" % PROJECT_ID, "ADMIN_USER": "%s" % ADMIN_USER, "ADMIN_PASSWORD": "%s" % ADMIN_PASSWORD, @@ -131,8 +143,20 @@ def get_project(self, ADMIN_PASSWORD) self.logger.debug("ADMIN_TOKEN=%s" % ADMIN_TOKEN) + # Ensure DOMAIN_NAME and PROJECT_NAME + DOMAIN_NAME = self.ensure_service_name(ADMIN_TOKEN, + DOMAIN_ID, + DOMAIN_NAME) + self.logger.addFilter(ContextFilterService(DOMAIN_NAME)) + PROJECT = self.idm.getProject(ADMIN_TOKEN, PROJECT_ID) + + PROJECT_NAME = self.ensure_subservice_name(ADMIN_TOKEN, + DOMAIN_ID, + PROJECT_ID, + None) + self.logger.addFilter(ContextFilterSubService(PROJECT_NAME)) # PROJECTS = self.idm.getDomainProjects(ADMIN_TOKEN, # DOMAIN_ID) # for project in PROJECTS: @@ -149,7 +173,11 @@ def get_project(self, "PROJECT": PROJECT } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return PROJECT + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return PROJECT, DOMAIN_NAME, PROJECT_NAME def update_project(self, DOMAIN_ID, @@ -205,10 +233,21 @@ def update_project(self, ADMIN_PASSWORD) self.logger.debug("ADMIN_TOKEN=%s" % ADMIN_TOKEN) + # Ensure DOMAIN_NAME and PROJECT_NAME + DOMAIN_NAME = self.ensure_service_name(ADMIN_TOKEN, + DOMAIN_ID, + DOMAIN_NAME) + self.logger.addFilter(ContextFilterService(DOMAIN_NAME)) + if not PROJECT_ID: PROJECT_ID = self.idm.getProjectId(ADMIN_TOKEN, DOMAIN_NAME, PROJECT_NAME) + PROJECT_NAME = self.ensure_subservice_name(ADMIN_TOKEN, + DOMAIN_ID, + PROJECT_ID, + PROJECT_NAME) + self.logger.addFilter(ContextFilterSubService(PROJECT_NAME)) PROJECT = self.idm.updateProject(ADMIN_TOKEN, DOMAIN_ID, @@ -225,7 +264,11 @@ def update_project(self, "PROJECT": PROJECT } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return PROJECT + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return PROJECT, DOMAIN_NAME, PROJECT_NAME def delete_project(self, DOMAIN_ID, @@ -346,7 +389,11 @@ def delete_project(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return PROJECT + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return PROJECT, DOMAIN_NAME, PROJECT_NAME def register_service(self, @@ -755,7 +802,11 @@ def register_service(self, "subscriptionid_sth": subscriptionid_sth, "subscriptionid_perseo": subscriptionid_perseo } - return result + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return result, DOMAIN_NAME, PROJECT_NAME def register_device(self, @@ -1085,7 +1136,11 @@ def register_device(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return DEVICE_ID + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return DEVICE_ID, DOMAIN_NAME, PROJECT_NAME def register_devices(self, @@ -1215,7 +1270,11 @@ def register_devices(self, "devices": DEVICES_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return DEVICES_ID + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return DEVICES_ID, DOMAIN_NAME, PROJECT_NAME def unregister_device(self, DOMAIN_NAME, @@ -1310,7 +1369,11 @@ def unregister_device(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, DOMAIN_NAME, PROJECT_NAME def activate_module(self, @@ -1451,7 +1514,11 @@ def activate_module(self, "subscriptionid": subscriptionid } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return subscriptionid + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return subscriptionid, DOMAIN_NAME, PROJECT_NAME def deactivate_module(self, DOMAIN_NAME, @@ -1565,7 +1632,10 @@ def deactivate_module(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return subscriptionid + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return subscriptionid, DOMAIN_NAME, PROJECT_NAME def list_activated_modules(self, @@ -1664,4 +1734,7 @@ def list_activated_modules(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return modules + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return modules, DOMAIN_NAME, PROJECT_NAME diff --git a/src/orchestrator/core/flow/Roles.py b/src/orchestrator/core/flow/Roles.py index 4b9e3eeb..6a8d03aa 100644 --- a/src/orchestrator/core/flow/Roles.py +++ b/src/orchestrator/core/flow/Roles.py @@ -85,6 +85,13 @@ def roles(self, ADMIN_PASSWORD) self.logger.debug("ADMIN_TOKEN=%s" % ADMIN_TOKEN) + # Ensure DOMAIN_NAME + DOMAIN_NAME = self.ensure_service_name(ADMIN_TOKEN, + DOMAIN_ID, + DOMAIN_NAME) + self.logger.addFilter(ContextFilterService(DOMAIN_NAME)) + self.logger.debug("DOMAIN_NAME=%s" % DOMAIN_NAME) + DOMAIN_ROLES = self.idm.getDomainRoles(ADMIN_TOKEN, DOMAIN_ID, START_INDEX, @@ -104,7 +111,10 @@ def roles(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return ROLES + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return ROLES, DOMAIN_NAME, None def roles_assignments(self, DOMAIN_ID, @@ -289,7 +299,11 @@ def roles_assignments(self, "role_assignments": role_assignments_expanded, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {"role_assignments": role_assignments_expanded} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {"role_assignments": role_assignments_expanded}, DOMAIN_NAME, PROJECT_NAME def assignRoleServiceUser(self, SERVICE_NAME, @@ -418,7 +432,11 @@ def assignRoleServiceUser(self, "ROLE_ID": "%s" % ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def assignRoleSubServiceUser(self, SERVICE_NAME, @@ -488,6 +506,8 @@ def assignRoleSubServiceUser(self, SERVICE_NAME = self.idm.getDomainNameFromToken(SERVICE_ADMIN_TOKEN, SERVICE_ID) self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + # # 1. Get service (aka domain) # @@ -548,7 +568,11 @@ def assignRoleSubServiceUser(self, "ROLE_ID": "%s" % ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, SUBSERVICE_NAME def assignInheritRoleServiceUser(self, SERVICE_NAME, @@ -654,7 +678,11 @@ def assignInheritRoleServiceUser(self, "INHERIT_ROLE_ID": "%s" % INHERIT_ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def revokeRoleServiceUser(self, SERVICE_NAME, @@ -763,7 +791,11 @@ def revokeRoleServiceUser(self, "ROLE_ID": "%s" % ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def revokeRoleSubServiceUser(self, SERVICE_NAME, @@ -887,7 +919,11 @@ def revokeRoleSubServiceUser(self, "ROLE_ID": "%s" % ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, SUBSERVICE_NAME def revokeInheritRoleServiceUser(self, SERVICE_NAME, @@ -992,7 +1028,11 @@ def revokeInheritRoleServiceUser(self, "INHERIT_ROLE_ID": "%s" % INHERIT_ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def roles_assignments_groups(self, @@ -1176,7 +1216,11 @@ def roles_assignments_groups(self, "role_assignments": role_assignments_expanded, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {"role_assignments": role_assignments_expanded} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {"role_assignments": role_assignments_expanded}, DOMAIN_NAME, PROJECT_NAME def assignRoleServiceGroup(self, @@ -1306,7 +1350,11 @@ def assignRoleServiceGroup(self, "ROLE_ID": "%s" % ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def assignRoleSubServiceGroup(self, @@ -1377,6 +1425,8 @@ def assignRoleSubServiceGroup(self, SERVICE_NAME = self.idm.getDomainNameFromToken(SERVICE_ADMIN_TOKEN, SERVICE_ID) self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + # # 1. Get service (aka domain) # @@ -1437,7 +1487,11 @@ def assignRoleSubServiceGroup(self, "ROLE_ID": "%s" % ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, SUBSERVICE_NAME def assignInheritRoleServiceGroup(self, @@ -1544,7 +1598,11 @@ def assignInheritRoleServiceGroup(self, "INHERIT_ROLE_ID": "%s" % INHERIT_ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def revokeRoleServiceGroup(self, @@ -1654,7 +1712,11 @@ def revokeRoleServiceGroup(self, "ROLE_ID": "%s" % ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def revokeRoleSubServiceGroup(self, @@ -1779,7 +1841,11 @@ def revokeRoleSubServiceGroup(self, "ROLE_ID": "%s" % ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, SUBSERVICE_NAME def revokeInheritRoleServiceGroup(self, SERVICE_NAME, @@ -1884,7 +1950,11 @@ def revokeInheritRoleServiceGroup(self, "INHERIT_ROLE_ID": "%s" % INHERIT_ROLE_ID } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def removeRole(self, @@ -1968,7 +2038,10 @@ def removeRole(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def setPolicyRole(self, SERVICE_NAME, @@ -2073,7 +2146,10 @@ def setPolicyRole(self, self.logger.error(ex) return self.composeErrorCode(ex) - return {} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def removePolicyFromRole(self, @@ -2134,6 +2210,7 @@ def removePolicyFromRole(self, SERVICE_ID, SERVICE_NAME) self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) # # 2. Get Role ID @@ -2180,7 +2257,10 @@ def removePolicyFromRole(self, self.logger.error(ex) return self.composeErrorCode(ex) - return {} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None def getPolicyFromRole(self, @@ -2235,10 +2315,12 @@ def getPolicyFromRole(self, SERVICE_ADMIN_PASSWORD) self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + # Ensure SERVICE_NAME SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, SERVICE_ID, SERVICE_NAME) self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) # # 2. Get Role ID @@ -2285,4 +2367,7 @@ def getPolicyFromRole(self, self.logger.error(ex) return self.composeErrorCode(ex) - return {} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None diff --git a/src/orchestrator/core/flow/Users.py b/src/orchestrator/core/flow/Users.py index 82717ccd..b55ca52f 100644 --- a/src/orchestrator/core/flow/Users.py +++ b/src/orchestrator/core/flow/Users.py @@ -24,7 +24,7 @@ import json from orchestrator.core.flow.base import FlowBase - +from orchestrator.common.util import ContextFilterService class Users(FlowBase): @@ -79,6 +79,13 @@ def users(self, SERVICE_ADMIN_PASSWORD) self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + # SERVICE_ROLES = self.idm.getDomainRoles(SERVICE_ADMIN_TOKEN, # SERVICE_ID) @@ -108,11 +115,16 @@ def users(self, "SERVICE_USERS": SERVICE_USERS, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return SERVICE_USERS + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return SERVICE_USERS, SERVICE_NAME, None def user(self, SERVICE_ID, + SERVICE_NAME, USER_ID, SERVICE_ADMIN_USER, SERVICE_ADMIN_PASSWORD, @@ -124,6 +136,7 @@ def user(self, Params: - SERVICE_ID: Service ID + - SERVICE_NAME: Service NAME - USER_ID: User ID - SERVICE_ADMIN_USER: Service admin username - SERVICE_ADMIN_PASSWORD: Service admin password @@ -132,6 +145,7 @@ def user(self, ''' data_log = { "SERVICE_ID": "%s" % SERVICE_ID, + "SERVICE_NAME": "%s" % SERVICE_NAME, "USER_ID": "%s" % USER_ID, "SERVICE_ADMIN_USER": "%s" % SERVICE_ADMIN_USER, "SERVICE_ADMIN_PASSWORD": "%s" % SERVICE_ADMIN_PASSWORD, @@ -149,6 +163,13 @@ def user(self, SERVICE_ADMIN_PASSWORD) self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + DETAIL_USER = self.idm.detailUser(SERVICE_ADMIN_TOKEN, USER_ID) self.logger.debug("DETAIL_USER=%s" % json.dumps(DETAIL_USER, indent=3)) @@ -161,4 +182,8 @@ def user(self, "DETAIL_USER": DETAIL_USER, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return DETAIL_USER + + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return DETAIL_USER, SERVICE_NAME, None diff --git a/src/orchestrator/core/flow/base.py b/src/orchestrator/core/flow/base.py index 888ced9c..20bf858d 100644 --- a/src/orchestrator/core/flow/base.py +++ b/src/orchestrator/core/flow/base.py @@ -59,7 +59,7 @@ def __init__(self, CA_PORT="9999", PERSEO_PROTOCOL="http", PERSEO_HOST="localhost", - PERSEO_PORT="9090", + PERSEO_PORT="9090", TRANSACTION_ID=None, CORRELATOR_ID=None): @@ -120,6 +120,15 @@ def __init__(self, self.endpoints['CA'] = \ CA_PROTOCOL + "://"+CA_HOST+":"+CA_PORT+""+"/v1/notifyGeolocation" + self.sum = { + "serviceTime": 0, + "serviceTimeTotal": 0, + "outgoingTransactions": 0, + "outgoingTransactionRequestSize": 0, + "outgoingTransactionResponseSize": 0, + "outgoingTransactionErrors": 0, + } + def composeErrorCode(self, ex): ''' @@ -142,7 +151,7 @@ def composeErrorCode(self, ex): if res['code'] == 400 and len(ex.message) > 1 and \ ex.message[1].startswith('SPASSWORD'): res['error'] = ex.message[1] - return res + return res, None, None def get_endpoint_iot_module(self, iot_module): @@ -236,3 +245,18 @@ def get_extended_token(self, USER_TOKEN): "error": ex.message } return token_extended + + def collectComponentMetrics(self): + all = [] + try: + all.append(self.idm.IdMRestOperations.getOutgoingMetrics()) + all.append(self.ac.AccessControlRestOperations.getOutgoingMetrics()) + all.append(self.iota.IoTACppRestOperations.getOutgoingMetrics()) + all.append(self.cb.CBRestOperations.getOutgoingMetrics()) + all.append(self.perseo.PerseoRestOperations.getOutgoingMetrics()) + self.sum = reduce(lambda x, y: dict((k, v + y[k]) for k, v in x.iteritems()), all) + except Exception, ex: + self.logger.error("ERROR collecting component metrics %s", ex) + + def getFlowMetrics(self): + return self.sum diff --git a/src/orchestrator/core/flow/createNewService.py b/src/orchestrator/core/flow/createNewService.py index f022b5c6..3351231a 100644 --- a/src/orchestrator/core/flow/createNewService.py +++ b/src/orchestrator/core/flow/createNewService.py @@ -336,7 +336,10 @@ def createNewService(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + return { "token": NEW_SERVICE_ADMIN_TOKEN, "id": ID_DOM1, - } + }, DOMAIN_NAME, None diff --git a/src/orchestrator/core/flow/createNewServiceRole.py b/src/orchestrator/core/flow/createNewServiceRole.py index 828f8913..ff257802 100644 --- a/src/orchestrator/core/flow/createNewServiceRole.py +++ b/src/orchestrator/core/flow/createNewServiceRole.py @@ -88,7 +88,6 @@ def createNewServiceRole(self, SERVICE_ID, SERVICE_NAME) self.logger.addFilter(ContextFilterService(SERVICE_NAME)) - self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) # @@ -138,4 +137,7 @@ def createNewServiceRole(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {"id": ID_ROLE} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {"id": ID_ROLE}, SERVICE_NAME, None diff --git a/src/orchestrator/core/flow/createNewServiceUser.py b/src/orchestrator/core/flow/createNewServiceUser.py index 190788d3..966c0f93 100644 --- a/src/orchestrator/core/flow/createNewServiceUser.py +++ b/src/orchestrator/core/flow/createNewServiceUser.py @@ -117,4 +117,7 @@ def createNewServiceUser(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {"id": ID_USER} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {"id": ID_USER}, SERVICE_NAME, None diff --git a/src/orchestrator/core/flow/createNewSubService.py b/src/orchestrator/core/flow/createNewSubService.py index a8980078..f09b68fb 100644 --- a/src/orchestrator/core/flow/createNewSubService.py +++ b/src/orchestrator/core/flow/createNewSubService.py @@ -163,4 +163,7 @@ def createNewSubService(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {"id": ID_PRO1} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {"id": ID_PRO1}, SERVICE_NAME, NEW_SUBSERVICE_NAME diff --git a/src/orchestrator/core/flow/createTrustToken.py b/src/orchestrator/core/flow/createTrustToken.py index be2a62d3..695ec1ff 100644 --- a/src/orchestrator/core/flow/createTrustToken.py +++ b/src/orchestrator/core/flow/createTrustToken.py @@ -187,7 +187,10 @@ def createTrustToken(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {"id": ID_TRUST} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {"id": ID_TRUST}, SERVICE_NAME, SUBSERVICE_NAME diff --git a/src/orchestrator/core/flow/removeUser.py b/src/orchestrator/core/flow/removeUser.py index d852b0f0..0e42d43c 100644 --- a/src/orchestrator/core/flow/removeUser.py +++ b/src/orchestrator/core/flow/removeUser.py @@ -24,7 +24,7 @@ import json from orchestrator.core.flow.base import FlowBase - +from orchestrator.common.util import ContextFilterService class RemoveUser(FlowBase): @@ -79,6 +79,13 @@ def removeUser(self, SERVICE_ADMIN_PASSWORD) self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + # # 2. Get user ID # @@ -106,4 +113,7 @@ def removeUser(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {}, SERVICE_NAME, None diff --git a/src/orchestrator/core/flow/updateUser.py b/src/orchestrator/core/flow/updateUser.py index bd4d966b..cf99bad9 100644 --- a/src/orchestrator/core/flow/updateUser.py +++ b/src/orchestrator/core/flow/updateUser.py @@ -24,7 +24,7 @@ import json from orchestrator.core.flow.base import FlowBase - +from orchestrator.common.util import ContextFilterService class UpdateUser(FlowBase): @@ -82,6 +82,13 @@ def updateUser(self, SERVICE_ADMIN_PASSWORD) self.logger.debug("SERVICE_ADMIN_TOKEN=%s" % SERVICE_ADMIN_TOKEN) + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_ADMIN_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) + # # 2. Get user ID # @@ -107,7 +114,10 @@ def updateUser(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {"id": USER_ID} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {"id": USER_ID}, SERVICE_NAME, None def changeUserPassword(self, @@ -164,6 +174,12 @@ def changeUserPassword(self, SCOPED=False) self.logger.debug("SERVICE_USER_TOKEN=%s" % SERVICE_USER_TOKEN) + # Ensure SERVICE_NAME + SERVICE_NAME = self.ensure_service_name(SERVICE_USER_TOKEN, + SERVICE_ID, + SERVICE_NAME) + self.logger.addFilter(ContextFilterService(SERVICE_NAME)) + self.logger.debug("SERVICE_NAME=%s" % SERVICE_NAME) # # 2. Get user ID # @@ -189,4 +205,7 @@ def changeUserPassword(self, } self.logger.info("Summary report : %s" % json.dumps(data_log, indent=3)) - return {"id": USER_ID} + # Consolidate opetions metrics into flow metrics + self.collectComponentMetrics() + + return {"id": USER_ID}, SERVICE_NAME, None diff --git a/src/tests/api/test_api.py b/src/tests/api/test_api.py index 42ccb861..f43d3175 100644 --- a/src/tests/api/test_api.py +++ b/src/tests/api/test_api.py @@ -3259,7 +3259,49 @@ def test_change_log_level_bad(self): assert res.code == 400, (res.code, res.msg, res.raw_json) +class Test_Metrics_RestView(object): + def __init__(self): + self.suffix = str(uuid.uuid4())[:8] + self.TestRestOps = TestRestOperations(PROTOCOL=ORC_PROTOCOL, + HOST=ORC_HOST, + PORT=ORC_PORT) + + def test_get_metrics_ok(self): + res = self.TestRestOps.rest_request(method="GET", + url="/v1.0/admin/metrics", + json_data=True, + auth_token=None, + data=None) + assert res.code == 200, (res.code, res.msg, res.raw_json) + response = res.read() + json_body_response = json.loads(response) + assert "service" in json_body_response + assert "sum" in json_body_response + + def test_get_and_reset_metrics_ok(self): + res = self.TestRestOps.rest_request(method="GET", + url="/v1.0/admin/metrics?reset=true", + json_data=True, + auth_token=None, + data=None) + assert res.code == 200, (res.code, res.msg, res.raw_json) + response = res.read() + json_body_response = json.loads(response) + assert "service" in json_body_response + assert "sum" in json_body_response + + def test_reset_metrics_ok(self): + res = self.TestRestOps.rest_request(method="DELETE", + url="/v1.0/admin/metrics", + json_data=True, + auth_token=None, + data=None) + assert res.code == 204, (res.code, res.msg, res.raw_json) + response = res.read() + json_body_response = json.loads(response) + assert "service" in json_body_response + assert "sum" in json_body_response if __name__ == '__main__': @@ -3401,3 +3443,8 @@ def test_change_log_level_bad(self): test_LogLevel = Test_LogLevel_RestView() test_LogLevel.test_change_log_level_ok() test_LogLevel.test_change_log_level_bad() + + test_Metrics = Test_Metrics_RestView() + test_Metrics.test_get_metrics_ok() + test_Metrics.test_get_and_reset_metrics_ok() + test_Metrics.test_reset_metrics_ok()