diff options
Diffstat (limited to 'components/pm-subscription-handler/pmsh_service/mod/api')
3 files changed, 98 insertions, 3 deletions
diff --git a/components/pm-subscription-handler/pmsh_service/mod/api/controller.py b/components/pm-subscription-handler/pmsh_service/mod/api/controller.py index aee6df0e..d887187a 100755 --- a/components/pm-subscription-handler/pmsh_service/mod/api/controller.py +++ b/components/pm-subscription-handler/pmsh_service/mod/api/controller.py @@ -1,5 +1,5 @@ # ============LICENSE_START=================================================== -# Copyright (C) 2019-2021 Nordix Foundation. +# Copyright (C) 2019-2022 Nordix Foundation. # ============================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -147,3 +147,42 @@ def get_meas_group_with_nfs(subscription_name, measurement_group_name): f'{exception}') return {'error': 'Request was not processed due to Exception : ' f'{exception}'}, HTTPStatus.INTERNAL_SERVER_ERROR.value + + +def delete_subscription_by_name(subscription_name): + """ Deletes the subscription by name + + Args: + subscription_name (String): Name of the subscription + + Returns: + NoneType, HTTPStatus: None, 204 + dict, HTTPStatus: subscription not defined, 404 + dict, HTTPStatus: Reason for not deleting subscription, 409 + dict, HTTPStatus: Exception details of failure, 500 + """ + logger.info(f'API call received to delete subscription by name: {subscription_name}') + try: + unlocked_locking_mgs = \ + subscription_service.query_unlocked_mg_by_sub_name(subscription_name) + if not unlocked_locking_mgs: + if subscription_service.query_to_delete_subscription_by_name(subscription_name) == 1: + return None, HTTPStatus.NO_CONTENT + else: + logger.error(f'Subscription is not defined with name {subscription_name}') + return {'error': f'Subscription is not defined with name {subscription_name}'}, \ + HTTPStatus.NOT_FOUND.value + else: + logger.error('Subscription is not deleted due to associated MGs were UNLOCKED ' + '(or) under update process to LOCKED') + return {'error': 'Subscription is not deleted due to the following MGs were UNLOCKED ' + '(or) under update process to LOCKED', 'measurementGroupNames': + [{'measurementGroupName': + mg.measurement_group_name}for mg in unlocked_locking_mgs]}, \ + HTTPStatus.CONFLICT.value + except Exception as exception: + logger.error(f'Try again, subscription with name {subscription_name}' + f'is not deleted due to following exception: {exception}') + return {'error': f'Try again, subscription with name {subscription_name}' + f'is not deleted due to following exception: {exception}'}, \ + HTTPStatus.INTERNAL_SERVER_ERROR.value diff --git a/components/pm-subscription-handler/pmsh_service/mod/api/pmsh_swagger.yml b/components/pm-subscription-handler/pmsh_service/mod/api/pmsh_swagger.yml index 4dd1cc70..3319b7ef 100644 --- a/components/pm-subscription-handler/pmsh_service/mod/api/pmsh_swagger.yml +++ b/components/pm-subscription-handler/pmsh_service/mod/api/pmsh_swagger.yml @@ -1,5 +1,5 @@ # ============LICENSE_START======================================================= -# Copyright (C) 2020-2021 Nordix Foundation. +# Copyright (C) 2020-2022 Nordix Foundation. # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -109,6 +109,27 @@ paths: 500: description: Exception occurred while querying database + delete: + description: Deletes the Subscription from PMSH specified by Name + operationId: mod.api.controller.delete_subscription_by_name + parameters: + - name: subscription_name + in: path + required: true + description: The name of the subscription to delete + type: string + responses: + 204: + description: Successfully deleted the subscription and returns NO Content + 404: + description: Subscription with the specified name not found + 409: + description: Subscription could not be deleted as it contains measurement groups + with state UNLOCKED OR state change to LOCKED was under process + 500: + description: Exception occurred on the server + + /subscription/{subscription_name}/measurementGroups/{measurement_group_name}: get: description: Get the measurement group and associated network functions diff --git a/components/pm-subscription-handler/pmsh_service/mod/api/services/subscription_service.py b/components/pm-subscription-handler/pmsh_service/mod/api/services/subscription_service.py index d9f44001..7b31de50 100644 --- a/components/pm-subscription-handler/pmsh_service/mod/api/services/subscription_service.py +++ b/components/pm-subscription-handler/pmsh_service/mod/api/services/subscription_service.py @@ -18,7 +18,7 @@ from mod import db, logger from mod.api.db_models import SubscriptionModel, NfSubRelationalModel, \ - NetworkFunctionFilterModel, NetworkFunctionModel + NetworkFunctionFilterModel, NetworkFunctionModel, MeasurementGroupModel from mod.api.services import measurement_group_service, nf_service from mod.api.custom_exception import InvalidDataException, DuplicateDataException from mod.subscription import AdministrativeState, SubNfState @@ -345,3 +345,38 @@ def get_subscriptions_list(): (len(subscription.measurement_groups) != 0): subscriptions_list.append(subscription.serialize()) return subscriptions_list + + +def query_unlocked_mg_by_sub_name(subscription_name): + """ + Queries the db for unlocked/locking measurement groups by subscription name + + Args: + subscription_name (String): Name of the Subscription + + Returns: + list (MeasurementGroupModel): If measurement groups with admin state + UNLOCKED exists else empty list + """ + logger.info(f'Attempting to fetch measurement groups by subscription name: {subscription_name}') + mg_model = db.session.query(MeasurementGroupModel) \ + .filter_by(subscription_name=subscription_name) \ + .filter(MeasurementGroupModel.administrative_state.in_(('UNLOCKED', 'LOCKING'))).all() + db.session.remove() + return mg_model + + +def query_to_delete_subscription_by_name(subscription_name): + """ + Deletes the subscription by name + + Args: + subscription_name (String): Name of the Subscription + + Returns: + int: Returns '1' if subscription exists and deleted successfully else '0' + """ + effected_rows = db.session.query(SubscriptionModel) \ + .filter_by(subscription_name=subscription_name).delete() + db.session.commit() + return effected_rows |