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 |
1 |
__author__ = "K Sai Kiran <saikiran.k@tataelxsi.co.in>, Selvi Jayaraman <selvi.j@tataelxsi.co.in>" |
17 |
1 |
__date__ = "$12-June-2021 8:30:59$" |
18 |
|
|
19 |
1 |
from copy import deepcopy |
20 |
1 |
from osm_nbi.descriptor_topics import NsdTopic |
21 |
1 |
from .base_methods import BaseMethod |
22 |
|
|
23 |
1 |
from osm_nbi.instance_topics import NsrTopic, VnfrTopic |
24 |
|
|
25 |
|
|
26 |
1 |
class VnfInstances2NsInstances: |
27 |
|
|
28 |
1 |
def __init__(self, db, fs, msg, auth): |
29 |
|
""" |
30 |
|
Constructor of Vnf Instances to Ns Instances |
31 |
|
""" |
32 |
1 |
self.new_vnf_instance = NewVnfInstance(db, fs, msg, auth) |
33 |
1 |
self.list_vnf_instance = ListVnfInstance(db, fs, msg, auth) |
34 |
1 |
self.show_vnf_instance = ShowVnfInstance(db, fs, msg, auth) |
35 |
1 |
self.delete_vnf_instance = DeleteVnfInstance(db, fs, msg, auth) |
36 |
|
|
37 |
1 |
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 |
1 |
return self.new_vnf_instance.action(rollback, session, indata, kwargs, headers) |
50 |
|
|
51 |
1 |
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 |
0 |
return self.list_vnf_instance.action(session, filter_q, api_req) |
60 |
|
|
61 |
1 |
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 |
1 |
return self.show_vnf_instance.action(session, _id, api_req) |
70 |
|
|
71 |
1 |
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 |
1 |
return self.delete_vnf_instance.action(session, _id, dry_run, not_send_msg) |
81 |
|
|
82 |
|
|
83 |
1 |
class NewVnfInstance(BaseMethod): |
84 |
|
|
85 |
|
# sample ns descriptor |
86 |
1 |
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 |
1 |
@staticmethod |
123 |
1 |
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 |
1 |
formatted_indata = deepcopy(indata) |
131 |
1 |
formatted_indata["nsdId"] = nsd_id |
132 |
1 |
formatted_indata["nsName"] = indata["vnfInstanceName"] + "-ns" |
133 |
1 |
for invalid_key in ("vnfdId", "vnfInstanceName", "vnfInstanceDescription", "additionalParams"): |
134 |
1 |
formatted_indata.pop(invalid_key) |
135 |
1 |
return formatted_indata |
136 |
|
|
137 |
1 |
def __init__(self, db, fs, msg, auth): |
138 |
|
""" |
139 |
|
Constructor for new vnf instance |
140 |
|
""" |
141 |
1 |
super().__init__() |
142 |
1 |
self.msg = msg |
143 |
1 |
self.nsdtopic = NsdTopic(db, fs, msg, auth) |
144 |
1 |
self.nsrtopic = NsrTopic(db, fs, msg, auth) |
145 |
|
|
146 |
1 |
def __get_vnfd(self): |
147 |
|
# get vnfd from nfvo |
148 |
1 |
pass |
149 |
|
|
150 |
1 |
def __onboard_vnfd(self): |
151 |
1 |
self.__get_vnfd() |
152 |
1 |
pass |
153 |
|
|
154 |
1 |
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 |
1 |
_id, *others = self.nsdtopic.new(rollback, session, {}, None, headers) |
165 |
1 |
new_nsd = deepcopy(NewVnfInstance.sample_nsd) |
166 |
1 |
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 |
1 |
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 |
1 |
return _id, new_nsd |
197 |
|
|
198 |
1 |
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 |
1 |
return self.nsrtopic.new(rollback, session, indata, kwargs, headers) |
209 |
|
|
210 |
1 |
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 |
1 |
self.__onboard_vnfd() |
221 |
1 |
nsd_id, nsd = self.__create_nsd(rollback, session, indata, kwargs, headers) |
222 |
1 |
self.nsdtopic.upload_content(session, nsd_id, nsd, kwargs, headers) |
223 |
1 |
formatted_indata = NewVnfInstance.__get_formatted_indata(indata, nsd_id) |
224 |
1 |
nsr_id, _ = self.__create_nsr(rollback, session, formatted_indata, kwargs, headers) |
225 |
1 |
nsr = self.nsrtopic.show(session, nsr_id) |
226 |
1 |
vnfr_id = nsr['constituent-vnfr-ref'][0] |
227 |
1 |
if vnfr_id: |
228 |
1 |
links = {"vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + vnfr_id} |
229 |
1 |
indata["vnfInstanceId"] = vnfr_id |
230 |
1 |
indata["_links"] = links |
231 |
1 |
self.msg.write("vnf", "create", indata) |
232 |
1 |
return vnfr_id, None |
233 |
|
|
234 |
1 |
def action(self, rollback, session, indata=None, kwargs=None, headers=None): |
235 |
|
""" |
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 |
243 |
|
""" |
244 |
1 |
return self.__action_pre_processing(rollback, session, indata, kwargs, headers) |
245 |
|
|
246 |
|
|
247 |
1 |
class ListVnfInstance(BaseMethod): |
248 |
|
|
249 |
1 |
def __init__(self, db, fs, msg, auth): |
250 |
|
""" |
251 |
|
Constructor call for listing vnfs |
252 |
|
""" |
253 |
1 |
super().__init__() |
254 |
1 |
self.vnfrtopic = VnfrTopic(db, fs, msg, auth) |
255 |
|
|
256 |
1 |
def action(self, session, filter_q=None, api_req=False): |
257 |
|
""" |
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. |
263 |
|
""" |
264 |
0 |
return self.vnfrtopic.list(session, filter_q, api_req) |
265 |
|
|
266 |
|
|
267 |
1 |
class ShowVnfInstance(BaseMethod): |
268 |
|
|
269 |
1 |
def __init__(self, db, fs, msg, auth): |
270 |
|
""" |
271 |
|
Constructor call for showing vnf lcm operation |
272 |
|
""" |
273 |
1 |
super().__init__() |
274 |
1 |
self.vnfrtopic = VnfrTopic(db, fs, msg, auth) |
275 |
|
|
276 |
1 |
def action(self, session, _id, api_req=False): |
277 |
|
""" |
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. |
283 |
|
""" |
284 |
1 |
return self.vnfrtopic.show(session, _id, api_req) |
285 |
|
|
286 |
|
|
287 |
1 |
class DeleteVnfInstance(BaseMethod): |
288 |
|
|
289 |
1 |
def __init__(self, db, fs, msg, auth): |
290 |
|
""" |
291 |
|
Constructor call for deleting vnf |
292 |
|
""" |
293 |
1 |
super().__init__() |
294 |
1 |
self.msg = msg |
295 |
1 |
self.nsrtopic = NsrTopic(db, fs, msg, auth) |
296 |
1 |
self.nsdtopic = NsdTopic(db, fs, msg, auth) |
297 |
1 |
self.vnfrtopic = VnfrTopic(db, fs, msg, auth) |
298 |
|
|
299 |
1 |
def action(self, session, _id, dry_run=False, not_send_msg=None): |
300 |
|
""" |
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, ... |
307 |
|
""" |
308 |
1 |
vnfInstanceId = _id |
309 |
1 |
vnfr = self.vnfrtopic.show(session, vnfInstanceId) |
310 |
1 |
ns_id = vnfr.get("nsr-id-ref") |
311 |
1 |
nsr = self.nsrtopic.show(session, ns_id) |
312 |
1 |
nsd_to_del = nsr['nsd']['_id'] |
313 |
1 |
links = {"vnfInstance": "/osm/vnflcm/v1/vnf_instances/" + _id} |
314 |
1 |
params = {"vnfdId": vnfr["vnfd-ref"], |
315 |
|
"vnfInstanceId": _id, |
316 |
|
"_links": links} |
317 |
1 |
self.msg.write("vnf", "delete", params) |
318 |
1 |
self.nsrtopic.delete(session, ns_id, dry_run, not_send_msg) |
319 |
1 |
return self.nsdtopic.delete(session, nsd_to_del, dry_run, not_send_msg) |