1 # Copyright 2020 Preethika P(Tata Elxsi)
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 __author__
= "Preethika P,preethika.p@tataelxsi.co.in"
20 from osm_nbi
.base_topic
import BaseTopic
, EngineException
21 from osm_nbi
.validation
import subscription
24 class CommonSubscriptions(BaseTopic
):
25 topic
= "subscriptions"
28 def format_on_new(self
, content
, project_id
=None, make_public
=False):
29 super().format_on_new(content
, project_id
=project_id
, make_public
=make_public
)
31 # TODO check how to release Engine.write_lock during the check
32 def _check_endpoint(url
, auth
):
34 Checks if the notification endpoint is valid
35 :param url: the notification end
36 :param auth: contains the aunthentication details with type basic
40 response
= requests
.get(url
, timeout
=5)
41 if response
.status_code
!= 204:
42 raise EngineException("Cannot access to the notification URL '{}',received {}: {}"
43 .format(url
, response
.status_code
, response
.content
))
44 elif auth
["authType"] == "basic":
45 username
= auth
["paramsBasic"].get("userName")
46 password
= auth
["paramsBasic"].get("password")
47 response
= requests
.get(url
, auth
=(username
, password
), timeout
=5)
48 if response
.status_code
!= 204:
49 raise EngineException("Cannot access to the notification URL '{}',received {}: {}"
50 .format(url
, response
.status_code
, response
.content
))
51 except requests
.exceptions
.RequestException
as e
:
52 error_text
= type(e
).__name
__ + ": " + str(e
)
53 raise EngineException("Cannot access to the notification URL '{}': {}".format(url
, error_text
))
54 url
= content
["CallbackUri"]
55 auth
= content
.get("authentication")
56 _check_endpoint(url
, auth
)
57 content
["schema_version"] = schema_version
= "1.1"
58 if auth
is not None and auth
["authType"] == "basic":
59 if content
["authentication"]["paramsBasic"].get("password"):
60 content
["authentication"]["paramsBasic"]["password"] = \
61 self
.db
.encrypt(content
["authentication"]["paramsBasic"]["password"],
62 schema_version
=schema_version
, salt
=content
["_id"])
65 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
67 Uses BaseTopic.new to create entry into db
68 Once entry is made into subscriptions,mapper function is invoked
70 _id
, op_id
= BaseTopic
.new(self
, rollback
, session
, indata
=indata
, kwargs
=kwargs
, headers
=headers
)
71 rollback
.append({"topic": "mapped_subscriptions", "operation": "del_list", "filter": {"reference": _id
}})
72 self
._subscription
_mapper
(_id
, indata
, table
="mapped_subscriptions")
75 def delete_extra(self
, session
, _id
, db_content
, not_send_msg
=None):
77 Deletes the mapped_subscription entry for this particular subscriber
78 :param _id: subscription_id deleted
80 super().delete_extra(session
, _id
, db_content
, not_send_msg
)
82 filter_q
["reference"] = _id
83 self
.db
.del_list("mapped_subscriptions", filter_q
)
86 class NslcmSubscriptionsTopic(CommonSubscriptions
):
87 schema_new
= subscription
89 def _subscription_mapper(self
, _id
, data
, table
):
91 Performs data transformation on subscription request
92 :param data: data to be trasformed
93 :param table: table in which transformed data are inserted
96 formed_data
= {"reference": data
.get("_id"),
97 "CallbackUri": data
.get("CallbackUri")}
98 if data
.get("authentication"):
99 formed_data
.update({"authentication": data
.get("authentication")})
100 if data
.get("filter"):
101 if data
["filter"].get("nsInstanceSubscriptionFilter"):
102 key
= list(data
["filter"]["nsInstanceSubscriptionFilter"].keys())[0]
103 identifier
= data
["filter"]["nsInstanceSubscriptionFilter"][key
]
104 formed_data
.update({"identifier": identifier
})
105 if data
["filter"].get("notificationTypes"):
106 for elem
in data
["filter"].get("notificationTypes"):
107 update_dict
= formed_data
.copy()
108 update_dict
["notificationType"] = elem
109 if elem
== "NsIdentifierCreationNotification":
110 update_dict
["operationTypes"] = "INSTANTIATE"
111 update_dict
["operationStates"] = "ANY"
112 formatted_data
.append(update_dict
)
113 elif elem
== "NsIdentifierDeletionNotification":
114 update_dict
["operationTypes"] = "TERMINATE"
115 update_dict
["operationStates"] = "ANY"
116 formatted_data
.append(update_dict
)
117 elif elem
== "NsLcmOperationOccurrenceNotification":
118 if "operationTypes" in data
["filter"].keys():
119 update_dict
["operationTypes"] = data
["filter"]["operationTypes"]
121 update_dict
["operationTypes"] = "ANY"
122 if "operationStates" in data
["filter"].keys():
123 update_dict
["operationStates"] = data
["filter"]["operationStates"]
125 update_dict
["operationStates"] = "ANY"
126 formatted_data
.append(update_dict
)
127 elif elem
== "NsChangeNotification":
128 if "nsComponentTypes" in data
["filter"].keys():
129 update_dict
["nsComponentTypes"] = data
["filter"]["nsComponentTypes"]
131 update_dict
["nsComponentTypes"] = "ANY"
132 if "lcmOpNameImpactingNsComponent" in data
["filter"].keys():
133 update_dict
["lcmOpNameImpactingNsComponent"] = \
134 data
["filter"]["lcmOpNameImpactingNsComponent"]
136 update_dict
["lcmOpNameImpactingNsComponent"] = "ANY"
137 if "lcmOpOccStatusImpactingNsComponent" in data
["filter"].keys():
138 update_dict
["lcmOpOccStatusImpactingNsComponent"] = \
139 data
["filter"]["lcmOpOccStatusImpactingNsComponent"]
141 update_dict
["lcmOpOccStatusImpactingNsComponent"] = "ANY"
142 formatted_data
.append(update_dict
)
143 self
.db
.create_list(table
, formatted_data
)