blob: 3a8002ce24bbf78c7349757ff5cabbb7f9592b89 [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):
37
tierno744303e2020-01-13 16:46:31 +000038 timeout_nsi_deploy = 2 * 3600 # default global timeout for deployment a nsi
Felipe Vicens6559b4a2018-12-01 04:40:48 +010039
bravof922c4172020-11-24 21:21:43 -030040 def __init__(self, msg, lcm_tasks, config, loop, ns):
Felipe Vicensc2033f22018-11-15 15:09:58 +010041 """
42 Init, Connect to database, filesystem storage, and messaging
43 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
44 :return: None
45 """
46 # logging
garciadeblas5697b8b2021-03-24 09:17:02 +010047 self.logger = logging.getLogger("lcm.netslice")
Felipe Vicensc2033f22018-11-15 15:09:58 +010048 self.loop = loop
49 self.lcm_tasks = lcm_tasks
tiernob996d942020-07-03 14:52:28 +000050 self.ns = ns
tierno744303e2020-01-13 16:46:31 +000051 self.ro_config = config["ro_config"]
52 self.timeout = config["timeout"]
Felipe Vicensc2033f22018-11-15 15:09:58 +010053
bravof922c4172020-11-24 21:21:43 -030054 super().__init__(msg, self.logger)
Felipe Vicensc2033f22018-11-15 15:09:58 +010055
Felipe Vicens0f389ce2019-01-12 12:20:11 +010056 def nsi_update_nsir(self, nsi_update_nsir, db_nsir, nsir_desc_RO):
57 """
58 Updates database nsir with the RO info for the created vld
59 :param nsi_update_nsir: dictionary to be filled with the updated info
60 :param db_nsir: content of db_nsir. This is also modified
61 :param nsir_desc_RO: nsir descriptor from RO
62 :return: Nothing, LcmException is raised on errors
63 """
64
65 for vld_index, vld in enumerate(get_iterable(db_nsir, "vld")):
66 for net_RO in get_iterable(nsir_desc_RO, "nets"):
67 if vld["id"] != net_RO.get("ns_net_osm_id"):
68 continue
69 vld["vim-id"] = net_RO.get("vim_net_id")
70 vld["name"] = net_RO.get("vim_name")
71 vld["status"] = net_RO.get("status")
72 vld["status-detailed"] = net_RO.get("error_msg")
73 nsi_update_nsir["vld.{}".format(vld_index)] = vld
74 break
75 else:
garciadeblas5697b8b2021-03-24 09:17:02 +010076 raise LcmException(
77 "ns_update_nsir: Not found vld={} at RO info".format(vld["id"])
78 )
Felipe Vicens0f389ce2019-01-12 12:20:11 +010079
Felipe Vicensc2033f22018-11-15 15:09:58 +010080 async def instantiate(self, nsir_id, nsilcmop_id):
kuused124bfe2019-06-18 12:09:24 +020081
82 # Try to lock HA task here
garciadeblas5697b8b2021-03-24 09:17:02 +010083 task_is_locked_by_me = self.lcm_tasks.lock_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +020084 if not task_is_locked_by_me:
85 return
86
Felipe Vicensc2033f22018-11-15 15:09:58 +010087 logging_text = "Task netslice={} instantiate={} ".format(nsir_id, nsilcmop_id)
88 self.logger.debug(logging_text + "Enter")
89 # get all needed from database
Felipe Vicens0f389ce2019-01-12 12:20:11 +010090 exc = None
Felipe Vicensc2033f22018-11-15 15:09:58 +010091 db_nsir = None
92 db_nsilcmop = None
93 db_nsir_update = {"_admin.nsilcmop": nsilcmop_id}
94 db_nsilcmop_update = {}
95 nsilcmop_operation_state = None
Felipe Vicens6559b4a2018-12-01 04:40:48 +010096 vim_2_RO = {}
97 RO = ROclient.ROClient(self.loop, **self.ro_config)
bravof922c4172020-11-24 21:21:43 -030098 nsi_vld_instantiationi_params = {}
Felipe Vicens720b07a2019-01-31 02:32:09 +010099
100 def ip_profile_2_RO(ip_profile):
101 RO_ip_profile = deepcopy((ip_profile))
102 if "dns-server" in RO_ip_profile:
103 if isinstance(RO_ip_profile["dns-server"], list):
104 RO_ip_profile["dns-address"] = []
105 for ds in RO_ip_profile.pop("dns-server"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100106 RO_ip_profile["dns-address"].append(ds["address"])
Felipe Vicens720b07a2019-01-31 02:32:09 +0100107 else:
108 RO_ip_profile["dns-address"] = RO_ip_profile.pop("dns-server")
109 if RO_ip_profile.get("ip-version") == "ipv4":
110 RO_ip_profile["ip-version"] = "IPv4"
111 if RO_ip_profile.get("ip-version") == "ipv6":
112 RO_ip_profile["ip-version"] = "IPv6"
113 if "dhcp-params" in RO_ip_profile:
114 RO_ip_profile["dhcp"] = RO_ip_profile.pop("dhcp-params")
115 return RO_ip_profile
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100116
117 def vim_account_2_RO(vim_account):
118 """
119 Translate a RO vim_account from OSM vim_account params
120 :param ns_params: OSM instantiate params
121 :return: The RO ns descriptor
122 """
123 if vim_account in vim_2_RO:
124 return vim_2_RO[vim_account]
125
126 db_vim = self.db.get_one("vim_accounts", {"_id": vim_account})
127 if db_vim["_admin"]["operationalState"] != "ENABLED":
garciadeblas5697b8b2021-03-24 09:17:02 +0100128 raise LcmException(
129 "VIM={} is not available. operationalState={}".format(
130 vim_account, db_vim["_admin"]["operationalState"]
131 )
132 )
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100133 RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
134 vim_2_RO[vim_account] = RO_vim_id
135 return RO_vim_id
136
garciadeblas5697b8b2021-03-24 09:17:02 +0100137 async def netslice_scenario_create(
138 self, vld_item, nsir_id, db_nsir, db_nsir_admin, db_nsir_update
139 ):
Felipe Vicens720b07a2019-01-31 02:32:09 +0100140 """
141 Create a network slice VLD through RO Scenario
142 :param vld_id The VLD id inside nsir to be created
143 :param nsir_id The nsir id
144 """
bravof922c4172020-11-24 21:21:43 -0300145 nonlocal nsi_vld_instantiationi_params
Felipe Vicens720b07a2019-01-31 02:32:09 +0100146 ip_vld = None
147 mgmt_network = False
148 RO_vld_sites = []
149 vld_id = vld_item["id"]
150 netslice_vld = vld_item
151 # logging_text = "Task netslice={} instantiate_vld={} ".format(nsir_id, vld_id)
152 # self.logger.debug(logging_text + "Enter")
153
154 vld_shared = None
155 for shared_nsrs_item in get_iterable(vld_item, "shared-nsrs-list"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100156 _filter = {
157 "_id.ne": nsir_id,
158 "_admin.nsrs-detailed-list.ANYINDEX.nsrId": shared_nsrs_item,
159 }
160 shared_nsi = self.db.get_one(
161 "nsis", _filter, fail_on_empty=False, fail_on_more=False
162 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100163 if shared_nsi:
164 for vlds in get_iterable(shared_nsi["_admin"]["deployed"], "RO"):
165 if vld_id == vlds["vld_id"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100166 vld_shared = {
167 "instance_scenario_id": vlds["netslice_scenario_id"],
168 "osm_id": vld_id,
169 }
Felipe Vicens720b07a2019-01-31 02:32:09 +0100170 break
171 break
172
173 # Creating netslice-vld at RO
tierno744303e2020-01-13 16:46:31 +0000174 RO_nsir = deep_get(db_nsir, ("_admin", "deployed", "RO"), [])
Felipe Vicens720b07a2019-01-31 02:32:09 +0100175
176 if vld_id in RO_nsir:
177 db_nsir_update["_admin.deployed.RO"] = RO_nsir
178
179 # If netslice-vld doesn't exists then create it
180 else:
181 # TODO: Check VDU type in all descriptors finding SRIOV / PT
182 # Updating network names and datacenters from instantiation parameters for each VLD
garciadeblas5697b8b2021-03-24 09:17:02 +0100183 for instantiation_params_vld in get_iterable(
184 db_nsir["instantiation_parameters"], "netslice-vld"
185 ):
Felipe Vicens720b07a2019-01-31 02:32:09 +0100186 if instantiation_params_vld.get("name") == netslice_vld["name"]:
187 ip_vld = deepcopy(instantiation_params_vld)
bravof922c4172020-11-24 21:21:43 -0300188 ip_vld.pop("name")
189 nsi_vld_instantiationi_params[netslice_vld["name"]] = ip_vld
Felipe Vicens720b07a2019-01-31 02:32:09 +0100190
bravof922c4172020-11-24 21:21:43 -0300191 db_nsir_update_RO = {}
192 db_nsir_update_RO["vld_id"] = netslice_vld["name"]
193 if self.ro_config["ng"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100194 db_nsir_update_RO["netslice_scenario_id"] = (
195 vld_shared.get("instance_scenario_id")
196 if vld_shared
bravof922c4172020-11-24 21:21:43 -0300197 else "nsir:{}:vld.{}".format(nsir_id, netslice_vld["name"])
garciadeblas5697b8b2021-03-24 09:17:02 +0100198 )
bravof922c4172020-11-24 21:21:43 -0300199 else: # if not self.ro_config["ng"]:
200 if netslice_vld.get("mgmt-network"):
201 mgmt_network = True
202 RO_ns_params = {}
203 RO_ns_params["name"] = netslice_vld["name"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100204 RO_ns_params["datacenter"] = vim_account_2_RO(
205 db_nsir["instantiation_parameters"]["vimAccountId"]
206 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100207
bravof922c4172020-11-24 21:21:43 -0300208 # Creating scenario if vim-network-name / vim-network-id are present as instantiation parameter
209 # Use vim-network-id instantiation parameter
210 vim_network_option = None
211 if ip_vld:
212 if ip_vld.get("vim-network-id"):
213 vim_network_option = "vim-network-id"
214 elif ip_vld.get("vim-network-name"):
215 vim_network_option = "vim-network-name"
216 if ip_vld.get("ip-profile"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100217 populate_dict(
218 RO_ns_params,
219 ("networks", netslice_vld["name"], "ip-profile"),
220 ip_profile_2_RO(ip_vld["ip-profile"]),
221 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100222
bravof922c4172020-11-24 21:21:43 -0300223 if vim_network_option:
224 if ip_vld.get(vim_network_option):
225 if isinstance(ip_vld.get(vim_network_option), list):
226 for vim_net_id in ip_vld.get(vim_network_option):
227 for vim_account, vim_net in vim_net_id.items():
garciadeblas5697b8b2021-03-24 09:17:02 +0100228 RO_vld_sites.append(
229 {
230 "netmap-use": vim_net,
231 "datacenter": vim_account_2_RO(
232 vim_account
233 ),
234 }
235 )
bravof922c4172020-11-24 21:21:43 -0300236 elif isinstance(ip_vld.get(vim_network_option), dict):
garciadeblas5697b8b2021-03-24 09:17:02 +0100237 for vim_account, vim_net in ip_vld.get(
238 vim_network_option
239 ).items():
240 RO_vld_sites.append(
241 {
242 "netmap-use": vim_net,
243 "datacenter": vim_account_2_RO(vim_account),
244 }
245 )
bravof922c4172020-11-24 21:21:43 -0300246 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100247 RO_vld_sites.append(
248 {
249 "netmap-use": ip_vld[vim_network_option],
250 "datacenter": vim_account_2_RO(
251 netslice_vld["vimAccountId"]
252 ),
253 }
254 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100255
bravof922c4172020-11-24 21:21:43 -0300256 # Use default netslice vim-network-name from template
257 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100258 for nss_conn_point_ref in get_iterable(
259 netslice_vld, "nss-connection-point-ref"
260 ):
bravof922c4172020-11-24 21:21:43 -0300261 if nss_conn_point_ref.get("vimAccountId"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100262 if (
263 nss_conn_point_ref["vimAccountId"]
264 != netslice_vld["vimAccountId"]
265 ):
266 RO_vld_sites.append(
267 {
268 "netmap-create": None,
269 "datacenter": vim_account_2_RO(
270 nss_conn_point_ref["vimAccountId"]
271 ),
272 }
273 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100274
bravof922c4172020-11-24 21:21:43 -0300275 if vld_shared:
garciadeblas5697b8b2021-03-24 09:17:02 +0100276 populate_dict(
277 RO_ns_params,
278 ("networks", netslice_vld["name"], "use-network"),
279 vld_shared,
280 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100281
bravof922c4172020-11-24 21:21:43 -0300282 if RO_vld_sites:
garciadeblas5697b8b2021-03-24 09:17:02 +0100283 populate_dict(
284 RO_ns_params,
285 ("networks", netslice_vld["name"], "sites"),
286 RO_vld_sites,
287 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100288
garciadeblas5697b8b2021-03-24 09:17:02 +0100289 RO_ns_params["scenario"] = {
290 "nets": [
291 {
292 "name": netslice_vld["name"],
293 "external": mgmt_network,
294 "type": "bridge",
295 }
296 ]
297 }
bravof922c4172020-11-24 21:21:43 -0300298
299 # self.logger.debug(logging_text + step)
300 desc = await RO.create("ns", descriptor=RO_ns_params)
301 db_nsir_update_RO["netslice_scenario_id"] = desc["uuid"]
Felipe Vicens720b07a2019-01-31 02:32:09 +0100302 db_nsir_update["_admin.deployed.RO"].append(db_nsir_update_RO)
kuused124bfe2019-06-18 12:09:24 +0200303
Felipe Vicens720b07a2019-01-31 02:32:09 +0100304 def overwrite_nsd_params(self, db_nsir, nslcmop):
bravof922c4172020-11-24 21:21:43 -0300305 nonlocal nsi_vld_instantiationi_params
306 nonlocal db_nsir_update
Felipe Vicens720b07a2019-01-31 02:32:09 +0100307 vld_op_list = []
308 vld = None
309 nsr_id = nslcmop.get("nsInstanceId")
310 # Overwrite instantiation parameters in netslice runtime
bravof922c4172020-11-24 21:21:43 -0300311 RO_list = db_nsir_admin["deployed"]["RO"]
Felipe Vicens720b07a2019-01-31 02:32:09 +0100312
bravof922c4172020-11-24 21:21:43 -0300313 for ro_item_index, RO_item in enumerate(RO_list):
garciadeblas5697b8b2021-03-24 09:17:02 +0100314 netslice_vld = next(
315 (
316 n
317 for n in get_iterable(db_nsir["_admin"], "netslice-vld")
318 if RO_item.get("vld_id") == n.get("id")
319 ),
320 None,
321 )
bravof922c4172020-11-24 21:21:43 -0300322 if not netslice_vld:
323 continue
324 # if is equal vld of _admin with vld of netslice-vld then go for the CPs
325 # Search the cp of netslice-vld that match with nst:netslice-subnet
garciadeblas5697b8b2021-03-24 09:17:02 +0100326 for nss_cp_item in get_iterable(
327 netslice_vld, "nss-connection-point-ref"
328 ):
bravof922c4172020-11-24 21:21:43 -0300329 # Search the netslice-subnet of nst that match
garciadeblas5697b8b2021-03-24 09:17:02 +0100330 nss = next(
331 (
332 nss
333 for nss in get_iterable(
334 db_nsir["_admin"], "netslice-subnet"
335 )
336 if nss_cp_item["nss-ref"] == nss["nss-id"]
337 ),
338 None,
339 )
bravof922c4172020-11-24 21:21:43 -0300340 # Compare nss-ref equal nss from nst
341 if not nss:
342 continue
343 db_nsds = self.db.get_one("nsds", {"_id": nss["nsdId"]})
344 # Go for nsd, and search the CP that match with nst:CP to get vld-id-ref
bravofa7cb70b2020-12-11 16:37:01 -0300345 for cp_nsd in db_nsds.get("sapd", ()):
346 if cp_nsd["id"] == nss_cp_item["nsd-connection-point-ref"]:
bravof922c4172020-11-24 21:21:43 -0300347 if nslcmop.get("operationParams"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100348 if (
349 nslcmop["operationParams"].get("nsName")
350 == nss["nsName"]
351 ):
bravof922c4172020-11-24 21:21:43 -0300352 vld_id = RO_item["vld_id"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100353 netslice_scenario_id = RO_item[
354 "netslice_scenario_id"
355 ]
bravof922c4172020-11-24 21:21:43 -0300356 nslcmop_vld = {}
bravofa7cb70b2020-12-11 16:37:01 -0300357 nslcmop_vld["name"] = cp_nsd["virtual-link-desc"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100358 for vld in get_iterable(
359 nslcmop["operationParams"], "vld"
360 ):
bravofa7cb70b2020-12-11 16:37:01 -0300361 if vld["name"] == cp_nsd["virtual-link-desc"]:
bravof922c4172020-11-24 21:21:43 -0300362 nslcmop_vld.update(vld)
363 if self.ro_config["ng"]:
364 nslcmop_vld["common_id"] = netslice_scenario_id
garciadeblas5697b8b2021-03-24 09:17:02 +0100365 nslcmop_vld.update(
366 nsi_vld_instantiationi_params.get(
367 RO_item["vld_id"], {}
368 )
369 )
bravof922c4172020-11-24 21:21:43 -0300370 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100371 nslcmop_vld["ns-net"] = {
372 vld_id: netslice_scenario_id
373 }
bravof922c4172020-11-24 21:21:43 -0300374 vld_op_list.append(nslcmop_vld)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100375 nslcmop["operationParams"]["vld"] = vld_op_list
garciadeblas5697b8b2021-03-24 09:17:02 +0100376 self.update_db_2(
377 "nslcmops", nslcmop["_id"], {"operationParams.vld": vld_op_list}
378 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100379 return nsr_id, nslcmop
380
Felipe Vicensc2033f22018-11-15 15:09:58 +0100381 try:
kuused124bfe2019-06-18 12:09:24 +0200382 # wait for any previous tasks in process
garciadeblas5697b8b2021-03-24 09:17:02 +0100383 await self.lcm_tasks.waitfor_related_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +0200384
Felipe Vicensc2033f22018-11-15 15:09:58 +0100385 step = "Getting nsir={} from db".format(nsir_id)
386 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
387 step = "Getting nsilcmop={} from db".format(nsilcmop_id)
388 db_nsilcmop = self.db.get_one("nsilcmops", {"_id": nsilcmop_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100389
tierno744303e2020-01-13 16:46:31 +0000390 start_deploy = time()
391 nsi_params = db_nsilcmop.get("operationParams")
392 if nsi_params and nsi_params.get("timeout_nsi_deploy"):
393 timeout_nsi_deploy = nsi_params["timeout_nsi_deploy"]
394 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100395 timeout_nsi_deploy = self.timeout.get(
396 "nsi_deploy", self.timeout_nsi_deploy
397 )
tierno744303e2020-01-13 16:46:31 +0000398
Felipe Vicensc2033f22018-11-15 15:09:58 +0100399 # Empty list to keep track of network service records status in the netslice
Felipe Vicens720b07a2019-01-31 02:32:09 +0100400 nsir_admin = db_nsir_admin = db_nsir.get("_admin")
Felipe Vicensc2033f22018-11-15 15:09:58 +0100401
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100402 step = "Creating slice operational-status init"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100403 # Slice status Creating
404 db_nsir_update["detailed-status"] = "creating"
405 db_nsir_update["operational-status"] = "init"
tierno5e98d462020-08-10 13:21:28 +0000406 db_nsir_update["_admin.nsiState"] = "INSTANTIATED"
kuused124bfe2019-06-18 12:09:24 +0200407
tierno5e98d462020-08-10 13:21:28 +0000408 step = "Instantiating netslice VLDs before NS instantiation"
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100409 # Creating netslice VLDs networking before NS instantiation
tierno5e98d462020-08-10 13:21:28 +0000410 db_nsir_update["detailed-status"] = step
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100411 self.update_db_2("nsis", nsir_id, db_nsir_update)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100412 db_nsir_update["_admin.deployed.RO"] = db_nsir_admin["deployed"]["RO"]
413 for vld_item in get_iterable(nsir_admin, "netslice-vld"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100414 await netslice_scenario_create(
415 self, vld_item, nsir_id, db_nsir, db_nsir_admin, db_nsir_update
416 )
kuused124bfe2019-06-18 12:09:24 +0200417
tierno5e98d462020-08-10 13:21:28 +0000418 step = "Instantiating netslice subnets"
419 db_nsir_update["detailed-status"] = step
kuused124bfe2019-06-18 12:09:24 +0200420 self.update_db_2("nsis", nsir_id, db_nsir_update)
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100421
Felipe Vicens720b07a2019-01-31 02:32:09 +0100422 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100423
Felipe Vicens720b07a2019-01-31 02:32:09 +0100424 # Check status of the VLDs and wait for creation
425 # netslice_scenarios = db_nsir["_admin"]["deployed"]["RO"]
426 # db_nsir_update_RO = deepcopy(netslice_scenarios)
427 # for netslice_scenario in netslice_scenarios:
kuused124bfe2019-06-18 12:09:24 +0200428 # await netslice_scenario_check(self, netslice_scenario["netslice_scenario_id"],
Felipe Vicens720b07a2019-01-31 02:32:09 +0100429 # nsir_id, db_nsir_update_RO)
kuused124bfe2019-06-18 12:09:24 +0200430
Felipe Vicens720b07a2019-01-31 02:32:09 +0100431 # db_nsir_update["_admin.deployed.RO"] = db_nsir_update_RO
432 # self.update_db_2("nsis", nsir_id, db_nsir_update)
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100433
Felipe Vicensc2033f22018-11-15 15:09:58 +0100434 # Iterate over the network services operation ids to instantiate NSs
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100435 step = "Instantiating Netslice Subnets"
Felipe Vicens0f389ce2019-01-12 12:20:11 +0100436 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicensc2033f22018-11-15 15:09:58 +0100437 nslcmop_ids = db_nsilcmop["operationParams"].get("nslcmops_ids")
438 for nslcmop_id in nslcmop_ids:
439 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100440 # Overwriting netslice-vld vim-net-id to ns
441 nsr_id, nslcmop = overwrite_nsd_params(self, db_nsir, nslcmop)
442 step = "Launching ns={} instantiate={} task".format(nsr_id, nslcmop_id)
Felipe Vicensc2033f22018-11-15 15:09:58 +0100443 task = asyncio.ensure_future(self.ns.instantiate(nsr_id, nslcmop_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100444 self.lcm_tasks.register(
445 "ns", nsr_id, nslcmop_id, "ns_instantiate", task
446 )
kuused124bfe2019-06-18 12:09:24 +0200447
Felipe Vicensc2033f22018-11-15 15:09:58 +0100448 # Wait until Network Slice is ready
tierno5e98d462020-08-10 13:21:28 +0000449 step = " Waiting nsi ready."
Felipe Vicensc2033f22018-11-15 15:09:58 +0100450 nsrs_detailed_list_old = None
451 self.logger.debug(logging_text + step)
kuused124bfe2019-06-18 12:09:24 +0200452
tierno5e98d462020-08-10 13:21:28 +0000453 # For HA, it is checked from database, as the ns operation may be managed by other LCM worker
tierno744303e2020-01-13 16:46:31 +0000454 while time() <= start_deploy + timeout_nsi_deploy:
Felipe Vicensc2033f22018-11-15 15:09:58 +0100455 # Check ns instantiation status
456 nsi_ready = True
Felipe Vicens720b07a2019-01-31 02:32:09 +0100457 nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100458 nsrs_detailed_list = nsir["_admin"]["nsrs-detailed-list"]
459 nsrs_detailed_list_new = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100460 for nslcmop_item in nslcmop_ids:
461 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_item})
462 status = nslcmop.get("operationState")
463 # TODO: (future improvement) other possible status: ROLLING_BACK,ROLLED_BACK
Felipe Vicens720b07a2019-01-31 02:32:09 +0100464 for nss in nsrs_detailed_list:
465 if nss["nsrId"] == nslcmop["nsInstanceId"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100466 nss.update(
467 {
468 "nsrId": nslcmop["nsInstanceId"],
469 "status": nslcmop["operationState"],
470 "detailed-status": nslcmop.get("detailed-status"),
471 "instantiated": True,
472 }
473 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100474 nsrs_detailed_list_new.append(nss)
garciadeblas5697b8b2021-03-24 09:17:02 +0100475 if status not in [
476 "COMPLETED",
477 "PARTIALLY_COMPLETED",
478 "FAILED",
479 "FAILED_TEMP",
480 ]:
Felipe Vicensc2033f22018-11-15 15:09:58 +0100481 nsi_ready = False
482
Felipe Vicens720b07a2019-01-31 02:32:09 +0100483 if nsrs_detailed_list_new != nsrs_detailed_list_old:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100484 nsrs_detailed_list_old = nsrs_detailed_list_new
garciadeblas5697b8b2021-03-24 09:17:02 +0100485 self.update_db_2(
486 "nsis",
487 nsir_id,
488 {"_admin.nsrs-detailed-list": nsrs_detailed_list_new},
489 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100490
491 if nsi_ready:
tierno5e98d462020-08-10 13:21:28 +0000492 error_list = []
493 step = "Network Slice Instance instantiated"
494 for nss in nsrs_detailed_list:
495 if nss["status"] in ("FAILED", "FAILED_TEMP"):
garciadeblas5697b8b2021-03-24 09:17:02 +0100496 error_list.append(
497 "NS {} {}: {}".format(
498 nss["nsrId"], nss["status"], nss["detailed-status"]
499 )
500 )
tierno5e98d462020-08-10 13:21:28 +0000501 if error_list:
502 step = "instantiating"
503 raise LcmException("; ".join(error_list))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100504 break
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100505
Felipe Vicensc2033f22018-11-15 15:09:58 +0100506 # TODO: future improvement due to synchronism -> await asyncio.wait(vca_task_list, timeout=300)
507 await asyncio.sleep(5, loop=self.loop)
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100508
garciadeblas5697b8b2021-03-24 09:17:02 +0100509 else: # timeout_nsi_deploy reached:
tierno5e98d462020-08-10 13:21:28 +0000510 raise LcmException("Timeout waiting nsi to be ready.")
Felipe Vicensc2033f22018-11-15 15:09:58 +0100511
512 db_nsir_update["operational-status"] = "running"
513 db_nsir_update["detailed-status"] = "done"
514 db_nsir_update["config-status"] = "configured"
garciadeblas5697b8b2021-03-24 09:17:02 +0100515 db_nsilcmop_update[
516 "operationState"
517 ] = nsilcmop_operation_state = "COMPLETED"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100518 db_nsilcmop_update["statusEnteredTime"] = time()
519 db_nsilcmop_update["detailed-status"] = "done"
520 return
521
522 except (LcmException, DbException) as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100523 self.logger.error(
524 logging_text + "Exit Exception while '{}': {}".format(step, e)
525 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100526 exc = e
527 except asyncio.CancelledError:
garciadeblas5697b8b2021-03-24 09:17:02 +0100528 self.logger.error(
529 logging_text + "Cancelled Exception while '{}'".format(step)
530 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100531 exc = "Operation was cancelled"
532 except Exception as e:
533 exc = traceback.format_exc()
garciadeblas5697b8b2021-03-24 09:17:02 +0100534 self.logger.critical(
535 logging_text
536 + "Exit Exception {} while '{}': {}".format(type(e).__name__, step, e),
537 exc_info=True,
538 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100539 finally:
540 if exc:
541 if db_nsir:
542 db_nsir_update["detailed-status"] = "ERROR {}: {}".format(step, exc)
543 db_nsir_update["operational-status"] = "failed"
tierno5e98d462020-08-10 13:21:28 +0000544 db_nsir_update["config-status"] = "configured"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100545 if db_nsilcmop:
garciadeblas5697b8b2021-03-24 09:17:02 +0100546 db_nsilcmop_update["detailed-status"] = "FAILED {}: {}".format(
547 step, exc
548 )
549 db_nsilcmop_update[
550 "operationState"
551 ] = nsilcmop_operation_state = "FAILED"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100552 db_nsilcmop_update["statusEnteredTime"] = time()
tiernobaa51102018-12-14 13:16:18 +0000553 try:
554 if db_nsir:
tiernobaa51102018-12-14 13:16:18 +0000555 db_nsir_update["_admin.nsilcmop"] = None
556 self.update_db_2("nsis", nsir_id, db_nsir_update)
557 if db_nsilcmop:
558 self.update_db_2("nsilcmops", nsilcmop_id, db_nsilcmop_update)
559 except DbException as e:
560 self.logger.error(logging_text + "Cannot update database: {}".format(e))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100561 if nsilcmop_operation_state:
562 try:
garciadeblas5697b8b2021-03-24 09:17:02 +0100563 await self.msg.aiowrite(
564 "nsi",
565 "instantiated",
566 {
567 "nsir_id": nsir_id,
568 "nsilcmop_id": nsilcmop_id,
569 "operationState": nsilcmop_operation_state,
570 },
571 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100572 except Exception as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100573 self.logger.error(
574 logging_text + "kafka_write notification Exception {}".format(e)
575 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100576 self.logger.debug(logging_text + "Exit")
577 self.lcm_tasks.remove("nsi", nsir_id, nsilcmop_id, "nsi_instantiate")
578
579 async def terminate(self, nsir_id, nsilcmop_id):
kuused124bfe2019-06-18 12:09:24 +0200580
581 # Try to lock HA task here
garciadeblas5697b8b2021-03-24 09:17:02 +0100582 task_is_locked_by_me = self.lcm_tasks.lock_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +0200583 if not task_is_locked_by_me:
584 return
585
Felipe Vicensc2033f22018-11-15 15:09:58 +0100586 logging_text = "Task nsi={} terminate={} ".format(nsir_id, nsilcmop_id)
587 self.logger.debug(logging_text + "Enter")
kuused124bfe2019-06-18 12:09:24 +0200588 exc = None
Felipe Vicensc2033f22018-11-15 15:09:58 +0100589 db_nsir = None
590 db_nsilcmop = None
591 db_nsir_update = {"_admin.nsilcmop": nsilcmop_id}
592 db_nsilcmop_update = {}
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100593 RO = ROclient.ROClient(self.loop, **self.ro_config)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100594 nsir_deployed = None
garciadeblas5697b8b2021-03-24 09:17:02 +0100595 failed_detail = [] # annotates all failed error messages
Felipe Vicensc2033f22018-11-15 15:09:58 +0100596 nsilcmop_operation_state = None
tiernoc2564fe2019-01-28 16:18:56 +0000597 autoremove = False # autoremove after terminated
Felipe Vicensc2033f22018-11-15 15:09:58 +0100598 try:
kuused124bfe2019-06-18 12:09:24 +0200599 # wait for any previous tasks in process
garciadeblas5697b8b2021-03-24 09:17:02 +0100600 await self.lcm_tasks.waitfor_related_HA("nsi", "nsilcmops", nsilcmop_id)
kuused124bfe2019-06-18 12:09:24 +0200601
Felipe Vicensc2033f22018-11-15 15:09:58 +0100602 step = "Getting nsir={} from db".format(nsir_id)
603 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100604 nsir_deployed = deepcopy(db_nsir["_admin"].get("deployed"))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100605 step = "Getting nsilcmop={} from db".format(nsilcmop_id)
606 db_nsilcmop = self.db.get_one("nsilcmops", {"_id": nsilcmop_id})
kuused124bfe2019-06-18 12:09:24 +0200607
Felipe Vicensc2033f22018-11-15 15:09:58 +0100608 # TODO: Check if makes sense check the nsiState=NOT_INSTANTIATED when terminate
609 # CASE: Instance was terminated but there is a second request to terminate the instance
610 if db_nsir["_admin"]["nsiState"] == "NOT_INSTANTIATED":
611 return
612
613 # Slice status Terminating
614 db_nsir_update["operational-status"] = "terminating"
615 db_nsir_update["config-status"] = "terminating"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100616 db_nsir_update["detailed-status"] = "Terminating Netslice subnets"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100617 self.update_db_2("nsis", nsir_id, db_nsir_update)
618
Felipe Vicensc2033f22018-11-15 15:09:58 +0100619 # Gets the list to keep track of network service records status in the netslice
kuused124bfe2019-06-18 12:09:24 +0200620 nsrs_detailed_list = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100621
622 # Iterate over the network services operation ids to terminate NSs
kuused124bfe2019-06-18 12:09:24 +0200623 # TODO: (future improvement) look another way check the tasks instead of keep asking
Felipe Vicensc2033f22018-11-15 15:09:58 +0100624 # -> https://docs.python.org/3/library/asyncio-task.html#waiting-primitives
625 # 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 +0100626 step = "Terminating Netslice Subnets"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100627 nslcmop_ids = db_nsilcmop["operationParams"].get("nslcmops_ids")
Felipe Vicens720b07a2019-01-31 02:32:09 +0100628 nslcmop_new = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100629 for nslcmop_id in nslcmop_ids:
630 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_id})
631 nsr_id = nslcmop["operationParams"].get("nsInstanceId")
garciadeblas5697b8b2021-03-24 09:17:02 +0100632 nss_in_use = self.db.get_list(
633 "nsis",
634 {
635 "_admin.netslice-vld.ANYINDEX.shared-nsrs-list": nsr_id,
636 "operational-status": {"$nin": ["terminated", "failed"]},
637 },
638 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100639 if len(nss_in_use) < 2:
640 task = asyncio.ensure_future(self.ns.terminate(nsr_id, nslcmop_id))
garciadeblas5697b8b2021-03-24 09:17:02 +0100641 self.lcm_tasks.register(
642 "ns", nsr_id, nslcmop_id, "ns_instantiate", task
643 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100644 nslcmop_new.append(nslcmop_id)
645 else:
646 # Update shared nslcmop shared with active nsi
647 netsliceInstanceId = db_nsir["_id"]
648 for nsis_item in nss_in_use:
649 if db_nsir["_id"] != nsis_item["_id"]:
650 netsliceInstanceId = nsis_item["_id"]
651 break
garciadeblas5697b8b2021-03-24 09:17:02 +0100652 self.db.set_one(
653 "nslcmops",
654 {"_id": nslcmop_id},
655 {"operationParams.netsliceInstanceId": netsliceInstanceId},
656 )
657 self.db.set_one(
658 "nsilcmops",
659 {"_id": nsilcmop_id},
660 {"operationParams.nslcmops_ids": nslcmop_new},
661 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100662
663 # Wait until Network Slice is terminated
garciadeblas5697b8b2021-03-24 09:17:02 +0100664 step = nsir_status_detailed = " Waiting nsi terminated. nsi_id={}".format(
665 nsir_id
666 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100667 nsrs_detailed_list_old = None
668 self.logger.debug(logging_text + step)
kuused124bfe2019-06-18 12:09:24 +0200669
garciadeblas5697b8b2021-03-24 09:17:02 +0100670 termination_timeout = 2 * 3600 # Two hours
Felipe Vicensc2033f22018-11-15 15:09:58 +0100671 while termination_timeout > 0:
672 # Check ns termination status
673 nsi_ready = True
Felipe Vicens720b07a2019-01-31 02:32:09 +0100674 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100675 nsrs_detailed_list = db_nsir["_admin"].get("nsrs-detailed-list")
676 nsrs_detailed_list_new = []
Felipe Vicensc2033f22018-11-15 15:09:58 +0100677 for nslcmop_item in nslcmop_ids:
678 nslcmop = self.db.get_one("nslcmops", {"_id": nslcmop_item})
679 status = nslcmop["operationState"]
garciadeblas5697b8b2021-03-24 09:17:02 +0100680 # TODO: (future improvement) other possible status: ROLLING_BACK,ROLLED_BACK
Felipe Vicens720b07a2019-01-31 02:32:09 +0100681 for nss in nsrs_detailed_list:
682 if nss["nsrId"] == nslcmop["nsInstanceId"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100683 nss.update(
684 {
685 "nsrId": nslcmop["nsInstanceId"],
686 "status": nslcmop["operationState"],
687 "detailed-status": nsir_status_detailed
688 + "; {}".format(nslcmop.get("detailed-status")),
689 }
690 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100691 nsrs_detailed_list_new.append(nss)
garciadeblas5697b8b2021-03-24 09:17:02 +0100692 if status not in [
693 "COMPLETED",
694 "PARTIALLY_COMPLETED",
695 "FAILED",
696 "FAILED_TEMP",
697 ]:
Felipe Vicensc2033f22018-11-15 15:09:58 +0100698 nsi_ready = False
699
Felipe Vicens720b07a2019-01-31 02:32:09 +0100700 if nsrs_detailed_list_new != nsrs_detailed_list_old:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100701 nsrs_detailed_list_old = nsrs_detailed_list_new
garciadeblas5697b8b2021-03-24 09:17:02 +0100702 self.update_db_2(
703 "nsis",
704 nsir_id,
705 {"_admin.nsrs-detailed-list": nsrs_detailed_list_new},
706 )
Felipe Vicensb0e5fe42019-12-05 10:30:38 +0100707
Felipe Vicensc2033f22018-11-15 15:09:58 +0100708 if nsi_ready:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100709 # Check if it is the last used nss and mark isinstantiate: False
710 db_nsir = self.db.get_one("nsis", {"_id": nsir_id})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100711 nsrs_detailed_list = db_nsir["_admin"].get("nsrs-detailed-list")
712 for nss in nsrs_detailed_list:
garciadeblas5697b8b2021-03-24 09:17:02 +0100713 _filter = {
714 "_admin.nsrs-detailed-list.ANYINDEX.nsrId": nss["nsrId"],
715 "operational-status.ne": "terminated",
716 "_id.ne": nsir_id,
717 }
718 nsis_list = self.db.get_one(
719 "nsis", _filter, fail_on_empty=False, fail_on_more=False
720 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100721 if not nsis_list:
722 nss.update({"instantiated": False})
Felipe Vicens720b07a2019-01-31 02:32:09 +0100723
garciadeblas5697b8b2021-03-24 09:17:02 +0100724 step = "Network Slice Instance is terminated. nsi_id={}".format(
725 nsir_id
726 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100727 for items in nsrs_detailed_list:
728 if "FAILED" in items.values():
garciadeblas5697b8b2021-03-24 09:17:02 +0100729 raise LcmException(
730 "Error terminating NSI: {}".format(nsir_id)
731 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100732 break
733
734 await asyncio.sleep(5, loop=self.loop)
735 termination_timeout -= 5
736
737 if termination_timeout <= 0:
garciadeblas5697b8b2021-03-24 09:17:02 +0100738 raise LcmException(
739 "Timeout waiting nsi to be terminated. nsi_id={}".format(nsir_id)
740 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100741
Felipe Vicens720b07a2019-01-31 02:32:09 +0100742 # Delete netslice-vlds
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100743 RO_nsir_id = RO_delete_action = None
Felipe Vicens720b07a2019-01-31 02:32:09 +0100744 for nsir_deployed_RO in get_iterable(nsir_deployed, "RO"):
745 RO_nsir_id = nsir_deployed_RO.get("netslice_scenario_id")
746 try:
bravof922c4172020-11-24 21:21:43 -0300747 if not self.ro_config["ng"]:
garciadeblas5697b8b2021-03-24 09:17:02 +0100748 step = db_nsir_update[
749 "detailed-status"
750 ] = "Deleting netslice-vld at RO"
751 db_nsilcmop_update[
752 "detailed-status"
753 ] = "Deleting netslice-vld at RO"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100754 self.logger.debug(logging_text + step)
bravof922c4172020-11-24 21:21:43 -0300755 desc = await RO.delete("ns", RO_nsir_id)
756 RO_delete_action = desc["action_id"]
757 nsir_deployed_RO["vld_delete_action_id"] = RO_delete_action
758 nsir_deployed_RO["vld_status"] = "DELETING"
759 db_nsir_update["_admin.deployed"] = nsir_deployed
760 self.update_db_2("nsis", nsir_id, db_nsir_update)
761 if RO_delete_action:
762 # wait until NS is deleted from VIM
garciadeblas5697b8b2021-03-24 09:17:02 +0100763 step = "Waiting ns deleted from VIM. RO_id={}".format(
764 RO_nsir_id
765 )
bravof922c4172020-11-24 21:21:43 -0300766 self.logger.debug(logging_text + step)
Felipe Vicens720b07a2019-01-31 02:32:09 +0100767 except ROclient.ROClientException as e:
768 if e.http_code == 404: # not found
769 nsir_deployed_RO["vld_id"] = None
770 nsir_deployed_RO["vld_status"] = "DELETED"
garciadeblas5697b8b2021-03-24 09:17:02 +0100771 self.logger.debug(
772 logging_text
773 + "RO_ns_id={} already deleted".format(RO_nsir_id)
774 )
775 elif e.http_code == 409: # conflict
776 failed_detail.append(
777 "RO_ns_id={} delete conflict: {}".format(RO_nsir_id, e)
778 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100779 self.logger.debug(logging_text + failed_detail[-1])
780 else:
garciadeblas5697b8b2021-03-24 09:17:02 +0100781 failed_detail.append(
782 "RO_ns_id={} delete error: {}".format(RO_nsir_id, e)
783 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100784 self.logger.error(logging_text + failed_detail[-1])
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100785
Felipe Vicens720b07a2019-01-31 02:32:09 +0100786 if failed_detail:
787 self.logger.error(logging_text + " ;".join(failed_detail))
788 db_nsir_update["operational-status"] = "failed"
garciadeblas5697b8b2021-03-24 09:17:02 +0100789 db_nsir_update["detailed-status"] = "Deletion errors " + "; ".join(
790 failed_detail
791 )
Felipe Vicens720b07a2019-01-31 02:32:09 +0100792 db_nsilcmop_update["detailed-status"] = "; ".join(failed_detail)
garciadeblas5697b8b2021-03-24 09:17:02 +0100793 db_nsilcmop_update[
794 "operationState"
795 ] = nsilcmop_operation_state = "FAILED"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100796 db_nsilcmop_update["statusEnteredTime"] = time()
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100797 else:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100798 db_nsir_update["operational-status"] = "terminating"
799 db_nsir_update["config-status"] = "terminating"
800 db_nsir_update["_admin.nsiState"] = "NOT_INSTANTIATED"
garciadeblas5697b8b2021-03-24 09:17:02 +0100801 db_nsilcmop_update[
802 "operationState"
803 ] = nsilcmop_operation_state = "COMPLETED"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100804 db_nsilcmop_update["statusEnteredTime"] = time()
805 if db_nsilcmop["operationParams"].get("autoremove"):
806 autoremove = True
Felipe Vicens6559b4a2018-12-01 04:40:48 +0100807
Felipe Vicens720b07a2019-01-31 02:32:09 +0100808 db_nsir_update["detailed-status"] = "done"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100809 db_nsir_update["operational-status"] = "terminated"
Felipe Vicens720b07a2019-01-31 02:32:09 +0100810 db_nsir_update["config-status"] = "terminated"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100811 db_nsilcmop_update["statusEnteredTime"] = time()
812 db_nsilcmop_update["detailed-status"] = "done"
813 return
814
815 except (LcmException, DbException) as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100816 self.logger.error(
817 logging_text + "Exit Exception while '{}': {}".format(step, e)
818 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100819 exc = e
820 except asyncio.CancelledError:
garciadeblas5697b8b2021-03-24 09:17:02 +0100821 self.logger.error(
822 logging_text + "Cancelled Exception while '{}'".format(step)
823 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100824 exc = "Operation was cancelled"
825 except Exception as e:
826 exc = traceback.format_exc()
garciadeblas5697b8b2021-03-24 09:17:02 +0100827 self.logger.critical(
828 logging_text
829 + "Exit Exception {} while '{}': {}".format(type(e).__name__, step, e),
830 exc_info=True,
831 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100832 finally:
833 if exc:
834 if db_nsir:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100835 db_nsir_update["_admin.deployed"] = nsir_deployed
Felipe Vicensc2033f22018-11-15 15:09:58 +0100836 db_nsir_update["detailed-status"] = "ERROR {}: {}".format(step, exc)
837 db_nsir_update["operational-status"] = "failed"
838 if db_nsilcmop:
garciadeblas5697b8b2021-03-24 09:17:02 +0100839 db_nsilcmop_update["detailed-status"] = "FAILED {}: {}".format(
840 step, exc
841 )
842 db_nsilcmop_update[
843 "operationState"
844 ] = nsilcmop_operation_state = "FAILED"
Felipe Vicensc2033f22018-11-15 15:09:58 +0100845 db_nsilcmop_update["statusEnteredTime"] = time()
tiernobaa51102018-12-14 13:16:18 +0000846 try:
847 if db_nsir:
Felipe Vicens720b07a2019-01-31 02:32:09 +0100848 db_nsir_update["_admin.deployed"] = nsir_deployed
tiernobaa51102018-12-14 13:16:18 +0000849 db_nsir_update["_admin.nsilcmop"] = None
tiernobaa51102018-12-14 13:16:18 +0000850 self.update_db_2("nsis", nsir_id, db_nsir_update)
851 if db_nsilcmop:
852 self.update_db_2("nsilcmops", nsilcmop_id, db_nsilcmop_update)
853 except DbException as e:
854 self.logger.error(logging_text + "Cannot update database: {}".format(e))
Felipe Vicensc2033f22018-11-15 15:09:58 +0100855
856 if nsilcmop_operation_state:
857 try:
garciadeblas5697b8b2021-03-24 09:17:02 +0100858 await self.msg.aiowrite(
859 "nsi",
860 "terminated",
861 {
862 "nsir_id": nsir_id,
863 "nsilcmop_id": nsilcmop_id,
864 "operationState": nsilcmop_operation_state,
865 "autoremove": autoremove,
866 },
867 loop=self.loop,
868 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100869 except Exception as e:
garciadeblas5697b8b2021-03-24 09:17:02 +0100870 self.logger.error(
871 logging_text + "kafka_write notification Exception {}".format(e)
872 )
Felipe Vicensc2033f22018-11-15 15:09:58 +0100873 self.logger.debug(logging_text + "Exit")
874 self.lcm_tasks.remove("nsi", nsir_id, nsilcmop_id, "nsi_terminate")