blob: 2256540d027ba3fa4f188cd4ebeab523ad61002f [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):
bravof922c4172020-11-24 21:21:43 -030037 def __init__(self, msg, lcm_tasks, config, loop, 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.loop = loop
46 self.lcm_tasks = lcm_tasks
tiernob996d942020-07-03 14:52:28 +000047 self.ns = ns
Mark Beierl3fd1aea2023-01-06 11:53:37 -050048 self.ro_config = config["RO"]
tierno744303e2020-01-13 16:46:31 +000049 self.timeout = config["timeout"]
Felipe Vicensc2033f22018-11-15 15:09:58 +010050
bravof922c4172020-11-24 21:21:43 -030051 super().__init__(msg, self.logger)
Felipe Vicensc2033f22018-11-15 15:09:58 +010052
Felipe Vicens0f389ce2019-01-12 12:20:11 +010053 def nsi_update_nsir(self, nsi_update_nsir, db_nsir, nsir_desc_RO):
54 """
55 Updates database nsir with the RO info for the created vld
56 :param nsi_update_nsir: dictionary to be filled with the updated info
57 :param db_nsir: content of db_nsir. This is also modified
58 :param nsir_desc_RO: nsir descriptor from RO
59 :return: Nothing, LcmException is raised on errors
60 """
61
62 for vld_index, vld in enumerate(get_iterable(db_nsir, "vld")):
63 for net_RO in get_iterable(nsir_desc_RO, "nets"):
64 if vld["id"] != net_RO.get("ns_net_osm_id"):
65 continue
66 vld["vim-id"] = net_RO.get("vim_net_id")
67 vld["name"] = net_RO.get("vim_name")
68 vld["status"] = net_RO.get("status")
69 vld["status-detailed"] = net_RO.get("error_msg")
70 nsi_update_nsir["vld.{}".format(vld_index)] = vld
71 break
72 else:
garciadeblas5697b8b2021-03-24 09:17:02 +010073 raise LcmException(
74 "ns_update_nsir: Not found vld={} at RO info".format(vld["id"])
75 )
Felipe Vicens0f389ce2019-01-12 12:20:11 +010076
Felipe Vicensc2033f22018-11-15 15:09:58 +010077 async def instantiate(self, nsir_id, nsilcmop_id):
kuused124bfe2019-06-18 12:09:24 +020078
79 # Try to lock HA task here
garciadeblas5697b8b2021-03-24 09:17:02 +010080 task_is_locked_by_me = self.lcm_tasks.lock_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +020081 if not task_is_locked_by_me:
82 return
83
Felipe Vicensc2033f22018-11-15 15:09:58 +010084 logging_text = "Task netslice={} instantiate={} ".format(nsir_id, nsilcmop_id)
85 self.logger.debug(logging_text + "Enter")
86 # get all needed from database
Felipe Vicens0f389ce2019-01-12 12:20:11 +010087 exc = None
Felipe Vicensc2033f22018-11-15 15:09:58 +010088 db_nsir = None
89 db_nsilcmop = None
90 db_nsir_update = {"_admin.nsilcmop": nsilcmop_id}
91 db_nsilcmop_update = {}
92 nsilcmop_operation_state = None
Felipe Vicens6559b4a2018-12-01 04:40:48 +010093 vim_2_RO = {}
94 RO = ROclient.ROClient(self.loop, **self.ro_config)
bravof922c4172020-11-24 21:21:43 -030095 nsi_vld_instantiationi_params = {}
Felipe Vicens720b07a2019-01-31 02:32:09 +010096
97 def ip_profile_2_RO(ip_profile):
98 RO_ip_profile = deepcopy((ip_profile))
99 if "dns-server" in RO_ip_profile:
100 if isinstance(RO_ip_profile["dns-server"], list):
101 RO_ip_profile["dns-address"] = []
102 for ds in RO_ip_profile.pop("dns-server"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100103 RO_ip_profile["dns-address"].append(ds["address"])
Felipe Vicens720b07a2019-01-31 02:32:09 +0100104 else:
105 RO_ip_profile["dns-address"] = RO_ip_profile.pop("dns-server")
106 if RO_ip_profile.get("ip-version") == "ipv4":
107 RO_ip_profile["ip-version"] = "IPv4"
108 if RO_ip_profile.get("ip-version") == "ipv6":
109 RO_ip_profile["ip-version"] = "IPv6"
110 if "dhcp-params" in RO_ip_profile:
111 RO_ip_profile["dhcp"] = RO_ip_profile.pop("dhcp-params")
112 return RO_ip_profile
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100113
114 def vim_account_2_RO(vim_account):
115 """
116 Translate a RO vim_account from OSM vim_account params
117 :param ns_params: OSM instantiate params
118 :return: The RO ns descriptor
119 """
120 if vim_account in vim_2_RO:
121 return vim_2_RO[vim_account]
122
123 db_vim = self.db.get_one("vim_accounts", {"_id": vim_account})
124 if db_vim["_admin"]["operationalState"] != "ENABLED":
garciadeblas5697b8b2021-03-24 09:17:02 +0100125 raise LcmException(
126 "VIM={} is not available. operationalState={}".format(
127 vim_account, db_vim["_admin"]["operationalState"]
128 )
129 )
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100130 RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
131 vim_2_RO[vim_account] = RO_vim_id
132 return RO_vim_id
133
garciadeblas5697b8b2021-03-24 09:17:02 +0100134 async def netslice_scenario_create(
135 self, vld_item, nsir_id, db_nsir, db_nsir_admin, db_nsir_update
136 ):
Felipe Vicens720b07a2019-01-31 02:32:09 +0100137 """
138 Create a network slice VLD through RO Scenario
139 :param vld_id The VLD id inside nsir to be created
140 :param nsir_id The nsir id
141 """
bravof922c4172020-11-24 21:21:43 -0300142 nonlocal nsi_vld_instantiationi_params
Felipe Vicens720b07a2019-01-31 02:32:09 +0100143 ip_vld = None
144 mgmt_network = False
145 RO_vld_sites = []
146 vld_id = vld_item["id"]
147 netslice_vld = vld_item
148 # logging_text = "Task netslice={} instantiate_vld={} ".format(nsir_id, vld_id)
149 # self.logger.debug(logging_text + "Enter")
150
151 vld_shared = None
152 for shared_nsrs_item in get_iterable(vld_item, "shared-nsrs-list"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100153 _filter = {
154 "_id.ne": nsir_id,
155 "_admin.nsrs-detailed-list.ANYINDEX.nsrId": shared_nsrs_item,
156 }
157 shared_nsi = self.db.get_one(
158 "nsis", _filter, fail_on_empty=False, fail_on_more=False
159 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100160 if shared_nsi:
161 for vlds in get_iterable(shared_nsi["_admin"]["deployed"], "RO"):
162 if vld_id == vlds["vld_id"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100163 vld_shared = {
164 "instance_scenario_id": vlds["netslice_scenario_id"],
165 "osm_id": vld_id,
166 }
Felipe Vicens720b07a2019-01-31 02:32:09 +0100167 break
168 break
169
170 # Creating netslice-vld at RO
tierno744303e2020-01-13 16:46:31 +0000171 RO_nsir = deep_get(db_nsir, ("_admin", "deployed", "RO"), [])
Felipe Vicens720b07a2019-01-31 02:32:09 +0100172
173 if vld_id in RO_nsir:
174 db_nsir_update["_admin.deployed.RO"] = RO_nsir
175
176 # If netslice-vld doesn't exists then create it
177 else:
178 # TODO: Check VDU type in all descriptors finding SRIOV / PT
179 # Updating network names and datacenters from instantiation parameters for each VLD
garciadeblas5697b8b2021-03-24 09:17:02 +0100180 for instantiation_params_vld in get_iterable(
181 db_nsir["instantiation_parameters"], "netslice-vld"
182 ):
Felipe Vicens720b07a2019-01-31 02:32:09 +0100183 if instantiation_params_vld.get("name") == netslice_vld["name"]:
184 ip_vld = deepcopy(instantiation_params_vld)
bravof922c4172020-11-24 21:21:43 -0300185 ip_vld.pop("name")
186 nsi_vld_instantiationi_params[netslice_vld["name"]] = ip_vld
Felipe Vicens720b07a2019-01-31 02:32:09 +0100187
bravof922c4172020-11-24 21:21:43 -0300188 db_nsir_update_RO = {}
189 db_nsir_update_RO["vld_id"] = netslice_vld["name"]
190 if self.ro_config["ng"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100191 db_nsir_update_RO["netslice_scenario_id"] = (
192 vld_shared.get("instance_scenario_id")
193 if vld_shared
bravof922c4172020-11-24 21:21:43 -0300194 else "nsir:{}:vld.{}".format(nsir_id, netslice_vld["name"])
garciadeblas5697b8b2021-03-24 09:17:02 +0100195 )
bravof922c4172020-11-24 21:21:43 -0300196 else: # if not self.ro_config["ng"]:
197 if netslice_vld.get("mgmt-network"):
198 mgmt_network = True
199 RO_ns_params = {}
200 RO_ns_params["name"] = netslice_vld["name"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100201 RO_ns_params["datacenter"] = vim_account_2_RO(
202 db_nsir["instantiation_parameters"]["vimAccountId"]
203 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100204
bravof922c4172020-11-24 21:21:43 -0300205 # Creating scenario if vim-network-name / vim-network-id are present as instantiation parameter
206 # Use vim-network-id instantiation parameter
207 vim_network_option = None
208 if ip_vld:
209 if ip_vld.get("vim-network-id"):
210 vim_network_option = "vim-network-id"
211 elif ip_vld.get("vim-network-name"):
212 vim_network_option = "vim-network-name"
213 if ip_vld.get("ip-profile"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100214 populate_dict(
215 RO_ns_params,
216 ("networks", netslice_vld["name"], "ip-profile"),
217 ip_profile_2_RO(ip_vld["ip-profile"]),
218 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100219
bravof922c4172020-11-24 21:21:43 -0300220 if vim_network_option:
221 if ip_vld.get(vim_network_option):
222 if isinstance(ip_vld.get(vim_network_option), list):
223 for vim_net_id in ip_vld.get(vim_network_option):
224 for vim_account, vim_net in vim_net_id.items():
garciadeblas5697b8b2021-03-24 09:17:02 +0100225 RO_vld_sites.append(
226 {
227 "netmap-use": vim_net,
228 "datacenter": vim_account_2_RO(
229 vim_account
230 ),
231 }
232 )
bravof922c4172020-11-24 21:21:43 -0300233 elif isinstance(ip_vld.get(vim_network_option), dict):
garciadeblas5697b8b2021-03-24 09:17:02 +0100234 for vim_account, vim_net in ip_vld.get(
235 vim_network_option
236 ).items():
237 RO_vld_sites.append(
238 {
239 "netmap-use": vim_net,
240 "datacenter": vim_account_2_RO(vim_account),
241 }
242 )
bravof922c4172020-11-24 21:21:43 -0300243 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100244 RO_vld_sites.append(
245 {
246 "netmap-use": ip_vld[vim_network_option],
247 "datacenter": vim_account_2_RO(
248 netslice_vld["vimAccountId"]
249 ),
250 }
251 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100252
bravof922c4172020-11-24 21:21:43 -0300253 # Use default netslice vim-network-name from template
254 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100255 for nss_conn_point_ref in get_iterable(
256 netslice_vld, "nss-connection-point-ref"
257 ):
bravof922c4172020-11-24 21:21:43 -0300258 if nss_conn_point_ref.get("vimAccountId"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100259 if (
260 nss_conn_point_ref["vimAccountId"]
261 != netslice_vld["vimAccountId"]
262 ):
263 RO_vld_sites.append(
264 {
265 "netmap-create": None,
266 "datacenter": vim_account_2_RO(
267 nss_conn_point_ref["vimAccountId"]
268 ),
269 }
270 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100271
bravof922c4172020-11-24 21:21:43 -0300272 if vld_shared:
garciadeblas5697b8b2021-03-24 09:17:02 +0100273 populate_dict(
274 RO_ns_params,
275 ("networks", netslice_vld["name"], "use-network"),
276 vld_shared,
277 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100278
bravof922c4172020-11-24 21:21:43 -0300279 if RO_vld_sites:
garciadeblas5697b8b2021-03-24 09:17:02 +0100280 populate_dict(
281 RO_ns_params,
282 ("networks", netslice_vld["name"], "sites"),
283 RO_vld_sites,
284 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100285
garciadeblas5697b8b2021-03-24 09:17:02 +0100286 RO_ns_params["scenario"] = {
287 "nets": [
288 {
289 "name": netslice_vld["name"],
290 "external": mgmt_network,
291 "type": "bridge",
292 }
293 ]
294 }
bravof922c4172020-11-24 21:21:43 -0300295
296 # self.logger.debug(logging_text + step)
297 desc = await RO.create("ns", descriptor=RO_ns_params)
298 db_nsir_update_RO["netslice_scenario_id"] = desc["uuid"]
Felipe Vicens720b07a2019-01-31 02:32:09 +0100299 db_nsir_update["_admin.deployed.RO"].append(db_nsir_update_RO)
kuused124bfe2019-06-18 12:09:24 +0200300
Felipe Vicens720b07a2019-01-31 02:32:09 +0100301 def overwrite_nsd_params(self, db_nsir, nslcmop):
bravof922c4172020-11-24 21:21:43 -0300302 nonlocal nsi_vld_instantiationi_params
303 nonlocal db_nsir_update
Felipe Vicens720b07a2019-01-31 02:32:09 +0100304 vld_op_list = []
305 vld = None
306 nsr_id = nslcmop.get("nsInstanceId")
307 # Overwrite instantiation parameters in netslice runtime
bravof922c4172020-11-24 21:21:43 -0300308 RO_list = db_nsir_admin["deployed"]["RO"]
Felipe Vicens720b07a2019-01-31 02:32:09 +0100309
bravof922c4172020-11-24 21:21:43 -0300310 for ro_item_index, RO_item in enumerate(RO_list):
garciadeblas5697b8b2021-03-24 09:17:02 +0100311 netslice_vld = next(
312 (
313 n
314 for n in get_iterable(db_nsir["_admin"], "netslice-vld")
315 if RO_item.get("vld_id") == n.get("id")
316 ),
317 None,
318 )
bravof922c4172020-11-24 21:21:43 -0300319 if not netslice_vld:
320 continue
321 # if is equal vld of _admin with vld of netslice-vld then go for the CPs
322 # Search the cp of netslice-vld that match with nst:netslice-subnet
garciadeblas5697b8b2021-03-24 09:17:02 +0100323 for nss_cp_item in get_iterable(
324 netslice_vld, "nss-connection-point-ref"
325 ):
bravof922c4172020-11-24 21:21:43 -0300326 # Search the netslice-subnet of nst that match
garciadeblas5697b8b2021-03-24 09:17:02 +0100327 nss = next(
328 (
329 nss
330 for nss in get_iterable(
331 db_nsir["_admin"], "netslice-subnet"
332 )
333 if nss_cp_item["nss-ref"] == nss["nss-id"]
334 ),
335 None,
336 )
bravof922c4172020-11-24 21:21:43 -0300337 # Compare nss-ref equal nss from nst
338 if not nss:
339 continue
340 db_nsds = self.db.get_one("nsds", {"_id": nss["nsdId"]})
341 # Go for nsd, and search the CP that match with nst:CP to get vld-id-ref
bravofa7cb70b2020-12-11 16:37:01 -0300342 for cp_nsd in db_nsds.get("sapd", ()):
343 if cp_nsd["id"] == nss_cp_item["nsd-connection-point-ref"]:
bravof922c4172020-11-24 21:21:43 -0300344 if nslcmop.get("operationParams"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100345 if (
346 nslcmop["operationParams"].get("nsName")
347 == nss["nsName"]
348 ):
bravof922c4172020-11-24 21:21:43 -0300349 vld_id = RO_item["vld_id"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100350 netslice_scenario_id = RO_item[
351 "netslice_scenario_id"
352 ]
bravof922c4172020-11-24 21:21:43 -0300353 nslcmop_vld = {}
bravofa7cb70b2020-12-11 16:37:01 -0300354 nslcmop_vld["name"] = cp_nsd["virtual-link-desc"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100355 for vld in get_iterable(
356 nslcmop["operationParams"], "vld"
357 ):
bravofa7cb70b2020-12-11 16:37:01 -0300358 if vld["name"] == cp_nsd["virtual-link-desc"]:
bravof922c4172020-11-24 21:21:43 -0300359 nslcmop_vld.update(vld)
360 if self.ro_config["ng"]:
361 nslcmop_vld["common_id"] = netslice_scenario_id
garciadeblas5697b8b2021-03-24 09:17:02 +0100362 nslcmop_vld.update(
363 nsi_vld_instantiationi_params.get(
364 RO_item["vld_id"], {}
365 )
366 )
bravof922c4172020-11-24 21:21:43 -0300367 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100368 nslcmop_vld["ns-net"] = {
369 vld_id: netslice_scenario_id
370 }
bravof922c4172020-11-24 21:21:43 -0300371 vld_op_list.append(nslcmop_vld)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100372 nslcmop["operationParams"]["vld"] = vld_op_list
garciadeblas5697b8b2021-03-24 09:17:02 +0100373 self.update_db_2(
374 "nslcmops", nslcmop["_id"], {"operationParams.vld": vld_op_list}
375 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100376 return nsr_id, nslcmop
377
Felipe Vicensc2033f22018-11-15 15:09:58 +0100378 try:
kuused124bfe2019-06-18 12:09:24 +0200379 # wait for any previous tasks in process
garciadeblas5697b8b2021-03-24 09:17:02 +0100380 await self.lcm_tasks.waitfor_related_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +0200381
Felipe Vicensc2033f22018-11-15 15:09:58 +0100382 step = "Getting nsir={} from db".format(nsir_id)
383 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
384 step = "Getting nsilcmop={} from db".format(nsilcmop_id)
385 db_nsilcmop = self.db.get_one("nsilcmops", {"_id": nsilcmop_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100386
tierno744303e2020-01-13 16:46:31 +0000387 start_deploy = time()
388 nsi_params = db_nsilcmop.get("operationParams")
389 if nsi_params and nsi_params.get("timeout_nsi_deploy"):
390 timeout_nsi_deploy = nsi_params["timeout_nsi_deploy"]
391 else:
Mark Beierl3fd1aea2023-01-06 11:53:37 -0500392 timeout_nsi_deploy = self.timeout.get("nsi_deploy")
tierno744303e2020-01-13 16:46:31 +0000393
Felipe Vicensc2033f22018-11-15 15:09:58 +0100394 # Empty list to keep track of network service records status in the netslice
Felipe Vicens720b07a2019-01-31 02:32:09 +0100395 nsir_admin = db_nsir_admin = db_nsir.get("_admin")
Felipe Vicensc2033f22018-11-15 15:09:58 +0100396
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100397 step = "Creating slice operational-status init"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100398 # Slice status Creating
399 db_nsir_update["detailed-status"] = "creating"
400 db_nsir_update["operational-status"] = "init"
tierno5e98d462020-08-10 13:21:28 +0000401 db_nsir_update["_admin.nsiState"] = "INSTANTIATED"
kuused124bfe2019-06-18 12:09:24 +0200402
tierno5e98d462020-08-10 13:21:28 +0000403 step = "Instantiating netslice VLDs before NS instantiation"
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100404 # Creating netslice VLDs networking before NS instantiation
tierno5e98d462020-08-10 13:21:28 +0000405 db_nsir_update["detailed-status"] = step
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100406 self.update_db_2("nsis", nsir_id, db_nsir_update)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100407 db_nsir_update["_admin.deployed.RO"] = db_nsir_admin["deployed"]["RO"]
408 for vld_item in get_iterable(nsir_admin, "netslice-vld"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100409 await netslice_scenario_create(
410 self, vld_item, nsir_id, db_nsir, db_nsir_admin, db_nsir_update
411 )
kuused124bfe2019-06-18 12:09:24 +0200412
tierno5e98d462020-08-10 13:21:28 +0000413 step = "Instantiating netslice subnets"
414 db_nsir_update["detailed-status"] = step
kuused124bfe2019-06-18 12:09:24 +0200415 self.update_db_2("nsis", nsir_id, db_nsir_update)
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100416
Felipe Vicens720b07a2019-01-31 02:32:09 +0100417 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100418
Felipe Vicens720b07a2019-01-31 02:32:09 +0100419 # Check status of the VLDs and wait for creation
420 # netslice_scenarios = db_nsir["_admin"]["deployed"]["RO"]
421 # db_nsir_update_RO = deepcopy(netslice_scenarios)
422 # for netslice_scenario in netslice_scenarios:
kuused124bfe2019-06-18 12:09:24 +0200423 # await netslice_scenario_check(self, netslice_scenario["netslice_scenario_id"],
Felipe Vicens720b07a2019-01-31 02:32:09 +0100424 # nsir_id, db_nsir_update_RO)
kuused124bfe2019-06-18 12:09:24 +0200425
Felipe Vicens720b07a2019-01-31 02:32:09 +0100426 # db_nsir_update["_admin.deployed.RO"] = db_nsir_update_RO
427 # self.update_db_2("nsis", nsir_id, db_nsir_update)
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100428
Felipe Vicensc2033f22018-11-15 15:09:58 +0100429 # Iterate over the network services operation ids to instantiate NSs
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100430 step = "Instantiating Netslice Subnets"
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100431 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicensc2033f22018-11-15 15:09:58 +0100432 nslcmop_ids = db_nsilcmop["operationParams"].get("nslcmops_ids")
433 for nslcmop_id in nslcmop_ids:
434 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100435 # Overwriting netslice-vld vim-net-id to ns
436 nsr_id, nslcmop = overwrite_nsd_params(self, db_nsir, nslcmop)
437 step = "Launching ns={} instantiate={} task".format(nsr_id, nslcmop_id)
Felipe Vicensc2033f22018-11-15 15:09:58 +0100438 task = asyncio.ensure_future(self.ns.instantiate(nsr_id, nslcmop_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100439 self.lcm_tasks.register(
440 "ns", nsr_id, nslcmop_id, "ns_instantiate", task
441 )
kuused124bfe2019-06-18 12:09:24 +0200442
Felipe Vicensc2033f22018-11-15 15:09:58 +0100443 # Wait until Network Slice is ready
tierno5e98d462020-08-10 13:21:28 +0000444 step = " Waiting nsi ready."
Felipe Vicensc2033f22018-11-15 15:09:58 +0100445 nsrs_detailed_list_old = None
446 self.logger.debug(logging_text + step)
kuused124bfe2019-06-18 12:09:24 +0200447
tierno5e98d462020-08-10 13:21:28 +0000448 # For HA, it is checked from database, as the ns operation may be managed by other LCM worker
tierno744303e2020-01-13 16:46:31 +0000449 while time() <= start_deploy + timeout_nsi_deploy:
Felipe Vicensc2033f22018-11-15 15:09:58 +0100450 # Check ns instantiation status
451 nsi_ready = True
Felipe Vicens720b07a2019-01-31 02:32:09 +0100452 nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100453 nsrs_detailed_list = nsir["_admin"]["nsrs-detailed-list"]
454 nsrs_detailed_list_new = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100455 for nslcmop_item in nslcmop_ids:
456 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_item})
457 status = nslcmop.get("operationState")
458 # TODO: (future improvement) other possible status: ROLLING_BACK,ROLLED_BACK
Felipe Vicens720b07a2019-01-31 02:32:09 +0100459 for nss in nsrs_detailed_list:
460 if nss["nsrId"] == nslcmop["nsInstanceId"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100461 nss.update(
462 {
463 "nsrId": nslcmop["nsInstanceId"],
464 "status": nslcmop["operationState"],
465 "detailed-status": nslcmop.get("detailed-status"),
466 "instantiated": True,
467 }
468 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100469 nsrs_detailed_list_new.append(nss)
garciadeblas5697b8b2021-03-24 09:17:02 +0100470 if status not in [
471 "COMPLETED",
472 "PARTIALLY_COMPLETED",
473 "FAILED",
474 "FAILED_TEMP",
475 ]:
Felipe Vicensc2033f22018-11-15 15:09:58 +0100476 nsi_ready = False
477
Felipe Vicens720b07a2019-01-31 02:32:09 +0100478 if nsrs_detailed_list_new != nsrs_detailed_list_old:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100479 nsrs_detailed_list_old = nsrs_detailed_list_new
garciadeblas5697b8b2021-03-24 09:17:02 +0100480 self.update_db_2(
481 "nsis",
482 nsir_id,
483 {"_admin.nsrs-detailed-list": nsrs_detailed_list_new},
484 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100485
486 if nsi_ready:
tierno5e98d462020-08-10 13:21:28 +0000487 error_list = []
488 step = "Network Slice Instance instantiated"
489 for nss in nsrs_detailed_list:
490 if nss["status"] in ("FAILED", "FAILED_TEMP"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100491 error_list.append(
492 "NS {} {}: {}".format(
493 nss["nsrId"], nss["status"], nss["detailed-status"]
494 )
495 )
tierno5e98d462020-08-10 13:21:28 +0000496 if error_list:
497 step = "instantiating"
498 raise LcmException("; ".join(error_list))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100499 break
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100500
Felipe Vicensc2033f22018-11-15 15:09:58 +0100501 # TODO: future improvement due to synchronism -> await asyncio.wait(vca_task_list, timeout=300)
502 await asyncio.sleep(5, loop=self.loop)
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100503
garciadeblas5697b8b2021-03-24 09:17:02 +0100504 else: # timeout_nsi_deploy reached:
tierno5e98d462020-08-10 13:21:28 +0000505 raise LcmException("Timeout waiting nsi to be ready.")
Felipe Vicensc2033f22018-11-15 15:09:58 +0100506
507 db_nsir_update["operational-status"] = "running"
508 db_nsir_update["detailed-status"] = "done"
509 db_nsir_update["config-status"] = "configured"
garciadeblas5697b8b2021-03-24 09:17:02 +0100510 db_nsilcmop_update[
511 "operationState"
512 ] = nsilcmop_operation_state = "COMPLETED"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100513 db_nsilcmop_update["statusEnteredTime"] = time()
514 db_nsilcmop_update["detailed-status"] = "done"
515 return
516
517 except (LcmException, DbException) as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100518 self.logger.error(
519 logging_text + "Exit Exception while '{}': {}".format(step, e)
520 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100521 exc = e
522 except asyncio.CancelledError:
garciadeblas5697b8b2021-03-24 09:17:02 +0100523 self.logger.error(
524 logging_text + "Cancelled Exception while '{}'".format(step)
525 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100526 exc = "Operation was cancelled"
527 except Exception as e:
528 exc = traceback.format_exc()
garciadeblas5697b8b2021-03-24 09:17:02 +0100529 self.logger.critical(
530 logging_text
531 + "Exit Exception {} while '{}': {}".format(type(e).__name__, step, e),
532 exc_info=True,
533 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100534 finally:
535 if exc:
536 if db_nsir:
537 db_nsir_update["detailed-status"] = "ERROR {}: {}".format(step, exc)
538 db_nsir_update["operational-status"] = "failed"
tierno5e98d462020-08-10 13:21:28 +0000539 db_nsir_update["config-status"] = "configured"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100540 if db_nsilcmop:
garciadeblas5697b8b2021-03-24 09:17:02 +0100541 db_nsilcmop_update["detailed-status"] = "FAILED {}: {}".format(
542 step, exc
543 )
544 db_nsilcmop_update[
545 "operationState"
546 ] = nsilcmop_operation_state = "FAILED"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100547 db_nsilcmop_update["statusEnteredTime"] = time()
tiernobaa51102018-12-14 13:16:18 +0000548 try:
549 if db_nsir:
tiernobaa51102018-12-14 13:16:18 +0000550 db_nsir_update["_admin.nsilcmop"] = None
551 self.update_db_2("nsis", nsir_id, db_nsir_update)
552 if db_nsilcmop:
553 self.update_db_2("nsilcmops", nsilcmop_id, db_nsilcmop_update)
554 except DbException as e:
555 self.logger.error(logging_text + "Cannot update database: {}".format(e))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100556 if nsilcmop_operation_state:
557 try:
garciadeblas5697b8b2021-03-24 09:17:02 +0100558 await self.msg.aiowrite(
559 "nsi",
560 "instantiated",
561 {
562 "nsir_id": nsir_id,
563 "nsilcmop_id": nsilcmop_id,
564 "operationState": nsilcmop_operation_state,
565 },
566 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100567 except Exception as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100568 self.logger.error(
569 logging_text + "kafka_write notification Exception {}".format(e)
570 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100571 self.logger.debug(logging_text + "Exit")
572 self.lcm_tasks.remove("nsi", nsir_id, nsilcmop_id, "nsi_instantiate")
573
574 async def terminate(self, nsir_id, nsilcmop_id):
kuused124bfe2019-06-18 12:09:24 +0200575
576 # Try to lock HA task here
garciadeblas5697b8b2021-03-24 09:17:02 +0100577 task_is_locked_by_me = self.lcm_tasks.lock_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +0200578 if not task_is_locked_by_me:
579 return
580
Felipe Vicensc2033f22018-11-15 15:09:58 +0100581 logging_text = "Task nsi={} terminate={} ".format(nsir_id, nsilcmop_id)
582 self.logger.debug(logging_text + "Enter")
kuused124bfe2019-06-18 12:09:24 +0200583 exc = None
Felipe Vicensc2033f22018-11-15 15:09:58 +0100584 db_nsir = None
585 db_nsilcmop = None
586 db_nsir_update = {"_admin.nsilcmop": nsilcmop_id}
587 db_nsilcmop_update = {}
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100588 RO = ROclient.ROClient(self.loop, **self.ro_config)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100589 nsir_deployed = None
garciadeblas5697b8b2021-03-24 09:17:02 +0100590 failed_detail = [] # annotates all failed error messages
Felipe Vicensc2033f22018-11-15 15:09:58 +0100591 nsilcmop_operation_state = None
tiernoc2564fe2019-01-28 16:18:56 +0000592 autoremove = False # autoremove after terminated
Felipe Vicensc2033f22018-11-15 15:09:58 +0100593 try:
kuused124bfe2019-06-18 12:09:24 +0200594 # wait for any previous tasks in process
garciadeblas5697b8b2021-03-24 09:17:02 +0100595 await self.lcm_tasks.waitfor_related_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +0200596
Felipe Vicensc2033f22018-11-15 15:09:58 +0100597 step = "Getting nsir={} from db".format(nsir_id)
598 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100599 nsir_deployed = deepcopy(db_nsir["_admin"].get("deployed"))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100600 step = "Getting nsilcmop={} from db".format(nsilcmop_id)
601 db_nsilcmop = self.db.get_one("nsilcmops", {"_id": nsilcmop_id})
kuused124bfe2019-06-18 12:09:24 +0200602
Felipe Vicensc2033f22018-11-15 15:09:58 +0100603 # TODO: Check if makes sense check the nsiState=NOT_INSTANTIATED when terminate
604 # CASE: Instance was terminated but there is a second request to terminate the instance
605 if db_nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED":
606 return
607
608 # Slice status Terminating
609 db_nsir_update["operational-status"] = "terminating"
610 db_nsir_update["config-status"] = "terminating"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100611 db_nsir_update["detailed-status"] = "Terminating Netslice subnets"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100612 self.update_db_2("nsis", nsir_id, db_nsir_update)
613
Felipe Vicensc2033f22018-11-15 15:09:58 +0100614 # Gets the list to keep track of network service records status in the netslice
kuused124bfe2019-06-18 12:09:24 +0200615 nsrs_detailed_list = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100616
617 # Iterate over the network services operation ids to terminate NSs
kuused124bfe2019-06-18 12:09:24 +0200618 # TODO: (future improvement) look another way check the tasks instead of keep asking
Felipe Vicensc2033f22018-11-15 15:09:58 +0100619 # -> https://docs.python.org/3/library/asyncio-task.html#waiting-primitives
620 # 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 +0100621 step = "Terminating Netslice Subnets"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100622 nslcmop_ids = db_nsilcmop["operationParams"].get("nslcmops_ids")
Felipe Vicens720b07a2019-01-31 02:32:09 +0100623 nslcmop_new = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100624 for nslcmop_id in nslcmop_ids:
625 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
626 nsr_id = nslcmop["operationParams"].get("nsInstanceId")
garciadeblas5697b8b2021-03-24 09:17:02 +0100627 nss_in_use = self.db.get_list(
628 "nsis",
629 {
630 "_admin.netslice-vld.ANYINDEX.shared-nsrs-list": nsr_id,
631 "operational-status": {"$nin": ["terminated", "failed"]},
632 },
633 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100634 if len(nss_in_use) < 2:
635 task = asyncio.ensure_future(self.ns.terminate(nsr_id, nslcmop_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100636 self.lcm_tasks.register(
637 "ns", nsr_id, nslcmop_id, "ns_instantiate", task
638 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100639 nslcmop_new.append(nslcmop_id)
640 else:
641 # Update shared nslcmop shared with active nsi
642 netsliceInstanceId = db_nsir["_id"]
643 for nsis_item in nss_in_use:
644 if db_nsir["_id"] != nsis_item["_id"]:
645 netsliceInstanceId = nsis_item["_id"]
646 break
garciadeblas5697b8b2021-03-24 09:17:02 +0100647 self.db.set_one(
648 "nslcmops",
649 {"_id": nslcmop_id},
650 {"operationParams.netsliceInstanceId": netsliceInstanceId},
651 )
652 self.db.set_one(
653 "nsilcmops",
654 {"_id": nsilcmop_id},
655 {"operationParams.nslcmops_ids": nslcmop_new},
656 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100657
658 # Wait until Network Slice is terminated
garciadeblas5697b8b2021-03-24 09:17:02 +0100659 step = nsir_status_detailed = " Waiting nsi terminated. nsi_id={}".format(
660 nsir_id
661 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100662 nsrs_detailed_list_old = None
663 self.logger.debug(logging_text + step)
kuused124bfe2019-06-18 12:09:24 +0200664
garciadeblas5697b8b2021-03-24 09:17:02 +0100665 termination_timeout = 2 * 3600 # Two hours
Felipe Vicensc2033f22018-11-15 15:09:58 +0100666 while termination_timeout > 0:
667 # Check ns termination status
668 nsi_ready = True
Felipe Vicens720b07a2019-01-31 02:32:09 +0100669 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100670 nsrs_detailed_list = db_nsir["_admin"].get("nsrs-detailed-list")
671 nsrs_detailed_list_new = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100672 for nslcmop_item in nslcmop_ids:
673 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_item})
674 status = nslcmop["operationState"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100675 # TODO: (future improvement) other possible status: ROLLING_BACK,ROLLED_BACK
Felipe Vicens720b07a2019-01-31 02:32:09 +0100676 for nss in nsrs_detailed_list:
677 if nss["nsrId"] == nslcmop["nsInstanceId"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100678 nss.update(
679 {
680 "nsrId": nslcmop["nsInstanceId"],
681 "status": nslcmop["operationState"],
682 "detailed-status": nsir_status_detailed
683 + "; {}".format(nslcmop.get("detailed-status")),
684 }
685 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100686 nsrs_detailed_list_new.append(nss)
garciadeblas5697b8b2021-03-24 09:17:02 +0100687 if status not in [
688 "COMPLETED",
689 "PARTIALLY_COMPLETED",
690 "FAILED",
691 "FAILED_TEMP",
692 ]:
Felipe Vicensc2033f22018-11-15 15:09:58 +0100693 nsi_ready = False
694
Felipe Vicens720b07a2019-01-31 02:32:09 +0100695 if nsrs_detailed_list_new != nsrs_detailed_list_old:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100696 nsrs_detailed_list_old = nsrs_detailed_list_new
garciadeblas5697b8b2021-03-24 09:17:02 +0100697 self.update_db_2(
698 "nsis",
699 nsir_id,
700 {"_admin.nsrs-detailed-list": nsrs_detailed_list_new},
701 )
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100702
Felipe Vicensc2033f22018-11-15 15:09:58 +0100703 if nsi_ready:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100704 # Check if it is the last used nss and mark isinstantiate: False
705 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100706 nsrs_detailed_list = db_nsir["_admin"].get("nsrs-detailed-list")
707 for nss in nsrs_detailed_list:
garciadeblas5697b8b2021-03-24 09:17:02 +0100708 _filter = {
709 "_admin.nsrs-detailed-list.ANYINDEX.nsrId": nss["nsrId"],
710 "operational-status.ne": "terminated",
711 "_id.ne": nsir_id,
712 }
713 nsis_list = self.db.get_one(
714 "nsis", _filter, fail_on_empty=False, fail_on_more=False
715 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100716 if not nsis_list:
717 nss.update({"instantiated": False})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100718
garciadeblas5697b8b2021-03-24 09:17:02 +0100719 step = "Network Slice Instance is terminated. nsi_id={}".format(
720 nsir_id
721 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100722 for items in nsrs_detailed_list:
723 if "FAILED" in items.values():
garciadeblas5697b8b2021-03-24 09:17:02 +0100724 raise LcmException(
725 "Error terminating NSI: {}".format(nsir_id)
726 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100727 break
728
729 await asyncio.sleep(5, loop=self.loop)
730 termination_timeout -= 5
731
732 if termination_timeout <= 0:
garciadeblas5697b8b2021-03-24 09:17:02 +0100733 raise LcmException(
734 "Timeout waiting nsi to be terminated. nsi_id={}".format(nsir_id)
735 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100736
Felipe Vicens720b07a2019-01-31 02:32:09 +0100737 # Delete netslice-vlds
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100738 RO_nsir_id = RO_delete_action = None
Felipe Vicens720b07a2019-01-31 02:32:09 +0100739 for nsir_deployed_RO in get_iterable(nsir_deployed, "RO"):
740 RO_nsir_id = nsir_deployed_RO.get("netslice_scenario_id")
741 try:
bravof922c4172020-11-24 21:21:43 -0300742 if not self.ro_config["ng"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100743 step = db_nsir_update[
744 "detailed-status"
745 ] = "Deleting netslice-vld at RO"
746 db_nsilcmop_update[
747 "detailed-status"
748 ] = "Deleting netslice-vld at RO"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100749 self.logger.debug(logging_text + step)
bravof922c4172020-11-24 21:21:43 -0300750 desc = await RO.delete("ns", RO_nsir_id)
751 RO_delete_action = desc["action_id"]
752 nsir_deployed_RO["vld_delete_action_id"] = RO_delete_action
753 nsir_deployed_RO["vld_status"] = "DELETING"
754 db_nsir_update["_admin.deployed"] = nsir_deployed
755 self.update_db_2("nsis", nsir_id, db_nsir_update)
756 if RO_delete_action:
757 # wait until NS is deleted from VIM
garciadeblas5697b8b2021-03-24 09:17:02 +0100758 step = "Waiting ns deleted from VIM. RO_id={}".format(
759 RO_nsir_id
760 )
bravof922c4172020-11-24 21:21:43 -0300761 self.logger.debug(logging_text + step)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100762 except ROclient.ROClientException as e:
763 if e.http_code == 404: # not found
764 nsir_deployed_RO["vld_id"] = None
765 nsir_deployed_RO["vld_status"] = "DELETED"
garciadeblas5697b8b2021-03-24 09:17:02 +0100766 self.logger.debug(
767 logging_text
768 + "RO_ns_id={} already deleted".format(RO_nsir_id)
769 )
770 elif e.http_code == 409: # conflict
771 failed_detail.append(
772 "RO_ns_id={} delete conflict: {}".format(RO_nsir_id, e)
773 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100774 self.logger.debug(logging_text + failed_detail[-1])
775 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100776 failed_detail.append(
777 "RO_ns_id={} delete error: {}".format(RO_nsir_id, e)
778 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100779 self.logger.error(logging_text + failed_detail[-1])
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100780
Felipe Vicens720b07a2019-01-31 02:32:09 +0100781 if failed_detail:
782 self.logger.error(logging_text + " ;".join(failed_detail))
783 db_nsir_update["operational-status"] = "failed"
garciadeblas5697b8b2021-03-24 09:17:02 +0100784 db_nsir_update["detailed-status"] = "Deletion errors " + "; ".join(
785 failed_detail
786 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100787 db_nsilcmop_update["detailed-status"] = "; ".join(failed_detail)
garciadeblas5697b8b2021-03-24 09:17:02 +0100788 db_nsilcmop_update[
789 "operationState"
790 ] = nsilcmop_operation_state = "FAILED"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100791 db_nsilcmop_update["statusEnteredTime"] = time()
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100792 else:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100793 db_nsir_update["operational-status"] = "terminating"
794 db_nsir_update["config-status"] = "terminating"
795 db_nsir_update["_admin.nsiState"] = "NOT_INSTANTIATED"
garciadeblas5697b8b2021-03-24 09:17:02 +0100796 db_nsilcmop_update[
797 "operationState"
798 ] = nsilcmop_operation_state = "COMPLETED"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100799 db_nsilcmop_update["statusEnteredTime"] = time()
800 if db_nsilcmop["operationParams"].get("autoremove"):
801 autoremove = True
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100802
Felipe Vicens720b07a2019-01-31 02:32:09 +0100803 db_nsir_update["detailed-status"] = "done"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100804 db_nsir_update["operational-status"] = "terminated"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100805 db_nsir_update["config-status"] = "terminated"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100806 db_nsilcmop_update["statusEnteredTime"] = time()
807 db_nsilcmop_update["detailed-status"] = "done"
808 return
809
810 except (LcmException, DbException) as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100811 self.logger.error(
812 logging_text + "Exit Exception while '{}': {}".format(step, e)
813 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100814 exc = e
815 except asyncio.CancelledError:
garciadeblas5697b8b2021-03-24 09:17:02 +0100816 self.logger.error(
817 logging_text + "Cancelled Exception while '{}'".format(step)
818 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100819 exc = "Operation was cancelled"
820 except Exception as e:
821 exc = traceback.format_exc()
garciadeblas5697b8b2021-03-24 09:17:02 +0100822 self.logger.critical(
823 logging_text
824 + "Exit Exception {} while '{}': {}".format(type(e).__name__, step, e),
825 exc_info=True,
826 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100827 finally:
828 if exc:
829 if db_nsir:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100830 db_nsir_update["_admin.deployed"] = nsir_deployed
Felipe Vicensc2033f22018-11-15 15:09:58 +0100831 db_nsir_update["detailed-status"] = "ERROR {}: {}".format(step, exc)
832 db_nsir_update["operational-status"] = "failed"
833 if db_nsilcmop:
garciadeblas5697b8b2021-03-24 09:17:02 +0100834 db_nsilcmop_update["detailed-status"] = "FAILED {}: {}".format(
835 step, exc
836 )
837 db_nsilcmop_update[
838 "operationState"
839 ] = nsilcmop_operation_state = "FAILED"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100840 db_nsilcmop_update["statusEnteredTime"] = time()
tiernobaa51102018-12-14 13:16:18 +0000841 try:
842 if db_nsir:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100843 db_nsir_update["_admin.deployed"] = nsir_deployed
tiernobaa51102018-12-14 13:16:18 +0000844 db_nsir_update["_admin.nsilcmop"] = None
tiernobaa51102018-12-14 13:16:18 +0000845 self.update_db_2("nsis", nsir_id, db_nsir_update)
846 if db_nsilcmop:
847 self.update_db_2("nsilcmops", nsilcmop_id, db_nsilcmop_update)
848 except DbException as e:
849 self.logger.error(logging_text + "Cannot update database: {}".format(e))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100850
851 if nsilcmop_operation_state:
852 try:
garciadeblas5697b8b2021-03-24 09:17:02 +0100853 await self.msg.aiowrite(
854 "nsi",
855 "terminated",
856 {
857 "nsir_id": nsir_id,
858 "nsilcmop_id": nsilcmop_id,
859 "operationState": nsilcmop_operation_state,
860 "autoremove": autoremove,
861 },
862 loop=self.loop,
863 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100864 except Exception as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100865 self.logger.error(
866 logging_text + "kafka_write notification Exception {}".format(e)
867 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100868 self.logger.debug(logging_text + "Exit")
869 self.lcm_tasks.remove("nsi", nsir_id, nsilcmop_id, "nsi_terminate")