Enable pylint, black and flake8 in tox.ini
[osm/NBI.git] / osm_nbi / osm_vnfm / vnf_instances.py
1 # Copyright 2021 K Sai Kiran (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 __author__ = "K Sai Kiran <saikiran.k@tataelxsi.co.in>, Selvi Jayaraman <selvi.j@tataelxsi.co.in>"
17 __date__ = "$12-June-2021 8:30:59$"
18
19 from copy import deepcopy
20 from osm_nbi.descriptor_topics import NsdTopic
21 from .base_methods import BaseMethod
22
23 from osm_nbi.instance_topics import NsrTopic, VnfrTopic
24
25
26 class VnfInstances2NsInstances:
27 def __init__(self, db, fs, msg, auth):
28 """
29 Constructor of Vnf Instances to Ns Instances
30 """
31 self.new_vnf_instance = NewVnfInstance(db, fs, msg, auth)
32 self.list_vnf_instance = ListVnfInstance(db, fs, msg, auth)
33 self.show_vnf_instance = ShowVnfInstance(db, fs, msg, auth)
34 self.delete_vnf_instance = DeleteVnfInstance(db, fs, msg, auth)
35
36 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
37 """
38 Creates a new entry into database.
39 :param rollback: list to append created items at database in case a rollback may have to be done
40 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
41 :param indata: data to be inserted
42 :param kwargs: used to override the indata descriptor
43 :param headers: http request headers
44 :return: _id, op_id:
45 _id: identity of the inserted data.
46 op_id: operation id if this is asynchronous, None otherwise
47 """
48 return self.new_vnf_instance.action(rollback, session, indata, kwargs, headers)
49
50 def list(self, session, filter_q=None, api_req=False):
51 """
52 Get a list of the Vnfs that match a filter
53 :param session: contains the used login username and working project
54 :param filter_q: filter of data to be applied
55 :param api_req: True if this call is serving an external API request. False if serving internal request.
56 :return: The list, it can be empty if no one match the filter.
57 """
58 return self.list_vnf_instance.action(session, filter_q, api_req)
59
60 def show(self, session, _id, api_req=False):
61 """
62 Get complete information on an Vnf
63 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
64 :param _id: server internal id
65 :param api_req: True if this call is serving an external API request. False if serving internal request.
66 :return: dictionary, raise exception if not found.
67 """
68 return self.show_vnf_instance.action(session, _id, api_req)
69
70 def delete(self, session, _id, dry_run=False, not_send_msg=None):
71 """
72 Delete item by its internal _id
73 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
74 :param _id: server internal id
75 :param dry_run: make checking but do not delete
76 :param not_send_msg: To not send message (False) or store content (list) instead
77 :return: operation id (None if there is not operation), raise exception if error or not found, conflict, ...
78 """
79 return self.delete_vnf_instance.action(session, _id, dry_run, not_send_msg)
80
81
82 class NewVnfInstance(BaseMethod):
83
84 # sample ns descriptor
85 sample_nsd = {
86 "nsd": {
87 "nsd": [
88 {
89 "df": [
90 {
91 "id": "default-df",
92 "vnf-profile": [
93 {
94 "id": 1,
95 "virtual-link-connectivity": [
96 {
97 "constituent-cpd-id": [
98 {
99 "constituent-base-element-id": 1,
100 "constituent-cpd-id": "eth0-ext",
101 }
102 ],
103 "virtual-link-profile-id": "mgmtnet",
104 }
105 ],
106 "vnfd-id": "cirros_vnfd",
107 }
108 ],
109 }
110 ],
111 "vnfd-id": ["cirros_vnfd"],
112 "description": "Generated by OSM pacakage generator",
113 "id": "cirros_2vnf_nsd",
114 "name": "cirros_2vnf_ns",
115 "short-name": "cirros_2vnf_ns",
116 "vendor": "OSM",
117 "version": "1.0",
118 }
119 ]
120 }
121 }
122
123 @staticmethod
124 def __get_formatted_indata(indata, nsd_id):
125 """
126 Create indata for nsd_id
127 :param indata: Contains unformatted data for new vnf instance
128 :param nsd_id: Id of nsd
129 :return: formatted indata for nsd_id
130 """
131 formatted_indata = deepcopy(indata)
132 formatted_indata["nsdId"] = nsd_id
133 formatted_indata["nsName"] = indata["vnfInstanceName"] + "-ns"
134 for invalid_key in (
135 "vnfdId",
136 "vnfInstanceName",
137 "vnfInstanceDescription",
138 "additionalParams",
139 ):
140 formatted_indata.pop(invalid_key)
141 return formatted_indata
142
143 def __init__(self, db, fs, msg, auth):
144 """
145 Constructor for new vnf instance
146 """
147 super().__init__()
148 self.msg = msg
149 self.nsdtopic = NsdTopic(db, fs, msg, auth)
150 self.nsrtopic = NsrTopic(db, fs, msg, auth)
151
152 def __get_vnfd(self):
153 # get vnfd from nfvo
154 pass
155
156 def __onboard_vnfd(self):
157 self.__get_vnfd()
158 pass
159
160 def __create_nsd(self, rollback, session, indata=None, kwargs=None, headers=None):
161 """
162 Creates new ns descriptor from a vnfd.
163 :param rollback: list to append the created items at database in case a rollback must be done
164 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
165 :param indata: params to be used for the nsr
166 :param kwargs: used to override the indata
167 :param headers: http request headers
168 :return: id of new nsd created
169 """
170 _id, *others = self.nsdtopic.new(rollback, session, {}, None, headers)
171 new_nsd = deepcopy(NewVnfInstance.sample_nsd)
172 vnf_content = {
173 "id": "default-df",
174 "vnf-profile": [
175 {
176 "id": "1",
177 "virtual-link-connectivity": [
178 {
179 "constituent-cpd-id": [
180 {
181 "constituent-base-element-id": "1",
182 "constituent-cpd-id": indata["additionalParams"][
183 "constituent-cpd-id"
184 ],
185 }
186 ],
187 "virtual-link-profile-id": indata["additionalParams"][
188 "virtual-link-profile-id"
189 ],
190 }
191 ],
192 "vnfd-id": indata["vnfdId"],
193 }
194 ],
195 }
196 new_nsd["nsd"]["nsd"][0] = {
197 "description": indata["vnfInstanceDescription"],
198 "designer": "OSM",
199 "id": indata["vnfdId"] + "-ns",
200 "name": indata["vnfdId"] + "-ns",
201 "version": "1.0",
202 "df": [
203 vnf_content,
204 ],
205 "virtual-link-desc": indata["additionalParams"]["virtual-link-desc"],
206 "vnfd-id": [indata["vnfdId"]],
207 }
208 return _id, new_nsd
209
210 def __create_nsr(self, rollback, session, indata=None, kwargs=None, headers=None):
211 """
212 Creates an new ns record in database
213 :param rollback: list to append the created items at database in case a rollback must be done
214 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
215 :param indata: params to be used for the nsr
216 :param kwargs: used to override the indata
217 :param headers: http request headers
218 :return: id of new nsr
219 """
220 return self.nsrtopic.new(rollback, session, indata, kwargs, headers)
221
222 def __action_pre_processing(
223 self, rollback, session, indata=None, kwargs=None, headers=None
224 ):
225 """
226 Pre process for creating new vnf instance
227 :param rollback: list to append the created items at database in case a rollback must be done
228 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
229 :param indata: params to be used for the nsr
230 :param kwargs: used to override the indata
231 :param headers: http request headers
232 :return: id nsr
233 """
234 self.__onboard_vnfd()
235 nsd_id, nsd = self.__create_nsd(rollback, session, indata, kwargs, headers)
236 self.nsdtopic.upload_content(session, nsd_id, nsd, kwargs, headers)
237 formatted_indata = NewVnfInstance.__get_formatted_indata(indata, nsd_id)
238 nsr_id, _ = self.__create_nsr(
239 rollback, session, formatted_indata, kwargs, headers
240 )
241 nsr = self.nsrtopic.show(session, nsr_id)
242 vnfr_id = nsr["constituent-vnfr-ref"][0]
243 if vnfr_id:
244 links = {"vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + vnfr_id}
245 indata["vnfInstanceId"] = vnfr_id
246 indata["_links"] = links
247 self.msg.write("vnf", "create", indata)
248 return vnfr_id, None
249
250 def action(self, rollback, session, indata=None, kwargs=None, headers=None):
251 """
252 Creates an new vnf instance
253 :param rollback: list to append the created items at database in case a rollback must be done
254 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
255 :param indata: params to be used for the nsr
256 :param kwargs: used to override the indata
257 :param headers: http request headers
258 :return: id of new vnf instance
259 """
260 return self.__action_pre_processing(rollback, session, indata, kwargs, headers)
261
262
263 class ListVnfInstance(BaseMethod):
264 def __init__(self, db, fs, msg, auth):
265 """
266 Constructor call for listing vnfs
267 """
268 super().__init__()
269 self.vnfrtopic = VnfrTopic(db, fs, msg, auth)
270
271 def action(self, session, filter_q=None, api_req=False):
272 """
273 To get list of vnfs that matches a filter
274 :param session: contains the used login username and working project
275 :param filter_q: filter of data to be applied
276 :param api_req: True if this call is serving an external API request. False if serving internal request.
277 :return: The list, it can be empty if no one match the filter.
278 """
279 return self.vnfrtopic.list(session, filter_q, api_req)
280
281
282 class ShowVnfInstance(BaseMethod):
283 def __init__(self, db, fs, msg, auth):
284 """
285 Constructor call for showing vnf lcm operation
286 """
287 super().__init__()
288 self.vnfrtopic = VnfrTopic(db, fs, msg, auth)
289
290 def action(self, session, _id, api_req=False):
291 """
292 Get complete information on an Vnf Lcm Operation.
293 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
294 :param _id: Vnf Lcm operation id
295 :param api_req: True if this call is serving an external API request. False if serving internal request.
296 :return: dictionary, raise exception if not found.
297 """
298 return self.vnfrtopic.show(session, _id, api_req)
299
300
301 class DeleteVnfInstance(BaseMethod):
302 def __init__(self, db, fs, msg, auth):
303 """
304 Constructor call for deleting vnf
305 """
306 super().__init__()
307 self.msg = msg
308 self.nsrtopic = NsrTopic(db, fs, msg, auth)
309 self.nsdtopic = NsdTopic(db, fs, msg, auth)
310 self.vnfrtopic = VnfrTopic(db, fs, msg, auth)
311
312 def action(self, session, _id, dry_run=False, not_send_msg=None):
313 """
314 Delete vnf instance by its internal _id
315 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
316 :param _id: server internal id
317 :param dry_run: make checking but do not delete
318 :param not_send_msg: To not send message (False) or store content (list) instead
319 :return: operation id (None if there is not operation), raise exception if error or not found, conflict, ...
320 """
321 vnfInstanceId = _id
322 vnfr = self.vnfrtopic.show(session, vnfInstanceId)
323 ns_id = vnfr.get("nsr-id-ref")
324 nsr = self.nsrtopic.show(session, ns_id)
325 nsd_to_del = nsr["nsd"]["_id"]
326 links = {"vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + _id}
327 params = {"vnfdId": vnfr["vnfd-ref"], "vnfInstanceId": _id, "_links": links}
328 self.msg.write("vnf", "delete", params)
329 self.nsrtopic.delete(session, ns_id, dry_run, not_send_msg)
330 return self.nsdtopic.delete(session, nsd_to_del, dry_run, not_send_msg)