aedb467d056225409da9de5f3cdf87b3522d1d0a
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / biz / query_subscription.py
1 # Copyright (C) 2018 Verizon. All Rights Reserved
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import ast
16 import json
17 import logging
18
19 from lcm.pub.database.models import SubscriptionModel
20 from lcm.pub.exceptions import NFLCMException
21
22 logger = logging.getLogger(__name__)
23 ROOT_FILTERS = {
24     'operationTypes': 'operation_types',
25     'operationStates': 'operation_states',
26     'notificationTypes': 'notification_types'
27 }
28 VNF_INSTANCE_FILTERS = {
29     "vnfInstanceId": "vnf_instance_filter"
30 }
31
32
33 class QuerySubscription:
34     def __init__(self, data, subscription_id=''):
35         self.subscription_id = subscription_id
36         self.params = data
37
38     def query_multi_subscriptions(self):
39         query_data = {}
40         logger.debug("QueryMultiSubscriptions--get--biz::> Check for filters in query params" % self.params)
41         for query, value in self.params.iteritems():
42             if query in ROOT_FILTERS:
43                 query_data[ROOT_FILTERS[query] + '__icontains'] = value
44         for query, value in self.params.iteritems():
45             if query in VNF_INSTANCE_FILTERS:
46                 query_data[VNF_INSTANCE_FILTERS[query] + '__icontains'] = value
47         # Query the database with filters if the request has fields in request params, else fetch all records
48         if query_data:
49             subscriptions = SubscriptionModel.objects.filter(**query_data)
50         else:
51             subscriptions = SubscriptionModel.objects.all()
52         if not subscriptions.exists():
53             raise NFLCMException('Subscriptions do not exist')
54         return [self.fill_resp_data(subscription) for subscription in subscriptions]
55
56     def fill_resp_data(self, subscription):
57         subscription_filter = {
58             "notificationTypes": ast.literal_eval(subscription.notification_types),
59             "operationTypes": ast.literal_eval(subscription.operation_types),
60             "operationStates": ast.literal_eval(subscription.operation_states),
61             "vnfInstanceSubscriptionFilter": json.loads(subscription.vnf_instance_filter)
62         }
63         resp_data = {
64             'id': subscription.subscription_id,
65             'callbackUri': subscription.callback_uri,
66             'filter': subscription_filter,
67             '_links': json.loads(subscription.links)
68         }
69         return resp_data