1 |
|
# Copyright 2020 Preethika P(Tata Elxsi) |
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 |
12 |
|
# implied. |
13 |
|
# See the License for the specific language governing permissions and |
14 |
|
# limitations under the License. |
15 |
|
|
16 |
1 |
__author__ = "Preethika P,preethika.p@tataelxsi.co.in" |
17 |
|
|
18 |
1 |
import requests |
19 |
1 |
from osm_nbi.base_topic import BaseTopic, EngineException |
20 |
1 |
from osm_nbi.validation import subscription |
21 |
1 |
from http import HTTPStatus |
22 |
|
|
23 |
|
|
24 |
1 |
class CommonSubscriptions(BaseTopic): |
25 |
1 |
topic = "subscriptions" |
26 |
1 |
topic_msg = None |
27 |
|
|
28 |
1 |
def format_subscription(self, subs_data): |
29 |
|
""" |
30 |
|
Brings lexicographical order for list items at any nested level. For subscriptions max level of nesting is 4. |
31 |
|
:param subs_data: Subscription data to be ordered. |
32 |
|
:return: None |
33 |
|
""" |
34 |
0 |
if isinstance(subs_data, dict): |
35 |
0 |
for key in subs_data.keys(): |
36 |
|
# Base case |
37 |
0 |
if isinstance(subs_data[key], list): |
38 |
0 |
subs_data[key].sort() |
39 |
0 |
return |
40 |
|
# Recursive case |
41 |
0 |
self.format_subscription(subs_data[key]) |
42 |
0 |
return |
43 |
|
|
44 |
1 |
def check_conflict_on_new(self, session, content): |
45 |
|
""" |
46 |
|
Two subscriptions are equal if Auth username, CallbackUri and filter are same. |
47 |
|
:param session: Session object. |
48 |
|
:param content: Subscription data. |
49 |
|
:return: None if no conflict otherwise, raises an exception. |
50 |
|
""" |
51 |
|
# Get all subscriptions from db table subscriptions and compare. |
52 |
0 |
self.format_subscription(content) |
53 |
0 |
filter_dict = {"CallbackUri": content["CallbackUri"]} |
54 |
0 |
if content.get("authentication"): |
55 |
0 |
if content["authentication"].get("authType") == "basic": |
56 |
0 |
filter_dict["authentication.authType"] = "basic" |
57 |
|
# elif add other authTypes here |
58 |
|
else: |
59 |
0 |
filter_dict["authentication"] = None # For Items without authentication |
60 |
0 |
existing_subscriptions = self.db.get_list("subscriptions", q_filter=filter_dict) |
61 |
0 |
new_sub_pwd = None |
62 |
0 |
if content.get("authentication") and content["authentication"].get("authType") == "basic": |
63 |
0 |
new_sub_pwd = content["authentication"]["paramsBasic"]["password"] |
64 |
0 |
content["authentication"]["paramsBasic"].pop("password", None) |
65 |
0 |
for existing_subscription in existing_subscriptions: |
66 |
0 |
sub_id = existing_subscription.pop("_id", None) |
67 |
0 |
existing_subscription.pop("_admin", None) |
68 |
0 |
existing_subscription.pop("schema_version", None) |
69 |
0 |
if existing_subscription.get("authentication") and \ |
70 |
|
existing_subscription["authentication"].get("authType") == "basic": |
71 |
0 |
existing_subscription["authentication"]["paramsBasic"].pop("password", None) |
72 |
|
# self.logger.debug(existing_subscription) |
73 |
0 |
if existing_subscription == content: |
74 |
0 |
raise EngineException("Subscription already exists with id: {}".format(sub_id), |
75 |
|
HTTPStatus.CONFLICT) |
76 |
0 |
if new_sub_pwd: |
77 |
0 |
content["authentication"]["paramsBasic"]["password"] = new_sub_pwd |
78 |
0 |
return |
79 |
|
|
80 |
1 |
def format_on_new(self, content, project_id=None, make_public=False): |
81 |
0 |
super().format_on_new(content, project_id=project_id, make_public=make_public) |
82 |
|
|
83 |
|
# TODO check how to release Engine.write_lock during the check |
84 |
0 |
def _check_endpoint(url, auth): |
85 |
|
""" |
86 |
|
Checks if the notification endpoint is valid |
87 |
|
:param url: the notification end |
88 |
|
:param auth: contains the authentication details with type basic |
89 |
|
""" |
90 |
0 |
try: |
91 |
0 |
if auth is None: |
92 |
0 |
response = requests.get(url, timeout=5) |
93 |
0 |
if response.status_code != HTTPStatus.NO_CONTENT: |
94 |
0 |
raise EngineException("Cannot access to the notification URL '{}',received {}: {}" |
95 |
|
.format(url, response.status_code, response.content)) |
96 |
0 |
elif auth["authType"] == "basic": |
97 |
0 |
username = auth["paramsBasic"].get("userName") |
98 |
0 |
password = auth["paramsBasic"].get("password") |
99 |
0 |
response = requests.get(url, auth=(username, password), timeout=5) |
100 |
0 |
if response.status_code != HTTPStatus.NO_CONTENT: |
101 |
0 |
raise EngineException("Cannot access to the notification URL '{}',received {}: {}" |
102 |
|
.format(url, response.status_code, response.content)) |
103 |
0 |
except requests.exceptions.RequestException as e: |
104 |
0 |
error_text = type(e).__name__ + ": " + str(e) |
105 |
0 |
raise EngineException("Cannot access to the notification URL '{}': {}".format(url, error_text)) |
106 |
|
|
107 |
0 |
url = content["CallbackUri"] |
108 |
0 |
auth = content.get("authentication") |
109 |
0 |
_check_endpoint(url, auth) |
110 |
0 |
content["schema_version"] = schema_version = "1.1" |
111 |
0 |
if auth is not None and auth["authType"] == "basic": |
112 |
0 |
if content["authentication"]["paramsBasic"].get("password"): |
113 |
0 |
content["authentication"]["paramsBasic"]["password"] = \ |
114 |
|
self.db.encrypt(content["authentication"]["paramsBasic"]["password"], |
115 |
|
schema_version=schema_version, salt=content["_id"]) |
116 |
0 |
return None |
117 |
|
|
118 |
1 |
def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
119 |
|
""" |
120 |
|
Uses BaseTopic.new to create entry into db |
121 |
|
Once entry is made into subscriptions,mapper function is invoked |
122 |
|
""" |
123 |
0 |
_id, op_id = BaseTopic.new(self, rollback, session, indata=indata, kwargs=kwargs, headers=headers) |
124 |
0 |
rollback.append({"topic": "mapped_subscriptions", "operation": "del_list", "filter": {"reference": _id}}) |
125 |
0 |
self._subscription_mapper(_id, indata, table="mapped_subscriptions") |
126 |
0 |
return _id, op_id |
127 |
|
|
128 |
1 |
def delete_extra(self, session, _id, db_content, not_send_msg=None): |
129 |
|
""" |
130 |
|
Deletes the mapped_subscription entry for this particular subscriber |
131 |
|
:param _id: subscription_id deleted |
132 |
|
""" |
133 |
0 |
super().delete_extra(session, _id, db_content, not_send_msg) |
134 |
0 |
filter_q = {"reference": _id} |
135 |
0 |
self.db.del_list("mapped_subscriptions", filter_q) |
136 |
|
|
137 |
|
|
138 |
1 |
class NslcmSubscriptionsTopic(CommonSubscriptions): |
139 |
1 |
schema_new = subscription |
140 |
|
|
141 |
1 |
def _subscription_mapper(self, _id, data, table): |
142 |
|
""" |
143 |
|
Performs data transformation on subscription request |
144 |
|
:param data: data to be trasformed |
145 |
|
:param table: table in which transformed data are inserted |
146 |
|
""" |
147 |
0 |
formatted_data = [] |
148 |
0 |
formed_data = {"reference": data.get("_id"), |
149 |
|
"CallbackUri": data.get("CallbackUri")} |
150 |
0 |
if data.get("authentication"): |
151 |
0 |
formed_data.update({"authentication": data.get("authentication")}) |
152 |
0 |
if data.get("filter"): |
153 |
0 |
if data["filter"].get("nsInstanceSubscriptionFilter"): |
154 |
0 |
key = list(data["filter"]["nsInstanceSubscriptionFilter"].keys())[0] |
155 |
0 |
identifier = data["filter"]["nsInstanceSubscriptionFilter"][key] |
156 |
0 |
formed_data.update({"identifier": identifier}) |
157 |
0 |
if data["filter"].get("notificationTypes"): |
158 |
0 |
for elem in data["filter"].get("notificationTypes"): |
159 |
0 |
update_dict = formed_data.copy() |
160 |
0 |
update_dict["notificationType"] = elem |
161 |
0 |
if elem == "NsIdentifierCreationNotification": |
162 |
0 |
update_dict["operationTypes"] = "INSTANTIATE" |
163 |
0 |
update_dict["operationStates"] = "ANY" |
164 |
0 |
formatted_data.append(update_dict) |
165 |
0 |
elif elem == "NsIdentifierDeletionNotification": |
166 |
0 |
update_dict["operationTypes"] = "TERMINATE" |
167 |
0 |
update_dict["operationStates"] = "ANY" |
168 |
0 |
formatted_data.append(update_dict) |
169 |
0 |
elif elem == "NsLcmOperationOccurrenceNotification": |
170 |
0 |
if "operationTypes" in data["filter"].keys(): |
171 |
0 |
update_dict["operationTypes"] = data["filter"]["operationTypes"] |
172 |
|
else: |
173 |
0 |
update_dict["operationTypes"] = "ANY" |
174 |
0 |
if "operationStates" in data["filter"].keys(): |
175 |
0 |
update_dict["operationStates"] = data["filter"]["operationStates"] |
176 |
|
else: |
177 |
0 |
update_dict["operationStates"] = "ANY" |
178 |
0 |
formatted_data.append(update_dict) |
179 |
0 |
elif elem == "NsChangeNotification": |
180 |
0 |
if "nsComponentTypes" in data["filter"].keys(): |
181 |
0 |
update_dict["nsComponentTypes"] = data["filter"]["nsComponentTypes"] |
182 |
|
else: |
183 |
0 |
update_dict["nsComponentTypes"] = "ANY" |
184 |
0 |
if "lcmOpNameImpactingNsComponent" in data["filter"].keys(): |
185 |
0 |
update_dict["lcmOpNameImpactingNsComponent"] = \ |
186 |
|
data["filter"]["lcmOpNameImpactingNsComponent"] |
187 |
|
else: |
188 |
0 |
update_dict["lcmOpNameImpactingNsComponent"] = "ANY" |
189 |
0 |
if "lcmOpOccStatusImpactingNsComponent" in data["filter"].keys(): |
190 |
0 |
update_dict["lcmOpOccStatusImpactingNsComponent"] = \ |
191 |
|
data["filter"]["lcmOpOccStatusImpactingNsComponent"] |
192 |
|
else: |
193 |
0 |
update_dict["lcmOpOccStatusImpactingNsComponent"] = "ANY" |
194 |
0 |
formatted_data.append(update_dict) |
195 |
0 |
self.db.create_list(table, formatted_data) |
196 |
0 |
return None |