Revert "Revert "Feature 10915: SOL003 STD Support for OSM""
[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
28 def __init__(self, db, fs, msg, auth):
29 """
30 Constructor of Vnf Instances to Ns Instances
31 """
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)
36
37 def new(self, rollback, session, indata=None, kwargs=None, headers=None):
38 """
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
45 :return: _id, op_id:
46 _id: identity of the inserted data.
47 op_id: operation id if this is asynchronous, None otherwise
48 """
49 return self.new_vnf_instance.action(rollback, session, indata, kwargs, headers)
50
51 def list(self, session, filter_q=None, api_req=False):
52 """
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.
58 """
59 return self.list_vnf_instance.action(session, filter_q, api_req)
60
61 def show(self, session, _id, api_req=False):
62 """
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.
68 """
69 return self.show_vnf_instance.action(session, _id, api_req)
70
71 def delete(self, session, _id, dry_run=False, not_send_msg=None):
72 """
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, ...
79 """
80 return self.delete_vnf_instance.action(session, _id, dry_run, not_send_msg)
81
82
83 class NewVnfInstance(BaseMethod):
84
85 # sample ns descriptor
86 sample_nsd = {
87 "nsd": {
88 "nsd": [
89 {
90 "df": [
91 {
92 "id": "default-df",
93 "vnf-profile": [
94 {
95 "id": 1,
96 "virtual-link-connectivity": [
97 {
98 "constituent-cpd-id": [
99 {"constituent-base-element-id": 1,
100 "constituent-cpd-id": "eth0-ext"}
101 ],
102 "virtual-link-profile-id": "mgmtnet",
103 }
104 ],
105 "vnfd-id": "cirros_vnfd"
106 }
107 ],
108 }
109 ],
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",
115 "vendor": "OSM",
116 "version": "1.0",
117 }
118 ]
119 }
120 }
121
122 @staticmethod
123 def __get_formatted_indata(indata, nsd_id):
124 """
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
129 """
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
136
137 def __init__(self, db, fs, msg, auth):
138 """
139 Constructor for new vnf instance
140 """
141 super().__init__()
142 self.msg = msg
143 self.nsdtopic = NsdTopic(db, fs, msg, auth)
144 self.nsrtopic = NsrTopic(db, fs, msg, auth)
145
146 def __get_vnfd(self):
147 # get vnfd from nfvo
148 pass
149
150 def __onboard_vnfd(self):
151 self.__get_vnfd()
152 pass
153
154 def __create_nsd(self, rollback, session, indata=None, kwargs=None, headers=None):
155 """
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
163 """
164 _id, *others = self.nsdtopic.new(rollback, session, {}, None, headers)
165 new_nsd = deepcopy(NewVnfInstance.sample_nsd)
166 vnf_content = {
167 "id":"default-df",
168 "vnf-profile": [
169 {
170 "id": "1",
171 "virtual-link-connectivity": [
172 {
173 "constituent-cpd-id": [
174 {
175 "constituent-base-element-id": "1",
176 "constituent-cpd-id": indata["additionalParams"]["constituent-cpd-id"]
177 }
178 ],
179 "virtual-link-profile-id": indata["additionalParams"]["virtual-link-profile-id"]
180 }
181 ],
182 "vnfd-id": indata["vnfdId"]
183 }
184 ]
185 }
186 new_nsd["nsd"]["nsd"][0] = {
187 "description": indata["vnfInstanceDescription"],
188 "designer": "OSM",
189 "id": indata["vnfdId"] + "-ns",
190 "name": indata["vnfdId"] + "-ns",
191 "version": "1.0",
192 "df": [vnf_content, ],
193 "virtual-link-desc": indata["additionalParams"]["virtual-link-desc"],
194 "vnfd-id": [indata["vnfdId"]]
195 }
196 return _id, new_nsd
197
198 def __create_nsr(self, rollback, session, indata=None, kwargs=None, headers=None):
199 """
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
207 """
208 return self.nsrtopic.new(rollback, session, indata, kwargs, headers)
209
210 def __action_pre_processing(self, rollback, session, indata=None, kwargs=None, headers=None):
211 """
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
218 :return: id nsr
219 """
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]
227 return vnfr_id, None
228
229 def action(self, rollback, session, indata=None, kwargs=None, headers=None):
230 """
231 Creates an new vnf instance
232 :param rollback: list to append the created items at database in case a rollback must be done
233 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
234 :param indata: params to be used for the nsr
235 :param kwargs: used to override the indata
236 :param headers: http request headers
237 :return: id of new vnf instance
238 """
239 return self.__action_pre_processing(rollback, session, indata, kwargs, headers)
240
241
242 class ListVnfInstance(BaseMethod):
243
244 def __init__(self, db, fs, msg, auth):
245 """
246 Constructor call for listing vnfs
247 """
248 super().__init__()
249 self.vnfrtopic = VnfrTopic(db, fs, msg, auth)
250
251 def action(self, session, filter_q=None, api_req=False):
252 """
253 To get list of vnfs that matches a filter
254 :param session: contains the used login username and working project
255 :param filter_q: filter of data to be applied
256 :param api_req: True if this call is serving an external API request. False if serving internal request.
257 :return: The list, it can be empty if no one match the filter.
258 """
259 return self.vnfrtopic.list(session, filter_q, api_req)
260
261
262 class ShowVnfInstance(BaseMethod):
263
264 def __init__(self, db, fs, msg, auth):
265 """
266 Constructor call for showing vnf lcm operation
267 """
268 super().__init__()
269 self.vnfrtopic = VnfrTopic(db, fs, msg, auth)
270
271 def action(self, session, _id, api_req=False):
272 """
273 Get complete information on an Vnf Lcm Operation.
274 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
275 :param _id: Vnf Lcm operation id
276 :param api_req: True if this call is serving an external API request. False if serving internal request.
277 :return: dictionary, raise exception if not found.
278 """
279 return self.vnfrtopic.show(session, _id, api_req)
280
281
282 class DeleteVnfInstance(BaseMethod):
283
284 def __init__(self, db, fs, msg, auth):
285 """
286 Constructor call for deleting vnf
287 """
288 super().__init__()
289 self.msg = msg
290 self.nsrtopic = NsrTopic(db, fs, msg, auth)
291 self.nsdtopic = NsdTopic(db, fs, msg, auth)
292 self.vnfrtopic = VnfrTopic(db, fs, msg, auth)
293
294 def action(self, session, _id, dry_run=False, not_send_msg=None):
295 """
296 Delete vnf instance by its internal _id
297 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
298 :param _id: server internal id
299 :param dry_run: make checking but do not delete
300 :param not_send_msg: To not send message (False) or store content (list) instead
301 :return: operation id (None if there is not operation), raise exception if error or not found, conflict, ...
302 """
303 vnfInstanceId = _id
304 vnfr = self.vnfrtopic.show(session, vnfInstanceId)
305 ns_id = vnfr.get("nsr-id-ref")
306 nsr = self.nsrtopic.show(session, ns_id)
307 nsd_to_del = nsr['nsd']['_id']
308 self.nsrtopic.delete(session, ns_id, dry_run, not_send_msg)
309 return self.nsdtopic.delete(session, nsd_to_del, dry_run, not_send_msg)