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
:
28 def __init__(self
, db
, fs
, msg
, auth
):
30 Constructor of Vnf Instances to Ns Instances
32 self
.new_vnf_instance
= NewVnfInstance(db
, fs
, msg
, auth
)
33 self
.list_vnf_instance
= ListVnfInstance(db
, fs
, msg
, auth
)
34 self
.show_vnf_instance
= ShowVnfInstance(db
, fs
, msg
, auth
)
35 self
.delete_vnf_instance
= DeleteVnfInstance(db
, fs
, msg
, auth
)
37 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
39 Creates a new entry into database.
40 :param rollback: list to append created items at database in case a rollback may have to be done
41 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
42 :param indata: data to be inserted
43 :param kwargs: used to override the indata descriptor
44 :param headers: http request headers
46 _id: identity of the inserted data.
47 op_id: operation id if this is asynchronous, None otherwise
49 return self
.new_vnf_instance
.action(rollback
, session
, indata
, kwargs
, headers
)
51 def list(self
, session
, filter_q
=None, api_req
=False):
53 Get a list of the Vnfs that match a filter
54 :param session: contains the used login username and working project
55 :param filter_q: filter of data to be applied
56 :param api_req: True if this call is serving an external API request. False if serving internal request.
57 :return: The list, it can be empty if no one match the filter.
59 return self
.list_vnf_instance
.action(session
, filter_q
, api_req
)
61 def show(self
, session
, _id
, api_req
=False):
63 Get complete information on an Vnf
64 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
65 :param _id: server internal id
66 :param api_req: True if this call is serving an external API request. False if serving internal request.
67 :return: dictionary, raise exception if not found.
69 return self
.show_vnf_instance
.action(session
, _id
, api_req
)
71 def delete(self
, session
, _id
, dry_run
=False, not_send_msg
=None):
73 Delete item by its internal _id
74 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
75 :param _id: server internal id
76 :param dry_run: make checking but do not delete
77 :param not_send_msg: To not send message (False) or store content (list) instead
78 :return: operation id (None if there is not operation), raise exception if error or not found, conflict, ...
80 return self
.delete_vnf_instance
.action(session
, _id
, dry_run
, not_send_msg
)
83 class NewVnfInstance(BaseMethod
):
85 # sample ns descriptor
96 "virtual-link-connectivity": [
98 "constituent-cpd-id": [
99 {"constituent-base-element-id": 1,
100 "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"
133 for invalid_key
in ("vnfdId", "vnfInstanceName", "vnfInstanceDescription", "additionalParams"):
134 formatted_indata
.pop(invalid_key
)
135 return formatted_indata
137 def __init__(self
, db
, fs
, msg
, auth
):
139 Constructor for new vnf instance
143 self
.nsdtopic
= NsdTopic(db
, fs
, msg
, auth
)
144 self
.nsrtopic
= NsrTopic(db
, fs
, msg
, auth
)
146 def __get_vnfd(self
):
150 def __onboard_vnfd(self
):
154 def __create_nsd(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
156 Creates new ns descriptor from a vnfd.
157 :param rollback: list to append the created items at database in case a rollback must be done
158 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
159 :param indata: params to be used for the nsr
160 :param kwargs: used to override the indata
161 :param headers: http request headers
162 :return: id of new nsd created
164 _id
, *others
= self
.nsdtopic
.new(rollback
, session
, {}, None, headers
)
165 new_nsd
= deepcopy(NewVnfInstance
.sample_nsd
)
171 "virtual-link-connectivity": [
173 "constituent-cpd-id": [
175 "constituent-base-element-id": "1",
176 "constituent-cpd-id": indata
["additionalParams"]["constituent-cpd-id"]
179 "virtual-link-profile-id": indata
["additionalParams"]["virtual-link-profile-id"]
182 "vnfd-id": indata
["vnfdId"]
186 new_nsd
["nsd"]["nsd"][0] = {
187 "description": indata
["vnfInstanceDescription"],
189 "id": indata
["vnfdId"] + "-ns",
190 "name": indata
["vnfdId"] + "-ns",
192 "df": [vnf_content
, ],
193 "virtual-link-desc": indata
["additionalParams"]["virtual-link-desc"],
194 "vnfd-id": [indata
["vnfdId"]]
198 def __create_nsr(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
200 Creates an new ns record in database
201 :param rollback: list to append the created items at database in case a rollback must be done
202 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
203 :param indata: params to be used for the nsr
204 :param kwargs: used to override the indata
205 :param headers: http request headers
206 :return: id of new nsr
208 return self
.nsrtopic
.new(rollback
, session
, indata
, kwargs
, headers
)
210 def __action_pre_processing(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
212 Pre process for creating new vnf instance
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
220 self
.__onboard
_vnfd
()
221 nsd_id
, nsd
= self
.__create
_nsd
(rollback
, session
, indata
, kwargs
, headers
)
222 self
.nsdtopic
.upload_content(session
, nsd_id
, nsd
, kwargs
, headers
)
223 formatted_indata
= NewVnfInstance
.__get
_formatted
_indata
(indata
, nsd_id
)
224 nsr_id
, _
= self
.__create
_nsr
(rollback
, session
, formatted_indata
, kwargs
, headers
)
225 nsr
= self
.nsrtopic
.show(session
, nsr_id
)
226 vnfr_id
= nsr
['constituent-vnfr-ref'][0]
228 links
= {"vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + vnfr_id
}
229 indata
["vnfInstanceId"] = vnfr_id
230 indata
["_links"] = links
231 self
.msg
.write("vnf", "create", indata
)
234 def action(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
236 Creates an new vnf instance
237 :param rollback: list to append the created items at database in case a rollback must be done
238 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
239 :param indata: params to be used for the nsr
240 :param kwargs: used to override the indata
241 :param headers: http request headers
242 :return: id of new vnf instance
244 return self
.__action
_pre
_processing
(rollback
, session
, indata
, kwargs
, headers
)
247 class ListVnfInstance(BaseMethod
):
249 def __init__(self
, db
, fs
, msg
, auth
):
251 Constructor call for listing vnfs
254 self
.vnfrtopic
= VnfrTopic(db
, fs
, msg
, auth
)
256 def action(self
, session
, filter_q
=None, api_req
=False):
258 To get list of vnfs that matches a filter
259 :param session: contains the used login username and working project
260 :param filter_q: filter of data to be applied
261 :param api_req: True if this call is serving an external API request. False if serving internal request.
262 :return: The list, it can be empty if no one match the filter.
264 return self
.vnfrtopic
.list(session
, filter_q
, api_req
)
267 class ShowVnfInstance(BaseMethod
):
269 def __init__(self
, db
, fs
, msg
, auth
):
271 Constructor call for showing vnf lcm operation
274 self
.vnfrtopic
= VnfrTopic(db
, fs
, msg
, auth
)
276 def action(self
, session
, _id
, api_req
=False):
278 Get complete information on an Vnf Lcm Operation.
279 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
280 :param _id: Vnf Lcm operation id
281 :param api_req: True if this call is serving an external API request. False if serving internal request.
282 :return: dictionary, raise exception if not found.
284 return self
.vnfrtopic
.show(session
, _id
, api_req
)
287 class DeleteVnfInstance(BaseMethod
):
289 def __init__(self
, db
, fs
, msg
, auth
):
291 Constructor call for deleting vnf
295 self
.nsrtopic
= NsrTopic(db
, fs
, msg
, auth
)
296 self
.nsdtopic
= NsdTopic(db
, fs
, msg
, auth
)
297 self
.vnfrtopic
= VnfrTopic(db
, fs
, msg
, auth
)
299 def action(self
, session
, _id
, dry_run
=False, not_send_msg
=None):
301 Delete vnf instance by its internal _id
302 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
303 :param _id: server internal id
304 :param dry_run: make checking but do not delete
305 :param not_send_msg: To not send message (False) or store content (list) instead
306 :return: operation id (None if there is not operation), raise exception if error or not found, conflict, ...
309 vnfr
= self
.vnfrtopic
.show(session
, vnfInstanceId
)
310 ns_id
= vnfr
.get("nsr-id-ref")
311 nsr
= self
.nsrtopic
.show(session
, ns_id
)
312 nsd_to_del
= nsr
['nsd']['_id']
313 links
= {"vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + _id
}
314 params
= {"vnfdId": vnfr
["vnfd-ref"],
315 "vnfInstanceId": _id
,
317 self
.msg
.write("vnf", "delete", params
)
318 self
.nsrtopic
.delete(session
, ns_id
, dry_run
, not_send_msg
)
319 return self
.nsdtopic
.delete(session
, nsd_to_del
, dry_run
, not_send_msg
)