1 # Copyright 2021 K Sai Kiran (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__
= "K Sai Kiran <saikiran.k@tataelxsi.co.in>, Selvi Jayaraman <selvi.j@tataelxsi.co.in>"
17 __date__
= "$12-June-2021 8:30:59$"
19 from copy
import deepcopy
20 from osm_nbi
.descriptor_topics
import NsdTopic
21 from .base_methods
import BaseMethod
23 from osm_nbi
.instance_topics
import NsrTopic
, VnfrTopic
26 class VnfInstances2NsInstances
:
27 def __init__(self
, db
, fs
, msg
, auth
):
29 Constructor of Vnf Instances to Ns Instances
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
)
36 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
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
45 _id: identity of the inserted data.
46 op_id: operation id if this is asynchronous, None otherwise
48 return self
.new_vnf_instance
.action(rollback
, session
, indata
, kwargs
, headers
)
50 def list(self
, session
, filter_q
=None, api_req
=False):
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.
58 return self
.list_vnf_instance
.action(session
, filter_q
, api_req
)
60 def show(self
, session
, _id
, api_req
=False):
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.
68 return self
.show_vnf_instance
.action(session
, _id
, api_req
)
70 def delete(self
, session
, _id
, dry_run
=False, not_send_msg
=None):
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, ...
79 return self
.delete_vnf_instance
.action(session
, _id
, dry_run
, not_send_msg
)
82 class NewVnfInstance(BaseMethod
):
83 # sample ns descriptor
94 "virtual-link-connectivity": [
96 "constituent-cpd-id": [
98 "constituent-base-element-id": 1,
99 "constituent-cpd-id": "eth0-ext",
102 "virtual-link-profile-id": "mgmtnet",
105 "vnfd-id": "cirros_vnfd",
110 "vnfd-id": ["cirros_vnfd"],
111 "description": "Generated by OSM pacakage generator",
112 "id": "cirros_2vnf_nsd",
113 "name": "cirros_2vnf_ns",
114 "short-name": "cirros_2vnf_ns",
123 def __get_formatted_indata(indata
, nsd_id
):
125 Create indata for nsd_id
126 :param indata: Contains unformatted data for new vnf instance
127 :param nsd_id: Id of nsd
128 :return: formatted indata for nsd_id
130 formatted_indata
= deepcopy(indata
)
131 formatted_indata
["nsdId"] = nsd_id
132 formatted_indata
["nsName"] = indata
["vnfInstanceName"] + "-ns"
136 "vnfInstanceDescription",
139 formatted_indata
.pop(invalid_key
)
140 return formatted_indata
142 def __init__(self
, db
, fs
, msg
, auth
):
144 Constructor for new vnf instance
148 self
.nsdtopic
= NsdTopic(db
, fs
, msg
, auth
)
149 self
.nsrtopic
= NsrTopic(db
, fs
, msg
, auth
)
151 def __get_vnfd(self
):
155 def __onboard_vnfd(self
):
159 def __create_nsd(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
161 Creates new ns descriptor from a vnfd.
162 :param rollback: list to append the created items at database in case a rollback must be done
163 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
164 :param indata: params to be used for the nsr
165 :param kwargs: used to override the indata
166 :param headers: http request headers
167 :return: id of new nsd created
169 _id
, *others
= self
.nsdtopic
.new(rollback
, session
, {}, None, headers
)
170 new_nsd
= deepcopy(NewVnfInstance
.sample_nsd
)
176 "virtual-link-connectivity": [
178 "constituent-cpd-id": [
180 "constituent-base-element-id": "1",
181 "constituent-cpd-id": indata
["additionalParams"][
186 "virtual-link-profile-id": indata
["additionalParams"][
187 "virtual-link-profile-id"
191 "vnfd-id": indata
["vnfdId"],
195 new_nsd
["nsd"]["nsd"][0] = {
196 "description": indata
["vnfInstanceDescription"],
198 "id": indata
["vnfdId"] + "-ns",
199 "name": indata
["vnfdId"] + "-ns",
204 "virtual-link-desc": indata
["additionalParams"]["virtual-link-desc"],
205 "vnfd-id": [indata
["vnfdId"]],
209 def __create_nsr(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
211 Creates an new ns record in database
212 :param rollback: list to append the created items at database in case a rollback must be done
213 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
214 :param indata: params to be used for the nsr
215 :param kwargs: used to override the indata
216 :param headers: http request headers
217 :return: id of new nsr
219 return self
.nsrtopic
.new(rollback
, session
, indata
, kwargs
, headers
)
221 def __action_pre_processing(
222 self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None
225 Pre process for creating new vnf instance
226 :param rollback: list to append the created items at database in case a rollback must be done
227 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
228 :param indata: params to be used for the nsr
229 :param kwargs: used to override the indata
230 :param headers: http request headers
233 self
.__onboard
_vnfd
()
234 nsd_id
, nsd
= self
.__create
_nsd
(rollback
, session
, indata
, kwargs
, headers
)
235 self
.nsdtopic
.upload_content(session
, nsd_id
, nsd
, kwargs
, headers
)
236 formatted_indata
= NewVnfInstance
.__get
_formatted
_indata
(indata
, nsd_id
)
237 nsr_id
, _
= self
.__create
_nsr
(
238 rollback
, session
, formatted_indata
, kwargs
, headers
240 nsr
= self
.nsrtopic
.show(session
, nsr_id
)
241 vnfr_id
= nsr
["constituent-vnfr-ref"][0]
243 links
= {"vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + vnfr_id
}
244 indata
["vnfInstanceId"] = vnfr_id
245 indata
["_links"] = links
246 self
.msg
.write("vnf", "create", indata
)
249 def action(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
251 Creates an new vnf instance
252 :param rollback: list to append the created items at database in case a rollback must be done
253 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
254 :param indata: params to be used for the nsr
255 :param kwargs: used to override the indata
256 :param headers: http request headers
257 :return: id of new vnf instance
259 return self
.__action
_pre
_processing
(rollback
, session
, indata
, kwargs
, headers
)
262 class ListVnfInstance(BaseMethod
):
263 def __init__(self
, db
, fs
, msg
, auth
):
265 Constructor call for listing vnfs
268 self
.vnfrtopic
= VnfrTopic(db
, fs
, msg
, auth
)
270 def action(self
, session
, filter_q
=None, api_req
=False):
272 To get list of vnfs that matches a filter
273 :param session: contains the used login username and working project
274 :param filter_q: filter of data to be applied
275 :param api_req: True if this call is serving an external API request. False if serving internal request.
276 :return: The list, it can be empty if no one match the filter.
278 return self
.vnfrtopic
.list(session
, filter_q
, api_req
)
281 class ShowVnfInstance(BaseMethod
):
282 def __init__(self
, db
, fs
, msg
, auth
):
284 Constructor call for showing vnf lcm operation
287 self
.vnfrtopic
= VnfrTopic(db
, fs
, msg
, auth
)
289 def action(self
, session
, _id
, api_req
=False):
291 Get complete information on an Vnf Lcm Operation.
292 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
293 :param _id: Vnf Lcm operation id
294 :param api_req: True if this call is serving an external API request. False if serving internal request.
295 :return: dictionary, raise exception if not found.
297 return self
.vnfrtopic
.show(session
, _id
, api_req
)
300 class DeleteVnfInstance(BaseMethod
):
301 def __init__(self
, db
, fs
, msg
, auth
):
303 Constructor call for deleting vnf
307 self
.nsrtopic
= NsrTopic(db
, fs
, msg
, auth
)
308 self
.nsdtopic
= NsdTopic(db
, fs
, msg
, auth
)
309 self
.vnfrtopic
= VnfrTopic(db
, fs
, msg
, auth
)
311 def action(self
, session
, _id
, dry_run
=False, not_send_msg
=None):
313 Delete vnf instance by its internal _id
314 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
315 :param _id: server internal id
316 :param dry_run: make checking but do not delete
317 :param not_send_msg: To not send message (False) or store content (list) instead
318 :return: operation id (None if there is not operation), raise exception if error or not found, conflict, ...
321 vnfr
= self
.vnfrtopic
.show(session
, vnfInstanceId
)
322 ns_id
= vnfr
.get("nsr-id-ref")
323 nsr
= self
.nsrtopic
.show(session
, ns_id
)
324 nsd_to_del
= nsr
["nsd"]["_id"]
325 links
= {"vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + _id
}
326 params
= {"vnfdId": vnfr
["vnfd-ref"], "vnfInstanceId": _id
, "_links": links
}
327 self
.msg
.write("vnf", "delete", params
)
328 self
.nsrtopic
.delete(session
, ns_id
, dry_run
, not_send_msg
)
329 return self
.nsdtopic
.delete(session
, nsd_to_del
, dry_run
, not_send_msg
)