Update python2 to python3
[vfc/nfvo/lcm.git] / lcm / ns / biz / query_subscription.py
1 # Copyright (c) 2019, CMCC Technologies Co., Ltd.
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
21 logger = logging.getLogger(__name__)
22 ROOT_FILTERS = {
23     'operationTypes': 'operation_types',
24     'operationStates': 'operation_states',
25     'notificationTypes': 'notification_types',
26     'nsComponentTypes': 'ns_component_types',
27     'lcmOpNameImpactingNsComponent': 'lcm_opname_impacting_nscomponent',
28     'lcmOpOccStatusImpactingNsComponent': 'lcm_opoccstatus_impacting_nscomponent'
29 }
30 NS_INSTANCE_FILTERS = {
31     "nsInstanceId": "ns_instance_filter"
32 }
33
34
35 class QuerySubscription:
36
37     def __init__(self, data, subscription_id=''):
38         self.subscription_id = subscription_id
39         self.params = data
40
41     def query_multi_subscriptions(self):
42         query_data = {}
43         logger.debug(
44             "QueryMultiSubscriptions--get--biz::> Check for filters in query params" % self.params)
45         for query, value in list(self.params.items()):
46             if query in ROOT_FILTERS:
47                 query_data[ROOT_FILTERS[query] + '__icontains'] = value
48         for query, value in list(self.params.items()):
49             if query in NS_INSTANCE_FILTERS:
50                 query_data[NS_INSTANCE_FILTERS[query] + '__icontains'] = value
51         # Query the database with filters if the request has fields in request
52         # params, else fetch all records
53         if query_data:
54             subscriptions = SubscriptionModel.objects.filter(**query_data)
55         else:
56             subscriptions = SubscriptionModel.objects.all()
57         return [self.fill_resp_data(subscription) for subscription in subscriptions]
58
59     def fill_resp_data(self, subscription):
60         subscription_filter = {
61             "notificationTypes": ast.literal_eval(subscription.notification_types),
62             "operationTypes": ast.literal_eval(subscription.operation_types),
63             "operationStates": ast.literal_eval(subscription.operation_states),
64             "nsInstanceSubscriptionFilter": json.loads(subscription.ns_instance_filter)
65         }
66         resp_data = {
67             'id': subscription.subscription_id,
68             'callbackUri': subscription.callback_uri,
69             'filter': subscription_filter,
70             '_links': json.loads(subscription.links)
71         }
72         return resp_data