blob: b5e664260ca40f4a353e43ecca3fb3f868922fc4 [file] [log] [blame]
Felipe Vicensc2033f22018-11-15 15:09:58 +01001# -*- coding: utf-8 -*-
tierno8069ce52019-08-28 15:34:33 +00002##
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# 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, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
14##
Felipe Vicensc2033f22018-11-15 15:09:58 +010015
16import asyncio
17import logging
18import logging.handlers
19import traceback
tiernob996d942020-07-03 14:52:28 +000020from osm_lcm import ROclient
garciadeblas5697b8b2021-03-24 09:17:02 +010021from osm_lcm.lcm_utils import (
22 LcmException,
23 LcmBase,
24 populate_dict,
25 get_iterable,
26 deep_get,
27)
Felipe Vicensc2033f22018-11-15 15:09:58 +010028from osm_common.dbbase import DbException
29from time import time
Felipe Vicens6559b4a2018-12-01 04:40:48 +010030from copy import deepcopy
31
Felipe Vicensc2033f22018-11-15 15:09:58 +010032
33__author__ = "Felipe Vicens, Pol Alemany, Alfonso Tierno"
34
35
36class NetsliceLcm(LcmBase):
Gabriel Cubae7898982023-05-11 01:57:21 -050037 def __init__(self, msg, lcm_tasks, config, ns):
Felipe Vicensc2033f22018-11-15 15:09:58 +010038 """
39 Init, Connect to database, filesystem storage, and messaging
40 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
41 :return: None
42 """
43 # logging
garciadeblas5697b8b2021-03-24 09:17:02 +010044 self.logger = logging.getLogger("lcm.netslice")
Felipe Vicensc2033f22018-11-15 15:09:58 +010045 self.lcm_tasks = lcm_tasks
tiernob996d942020-07-03 14:52:28 +000046 self.ns = ns
Luis Vegaa27dc532022-11-11 20:10:49 +000047 self.ro_config = config["RO"]
tierno744303e2020-01-13 16:46:31 +000048 self.timeout = config["timeout"]
Felipe Vicensc2033f22018-11-15 15:09:58 +010049
bravof922c4172020-11-24 21:21:43 -030050 super().__init__(msg, self.logger)
Felipe Vicensc2033f22018-11-15 15:09:58 +010051
Felipe Vicens0f389ce2019-01-12 12:20:11 +010052 def nsi_update_nsir(self, nsi_update_nsir, db_nsir, nsir_desc_RO):
53 """
54 Updates database nsir with the RO info for the created vld
55 :param nsi_update_nsir: dictionary to be filled with the updated info
56 :param db_nsir: content of db_nsir. This is also modified
57 :param nsir_desc_RO: nsir descriptor from RO
58 :return: Nothing, LcmException is raised on errors
59 """
60
61 for vld_index, vld in enumerate(get_iterable(db_nsir, "vld")):
62 for net_RO in get_iterable(nsir_desc_RO, "nets"):
63 if vld["id"] != net_RO.get("ns_net_osm_id"):
64 continue
65 vld["vim-id"] = net_RO.get("vim_net_id")
66 vld["name"] = net_RO.get("vim_name")
67 vld["status"] = net_RO.get("status")
68 vld["status-detailed"] = net_RO.get("error_msg")
69 nsi_update_nsir["vld.{}".format(vld_index)] = vld
70 break
71 else:
garciadeblas5697b8b2021-03-24 09:17:02 +010072 raise LcmException(
73 "ns_update_nsir: Not found vld={} at RO info".format(vld["id"])
74 )
Felipe Vicens0f389ce2019-01-12 12:20:11 +010075
Felipe Vicensc2033f22018-11-15 15:09:58 +010076 async def instantiate(self, nsir_id, nsilcmop_id):
kuused124bfe2019-06-18 12:09:24 +020077 # Try to lock HA task here
garciadeblas5697b8b2021-03-24 09:17:02 +010078 task_is_locked_by_me = self.lcm_tasks.lock_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +020079 if not task_is_locked_by_me:
80 return
81
Felipe Vicensc2033f22018-11-15 15:09:58 +010082 logging_text = "Task netslice={} instantiate={} ".format(nsir_id, nsilcmop_id)
83 self.logger.debug(logging_text + "Enter")
84 # get all needed from database
Felipe Vicens0f389ce2019-01-12 12:20:11 +010085 exc = None
Felipe Vicensc2033f22018-11-15 15:09:58 +010086 db_nsir = None
87 db_nsilcmop = None
88 db_nsir_update = {"_admin.nsilcmop": nsilcmop_id}
89 db_nsilcmop_update = {}
90 nsilcmop_operation_state = None
Felipe Vicens6559b4a2018-12-01 04:40:48 +010091 vim_2_RO = {}
Gabriel Cubae7898982023-05-11 01:57:21 -050092 RO = ROclient.ROClient(**self.ro_config)
bravof922c4172020-11-24 21:21:43 -030093 nsi_vld_instantiationi_params = {}
Felipe Vicens720b07a2019-01-31 02:32:09 +010094
95 def ip_profile_2_RO(ip_profile):
96 RO_ip_profile = deepcopy((ip_profile))
97 if "dns-server" in RO_ip_profile:
98 if isinstance(RO_ip_profile["dns-server"], list):
99 RO_ip_profile["dns-address"] = []
100 for ds in RO_ip_profile.pop("dns-server"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100101 RO_ip_profile["dns-address"].append(ds["address"])
Felipe Vicens720b07a2019-01-31 02:32:09 +0100102 else:
103 RO_ip_profile["dns-address"] = RO_ip_profile.pop("dns-server")
104 if RO_ip_profile.get("ip-version") == "ipv4":
105 RO_ip_profile["ip-version"] = "IPv4"
106 if RO_ip_profile.get("ip-version") == "ipv6":
107 RO_ip_profile["ip-version"] = "IPv6"
108 if "dhcp-params" in RO_ip_profile:
109 RO_ip_profile["dhcp"] = RO_ip_profile.pop("dhcp-params")
110 return RO_ip_profile
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100111
112 def vim_account_2_RO(vim_account):
113 """
114 Translate a RO vim_account from OSM vim_account params
115 :param ns_params: OSM instantiate params
116 :return: The RO ns descriptor
117 """
118 if vim_account in vim_2_RO:
119 return vim_2_RO[vim_account]
120
121 db_vim = self.db.get_one("vim_accounts", {"_id": vim_account})
122 if db_vim["_admin"]["operationalState"] != "ENABLED":
garciadeblas5697b8b2021-03-24 09:17:02 +0100123 raise LcmException(
124 "VIM={} is not available. operationalState={}".format(
125 vim_account, db_vim["_admin"]["operationalState"]
126 )
127 )
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100128 RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
129 vim_2_RO[vim_account] = RO_vim_id
130 return RO_vim_id
131
garciadeblas5697b8b2021-03-24 09:17:02 +0100132 async def netslice_scenario_create(
133 self, vld_item, nsir_id, db_nsir, db_nsir_admin, db_nsir_update
134 ):
Felipe Vicens720b07a2019-01-31 02:32:09 +0100135 """
136 Create a network slice VLD through RO Scenario
137 :param vld_id The VLD id inside nsir to be created
138 :param nsir_id The nsir id
139 """
Anirudh Guptac2be9492025-05-13 05:53:38 +0000140 # nonlocal nsi_vld_instantiationi_params
Felipe Vicens720b07a2019-01-31 02:32:09 +0100141 ip_vld = None
142 mgmt_network = False
143 RO_vld_sites = []
144 vld_id = vld_item["id"]
145 netslice_vld = vld_item
146 # logging_text = "Task netslice={} instantiate_vld={} ".format(nsir_id, vld_id)
147 # self.logger.debug(logging_text + "Enter")
148
149 vld_shared = None
150 for shared_nsrs_item in get_iterable(vld_item, "shared-nsrs-list"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100151 _filter = {
152 "_id.ne": nsir_id,
153 "_admin.nsrs-detailed-list.ANYINDEX.nsrId": shared_nsrs_item,
154 }
155 shared_nsi = self.db.get_one(
156 "nsis", _filter, fail_on_empty=False, fail_on_more=False
157 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100158 if shared_nsi:
159 for vlds in get_iterable(shared_nsi["_admin"]["deployed"], "RO"):
160 if vld_id == vlds["vld_id"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100161 vld_shared = {
162 "instance_scenario_id": vlds["netslice_scenario_id"],
163 "osm_id": vld_id,
164 }
Felipe Vicens720b07a2019-01-31 02:32:09 +0100165 break
166 break
167
168 # Creating netslice-vld at RO
tierno744303e2020-01-13 16:46:31 +0000169 RO_nsir = deep_get(db_nsir, ("_admin", "deployed", "RO"), [])
Felipe Vicens720b07a2019-01-31 02:32:09 +0100170
171 if vld_id in RO_nsir:
172 db_nsir_update["_admin.deployed.RO"] = RO_nsir
173
174 # If netslice-vld doesn't exists then create it
175 else:
176 # TODO: Check VDU type in all descriptors finding SRIOV / PT
177 # Updating network names and datacenters from instantiation parameters for each VLD
garciadeblas5697b8b2021-03-24 09:17:02 +0100178 for instantiation_params_vld in get_iterable(
179 db_nsir["instantiation_parameters"], "netslice-vld"
180 ):
Felipe Vicens720b07a2019-01-31 02:32:09 +0100181 if instantiation_params_vld.get("name") == netslice_vld["name"]:
182 ip_vld = deepcopy(instantiation_params_vld)
bravof922c4172020-11-24 21:21:43 -0300183 ip_vld.pop("name")
184 nsi_vld_instantiationi_params[netslice_vld["name"]] = ip_vld
Felipe Vicens720b07a2019-01-31 02:32:09 +0100185
bravof922c4172020-11-24 21:21:43 -0300186 db_nsir_update_RO = {}
187 db_nsir_update_RO["vld_id"] = netslice_vld["name"]
188 if self.ro_config["ng"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100189 db_nsir_update_RO["netslice_scenario_id"] = (
190 vld_shared.get("instance_scenario_id")
191 if vld_shared
bravof922c4172020-11-24 21:21:43 -0300192 else "nsir:{}:vld.{}".format(nsir_id, netslice_vld["name"])
garciadeblas5697b8b2021-03-24 09:17:02 +0100193 )
bravof922c4172020-11-24 21:21:43 -0300194 else: # if not self.ro_config["ng"]:
195 if netslice_vld.get("mgmt-network"):
196 mgmt_network = True
197 RO_ns_params = {}
198 RO_ns_params["name"] = netslice_vld["name"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100199 RO_ns_params["datacenter"] = vim_account_2_RO(
200 db_nsir["instantiation_parameters"]["vimAccountId"]
201 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100202
bravof922c4172020-11-24 21:21:43 -0300203 # Creating scenario if vim-network-name / vim-network-id are present as instantiation parameter
204 # Use vim-network-id instantiation parameter
205 vim_network_option = None
206 if ip_vld:
207 if ip_vld.get("vim-network-id"):
208 vim_network_option = "vim-network-id"
209 elif ip_vld.get("vim-network-name"):
210 vim_network_option = "vim-network-name"
211 if ip_vld.get("ip-profile"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100212 populate_dict(
213 RO_ns_params,
214 ("networks", netslice_vld["name"], "ip-profile"),
215 ip_profile_2_RO(ip_vld["ip-profile"]),
216 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100217
bravof922c4172020-11-24 21:21:43 -0300218 if vim_network_option:
219 if ip_vld.get(vim_network_option):
220 if isinstance(ip_vld.get(vim_network_option), list):
221 for vim_net_id in ip_vld.get(vim_network_option):
222 for vim_account, vim_net in vim_net_id.items():
garciadeblas5697b8b2021-03-24 09:17:02 +0100223 RO_vld_sites.append(
224 {
225 "netmap-use": vim_net,
226 "datacenter": vim_account_2_RO(
227 vim_account
228 ),
229 }
230 )
bravof922c4172020-11-24 21:21:43 -0300231 elif isinstance(ip_vld.get(vim_network_option), dict):
garciadeblas5697b8b2021-03-24 09:17:02 +0100232 for vim_account, vim_net in ip_vld.get(
233 vim_network_option
234 ).items():
235 RO_vld_sites.append(
236 {
237 "netmap-use": vim_net,
238 "datacenter": vim_account_2_RO(vim_account),
239 }
240 )
bravof922c4172020-11-24 21:21:43 -0300241 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100242 RO_vld_sites.append(
243 {
244 "netmap-use": ip_vld[vim_network_option],
245 "datacenter": vim_account_2_RO(
246 netslice_vld["vimAccountId"]
247 ),
248 }
249 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100250
bravof922c4172020-11-24 21:21:43 -0300251 # Use default netslice vim-network-name from template
252 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100253 for nss_conn_point_ref in get_iterable(
254 netslice_vld, "nss-connection-point-ref"
255 ):
bravof922c4172020-11-24 21:21:43 -0300256 if nss_conn_point_ref.get("vimAccountId"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100257 if (
258 nss_conn_point_ref["vimAccountId"]
259 != netslice_vld["vimAccountId"]
260 ):
261 RO_vld_sites.append(
262 {
263 "netmap-create": None,
264 "datacenter": vim_account_2_RO(
265 nss_conn_point_ref["vimAccountId"]
266 ),
267 }
268 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100269
bravof922c4172020-11-24 21:21:43 -0300270 if vld_shared:
garciadeblas5697b8b2021-03-24 09:17:02 +0100271 populate_dict(
272 RO_ns_params,
273 ("networks", netslice_vld["name"], "use-network"),
274 vld_shared,
275 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100276
bravof922c4172020-11-24 21:21:43 -0300277 if RO_vld_sites:
garciadeblas5697b8b2021-03-24 09:17:02 +0100278 populate_dict(
279 RO_ns_params,
280 ("networks", netslice_vld["name"], "sites"),
281 RO_vld_sites,
282 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100283
garciadeblas5697b8b2021-03-24 09:17:02 +0100284 RO_ns_params["scenario"] = {
285 "nets": [
286 {
287 "name": netslice_vld["name"],
288 "external": mgmt_network,
289 "type": "bridge",
290 }
291 ]
292 }
bravof922c4172020-11-24 21:21:43 -0300293
294 # self.logger.debug(logging_text + step)
295 desc = await RO.create("ns", descriptor=RO_ns_params)
296 db_nsir_update_RO["netslice_scenario_id"] = desc["uuid"]
Felipe Vicens720b07a2019-01-31 02:32:09 +0100297 db_nsir_update["_admin.deployed.RO"].append(db_nsir_update_RO)
kuused124bfe2019-06-18 12:09:24 +0200298
Felipe Vicens720b07a2019-01-31 02:32:09 +0100299 def overwrite_nsd_params(self, db_nsir, nslcmop):
Anirudh Guptac2be9492025-05-13 05:53:38 +0000300 # nonlocal nsi_vld_instantiationi_params
301 # nonlocal db_nsir_update
Felipe Vicens720b07a2019-01-31 02:32:09 +0100302 vld_op_list = []
303 vld = None
304 nsr_id = nslcmop.get("nsInstanceId")
305 # Overwrite instantiation parameters in netslice runtime
bravof922c4172020-11-24 21:21:43 -0300306 RO_list = db_nsir_admin["deployed"]["RO"]
Felipe Vicens720b07a2019-01-31 02:32:09 +0100307
bravof922c4172020-11-24 21:21:43 -0300308 for ro_item_index, RO_item in enumerate(RO_list):
garciadeblas5697b8b2021-03-24 09:17:02 +0100309 netslice_vld = next(
310 (
311 n
312 for n in get_iterable(db_nsir["_admin"], "netslice-vld")
313 if RO_item.get("vld_id") == n.get("id")
314 ),
315 None,
316 )
bravof922c4172020-11-24 21:21:43 -0300317 if not netslice_vld:
318 continue
319 # if is equal vld of _admin with vld of netslice-vld then go for the CPs
320 # Search the cp of netslice-vld that match with nst:netslice-subnet
garciadeblas5697b8b2021-03-24 09:17:02 +0100321 for nss_cp_item in get_iterable(
322 netslice_vld, "nss-connection-point-ref"
323 ):
bravof922c4172020-11-24 21:21:43 -0300324 # Search the netslice-subnet of nst that match
garciadeblas5697b8b2021-03-24 09:17:02 +0100325 nss = next(
326 (
327 nss
328 for nss in get_iterable(
329 db_nsir["_admin"], "netslice-subnet"
330 )
331 if nss_cp_item["nss-ref"] == nss["nss-id"]
332 ),
333 None,
334 )
bravof922c4172020-11-24 21:21:43 -0300335 # Compare nss-ref equal nss from nst
336 if not nss:
337 continue
338 db_nsds = self.db.get_one("nsds", {"_id": nss["nsdId"]})
339 # Go for nsd, and search the CP that match with nst:CP to get vld-id-ref
bravofa7cb70b2020-12-11 16:37:01 -0300340 for cp_nsd in db_nsds.get("sapd", ()):
341 if cp_nsd["id"] == nss_cp_item["nsd-connection-point-ref"]:
bravof922c4172020-11-24 21:21:43 -0300342 if nslcmop.get("operationParams"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100343 if (
344 nslcmop["operationParams"].get("nsName")
345 == nss["nsName"]
346 ):
bravof922c4172020-11-24 21:21:43 -0300347 vld_id = RO_item["vld_id"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100348 netslice_scenario_id = RO_item[
349 "netslice_scenario_id"
350 ]
bravof922c4172020-11-24 21:21:43 -0300351 nslcmop_vld = {}
bravofa7cb70b2020-12-11 16:37:01 -0300352 nslcmop_vld["name"] = cp_nsd["virtual-link-desc"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100353 for vld in get_iterable(
354 nslcmop["operationParams"], "vld"
355 ):
bravofa7cb70b2020-12-11 16:37:01 -0300356 if vld["name"] == cp_nsd["virtual-link-desc"]:
bravof922c4172020-11-24 21:21:43 -0300357 nslcmop_vld.update(vld)
358 if self.ro_config["ng"]:
359 nslcmop_vld["common_id"] = netslice_scenario_id
garciadeblas5697b8b2021-03-24 09:17:02 +0100360 nslcmop_vld.update(
361 nsi_vld_instantiationi_params.get(
362 RO_item["vld_id"], {}
363 )
364 )
bravof922c4172020-11-24 21:21:43 -0300365 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100366 nslcmop_vld["ns-net"] = {
367 vld_id: netslice_scenario_id
368 }
bravof922c4172020-11-24 21:21:43 -0300369 vld_op_list.append(nslcmop_vld)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100370 nslcmop["operationParams"]["vld"] = vld_op_list
garciadeblas5697b8b2021-03-24 09:17:02 +0100371 self.update_db_2(
372 "nslcmops", nslcmop["_id"], {"operationParams.vld": vld_op_list}
373 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100374 return nsr_id, nslcmop
375
Felipe Vicensc2033f22018-11-15 15:09:58 +0100376 try:
kuused124bfe2019-06-18 12:09:24 +0200377 # wait for any previous tasks in process
garciadeblas5697b8b2021-03-24 09:17:02 +0100378 await self.lcm_tasks.waitfor_related_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +0200379
Felipe Vicensc2033f22018-11-15 15:09:58 +0100380 step = "Getting nsir={} from db".format(nsir_id)
381 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
382 step = "Getting nsilcmop={} from db".format(nsilcmop_id)
383 db_nsilcmop = self.db.get_one("nsilcmops", {"_id": nsilcmop_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100384
tierno744303e2020-01-13 16:46:31 +0000385 start_deploy = time()
386 nsi_params = db_nsilcmop.get("operationParams")
387 if nsi_params and nsi_params.get("timeout_nsi_deploy"):
388 timeout_nsi_deploy = nsi_params["timeout_nsi_deploy"]
389 else:
Gabriel Cubaa89a5a72022-11-26 18:55:15 -0500390 timeout_nsi_deploy = self.timeout.get("nsi_deploy")
tierno744303e2020-01-13 16:46:31 +0000391
Felipe Vicensc2033f22018-11-15 15:09:58 +0100392 # Empty list to keep track of network service records status in the netslice
Felipe Vicens720b07a2019-01-31 02:32:09 +0100393 nsir_admin = db_nsir_admin = db_nsir.get("_admin")
Felipe Vicensc2033f22018-11-15 15:09:58 +0100394
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100395 step = "Creating slice operational-status init"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100396 # Slice status Creating
397 db_nsir_update["detailed-status"] = "creating"
398 db_nsir_update["operational-status"] = "init"
tierno5e98d462020-08-10 13:21:28 +0000399 db_nsir_update["_admin.nsiState"] = "INSTANTIATED"
kuused124bfe2019-06-18 12:09:24 +0200400
tierno5e98d462020-08-10 13:21:28 +0000401 step = "Instantiating netslice VLDs before NS instantiation"
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100402 # Creating netslice VLDs networking before NS instantiation
tierno5e98d462020-08-10 13:21:28 +0000403 db_nsir_update["detailed-status"] = step
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100404 self.update_db_2("nsis", nsir_id, db_nsir_update)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100405 db_nsir_update["_admin.deployed.RO"] = db_nsir_admin["deployed"]["RO"]
406 for vld_item in get_iterable(nsir_admin, "netslice-vld"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100407 await netslice_scenario_create(
408 self, vld_item, nsir_id, db_nsir, db_nsir_admin, db_nsir_update
409 )
kuused124bfe2019-06-18 12:09:24 +0200410
tierno5e98d462020-08-10 13:21:28 +0000411 step = "Instantiating netslice subnets"
412 db_nsir_update["detailed-status"] = step
kuused124bfe2019-06-18 12:09:24 +0200413 self.update_db_2("nsis", nsir_id, db_nsir_update)
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100414
Felipe Vicens720b07a2019-01-31 02:32:09 +0100415 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100416
Felipe Vicens720b07a2019-01-31 02:32:09 +0100417 # Check status of the VLDs and wait for creation
418 # netslice_scenarios = db_nsir["_admin"]["deployed"]["RO"]
419 # db_nsir_update_RO = deepcopy(netslice_scenarios)
420 # for netslice_scenario in netslice_scenarios:
kuused124bfe2019-06-18 12:09:24 +0200421 # await netslice_scenario_check(self, netslice_scenario["netslice_scenario_id"],
Felipe Vicens720b07a2019-01-31 02:32:09 +0100422 # nsir_id, db_nsir_update_RO)
kuused124bfe2019-06-18 12:09:24 +0200423
Felipe Vicens720b07a2019-01-31 02:32:09 +0100424 # db_nsir_update["_admin.deployed.RO"] = db_nsir_update_RO
425 # self.update_db_2("nsis", nsir_id, db_nsir_update)
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100426
Felipe Vicensc2033f22018-11-15 15:09:58 +0100427 # Iterate over the network services operation ids to instantiate NSs
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100428 step = "Instantiating Netslice Subnets"
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100429 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicensc2033f22018-11-15 15:09:58 +0100430 nslcmop_ids = db_nsilcmop["operationParams"].get("nslcmops_ids")
431 for nslcmop_id in nslcmop_ids:
432 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100433 # Overwriting netslice-vld vim-net-id to ns
434 nsr_id, nslcmop = overwrite_nsd_params(self, db_nsir, nslcmop)
435 step = "Launching ns={} instantiate={} task".format(nsr_id, nslcmop_id)
Felipe Vicensc2033f22018-11-15 15:09:58 +0100436 task = asyncio.ensure_future(self.ns.instantiate(nsr_id, nslcmop_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100437 self.lcm_tasks.register(
438 "ns", nsr_id, nslcmop_id, "ns_instantiate", task
439 )
kuused124bfe2019-06-18 12:09:24 +0200440
Felipe Vicensc2033f22018-11-15 15:09:58 +0100441 # Wait until Network Slice is ready
tierno5e98d462020-08-10 13:21:28 +0000442 step = " Waiting nsi ready."
Felipe Vicensc2033f22018-11-15 15:09:58 +0100443 nsrs_detailed_list_old = None
444 self.logger.debug(logging_text + step)
kuused124bfe2019-06-18 12:09:24 +0200445
tierno5e98d462020-08-10 13:21:28 +0000446 # For HA, it is checked from database, as the ns operation may be managed by other LCM worker
tierno744303e2020-01-13 16:46:31 +0000447 while time() <= start_deploy + timeout_nsi_deploy:
Felipe Vicensc2033f22018-11-15 15:09:58 +0100448 # Check ns instantiation status
449 nsi_ready = True
Felipe Vicens720b07a2019-01-31 02:32:09 +0100450 nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100451 nsrs_detailed_list = nsir["_admin"]["nsrs-detailed-list"]
452 nsrs_detailed_list_new = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100453 for nslcmop_item in nslcmop_ids:
454 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_item})
455 status = nslcmop.get("operationState")
456 # TODO: (future improvement) other possible status: ROLLING_BACK,ROLLED_BACK
Felipe Vicens720b07a2019-01-31 02:32:09 +0100457 for nss in nsrs_detailed_list:
458 if nss["nsrId"] == nslcmop["nsInstanceId"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100459 nss.update(
460 {
461 "nsrId": nslcmop["nsInstanceId"],
462 "status": nslcmop["operationState"],
463 "detailed-status": nslcmop.get("detailed-status"),
464 "instantiated": True,
465 }
466 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100467 nsrs_detailed_list_new.append(nss)
garciadeblas5697b8b2021-03-24 09:17:02 +0100468 if status not in [
469 "COMPLETED",
470 "PARTIALLY_COMPLETED",
471 "FAILED",
472 "FAILED_TEMP",
473 ]:
Felipe Vicensc2033f22018-11-15 15:09:58 +0100474 nsi_ready = False
475
Felipe Vicens720b07a2019-01-31 02:32:09 +0100476 if nsrs_detailed_list_new != nsrs_detailed_list_old:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100477 nsrs_detailed_list_old = nsrs_detailed_list_new
garciadeblas5697b8b2021-03-24 09:17:02 +0100478 self.update_db_2(
479 "nsis",
480 nsir_id,
481 {"_admin.nsrs-detailed-list": nsrs_detailed_list_new},
482 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100483
484 if nsi_ready:
tierno5e98d462020-08-10 13:21:28 +0000485 error_list = []
486 step = "Network Slice Instance instantiated"
487 for nss in nsrs_detailed_list:
488 if nss["status"] in ("FAILED", "FAILED_TEMP"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100489 error_list.append(
490 "NS {} {}: {}".format(
491 nss["nsrId"], nss["status"], nss["detailed-status"]
492 )
493 )
tierno5e98d462020-08-10 13:21:28 +0000494 if error_list:
495 step = "instantiating"
496 raise LcmException("; ".join(error_list))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100497 break
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100498
Felipe Vicensc2033f22018-11-15 15:09:58 +0100499 # TODO: future improvement due to synchronism -> await asyncio.wait(vca_task_list, timeout=300)
Gabriel Cubae7898982023-05-11 01:57:21 -0500500 await asyncio.sleep(5)
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100501
garciadeblas5697b8b2021-03-24 09:17:02 +0100502 else: # timeout_nsi_deploy reached:
tierno5e98d462020-08-10 13:21:28 +0000503 raise LcmException("Timeout waiting nsi to be ready.")
Felipe Vicensc2033f22018-11-15 15:09:58 +0100504
505 db_nsir_update["operational-status"] = "running"
506 db_nsir_update["detailed-status"] = "done"
507 db_nsir_update["config-status"] = "configured"
garciadeblas5697b8b2021-03-24 09:17:02 +0100508 db_nsilcmop_update[
509 "operationState"
510 ] = nsilcmop_operation_state = "COMPLETED"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100511 db_nsilcmop_update["statusEnteredTime"] = time()
512 db_nsilcmop_update["detailed-status"] = "done"
513 return
514
515 except (LcmException, DbException) as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100516 self.logger.error(
517 logging_text + "Exit Exception while '{}': {}".format(step, e)
518 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100519 exc = e
520 except asyncio.CancelledError:
garciadeblas5697b8b2021-03-24 09:17:02 +0100521 self.logger.error(
522 logging_text + "Cancelled Exception while '{}'".format(step)
523 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100524 exc = "Operation was cancelled"
525 except Exception as e:
526 exc = traceback.format_exc()
garciadeblas5697b8b2021-03-24 09:17:02 +0100527 self.logger.critical(
528 logging_text
529 + "Exit Exception {} while '{}': {}".format(type(e).__name__, step, e),
530 exc_info=True,
531 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100532 finally:
533 if exc:
534 if db_nsir:
535 db_nsir_update["detailed-status"] = "ERROR {}: {}".format(step, exc)
536 db_nsir_update["operational-status"] = "failed"
tierno5e98d462020-08-10 13:21:28 +0000537 db_nsir_update["config-status"] = "configured"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100538 if db_nsilcmop:
garciadeblas5697b8b2021-03-24 09:17:02 +0100539 db_nsilcmop_update["detailed-status"] = "FAILED {}: {}".format(
540 step, exc
541 )
542 db_nsilcmop_update[
543 "operationState"
544 ] = nsilcmop_operation_state = "FAILED"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100545 db_nsilcmop_update["statusEnteredTime"] = time()
tiernobaa51102018-12-14 13:16:18 +0000546 try:
547 if db_nsir:
tiernobaa51102018-12-14 13:16:18 +0000548 db_nsir_update["_admin.nsilcmop"] = None
549 self.update_db_2("nsis", nsir_id, db_nsir_update)
550 if db_nsilcmop:
551 self.update_db_2("nsilcmops", nsilcmop_id, db_nsilcmop_update)
552 except DbException as e:
553 self.logger.error(logging_text + "Cannot update database: {}".format(e))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100554 if nsilcmop_operation_state:
555 try:
garciadeblas5697b8b2021-03-24 09:17:02 +0100556 await self.msg.aiowrite(
557 "nsi",
558 "instantiated",
559 {
560 "nsir_id": nsir_id,
561 "nsilcmop_id": nsilcmop_id,
562 "operationState": nsilcmop_operation_state,
563 },
564 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100565 except Exception as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100566 self.logger.error(
567 logging_text + "kafka_write notification Exception {}".format(e)
568 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100569 self.logger.debug(logging_text + "Exit")
570 self.lcm_tasks.remove("nsi", nsir_id, nsilcmop_id, "nsi_instantiate")
571
572 async def terminate(self, nsir_id, nsilcmop_id):
kuused124bfe2019-06-18 12:09:24 +0200573 # Try to lock HA task here
garciadeblas5697b8b2021-03-24 09:17:02 +0100574 task_is_locked_by_me = self.lcm_tasks.lock_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +0200575 if not task_is_locked_by_me:
576 return
577
Felipe Vicensc2033f22018-11-15 15:09:58 +0100578 logging_text = "Task nsi={} terminate={} ".format(nsir_id, nsilcmop_id)
579 self.logger.debug(logging_text + "Enter")
kuused124bfe2019-06-18 12:09:24 +0200580 exc = None
Felipe Vicensc2033f22018-11-15 15:09:58 +0100581 db_nsir = None
582 db_nsilcmop = None
583 db_nsir_update = {"_admin.nsilcmop": nsilcmop_id}
584 db_nsilcmop_update = {}
Gabriel Cubae7898982023-05-11 01:57:21 -0500585 RO = ROclient.ROClient(**self.ro_config)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100586 nsir_deployed = None
garciadeblas5697b8b2021-03-24 09:17:02 +0100587 failed_detail = [] # annotates all failed error messages
Felipe Vicensc2033f22018-11-15 15:09:58 +0100588 nsilcmop_operation_state = None
tiernoc2564fe2019-01-28 16:18:56 +0000589 autoremove = False # autoremove after terminated
Felipe Vicensc2033f22018-11-15 15:09:58 +0100590 try:
kuused124bfe2019-06-18 12:09:24 +0200591 # wait for any previous tasks in process
garciadeblas5697b8b2021-03-24 09:17:02 +0100592 await self.lcm_tasks.waitfor_related_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +0200593
Felipe Vicensc2033f22018-11-15 15:09:58 +0100594 step = "Getting nsir={} from db".format(nsir_id)
595 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100596 nsir_deployed = deepcopy(db_nsir["_admin"].get("deployed"))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100597 step = "Getting nsilcmop={} from db".format(nsilcmop_id)
598 db_nsilcmop = self.db.get_one("nsilcmops", {"_id": nsilcmop_id})
kuused124bfe2019-06-18 12:09:24 +0200599
Felipe Vicensc2033f22018-11-15 15:09:58 +0100600 # TODO: Check if makes sense check the nsiState=NOT_INSTANTIATED when terminate
601 # CASE: Instance was terminated but there is a second request to terminate the instance
602 if db_nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED":
603 return
604
605 # Slice status Terminating
606 db_nsir_update["operational-status"] = "terminating"
607 db_nsir_update["config-status"] = "terminating"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100608 db_nsir_update["detailed-status"] = "Terminating Netslice subnets"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100609 self.update_db_2("nsis", nsir_id, db_nsir_update)
610
Felipe Vicensc2033f22018-11-15 15:09:58 +0100611 # Gets the list to keep track of network service records status in the netslice
kuused124bfe2019-06-18 12:09:24 +0200612 nsrs_detailed_list = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100613
614 # Iterate over the network services operation ids to terminate NSs
kuused124bfe2019-06-18 12:09:24 +0200615 # TODO: (future improvement) look another way check the tasks instead of keep asking
Felipe Vicensc2033f22018-11-15 15:09:58 +0100616 # -> https://docs.python.org/3/library/asyncio-task.html#waiting-primitives
617 # steps: declare ns_tasks, add task when terminate is called, await asyncio.wait(vca_task_list, timeout=300)
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100618 step = "Terminating Netslice Subnets"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100619 nslcmop_ids = db_nsilcmop["operationParams"].get("nslcmops_ids")
Felipe Vicens720b07a2019-01-31 02:32:09 +0100620 nslcmop_new = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100621 for nslcmop_id in nslcmop_ids:
622 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
623 nsr_id = nslcmop["operationParams"].get("nsInstanceId")
garciadeblas5697b8b2021-03-24 09:17:02 +0100624 nss_in_use = self.db.get_list(
625 "nsis",
626 {
627 "_admin.netslice-vld.ANYINDEX.shared-nsrs-list": nsr_id,
628 "operational-status": {"$nin": ["terminated", "failed"]},
629 },
630 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100631 if len(nss_in_use) < 2:
632 task = asyncio.ensure_future(self.ns.terminate(nsr_id, nslcmop_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100633 self.lcm_tasks.register(
634 "ns", nsr_id, nslcmop_id, "ns_instantiate", task
635 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100636 nslcmop_new.append(nslcmop_id)
637 else:
638 # Update shared nslcmop shared with active nsi
639 netsliceInstanceId = db_nsir["_id"]
640 for nsis_item in nss_in_use:
641 if db_nsir["_id"] != nsis_item["_id"]:
642 netsliceInstanceId = nsis_item["_id"]
643 break
garciadeblas5697b8b2021-03-24 09:17:02 +0100644 self.db.set_one(
645 "nslcmops",
646 {"_id": nslcmop_id},
647 {"operationParams.netsliceInstanceId": netsliceInstanceId},
648 )
649 self.db.set_one(
650 "nsilcmops",
651 {"_id": nsilcmop_id},
652 {"operationParams.nslcmops_ids": nslcmop_new},
653 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100654
655 # Wait until Network Slice is terminated
garciadeblas5697b8b2021-03-24 09:17:02 +0100656 step = nsir_status_detailed = " Waiting nsi terminated. nsi_id={}".format(
657 nsir_id
658 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100659 nsrs_detailed_list_old = None
660 self.logger.debug(logging_text + step)
kuused124bfe2019-06-18 12:09:24 +0200661
garciadeblas5697b8b2021-03-24 09:17:02 +0100662 termination_timeout = 2 * 3600 # Two hours
Felipe Vicensc2033f22018-11-15 15:09:58 +0100663 while termination_timeout > 0:
664 # Check ns termination status
665 nsi_ready = True
Felipe Vicens720b07a2019-01-31 02:32:09 +0100666 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100667 nsrs_detailed_list = db_nsir["_admin"].get("nsrs-detailed-list")
668 nsrs_detailed_list_new = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100669 for nslcmop_item in nslcmop_ids:
670 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_item})
671 status = nslcmop["operationState"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100672 # TODO: (future improvement) other possible status: ROLLING_BACK,ROLLED_BACK
Felipe Vicens720b07a2019-01-31 02:32:09 +0100673 for nss in nsrs_detailed_list:
674 if nss["nsrId"] == nslcmop["nsInstanceId"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100675 nss.update(
676 {
677 "nsrId": nslcmop["nsInstanceId"],
678 "status": nslcmop["operationState"],
679 "detailed-status": nsir_status_detailed
680 + "; {}".format(nslcmop.get("detailed-status")),
681 }
682 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100683 nsrs_detailed_list_new.append(nss)
garciadeblas5697b8b2021-03-24 09:17:02 +0100684 if status not in [
685 "COMPLETED",
686 "PARTIALLY_COMPLETED",
687 "FAILED",
688 "FAILED_TEMP",
689 ]:
Felipe Vicensc2033f22018-11-15 15:09:58 +0100690 nsi_ready = False
691
Felipe Vicens720b07a2019-01-31 02:32:09 +0100692 if nsrs_detailed_list_new != nsrs_detailed_list_old:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100693 nsrs_detailed_list_old = nsrs_detailed_list_new
garciadeblas5697b8b2021-03-24 09:17:02 +0100694 self.update_db_2(
695 "nsis",
696 nsir_id,
697 {"_admin.nsrs-detailed-list": nsrs_detailed_list_new},
698 )
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100699
Felipe Vicensc2033f22018-11-15 15:09:58 +0100700 if nsi_ready:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100701 # Check if it is the last used nss and mark isinstantiate: False
702 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100703 nsrs_detailed_list = db_nsir["_admin"].get("nsrs-detailed-list")
704 for nss in nsrs_detailed_list:
garciadeblas5697b8b2021-03-24 09:17:02 +0100705 _filter = {
706 "_admin.nsrs-detailed-list.ANYINDEX.nsrId": nss["nsrId"],
707 "operational-status.ne": "terminated",
708 "_id.ne": nsir_id,
709 }
710 nsis_list = self.db.get_one(
711 "nsis", _filter, fail_on_empty=False, fail_on_more=False
712 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100713 if not nsis_list:
714 nss.update({"instantiated": False})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100715
garciadeblas5697b8b2021-03-24 09:17:02 +0100716 step = "Network Slice Instance is terminated. nsi_id={}".format(
717 nsir_id
718 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100719 for items in nsrs_detailed_list:
720 if "FAILED" in items.values():
garciadeblas5697b8b2021-03-24 09:17:02 +0100721 raise LcmException(
722 "Error terminating NSI: {}".format(nsir_id)
723 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100724 break
725
Gabriel Cubae7898982023-05-11 01:57:21 -0500726 await asyncio.sleep(5)
Felipe Vicensc2033f22018-11-15 15:09:58 +0100727 termination_timeout -= 5
728
729 if termination_timeout <= 0:
garciadeblas5697b8b2021-03-24 09:17:02 +0100730 raise LcmException(
731 "Timeout waiting nsi to be terminated. nsi_id={}".format(nsir_id)
732 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100733
Felipe Vicens720b07a2019-01-31 02:32:09 +0100734 # Delete netslice-vlds
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100735 RO_nsir_id = RO_delete_action = None
Felipe Vicens720b07a2019-01-31 02:32:09 +0100736 for nsir_deployed_RO in get_iterable(nsir_deployed, "RO"):
737 RO_nsir_id = nsir_deployed_RO.get("netslice_scenario_id")
738 try:
bravof922c4172020-11-24 21:21:43 -0300739 if not self.ro_config["ng"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100740 step = db_nsir_update[
741 "detailed-status"
742 ] = "Deleting netslice-vld at RO"
743 db_nsilcmop_update[
744 "detailed-status"
745 ] = "Deleting netslice-vld at RO"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100746 self.logger.debug(logging_text + step)
bravof922c4172020-11-24 21:21:43 -0300747 desc = await RO.delete("ns", RO_nsir_id)
748 RO_delete_action = desc["action_id"]
749 nsir_deployed_RO["vld_delete_action_id"] = RO_delete_action
750 nsir_deployed_RO["vld_status"] = "DELETING"
751 db_nsir_update["_admin.deployed"] = nsir_deployed
752 self.update_db_2("nsis", nsir_id, db_nsir_update)
753 if RO_delete_action:
754 # wait until NS is deleted from VIM
garciadeblas5697b8b2021-03-24 09:17:02 +0100755 step = "Waiting ns deleted from VIM. RO_id={}".format(
756 RO_nsir_id
757 )
bravof922c4172020-11-24 21:21:43 -0300758 self.logger.debug(logging_text + step)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100759 except ROclient.ROClientException as e:
760 if e.http_code == 404: # not found
761 nsir_deployed_RO["vld_id"] = None
762 nsir_deployed_RO["vld_status"] = "DELETED"
garciadeblas5697b8b2021-03-24 09:17:02 +0100763 self.logger.debug(
764 logging_text
765 + "RO_ns_id={} already deleted".format(RO_nsir_id)
766 )
767 elif e.http_code == 409: # conflict
768 failed_detail.append(
769 "RO_ns_id={} delete conflict: {}".format(RO_nsir_id, e)
770 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100771 self.logger.debug(logging_text + failed_detail[-1])
772 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100773 failed_detail.append(
774 "RO_ns_id={} delete error: {}".format(RO_nsir_id, e)
775 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100776 self.logger.error(logging_text + failed_detail[-1])
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100777
Felipe Vicens720b07a2019-01-31 02:32:09 +0100778 if failed_detail:
779 self.logger.error(logging_text + " ;".join(failed_detail))
780 db_nsir_update["operational-status"] = "failed"
garciadeblas5697b8b2021-03-24 09:17:02 +0100781 db_nsir_update["detailed-status"] = "Deletion errors " + "; ".join(
782 failed_detail
783 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100784 db_nsilcmop_update["detailed-status"] = "; ".join(failed_detail)
garciadeblas5697b8b2021-03-24 09:17:02 +0100785 db_nsilcmop_update[
786 "operationState"
787 ] = nsilcmop_operation_state = "FAILED"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100788 db_nsilcmop_update["statusEnteredTime"] = time()
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100789 else:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100790 db_nsir_update["operational-status"] = "terminating"
791 db_nsir_update["config-status"] = "terminating"
792 db_nsir_update["_admin.nsiState"] = "NOT_INSTANTIATED"
garciadeblas5697b8b2021-03-24 09:17:02 +0100793 db_nsilcmop_update[
794 "operationState"
795 ] = nsilcmop_operation_state = "COMPLETED"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100796 db_nsilcmop_update["statusEnteredTime"] = time()
797 if db_nsilcmop["operationParams"].get("autoremove"):
798 autoremove = True
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100799
Felipe Vicens720b07a2019-01-31 02:32:09 +0100800 db_nsir_update["detailed-status"] = "done"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100801 db_nsir_update["operational-status"] = "terminated"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100802 db_nsir_update["config-status"] = "terminated"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100803 db_nsilcmop_update["statusEnteredTime"] = time()
804 db_nsilcmop_update["detailed-status"] = "done"
805 return
806
807 except (LcmException, DbException) as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100808 self.logger.error(
809 logging_text + "Exit Exception while '{}': {}".format(step, e)
810 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100811 exc = e
812 except asyncio.CancelledError:
garciadeblas5697b8b2021-03-24 09:17:02 +0100813 self.logger.error(
814 logging_text + "Cancelled Exception while '{}'".format(step)
815 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100816 exc = "Operation was cancelled"
817 except Exception as e:
818 exc = traceback.format_exc()
garciadeblas5697b8b2021-03-24 09:17:02 +0100819 self.logger.critical(
820 logging_text
821 + "Exit Exception {} while '{}': {}".format(type(e).__name__, step, e),
822 exc_info=True,
823 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100824 finally:
825 if exc:
826 if db_nsir:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100827 db_nsir_update["_admin.deployed"] = nsir_deployed
Felipe Vicensc2033f22018-11-15 15:09:58 +0100828 db_nsir_update["detailed-status"] = "ERROR {}: {}".format(step, exc)
829 db_nsir_update["operational-status"] = "failed"
830 if db_nsilcmop:
garciadeblas5697b8b2021-03-24 09:17:02 +0100831 db_nsilcmop_update["detailed-status"] = "FAILED {}: {}".format(
832 step, exc
833 )
834 db_nsilcmop_update[
835 "operationState"
836 ] = nsilcmop_operation_state = "FAILED"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100837 db_nsilcmop_update["statusEnteredTime"] = time()
tiernobaa51102018-12-14 13:16:18 +0000838 try:
839 if db_nsir:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100840 db_nsir_update["_admin.deployed"] = nsir_deployed
tiernobaa51102018-12-14 13:16:18 +0000841 db_nsir_update["_admin.nsilcmop"] = None
tiernobaa51102018-12-14 13:16:18 +0000842 self.update_db_2("nsis", nsir_id, db_nsir_update)
843 if db_nsilcmop:
844 self.update_db_2("nsilcmops", nsilcmop_id, db_nsilcmop_update)
845 except DbException as e:
846 self.logger.error(logging_text + "Cannot update database: {}".format(e))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100847
848 if nsilcmop_operation_state:
849 try:
garciadeblas5697b8b2021-03-24 09:17:02 +0100850 await self.msg.aiowrite(
851 "nsi",
852 "terminated",
853 {
854 "nsir_id": nsir_id,
855 "nsilcmop_id": nsilcmop_id,
856 "operationState": nsilcmop_operation_state,
857 "autoremove": autoremove,
858 },
garciadeblas5697b8b2021-03-24 09:17:02 +0100859 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100860 except Exception as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100861 self.logger.error(
862 logging_text + "kafka_write notification Exception {}".format(e)
863 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100864 self.logger.debug(logging_text + "Exit")
865 self.lcm_tasks.remove("nsi", nsir_id, nsilcmop_id, "nsi_terminate")