summaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
Diffstat (limited to 'components')
-rwxr-xr-xcomponents/pm-subscription-handler/Changelog.md1
-rwxr-xr-xcomponents/pm-subscription-handler/pmsh_service/mod/api/controller.py42
-rw-r--r--components/pm-subscription-handler/pmsh_service/mod/api/pmsh_swagger.yml19
-rw-r--r--components/pm-subscription-handler/pmsh_service/mod/api/services/subscription_service.py37
-rwxr-xr-xcomponents/pm-subscription-handler/tests/base_setup.py36
-rw-r--r--components/pm-subscription-handler/tests/services/test_subscription_service.py19
-rwxr-xr-xcomponents/pm-subscription-handler/tests/test_controller.py47
7 files changed, 173 insertions, 28 deletions
diff --git a/components/pm-subscription-handler/Changelog.md b/components/pm-subscription-handler/Changelog.md
index f02fc5eb..89237c35 100755
--- a/components/pm-subscription-handler/Changelog.md
+++ b/components/pm-subscription-handler/Changelog.md
@@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
* Implemented Create Subscription public API (DCAEGEN2-2819)
* Added 2 new attributes to the subscription model (DCAEGEN2-2913)
* Read subscription API by using subscription name (DCAEGEN2-2818)
+* Read All subscriptions API (DCAEGEN2-2847)
## [1.3.2]
### Changed
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 80d86362..e852324d 100755
--- a/components/pm-subscription-handler/pmsh_service/mod/api/controller.py
+++ b/components/pm-subscription-handler/pmsh_service/mod/api/controller.py
@@ -75,24 +75,40 @@ def get_subscription_by_name(subscription_name):
subscription_name (String): Name of the subscription.
Returns:
- success: dict of single Subscription, 200
- None: subscription not defined, 404
- Exception: Details about exception, 500
+ dict, HTTPStatus: single Sub in PMSH, 200
+ dict, HTTPStatus: subscription not defined, 404
+ dict, HTTPStatus: Exception details of failure, 500
"""
logger.info('API call received to fetch subscription by name')
try:
- subscription = subscription_service.get_subscription_by_name(subscription_name)
+ subscription = subscription_service.query_subscription_by_name(subscription_name)
if subscription is not None:
- logger.info(f'subscription object with the name "{subscription_name}" '
- 'was fetched successfully from database')
- return subscription.serialize(), HTTPStatus.OK
+ logger.info(f'queried subscription was successful with the name: {subscription_name}')
+ return subscription.serialize(), HTTPStatus.OK.value
else:
- logger.error(f'subscription object with the name "{subscription_name}" '
- 'was un successful to fetch from database')
+ logger.error('queried subscription was un successful with the name: '
+ f'{subscription_name}')
return {'error': 'Subscription was not defined with the name : '
- f'{subscription_name}'}, HTTPStatus.NOT_FOUND
+ f'{subscription_name}'}, HTTPStatus.NOT_FOUND.value
except Exception as exception:
- logger.error(f'The following exception occurred "{exception}" while fetching subscription '
- f'with the name "{subscription_name}"')
+ logger.error(f'While querying the subscription with name: {subscription_name}, '
+ f'it occurred the following exception "{exception}"')
return {'error': 'Request was not processed due to Exception : '
- f'{exception}'}, HTTPStatus.INTERNAL_SERVER_ERROR
+ f'{exception}'}, HTTPStatus.INTERNAL_SERVER_ERROR.value
+
+
+def get_subscriptions():
+ """ Retrieves all the subscriptions that are defined in PMSH.
+
+ Returns:
+ list (dict), HTTPStatus: All subs in PMSH, 200
+ dict, HTTPStatus: Exception details of failure, 500
+ """
+ logger.info('API call received to fetch all subscriptions')
+ try:
+ subscriptions = subscription_service.get_subscriptions_list()
+ return subscriptions, HTTPStatus.OK.value
+ except Exception as exception:
+ logger.error(f'The following exception occurred while fetching subscriptions: {exception}')
+ return {'error': 'Request was not processed due to Exception : '
+ f'{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 f27fb7ab..39f6dc3b 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
@@ -72,9 +72,24 @@ paths:
400:
description: Invalid input
+ get:
+ description: Get all the subscriptions from PMSH.
+ operationId: mod.api.controller.get_subscriptions
+ tags:
+ - "Subscription"
+ responses:
+ 200:
+ description: OK; Array of subscriptions are returned else empty if not found
+ schema:
+ type: array
+ items:
+ $ref: "#/definitions/subscription"
+ 500:
+ description: Exception occurred while querying database
+
/subscription/{subscription_name}:
get:
- description: Get the Subscription from ONAP specified by Name
+ description: Get the Subscription from PMSH specified by Name
operationId: mod.api.controller.get_subscription_by_name
tags:
- "Subscription"
@@ -92,7 +107,7 @@ paths:
404:
description: Subscription with specified name not found
500:
- description: Exception occurs while querying database
+ description: Exception occurred while querying database
definitions:
subscription:
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 c41bb18b..96d50c27 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
@@ -292,9 +292,9 @@ def save_nf_filter(nf_filter, subscription_name):
db.session.add(new_filter)
-def get_subscription_by_name(subscription_name):
+def query_subscription_by_name(subscription_name):
"""
- Retrieves the subscription information by name
+ Queries the db for existing subscription by name
Args:
subscription_name (String): Name of the Subscription
@@ -309,3 +309,36 @@ def get_subscription_by_name(subscription_name):
.filter_by(subscription_name=subscription_name).first()
db.session.remove()
return subscription_model
+
+
+def query_all_subscriptions():
+ """
+ Queries the db for all existing subscriptions defined in PMSH
+
+ Returns
+ list (SubscriptionModel): of all subscriptions else None
+ """
+ logger.info('Attempting to fetch all the subscriptions')
+ subscriptions = db.session.query(SubscriptionModel) \
+ .options(joinedload(SubscriptionModel.network_filter),
+ joinedload(SubscriptionModel.measurement_groups)) \
+ .all()
+ db.session.remove()
+ return subscriptions
+
+
+def get_subscriptions_list():
+ """ Converts all subscriptions to JSON and appends to list
+
+ Returns
+ list: dict of all subscriptions else empty
+ """
+ subscriptions = query_all_subscriptions()
+ subscriptions_list = []
+ if subscriptions is not None:
+ logger.info('Queried all the subscriptions was successful')
+ for subscription in subscriptions:
+ if (subscription.network_filter is not None) and \
+ (len(subscription.measurement_groups) != 0):
+ subscriptions_list.append(subscription.serialize())
+ return subscriptions_list
diff --git a/components/pm-subscription-handler/tests/base_setup.py b/components/pm-subscription-handler/tests/base_setup.py
index 4328f59c..be7d1b8f 100755
--- a/components/pm-subscription-handler/tests/base_setup.py
+++ b/components/pm-subscription-handler/tests/base_setup.py
@@ -29,11 +29,29 @@ from mod.pmsh_config import AppConfig as NewAppConfig
def get_pmsh_config(file_path='data/cbs_data_1.json'):
+ """
+ Gets PMSH config from the JSON file
+
+ Args:
+ file_path (String): Name of the file with path
+
+ Returns
+ dict: Dictionary representation of the the service configuration
+ """
with open(os.path.join(os.path.dirname(__file__), file_path), 'r') as data:
return json.load(data)
-def subscription_data(subscription_name):
+def create_subscription_data(subscription_name):
+ """
+ Creates subscription model object
+
+ Args:
+ subscription_name (String): Name of the Subscription
+
+ Returns
+ SubscriptionModel: single subscription model object
+ """
nf_filter = NetworkFunctionFilterModel(subscription_name, '{^pnf.*,^vnf.*}',
'{}', '{}', '{}')
mg_first = MeasurementGroupModel(subscription_name, 'MG1', 'UNLOCKED', 15, '/pm/pm.xml',
@@ -51,6 +69,22 @@ def subscription_data(subscription_name):
return subscription_model
+def create_multiple_subscription_data(subscription_names):
+ """
+ Creates a list of subscription model objects
+
+ Args:
+ subscription_names (List): Name of the Subscriptions
+
+ Returns
+ list (SubscriptionModel): of subscription model objects
+ """
+ subscriptions = []
+ for subscription_name in subscription_names:
+ subscriptions.append(create_subscription_data(subscription_name))
+ return subscriptions
+
+
class BaseClassSetup(TestCase):
app = None
app_context = None
diff --git a/components/pm-subscription-handler/tests/services/test_subscription_service.py b/components/pm-subscription-handler/tests/services/test_subscription_service.py
index a0e4d541..86bf2639 100644
--- a/components/pm-subscription-handler/tests/services/test_subscription_service.py
+++ b/components/pm-subscription-handler/tests/services/test_subscription_service.py
@@ -29,6 +29,7 @@ from mod.api.custom_exception import DuplicateDataException, InvalidDataExceptio
from mod.pmsh_config import AppConfig
from tests.base_setup import BaseClassSetup
from mod.api.services import subscription_service, nf_service, measurement_group_service
+from tests.base_setup import create_multiple_subscription_data
class SubscriptionServiceTestCase(BaseClassSetup):
@@ -357,3 +358,21 @@ class SubscriptionServiceTestCase(BaseClassSetup):
db_string = '{}'
db_array = convert_db_string_to_list(db_string)
self.assertEqual(len(db_array), 0)
+
+ @patch('mod.api.services.subscription_service.query_all_subscriptions',
+ MagicMock(return_value=create_multiple_subscription_data(
+ ['sub_demo_one', 'sub_demo_two'])))
+ def test_get_subscriptions_list(self):
+ subs = subscription_service.get_subscriptions_list()
+ self.assertEqual(subs[0]['subscription']['subscriptionName'], 'sub_demo_one')
+ self.assertEqual(subs[1]['subscription']['subscriptionName'], 'sub_demo_two')
+ self.assertEqual(subs[1]['subscription']['measurementGroups'][0]['measurementGroup']
+ ['measurementGroupName'], 'MG1')
+ self.assertEqual(len(subs[1]['subscription']['measurementGroups']), 2)
+ self.assertEqual(len(subs), 2)
+
+ @patch('mod.api.services.subscription_service.query_all_subscriptions',
+ MagicMock(return_value=[]))
+ def test_get_subscriptions_list_empty(self):
+ subs = subscription_service.get_subscriptions_list()
+ self.assertEqual(subs, [])
diff --git a/components/pm-subscription-handler/tests/test_controller.py b/components/pm-subscription-handler/tests/test_controller.py
index 7bd72a29..77ab889c 100755
--- a/components/pm-subscription-handler/tests/test_controller.py
+++ b/components/pm-subscription-handler/tests/test_controller.py
@@ -21,12 +21,13 @@ from unittest.mock import patch, MagicMock
from http import HTTPStatus
from mod import aai_client
-from mod.api.controller import status, post_subscription, get_subscription_by_name
+from mod.api.controller import status, post_subscription, get_subscription_by_name,\
+ get_subscriptions
from tests.base_setup import BaseClassSetup
from mod.api.db_models import SubscriptionModel, NfMeasureGroupRelationalModel
from mod.subscription import SubNfState
from mod.network_function import NetworkFunctionFilter
-from tests.base_setup import subscription_data
+from tests.base_setup import create_subscription_data, create_multiple_subscription_data
class ControllerTestCase(BaseClassSetup):
@@ -111,11 +112,11 @@ class ControllerTestCase(BaseClassSetup):
self.assertEqual(response[1], 400)
self.assertEqual(response[0], 'No value provided in subscription name')
- @patch('mod.api.services.subscription_service.get_subscription_by_name',
- MagicMock(return_value=subscription_data('sub_demo')))
+ @patch('mod.api.services.subscription_service.query_subscription_by_name',
+ MagicMock(return_value=create_subscription_data('sub_demo')))
def test_get_subscription_by_name_api(self):
sub, status_code = get_subscription_by_name('sub_demo')
- self.assertEqual(status_code, HTTPStatus.OK)
+ self.assertEqual(status_code, HTTPStatus.OK.value)
self.assertEqual(sub['subscription']['subscriptionName'], 'sub_demo')
self.assertEqual(sub['subscription']['nfFilter']['nfNames'],
['^pnf.*', '^vnf.*'])
@@ -125,16 +126,42 @@ class ControllerTestCase(BaseClassSetup):
self.assertEqual(sub['subscription']['operationalPolicyName'],
'pmsh_operational_policy')
- @patch('mod.api.services.subscription_service.get_subscription_by_name',
+ @patch('mod.api.services.subscription_service.query_subscription_by_name',
MagicMock(return_value=None))
- def test_get_subscription_by_name_api_error(self):
+ def test_get_subscription_by_name_api_none(self):
sub, status_code = get_subscription_by_name('sub_demo')
- self.assertEqual(status_code, HTTPStatus.NOT_FOUND)
+ self.assertEqual(status_code, HTTPStatus.NOT_FOUND.value)
self.assertEqual(sub['error'],
'Subscription was not defined with the name : sub_demo')
- @patch('mod.api.services.subscription_service.get_subscription_by_name',
+ @patch('mod.api.services.subscription_service.query_subscription_by_name',
MagicMock(side_effect=Exception('something failed')))
def test_get_subscription_by_name_api_exception(self):
sub, status_code = get_subscription_by_name('sub_demo')
- self.assertEqual(status_code, HTTPStatus.INTERNAL_SERVER_ERROR)
+ self.assertEqual(status_code, HTTPStatus.INTERNAL_SERVER_ERROR.value)
+
+ @patch('mod.api.services.subscription_service.query_all_subscriptions',
+ MagicMock(return_value=create_multiple_subscription_data(
+ ['sub_demo_one', 'sub_demo_two'])))
+ def test_get_subscriptions_api(self):
+ subs, status_code = get_subscriptions()
+ self.assertEqual(status_code, HTTPStatus.OK.value)
+ self.assertEqual(subs[0]['subscription']['subscriptionName'], 'sub_demo_one')
+ self.assertEqual(subs[1]['subscription']['subscriptionName'], 'sub_demo_two')
+ self.assertEqual(subs[1]['subscription']['measurementGroups'][0]['measurementGroup']
+ ['measurementGroupName'], 'MG1')
+ self.assertEqual(len(subs[1]['subscription']['measurementGroups']), 2)
+ self.assertEqual(len(subs), 2)
+
+ @patch('mod.api.services.subscription_service.query_all_subscriptions',
+ MagicMock(return_value=None))
+ def test_get_subscriptions_api_none(self):
+ subs, status_code = get_subscriptions()
+ self.assertEqual(status_code, HTTPStatus.OK.value)
+ self.assertEqual(subs, [])
+
+ @patch('mod.api.services.subscription_service.query_all_subscriptions',
+ MagicMock(side_effect=Exception('something failed')))
+ def test_get_subscriptions_api_exception(self):
+ subs, status_code = get_subscriptions()
+ self.assertEqual(status_code, HTTPStatus.INTERNAL_SERVER_ERROR.value)