1 # -*- coding: utf-8 -*-
4 # Copyright 2018 Telefonica S.A.
6 # Licensed under the Apache License, Version 2.0 (the "License"); you may
7 # not use this file except in compliance with the License. You may obtain
8 # a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 # License for the specific language governing permissions and limitations
22 import logging
.handlers
24 from jinja2
import Environment
, Template
, meta
, TemplateError
, TemplateNotFound
, TemplateSyntaxError
26 from osm_lcm
import ROclient
27 from osm_lcm
.lcm_utils
import LcmException
, LcmExceptionNoMgmtIP
, LcmBase
29 from osm_common
.dbbase
import DbException
30 from osm_common
.fsbase
import FsException
31 from n2vc
.vnf
import N2VC
, N2VCPrimitiveExecutionFailed
, NetworkServiceDoesNotExist
33 from copy
import copy
, deepcopy
34 from http
import HTTPStatus
36 from uuid
import uuid4
38 __author__
= "Alfonso Tierno"
41 def get_iterable(in_dict
, in_key
):
43 Similar to <dict>.get(), but if value is None, False, ..., An empty tuple is returned instead
44 :param in_dict: a dictionary
45 :param in_key: the key to look for at in_dict
46 :return: in_dict[in_var] or () if it is None or not present
48 if not in_dict
.get(in_key
):
50 return in_dict
[in_key
]
53 def populate_dict(target_dict
, key_list
, value
):
55 Update target_dict creating nested dictionaries with the key_list. Last key_list item is asigned the value.
56 Example target_dict={K: J}; key_list=[a,b,c]; target_dict will be {K: J, a: {b: {c: value}}}
57 :param target_dict: dictionary to be changed
58 :param key_list: list of keys to insert at target_dict
62 for key
in key_list
[0:-1]:
63 if key
not in target_dict
:
65 target_dict
= target_dict
[key
]
66 target_dict
[key_list
[-1]] = value
69 def deep_get(target_dict
, key_list
):
71 Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None
72 Example target_dict={a: {b: 5}}; key_list=[a,b] returns 5; both key_list=[a,b,c] and key_list=[f,h] return None
73 :param target_dict: dictionary to be read
74 :param key_list: list of keys to read from target_dict
75 :return: The wanted value if exist, None otherwise
78 if not isinstance(target_dict
, dict) or key
not in target_dict
:
80 target_dict
= target_dict
[key
]
85 timeout_vca_on_error
= 5 * 60 # Time for charm from first time at blocked,error status to mark as failed
86 total_deploy_timeout
= 2 * 3600 # global timeout for deployment
87 timeout_charm_delete
= 10 * 60
88 timeout_primitive
= 10 * 60 # timeout for primitive execution
90 SUBOPERATION_STATUS_NOT_FOUND
= -1
91 SUBOPERATION_STATUS_NEW
= -2
92 SUBOPERATION_STATUS_SKIP
= -3
94 def __init__(self
, db
, msg
, fs
, lcm_tasks
, ro_config
, vca_config
, loop
):
96 Init, Connect to database, filesystem storage, and messaging
97 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
101 self
.logger
= logging
.getLogger('lcm.ns')
103 self
.lcm_tasks
= lcm_tasks
105 super().__init
__(db
, msg
, fs
, self
.logger
)
107 self
.ro_config
= ro_config
111 server
=vca_config
['host'],
112 port
=vca_config
['port'],
113 user
=vca_config
['user'],
114 secret
=vca_config
['secret'],
115 # TODO: This should point to the base folder where charms are stored,
116 # if there is a common one (like object storage). Otherwise, leave
117 # it unset and pass it via DeployCharms
118 # artifacts=vca_config[''],
120 juju_public_key
=vca_config
.get('pubkey'),
121 ca_cert
=vca_config
.get('cacert'),
122 api_proxy
=vca_config
.get('apiproxy')
124 self
.RO
= ROclient
.ROClient(self
.loop
, **self
.ro_config
)
126 def vnfd2RO(self
, vnfd
, new_id
=None, additionalParams
=None, nsrId
=None):
128 Converts creates a new vnfd descriptor for RO base on input OSM IM vnfd
129 :param vnfd: input vnfd
130 :param new_id: overrides vnf id if provided
131 :param additionalParams: Instantiation params for VNFs provided
132 :param nsrId: Id of the NSR
133 :return: copy of vnfd
136 vnfd_RO
= deepcopy(vnfd
)
137 # remove unused by RO configuration, monitoring, scaling and internal keys
138 vnfd_RO
.pop("_id", None)
139 vnfd_RO
.pop("_admin", None)
140 vnfd_RO
.pop("vnf-configuration", None)
141 vnfd_RO
.pop("monitoring-param", None)
142 vnfd_RO
.pop("scaling-group-descriptor", None)
144 vnfd_RO
["id"] = new_id
146 # parse cloud-init or cloud-init-file with the provided variables using Jinja2
147 for vdu
in get_iterable(vnfd_RO
, "vdu"):
148 cloud_init_file
= None
149 if vdu
.get("cloud-init-file"):
150 base_folder
= vnfd
["_admin"]["storage"]
151 cloud_init_file
= "{}/{}/cloud_init/{}".format(base_folder
["folder"], base_folder
["pkg-dir"],
152 vdu
["cloud-init-file"])
153 with self
.fs
.file_open(cloud_init_file
, "r") as ci_file
:
154 cloud_init_content
= ci_file
.read()
155 vdu
.pop("cloud-init-file", None)
156 elif vdu
.get("cloud-init"):
157 cloud_init_content
= vdu
["cloud-init"]
162 ast
= env
.parse(cloud_init_content
)
163 mandatory_vars
= meta
.find_undeclared_variables(ast
)
165 for var
in mandatory_vars
:
166 if not additionalParams
or var
not in additionalParams
.keys():
167 raise LcmException("Variable '{}' defined at vnfd[id={}]:vdu[id={}]:cloud-init/cloud-init-"
168 "file, must be provided in the instantiation parameters inside the "
169 "'additionalParamsForVnf' block".format(var
, vnfd
["id"], vdu
["id"]))
170 template
= Template(cloud_init_content
)
171 cloud_init_content
= template
.render(additionalParams
or {})
172 vdu
["cloud-init"] = cloud_init_content
175 except FsException
as e
:
176 raise LcmException("Error reading vnfd[id={}]:vdu[id={}]:cloud-init-file={}: {}".
177 format(vnfd
["id"], vdu
["id"], cloud_init_file
, e
))
178 except (TemplateError
, TemplateNotFound
, TemplateSyntaxError
) as e
:
179 raise LcmException("Error parsing Jinja2 to cloud-init content at vnfd[id={}]:vdu[id={}]: {}".
180 format(vnfd
["id"], vdu
["id"], e
))
182 def n2vc_callback(self
, model_name
, application_name
, status
, message
, n2vc_info
, task
=None):
184 Callback both for charm status change and task completion
185 :param model_name: Charm model name
186 :param application_name: Charm application name
187 :param status: Can be
188 - blocked: The unit needs manual intervention
189 - maintenance: The unit is actively deploying/configuring
190 - waiting: The unit is waiting for another charm to be ready
191 - active: The unit is deployed, configured, and ready
192 - error: The charm has failed and needs attention.
193 - terminated: The charm has been destroyed
196 :param message: detailed message error
197 :param n2vc_info: dictionary with information shared with instantiate task. It contains:
200 lcmOperationType: currently "instantiate"
201 deployed: dictionary with {<application>: {operational-status: <status>, detailed-status: <text>}}
202 db_update: dictionary to be filled with the changes to be wrote to database with format key.key.key: value
203 n2vc_event: event used to notify instantiation task that some change has been produced
204 :param task: None for charm status change, or task for completion task callback
208 nsr_id
= n2vc_info
["nsr_id"]
209 deployed
= n2vc_info
["deployed"]
210 db_nsr_update
= n2vc_info
["db_update"]
211 nslcmop_id
= n2vc_info
["nslcmop_id"]
212 ns_operation
= n2vc_info
["lcmOperationType"]
213 n2vc_event
= n2vc_info
["n2vc_event"]
214 logging_text
= "Task ns={} {}={} [n2vc_callback] application={}".format(nsr_id
, ns_operation
, nslcmop_id
,
216 for vca_index
, vca_deployed
in enumerate(deployed
):
219 if model_name
== vca_deployed
["model"] and application_name
== vca_deployed
["application"]:
222 self
.logger
.error(logging_text
+ " Not present at nsr._admin.deployed.VCA. Received model_name={}".
227 self
.logger
.debug(logging_text
+ " task Cancelled")
228 vca_deployed
['operational-status'] = "error"
229 db_nsr_update
["_admin.deployed.VCA.{}.operational-status".format(vca_index
)] = "error"
230 vca_deployed
['detailed-status'] = "Task Cancelled"
231 db_nsr_update
["_admin.deployed.VCA.{}.detailed-status".format(vca_index
)] = "Task Cancelled"
234 exc
= task
.exception()
236 self
.logger
.error(logging_text
+ " task Exception={}".format(exc
))
237 vca_deployed
['operational-status'] = "error"
238 db_nsr_update
["_admin.deployed.VCA.{}.operational-status".format(vca_index
)] = "error"
239 vca_deployed
['detailed-status'] = str(exc
)
240 db_nsr_update
["_admin.deployed.VCA.{}.detailed-status".format(vca_index
)] = str(exc
)
242 self
.logger
.debug(logging_text
+ " task Done")
243 # task is Done, but callback is still ongoing. So ignore
246 self
.logger
.debug(logging_text
+ " Enter status={} message={}".format(status
, message
))
247 if vca_deployed
['operational-status'] == status
:
248 return # same status, ignore
249 vca_deployed
['operational-status'] = status
250 db_nsr_update
["_admin.deployed.VCA.{}.operational-status".format(vca_index
)] = status
251 vca_deployed
['detailed-status'] = str(message
)
252 db_nsr_update
["_admin.deployed.VCA.{}.detailed-status".format(vca_index
)] = str(message
)
254 self
.logger
.critical(logging_text
+ " Enter with bad parameters", exc_info
=True)
256 # wake up instantiate task
258 except Exception as e
:
259 self
.logger
.critical(logging_text
+ " Exception {}".format(e
), exc_info
=True)
261 def ns_params_2_RO(self
, ns_params
, nsd
, vnfd_dict
, n2vc_key_list
):
263 Creates a RO ns descriptor from OSM ns_instantiate params
264 :param ns_params: OSM instantiate params
265 :return: The RO ns descriptor
269 # TODO feature 1417: Check that no instantiation is set over PDU
270 # check if PDU forces a concrete vim-network-id and add it
271 # check if PDU contains a SDN-assist info (dpid, switch, port) and pass it to RO
273 def vim_account_2_RO(vim_account
):
274 if vim_account
in vim_2_RO
:
275 return vim_2_RO
[vim_account
]
277 db_vim
= self
.db
.get_one("vim_accounts", {"_id": vim_account
})
278 if db_vim
["_admin"]["operationalState"] != "ENABLED":
279 raise LcmException("VIM={} is not available. operationalState={}".format(
280 vim_account
, db_vim
["_admin"]["operationalState"]))
281 RO_vim_id
= db_vim
["_admin"]["deployed"]["RO"]
282 vim_2_RO
[vim_account
] = RO_vim_id
285 def wim_account_2_RO(wim_account
):
286 if isinstance(wim_account
, str):
287 if wim_account
in wim_2_RO
:
288 return wim_2_RO
[wim_account
]
290 db_wim
= self
.db
.get_one("wim_accounts", {"_id": wim_account
})
291 if db_wim
["_admin"]["operationalState"] != "ENABLED":
292 raise LcmException("WIM={} is not available. operationalState={}".format(
293 wim_account
, db_wim
["_admin"]["operationalState"]))
294 RO_wim_id
= db_wim
["_admin"]["deployed"]["RO-account"]
295 wim_2_RO
[wim_account
] = RO_wim_id
300 def ip_profile_2_RO(ip_profile
):
301 RO_ip_profile
= deepcopy((ip_profile
))
302 if "dns-server" in RO_ip_profile
:
303 if isinstance(RO_ip_profile
["dns-server"], list):
304 RO_ip_profile
["dns-address"] = []
305 for ds
in RO_ip_profile
.pop("dns-server"):
306 RO_ip_profile
["dns-address"].append(ds
['address'])
308 RO_ip_profile
["dns-address"] = RO_ip_profile
.pop("dns-server")
309 if RO_ip_profile
.get("ip-version") == "ipv4":
310 RO_ip_profile
["ip-version"] = "IPv4"
311 if RO_ip_profile
.get("ip-version") == "ipv6":
312 RO_ip_profile
["ip-version"] = "IPv6"
313 if "dhcp-params" in RO_ip_profile
:
314 RO_ip_profile
["dhcp"] = RO_ip_profile
.pop("dhcp-params")
320 # "name": ns_params["nsName"],
321 # "description": ns_params.get("nsDescription"),
322 "datacenter": vim_account_2_RO(ns_params
["vimAccountId"]),
323 "wim_account": wim_account_2_RO(ns_params
.get("wimAccountId")),
324 # "scenario": ns_params["nsdId"],
326 n2vc_key_list
= n2vc_key_list
or []
327 for vnfd_ref
, vnfd
in vnfd_dict
.items():
328 vdu_needed_access
= []
330 if vnfd
.get("vnf-configuration"):
331 ssh_required
= deep_get(vnfd
, ("vnf-configuration", "config-access", "ssh-access", "required"))
332 if ssh_required
and vnfd
.get("mgmt-interface"):
333 if vnfd
["mgmt-interface"].get("vdu-id"):
334 vdu_needed_access
.append(vnfd
["mgmt-interface"]["vdu-id"])
335 elif vnfd
["mgmt-interface"].get("cp"):
336 mgmt_cp
= vnfd
["mgmt-interface"]["cp"]
338 for vdu
in vnfd
.get("vdu", ()):
339 if vdu
.get("vdu-configuration"):
340 ssh_required
= deep_get(vdu
, ("vdu-configuration", "config-access", "ssh-access", "required"))
342 vdu_needed_access
.append(vdu
["id"])
344 for vdu_interface
in vdu
.get("interface"):
345 if vdu_interface
.get("external-connection-point-ref") and \
346 vdu_interface
["external-connection-point-ref"] == mgmt_cp
:
347 vdu_needed_access
.append(vdu
["id"])
351 if vdu_needed_access
:
352 for vnf_member
in nsd
.get("constituent-vnfd"):
353 if vnf_member
["vnfd-id-ref"] != vnfd_ref
:
355 for vdu
in vdu_needed_access
:
356 populate_dict(RO_ns_params
,
357 ("vnfs", vnf_member
["member-vnf-index"], "vdus", vdu
, "mgmt_keys"),
360 if ns_params
.get("vduImage"):
361 RO_ns_params
["vduImage"] = ns_params
["vduImage"]
363 if ns_params
.get("ssh_keys"):
364 RO_ns_params
["cloud-config"] = {"key-pairs": ns_params
["ssh_keys"]}
365 for vnf_params
in get_iterable(ns_params
, "vnf"):
366 for constituent_vnfd
in nsd
["constituent-vnfd"]:
367 if constituent_vnfd
["member-vnf-index"] == vnf_params
["member-vnf-index"]:
368 vnf_descriptor
= vnfd_dict
[constituent_vnfd
["vnfd-id-ref"]]
371 raise LcmException("Invalid instantiate parameter vnf:member-vnf-index={} is not present at nsd:"
372 "constituent-vnfd".format(vnf_params
["member-vnf-index"]))
373 if vnf_params
.get("vimAccountId"):
374 populate_dict(RO_ns_params
, ("vnfs", vnf_params
["member-vnf-index"], "datacenter"),
375 vim_account_2_RO(vnf_params
["vimAccountId"]))
377 for vdu_params
in get_iterable(vnf_params
, "vdu"):
378 # TODO feature 1417: check that this VDU exist and it is not a PDU
379 if vdu_params
.get("volume"):
380 for volume_params
in vdu_params
["volume"]:
381 if volume_params
.get("vim-volume-id"):
382 populate_dict(RO_ns_params
, ("vnfs", vnf_params
["member-vnf-index"], "vdus",
383 vdu_params
["id"], "devices", volume_params
["name"], "vim_id"),
384 volume_params
["vim-volume-id"])
385 if vdu_params
.get("interface"):
386 for interface_params
in vdu_params
["interface"]:
387 if interface_params
.get("ip-address"):
388 populate_dict(RO_ns_params
, ("vnfs", vnf_params
["member-vnf-index"], "vdus",
389 vdu_params
["id"], "interfaces", interface_params
["name"],
391 interface_params
["ip-address"])
392 if interface_params
.get("mac-address"):
393 populate_dict(RO_ns_params
, ("vnfs", vnf_params
["member-vnf-index"], "vdus",
394 vdu_params
["id"], "interfaces", interface_params
["name"],
396 interface_params
["mac-address"])
397 if interface_params
.get("floating-ip-required"):
398 populate_dict(RO_ns_params
, ("vnfs", vnf_params
["member-vnf-index"], "vdus",
399 vdu_params
["id"], "interfaces", interface_params
["name"],
401 interface_params
["floating-ip-required"])
403 for internal_vld_params
in get_iterable(vnf_params
, "internal-vld"):
404 if internal_vld_params
.get("vim-network-name"):
405 populate_dict(RO_ns_params
, ("vnfs", vnf_params
["member-vnf-index"], "networks",
406 internal_vld_params
["name"], "vim-network-name"),
407 internal_vld_params
["vim-network-name"])
408 if internal_vld_params
.get("vim-network-id"):
409 populate_dict(RO_ns_params
, ("vnfs", vnf_params
["member-vnf-index"], "networks",
410 internal_vld_params
["name"], "vim-network-id"),
411 internal_vld_params
["vim-network-id"])
412 if internal_vld_params
.get("ip-profile"):
413 populate_dict(RO_ns_params
, ("vnfs", vnf_params
["member-vnf-index"], "networks",
414 internal_vld_params
["name"], "ip-profile"),
415 ip_profile_2_RO(internal_vld_params
["ip-profile"]))
417 for icp_params
in get_iterable(internal_vld_params
, "internal-connection-point"):
420 for vdu_descriptor
in vnf_descriptor
["vdu"]:
421 for vdu_interface
in vdu_descriptor
["interface"]:
422 if vdu_interface
.get("internal-connection-point-ref") == icp_params
["id-ref"]:
423 if icp_params
.get("ip-address"):
424 populate_dict(RO_ns_params
, ("vnfs", vnf_params
["member-vnf-index"], "vdus",
425 vdu_descriptor
["id"], "interfaces",
426 vdu_interface
["name"], "ip_address"),
427 icp_params
["ip-address"])
429 if icp_params
.get("mac-address"):
430 populate_dict(RO_ns_params
, ("vnfs", vnf_params
["member-vnf-index"], "vdus",
431 vdu_descriptor
["id"], "interfaces",
432 vdu_interface
["name"], "mac_address"),
433 icp_params
["mac-address"])
439 raise LcmException("Invalid instantiate parameter vnf:member-vnf-index[{}]:"
440 "internal-vld:id-ref={} is not present at vnfd:internal-"
441 "connection-point".format(vnf_params
["member-vnf-index"],
442 icp_params
["id-ref"]))
444 for vld_params
in get_iterable(ns_params
, "vld"):
445 if "ip-profile" in vld_params
:
446 populate_dict(RO_ns_params
, ("networks", vld_params
["name"], "ip-profile"),
447 ip_profile_2_RO(vld_params
["ip-profile"]))
449 if "wimAccountId" in vld_params
and vld_params
["wimAccountId"] is not None:
450 populate_dict(RO_ns_params
, ("networks", vld_params
["name"], "wim_account"),
451 wim_account_2_RO(vld_params
["wimAccountId"])),
452 if vld_params
.get("vim-network-name"):
454 if isinstance(vld_params
["vim-network-name"], dict):
455 for vim_account
, vim_net
in vld_params
["vim-network-name"].items():
456 RO_vld_sites
.append({
457 "netmap-use": vim_net
,
458 "datacenter": vim_account_2_RO(vim_account
)
460 else: # isinstance str
461 RO_vld_sites
.append({"netmap-use": vld_params
["vim-network-name"]})
463 populate_dict(RO_ns_params
, ("networks", vld_params
["name"], "sites"), RO_vld_sites
)
464 if vld_params
.get("vim-network-id"):
466 if isinstance(vld_params
["vim-network-id"], dict):
467 for vim_account
, vim_net
in vld_params
["vim-network-id"].items():
468 RO_vld_sites
.append({
469 "netmap-use": vim_net
,
470 "datacenter": vim_account_2_RO(vim_account
)
472 else: # isinstance str
473 RO_vld_sites
.append({"netmap-use": vld_params
["vim-network-id"]})
475 populate_dict(RO_ns_params
, ("networks", vld_params
["name"], "sites"), RO_vld_sites
)
476 if vld_params
.get("ns-net"):
477 if isinstance(vld_params
["ns-net"], dict):
478 for vld_id
, instance_scenario_id
in vld_params
["ns-net"].items():
479 RO_vld_ns_net
= {"instance_scenario_id": instance_scenario_id
, "osm_id": vld_id
}
481 populate_dict(RO_ns_params
, ("networks", vld_params
["name"], "use-network"), RO_vld_ns_net
)
482 if "vnfd-connection-point-ref" in vld_params
:
483 for cp_params
in vld_params
["vnfd-connection-point-ref"]:
485 for constituent_vnfd
in nsd
["constituent-vnfd"]:
486 if constituent_vnfd
["member-vnf-index"] == cp_params
["member-vnf-index-ref"]:
487 vnf_descriptor
= vnfd_dict
[constituent_vnfd
["vnfd-id-ref"]]
491 "Invalid instantiate parameter vld:vnfd-connection-point-ref:member-vnf-index-ref={} "
492 "is not present at nsd:constituent-vnfd".format(cp_params
["member-vnf-index-ref"]))
494 for vdu_descriptor
in vnf_descriptor
["vdu"]:
495 for interface_descriptor
in vdu_descriptor
["interface"]:
496 if interface_descriptor
.get("external-connection-point-ref") == \
497 cp_params
["vnfd-connection-point-ref"]:
504 "Invalid instantiate parameter vld:vnfd-connection-point-ref:member-vnf-index-ref={}:"
505 "vnfd-connection-point-ref={} is not present at vnfd={}".format(
506 cp_params
["member-vnf-index-ref"],
507 cp_params
["vnfd-connection-point-ref"],
508 vnf_descriptor
["id"]))
509 if cp_params
.get("ip-address"):
510 populate_dict(RO_ns_params
, ("vnfs", cp_params
["member-vnf-index-ref"], "vdus",
511 vdu_descriptor
["id"], "interfaces",
512 interface_descriptor
["name"], "ip_address"),
513 cp_params
["ip-address"])
514 if cp_params
.get("mac-address"):
515 populate_dict(RO_ns_params
, ("vnfs", cp_params
["member-vnf-index-ref"], "vdus",
516 vdu_descriptor
["id"], "interfaces",
517 interface_descriptor
["name"], "mac_address"),
518 cp_params
["mac-address"])
521 def scale_vnfr(self
, db_vnfr
, vdu_create
=None, vdu_delete
=None):
522 # make a copy to do not change
523 vdu_create
= copy(vdu_create
)
524 vdu_delete
= copy(vdu_delete
)
526 vdurs
= db_vnfr
.get("vdur")
529 vdu_index
= len(vdurs
)
532 vdur
= vdurs
[vdu_index
]
533 if vdur
.get("pdu-type"):
535 vdu_id_ref
= vdur
["vdu-id-ref"]
536 if vdu_create
and vdu_create
.get(vdu_id_ref
):
537 for index
in range(0, vdu_create
[vdu_id_ref
]):
538 vdur
= deepcopy(vdur
)
539 vdur
["_id"] = str(uuid4())
540 vdur
["count-index"] += 1
541 vdurs
.insert(vdu_index
+1+index
, vdur
)
542 del vdu_create
[vdu_id_ref
]
543 if vdu_delete
and vdu_delete
.get(vdu_id_ref
):
545 vdu_delete
[vdu_id_ref
] -= 1
546 if not vdu_delete
[vdu_id_ref
]:
547 del vdu_delete
[vdu_id_ref
]
548 # check all operations are done
549 if vdu_create
or vdu_delete
:
550 raise LcmException("Error scaling OUT VNFR for {}. There is not any existing vnfr. Scaled to 0?".format(
553 raise LcmException("Error scaling IN VNFR for {}. There is not any existing vnfr. Scaled to 0?".format(
556 vnfr_update
= {"vdur": vdurs
}
557 db_vnfr
["vdur"] = vdurs
558 self
.update_db_2("vnfrs", db_vnfr
["_id"], vnfr_update
)
560 def ns_update_nsr(self
, ns_update_nsr
, db_nsr
, nsr_desc_RO
):
562 Updates database nsr with the RO info for the created vld
563 :param ns_update_nsr: dictionary to be filled with the updated info
564 :param db_nsr: content of db_nsr. This is also modified
565 :param nsr_desc_RO: nsr descriptor from RO
566 :return: Nothing, LcmException is raised on errors
569 for vld_index
, vld
in enumerate(get_iterable(db_nsr
, "vld")):
570 for net_RO
in get_iterable(nsr_desc_RO
, "nets"):
571 if vld
["id"] != net_RO
.get("ns_net_osm_id"):
573 vld
["vim-id"] = net_RO
.get("vim_net_id")
574 vld
["name"] = net_RO
.get("vim_name")
575 vld
["status"] = net_RO
.get("status")
576 vld
["status-detailed"] = net_RO
.get("error_msg")
577 ns_update_nsr
["vld.{}".format(vld_index
)] = vld
580 raise LcmException("ns_update_nsr: Not found vld={} at RO info".format(vld
["id"]))
582 def ns_update_vnfr(self
, db_vnfrs
, nsr_desc_RO
):
584 Updates database vnfr with the RO info, e.g. ip_address, vim_id... Descriptor db_vnfrs is also updated
585 :param db_vnfrs: dictionary with member-vnf-index: vnfr-content
586 :param nsr_desc_RO: nsr descriptor from RO
587 :return: Nothing, LcmException is raised on errors
589 for vnf_index
, db_vnfr
in db_vnfrs
.items():
590 for vnf_RO
in nsr_desc_RO
["vnfs"]:
591 if vnf_RO
["member_vnf_index"] != vnf_index
:
594 if vnf_RO
.get("ip_address"):
595 db_vnfr
["ip-address"] = vnfr_update
["ip-address"] = vnf_RO
["ip_address"].split(";")[0]
596 elif not db_vnfr
.get("ip-address"):
597 raise LcmExceptionNoMgmtIP("ns member_vnf_index '{}' has no IP address".format(vnf_index
))
599 for vdu_index
, vdur
in enumerate(get_iterable(db_vnfr
, "vdur")):
600 vdur_RO_count_index
= 0
601 if vdur
.get("pdu-type"):
603 for vdur_RO
in get_iterable(vnf_RO
, "vms"):
604 if vdur
["vdu-id-ref"] != vdur_RO
["vdu_osm_id"]:
606 if vdur
["count-index"] != vdur_RO_count_index
:
607 vdur_RO_count_index
+= 1
609 vdur
["vim-id"] = vdur_RO
.get("vim_vm_id")
610 if vdur_RO
.get("ip_address"):
611 vdur
["ip-address"] = vdur_RO
["ip_address"].split(";")[0]
613 vdur
["ip-address"] = None
614 vdur
["vdu-id-ref"] = vdur_RO
.get("vdu_osm_id")
615 vdur
["name"] = vdur_RO
.get("vim_name")
616 vdur
["status"] = vdur_RO
.get("status")
617 vdur
["status-detailed"] = vdur_RO
.get("error_msg")
618 for ifacer
in get_iterable(vdur
, "interfaces"):
619 for interface_RO
in get_iterable(vdur_RO
, "interfaces"):
620 if ifacer
["name"] == interface_RO
.get("internal_name"):
621 ifacer
["ip-address"] = interface_RO
.get("ip_address")
622 ifacer
["mac-address"] = interface_RO
.get("mac_address")
625 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vdur={} interface={} "
626 "from VIM info".format(vnf_index
, vdur
["vdu-id-ref"],
628 vnfr_update
["vdur.{}".format(vdu_index
)] = vdur
631 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vdur={} count_index={} from "
632 "VIM info".format(vnf_index
, vdur
["vdu-id-ref"], vdur
["count-index"]))
634 for vld_index
, vld
in enumerate(get_iterable(db_vnfr
, "vld")):
635 for net_RO
in get_iterable(nsr_desc_RO
, "nets"):
636 if vld
["id"] != net_RO
.get("vnf_net_osm_id"):
638 vld
["vim-id"] = net_RO
.get("vim_net_id")
639 vld
["name"] = net_RO
.get("vim_name")
640 vld
["status"] = net_RO
.get("status")
641 vld
["status-detailed"] = net_RO
.get("error_msg")
642 vnfr_update
["vld.{}".format(vld_index
)] = vld
645 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} vld={} from VIM info".format(
646 vnf_index
, vld
["id"]))
648 self
.update_db_2("vnfrs", db_vnfr
["_id"], vnfr_update
)
652 raise LcmException("ns_update_vnfr: Not found member_vnf_index={} from VIM info".format(vnf_index
))
654 async def instantiate_RO(self
, logging_text
, nsr_id
, nsd
, db_nsr
, db_nslcmop
, db_vnfrs
, db_vnfds_ref
,
657 RO_descriptor_number
= 0 # number of descriptors created at RO
658 vnf_index_2_RO_id
= {} # map between vnfd/nsd id to the id used at RO
659 start_deploy
= time()
660 ns_params
= db_nslcmop
.get("operationParams")
663 # get vnfds, instantiate at RO
664 for c_vnf
in nsd
.get("constituent-vnfd", ()):
665 member_vnf_index
= c_vnf
["member-vnf-index"]
666 vnfd
= db_vnfds_ref
[c_vnf
['vnfd-id-ref']]
667 vnfd_ref
= vnfd
["id"]
668 step
= db_nsr_update
["detailed-status"] = "Creating vnfd='{}' member_vnf_index='{}' at RO".format(
669 vnfd_ref
, member_vnf_index
)
670 # self.logger.debug(logging_text + step)
671 vnfd_id_RO
= "{}.{}.{}".format(nsr_id
, RO_descriptor_number
, member_vnf_index
[:23])
672 vnf_index_2_RO_id
[member_vnf_index
] = vnfd_id_RO
673 RO_descriptor_number
+= 1
675 # look position at deployed.RO.vnfd if not present it will be appended at the end
676 for index
, vnf_deployed
in enumerate(db_nsr
["_admin"]["deployed"]["RO"]["vnfd"]):
677 if vnf_deployed
["member-vnf-index"] == member_vnf_index
:
680 index
= len(db_nsr
["_admin"]["deployed"]["RO"]["vnfd"])
681 db_nsr
["_admin"]["deployed"]["RO"]["vnfd"].append(None)
684 RO_update
= {"member-vnf-index": member_vnf_index
}
685 vnfd_list
= await self
.RO
.get_list("vnfd", filter_by
={"osm_id": vnfd_id_RO
})
687 RO_update
["id"] = vnfd_list
[0]["uuid"]
688 self
.logger
.debug(logging_text
+ "vnfd='{}' member_vnf_index='{}' exists at RO. Using RO_id={}".
689 format(vnfd_ref
, member_vnf_index
, vnfd_list
[0]["uuid"]))
691 vnfd_RO
= self
.vnfd2RO(vnfd
, vnfd_id_RO
, db_vnfrs
[c_vnf
["member-vnf-index"]].
692 get("additionalParamsForVnf"), nsr_id
)
693 desc
= await self
.RO
.create("vnfd", descriptor
=vnfd_RO
)
694 RO_update
["id"] = desc
["uuid"]
695 self
.logger
.debug(logging_text
+ "vnfd='{}' member_vnf_index='{}' created at RO. RO_id={}".format(
696 vnfd_ref
, member_vnf_index
, desc
["uuid"]))
697 db_nsr_update
["_admin.deployed.RO.vnfd.{}".format(index
)] = RO_update
698 db_nsr
["_admin"]["deployed"]["RO"]["vnfd"][index
] = RO_update
699 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
703 step
= db_nsr_update
["detailed-status"] = "Creating nsd={} at RO".format(nsd_ref
)
704 # self.logger.debug(logging_text + step)
706 RO_osm_nsd_id
= "{}.{}.{}".format(nsr_id
, RO_descriptor_number
, nsd_ref
[:23])
707 RO_descriptor_number
+= 1
708 nsd_list
= await self
.RO
.get_list("nsd", filter_by
={"osm_id": RO_osm_nsd_id
})
710 db_nsr_update
["_admin.deployed.RO.nsd_id"] = RO_nsd_uuid
= nsd_list
[0]["uuid"]
711 self
.logger
.debug(logging_text
+ "nsd={} exists at RO. Using RO_id={}".format(
712 nsd_ref
, RO_nsd_uuid
))
714 nsd_RO
= deepcopy(nsd
)
715 nsd_RO
["id"] = RO_osm_nsd_id
716 nsd_RO
.pop("_id", None)
717 nsd_RO
.pop("_admin", None)
718 for c_vnf
in nsd_RO
.get("constituent-vnfd", ()):
719 member_vnf_index
= c_vnf
["member-vnf-index"]
720 c_vnf
["vnfd-id-ref"] = vnf_index_2_RO_id
[member_vnf_index
]
721 for c_vld
in nsd_RO
.get("vld", ()):
722 for cp
in c_vld
.get("vnfd-connection-point-ref", ()):
723 member_vnf_index
= cp
["member-vnf-index-ref"]
724 cp
["vnfd-id-ref"] = vnf_index_2_RO_id
[member_vnf_index
]
726 desc
= await self
.RO
.create("nsd", descriptor
=nsd_RO
)
727 db_nsr_update
["_admin.nsState"] = "INSTANTIATED"
728 db_nsr_update
["_admin.deployed.RO.nsd_id"] = RO_nsd_uuid
= desc
["uuid"]
729 self
.logger
.debug(logging_text
+ "nsd={} created at RO. RO_id={}".format(nsd_ref
, RO_nsd_uuid
))
730 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
733 # if present use it unless in error status
734 RO_nsr_id
= deep_get(db_nsr
, ("_admin", "deployed", "RO", "nsr_id"))
737 step
= db_nsr_update
["detailed-status"] = "Looking for existing ns at RO"
738 # self.logger.debug(logging_text + step + " RO_ns_id={}".format(RO_nsr_id))
739 desc
= await self
.RO
.show("ns", RO_nsr_id
)
740 except ROclient
.ROClientException
as e
:
741 if e
.http_code
!= HTTPStatus
.NOT_FOUND
:
743 RO_nsr_id
= db_nsr_update
["_admin.deployed.RO.nsr_id"] = None
745 ns_status
, ns_status_info
= self
.RO
.check_ns_status(desc
)
746 db_nsr_update
["_admin.deployed.RO.nsr_status"] = ns_status
747 if ns_status
== "ERROR":
748 step
= db_nsr_update
["detailed-status"] = "Deleting ns at RO. RO_ns_id={}".format(RO_nsr_id
)
749 self
.logger
.debug(logging_text
+ step
)
750 await self
.RO
.delete("ns", RO_nsr_id
)
751 RO_nsr_id
= db_nsr_update
["_admin.deployed.RO.nsr_id"] = None
753 step
= db_nsr_update
["detailed-status"] = "Checking dependencies"
754 # self.logger.debug(logging_text + step)
756 # check if VIM is creating and wait look if previous tasks in process
757 task_name
, task_dependency
= self
.lcm_tasks
.lookfor_related("vim_account", ns_params
["vimAccountId"])
759 step
= "Waiting for related tasks to be completed: {}".format(task_name
)
760 self
.logger
.debug(logging_text
+ step
)
761 await asyncio
.wait(task_dependency
, timeout
=3600)
762 if ns_params
.get("vnf"):
763 for vnf
in ns_params
["vnf"]:
764 if "vimAccountId" in vnf
:
765 task_name
, task_dependency
= self
.lcm_tasks
.lookfor_related("vim_account",
768 step
= "Waiting for related tasks to be completed: {}".format(task_name
)
769 self
.logger
.debug(logging_text
+ step
)
770 await asyncio
.wait(task_dependency
, timeout
=3600)
772 step
= db_nsr_update
["detailed-status"] = "Checking instantiation parameters"
774 RO_ns_params
= self
.ns_params_2_RO(ns_params
, nsd
, db_vnfds_ref
, n2vc_key_list
)
776 step
= db_nsr_update
["detailed-status"] = "Deploying ns at VIM"
777 desc
= await self
.RO
.create("ns", descriptor
=RO_ns_params
,
779 scenario
=RO_nsd_uuid
)
780 RO_nsr_id
= db_nsr_update
["_admin.deployed.RO.nsr_id"] = desc
["uuid"]
781 db_nsr_update
["_admin.nsState"] = "INSTANTIATED"
782 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "BUILD"
783 self
.logger
.debug(logging_text
+ "ns created at RO. RO_id={}".format(desc
["uuid"]))
784 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
786 # wait until NS is ready
787 step
= ns_status_detailed
= detailed_status
= "Waiting VIM to deploy ns. RO_ns_id={}".format(RO_nsr_id
)
788 detailed_status_old
= None
789 self
.logger
.debug(logging_text
+ step
)
791 while time() <= start_deploy
+ self
.total_deploy_timeout
:
792 desc
= await self
.RO
.show("ns", RO_nsr_id
)
793 ns_status
, ns_status_info
= self
.RO
.check_ns_status(desc
)
794 db_nsr_update
["_admin.deployed.RO.nsr_status"] = ns_status
795 if ns_status
== "ERROR":
796 raise ROclient
.ROClientException(ns_status_info
)
797 elif ns_status
== "BUILD":
798 detailed_status
= ns_status_detailed
+ "; {}".format(ns_status_info
)
799 elif ns_status
== "ACTIVE":
800 step
= detailed_status
= "Waiting for management IP address reported by the VIM. Updating VNFRs"
802 self
.ns_update_vnfr(db_vnfrs
, desc
)
804 except LcmExceptionNoMgmtIP
:
807 assert False, "ROclient.check_ns_status returns unknown {}".format(ns_status
)
808 if detailed_status
!= detailed_status_old
:
809 detailed_status_old
= db_nsr_update
["detailed-status"] = detailed_status
810 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
811 await asyncio
.sleep(5, loop
=self
.loop
)
812 else: # total_deploy_timeout
813 raise ROclient
.ROClientException("Timeout waiting ns to be ready")
815 step
= "Updating NSR"
816 self
.ns_update_nsr(db_nsr_update
, db_nsr
, desc
)
818 db_nsr_update
["operational-status"] = "running"
819 db_nsr
["detailed-status"] = "Configuring vnfr"
820 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
822 async def insert_key_ro(self
, logging_text
, pub_key
, nsr_id
, vnfr_id
, vdu_id
, vdu_index
, user
):
828 await asyncio
.sleep(10, loop
=self
.loop
)
829 # 1 wait until NS is deployed at RO
831 db_nsrs
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
832 ro_nsr_id
= deep_get(db_nsrs
, ("_admin", "deployed", "RO", "nsr_id"))
835 if not target_vdu_id
:
836 db_vnfr
= self
.db
.get_one("vnfrs", {"_id": vnfr_id
})
838 ip_address
= db_vnfr
.get("ip-address")
841 for vdur
in get_iterable(db_vnfr
, "vdur"):
842 if (vdur
["vdu-id-ref"] == vdu_id
and vdur
["count-index"] == vdu_index
) or \
843 (ip_address
and vdur
.get("ip-address") == ip_address
):
844 if vdur
["status"] == "ACTIVE":
845 target_vdu_id
= vdur
["vdu-id-ref"]
846 elif vdur
["status"] == "ERROR":
847 raise LcmException("Cannot inject ssh-key because target VM is in error state")
850 raise LcmException("Not found vnfr_id={}, vdu_index={}, vdu_index={}".format(
851 vnfr_id
, vdu_id
, vdu_index
853 if not target_vdu_id
:
856 ro_vm_id
= "{}-{}".format(db_vnfr
["member-vnf-index-ref"], target_vdu_id
) # TODO add vdu_index
857 result_dict
= await self
.RO
.create_action("ns", ro_nsr_id
,
858 {"add_public_key": pub_key
, "vms": [ro_vm_id
], "user": user
})
859 # result_dict contains the format {VM-id: {vim_result: 200, description: text}}
860 if not result_dict
or not isinstance(result_dict
, dict):
861 raise LcmException("Unknown response from RO when injecting key")
862 for result
in result_dict
.values():
863 if result
.get("vim_result") == 200:
866 raise ROclient
.ROClientException("error injecting key: {}".format(
867 result
.get("description")))
869 except ROclient
.ROClientException
as e
:
872 raise LcmException("Reaching max tries injecting key. Error: {}".format(e
))
873 self
.logger
.debug(logging_text
+ "error injecting key: {}".format(e
))
875 async def instantiate_N2VC(self
, logging_text
, vca_index
, nsi_id
, db_nsr
, db_vnfr
, vdu_id
, vdu_index
,
876 deploy_params
, config_descriptor
, base_folder
):
877 nsr_id
= db_nsr
["_id"]
878 db_update_entry
= "_admin.deployed.VCA.{}.".format(vca_index
)
879 vca_deployed
= db_nsr
["_admin"]["deployed"]["VCA"][vca_index
]
880 logging_text
+= "member_vnf_index={} vdu_id={}, vdu_index={} ".format(db_vnfr
["member-vnf-index-ref"],
886 vnfr_id
= db_vnfr
["_id"]
888 namespace
= "{nsi}.{ns}".format(
889 nsi
=nsi_id
if nsi_id
else "",
892 namespace
+= "." + vnfr_id
894 namespace
+= ".{}-{}".format(vdu_id
, vdu_index
or 0)
897 storage_params
= self
.fs
.get_params()
898 artifact_path
= "{}{}/{}/charms/{}".format(
899 storage_params
["path"],
900 base_folder
["folder"],
901 base_folder
["pkg-dir"],
902 config_descriptor
["juju"]["charm"]
905 is_proxy_charm
= True if config_descriptor
["juju"]["charm"] else False
906 if config_descriptor
["juju"].get("proxy") is False:
907 is_proxy_charm
= False
909 # n2vc_redesign STEP 3.1
910 ee_id
= vca_deployed
.get("ee_id")
913 step
= "create execution envioronment"
914 self
.logger
.debug(logging_text
+ step
)
915 ee_id
= await self
.n2vc
.CreateExecutionEnvironment(namespace
)
918 step
= "register execution envioronment"
919 # TODO wait until deployed by RO, when IP address has been filled. By pooling????
920 credentials
= {} # TODO db_credentials["ip_address"]
922 # TODO remove this when changes on IM regarding config-access:ssh-access:default-user were
923 # merged. Meanwhile let's get username from initial-config-primitive
924 if config_descriptor
.get("initial-config-primitive"):
925 for param
in config_descriptor
["initial-config-primitive"][0].get("parameter", ()):
926 if param
["name"] == "ssh-username":
927 credentials
["username"] = param
["value"]
928 if config_descriptor
.get("config-access") and config_descriptor
["config-access"].get("ssh-access"):
929 if config_descriptor
["config-access"]["ssh-access"].get("required"):
930 credentials
["username"] = \
931 config_descriptor
["config-access"]["ssh-access"].get("default-user")
933 # n2vc_redesign STEP 3.2
934 self
.logger
.debug(logging_text
+ step
)
935 ee_id
= await self
.n2vc
.RegisterExecutionEnvioronment(credentials
)
936 # for compatibility with MON/POL modules, the need model and application name at database
937 # TODO ask to N2VC instead of assuming the format "model_name.application_name"
938 model_name
, _
, application_name
= ee_id
.partition(".")
939 self
.update_db_2("nsrs", nsr_id
, {db_update_entry
+ "model": model_name
,
940 db_update_entry
+ "application": application_name
,
941 db_update_entry
+ "ee_id": ee_id
})
943 # n2vc_redesign STEP 3.3
944 # TODO check if already done
945 step
= "Install configuration Software"
946 # TODO db_dict with filter: 'nsr_id', path:'db_update_entry'
947 pub_key
= await self
.n2vc
.InstallConfigurationSW(self
, ee_id
, artifact_path
)
948 if is_proxy_charm
and pub_key
:
949 # check if ssh key must be injected
950 required
= deep_get(config_descriptor
, ("config-access", "ssh-access", "required"))
951 # TODO get from config primitive: if required not user:
953 user
= deep_get(config_descriptor
, ("config-access", "ssh-access", "default-user"))
954 # insert pub_key into VM
955 # n2vc_redesign STEP 5.1
956 step
= "Insert public key into VM"
957 await self
.insert_key_ro(logging_text
, pub_key
, nsr_id
, vnfr_id
, vdu_id
, vdu_index
, user
)
958 # TODO get ip_address and add to deploy params
959 deploy_params
["rw_mgmt_ip"] = "TODO-ip-address"
961 # n2vc_redesign STEP 6 Execute initial config primitive
962 initial_config_primitive_list
= config_descriptor
.get('initial-config-primitive', [])
963 for initial_config_primitive
in initial_config_primitive_list
:
964 # TODO check if already done
965 primitive_params_
= self
._map
_primitive
_params
(initial_config_primitive
, {}, deploy_params
)
966 step
= "execute primitive '{}' params '{}'".format(initial_config_primitive
["name"], primitive_params_
)
967 self
.logger
.debug(logging_text
+ step
)
968 await self
.n2vc
.ExecutePrimitive(ee_id
, initial_config_primitive
["name"], primitive_params_
)
969 # TODO register in database that primitive is done
970 except Exception as e
: # TODO not use Exception but N2VC exception
971 raise Exception("{} {}".format(step
, e
))
972 # TODO raise N2VC exception with 'step' extra information
974 async def instantiate(self
, nsr_id
, nslcmop_id
):
975 # Try to lock HA task here
976 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA('ns', 'nslcmops', nslcmop_id
)
977 if not task_is_locked_by_me
:
980 logging_text
= "Task ns={} instantiate={} ".format(nsr_id
, nslcmop_id
)
981 self
.logger
.debug(logging_text
+ "Enter")
982 # get all needed from database
983 # start_deploy = time()
986 db_nsr_update
= {"_admin.nslcmop": nslcmop_id
}
987 db_nslcmop_update
= {}
988 nslcmop_operation_state
= None
990 task_instantiation_list
= []
993 # wait for any previous tasks in process
994 step
= "Waiting for previous tasks"
995 await self
.lcm_tasks
.waitfor_related_HA('ns', 'nslcmops', nslcmop_id
)
996 # STEP 0: Reding database
997 step
= "Getting nslcmop={} from db".format(nslcmop_id
)
998 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
999 step
= "Getting nsr={} from db".format(nsr_id
)
1000 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
1002 # nsr_name = db_nsr["name"] # TODO short-name??
1004 step
= "Getting vnfrs from db"
1005 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
1009 for vnfr
in db_vnfrs_list
:
1010 db_vnfrs
[vnfr
["member-vnf-index-ref"]] = vnfr
1011 vnfd_id
= vnfr
["vnfd-id"]
1012 vnfd_ref
= vnfr
["vnfd-ref"]
1013 if vnfd_id
not in db_vnfds
:
1014 step
= "Getting vnfd={} id='{}' from db".format(vnfd_id
, vnfd_ref
)
1015 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
1016 db_vnfds_ref
[vnfd_ref
] = vnfd
1017 db_vnfds
[vnfd_id
] = vnfd
1018 db_vnfds_index
[vnfr
["member-vnf-index-ref"]] = db_vnfds
[vnfd_id
]
1020 # Get or generates the _admin.deployed,VCA list
1021 vca_deployed_list
= None
1022 # vca_model_name = None
1023 if db_nsr
["_admin"].get("deployed"):
1024 vca_deployed_list
= db_nsr
["_admin"]["deployed"].get("VCA")
1025 # vca_model_name = db_nsr["_admin"]["deployed"].get("VCA-model-name")
1026 if vca_deployed_list
is None:
1027 vca_deployed_list
= []
1028 db_nsr_update
["_admin.deployed.VCA"] = vca_deployed_list
1029 populate_dict(db_nsr
, ("_admin", "deployed", "VCA"), vca_deployed_list
)
1030 elif isinstance(vca_deployed_list
, dict):
1031 # maintain backward compatibility. Change a dict to list at database
1032 vca_deployed_list
= list(vca_deployed_list
.values())
1033 db_nsr_update
["_admin.deployed.VCA"] = vca_deployed_list
1034 populate_dict(db_nsr
, ("_admin", "deployed", "VCA"), vca_deployed_list
)
1036 db_nsr_update
["detailed-status"] = "creating"
1037 db_nsr_update
["operational-status"] = "init"
1038 if not isinstance(deep_get(db_nsr
, ("_admin", "deployed", "RO", "vnfd")), list):
1039 populate_dict(db_nsr
, ("_admin", "deployed", "RO", "vnfd"), [])
1040 db_nsr_update
["_admin.deployed.RO.vnfd"] = []
1042 # set state to INSTANTIATED. When instantiated NBI will not delete directly
1043 db_nsr_update
["_admin.nsState"] = "INSTANTIATED"
1044 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1046 # n2vc_redesign STEP 1 Get VCA public ssh-key
1047 # feature 1429. Add n2vc public key to needed VMs
1048 n2vc_key
= await self
.n2vc
.GetPublicKey()
1050 # n2vc_redesign STEP 2 Deploy Network Scenario
1051 task_ro
= asyncio
.ensure_future(
1052 self
.instantiate_RO(logging_text
, nsr_id
, nsd
, db_nsr
, db_nslcmop
, db_vnfrs
, db_vnfds_ref
, [n2vc_key
])
1054 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "instantiate_RO", task_ro
)
1055 task_instantiation_list
.append(task_ro
)
1057 # n2vc_redesign STEP 3 to 6 Deploy N2VC
1058 step
= "Looking for needed vnfd to configure with proxy charm"
1059 self
.logger
.debug(logging_text
+ step
)
1062 # launch instantiate_N2VC in a asyncio task and register task object
1063 # Look where information of this charm is at database <nsrs>._admin.deployed.VCA
1064 # if not found, create one entry and update database
1065 # read variables logging_text, nsi_id, db_nsr, db_vnfr, vdu_id, vdu_index, deploy_params,
1066 # descriptor_config, base_folder, member_vnf_index, vnfd_id
1067 # write variables: db_nsr
1069 # fill db_nsr._admin.deployed.VCA.<index>
1071 for vca_index
, vca_deployed
in enumerate(db_nsr
["_admin"]["deployed"]["VCA"]):
1072 if not vca_deployed
:
1074 if vca_deployed
.get("member-vnf-index") == member_vnf_index
and \
1075 vca_deployed
.get("vdu_id") == vdu_id
and \
1076 vca_deployed
.get("vdu_count_index", 0) == vdu_index
:
1079 # not found, create one.
1081 "member-vnf-index": member_vnf_index
,
1083 "vdu_count_index": vdu_index
,
1084 "operational-status": "init", # TODO revise
1085 "detailed-status": "", # TODO revise
1086 "step": "initial-deploy", # TODO revise
1088 "vdu_name": vdu_name
,
1091 self
.update_db_2("nsrs", nsr_id
, {"_admin.deployed.VCA.{}".format(vca_index
): vca_deployed
})
1092 db_nsr
["_admin"]["deployed"]["VCA"].append(vca_deployed
)
1095 task_n2vc
= asyncio
.ensure_future(
1096 self
.instantiate_N2VC(logging_text
,
1108 self
.lcm_tasks
.register("ns", nsr_id
, nslcmop_id
, "instantiate_N2VC-{}".format(vca_index
), task_n2vc
)
1109 task_instantiation_list
.append(task_n2vc
)
1111 nsi_id
= None # TODO put nsi_id when this nsr belongs to a NSI
1112 for c_vnf
in get_iterable(nsd
, "constituent-vnfd"):
1113 vnfd_id
= c_vnf
["vnfd-id-ref"]
1114 member_vnf_index
= str(c_vnf
["member-vnf-index"])
1115 db_vnfr
= db_vnfrs
[member_vnf_index
]
1116 base_folder
= vnfd
["_admin"]["storage"]
1120 vnfd
= db_vnfds_ref
[vnfd_id
]
1122 # Get additional parameters
1124 if db_vnfr
.get("additionalParamsForVnf"):
1125 deploy_params
= db_vnfr
["additionalParamsForVnf"].copy()
1126 for k
, v
in deploy_params
.items():
1127 if isinstance(v
, str) and v
.startswith("!!yaml "):
1128 deploy_params
[k
] = yaml
.safe_load(v
[7:])
1130 descriptor_config
= vnfd
.get("vnf-configuration")
1131 if descriptor_config
and descriptor_config
.get("juju"):
1134 # Deploy charms for each VDU that supports one.
1135 for vdud
in get_iterable(vnfd
, 'vdu'):
1137 descriptor_config
= vdud
.get('vdu-configuration')
1138 if descriptor_config
and descriptor_config
.get("juju"):
1139 # look for vdu index in the db_vnfr["vdu"] section
1140 # for vdur_index, vdur in enumerate(db_vnfr["vdur"]):
1141 # if vdur["vdu-id-ref"] == vdu_id:
1144 # raise LcmException("Mismatch vdu_id={} not found in the vnfr['vdur'] list for "
1145 # "member_vnf_index={}".format(vdu_id, member_vnf_index))
1146 # vdu_name = vdur.get("name")
1148 for vdu_index
in range(int(vdud
.get("count", 1))):
1149 # TODO vnfr_params["rw_mgmt_ip"] = vdur["ip-address"]
1152 # Check if this NS has a charm configuration
1153 descriptor_config
= nsd
.get("ns-configuration")
1154 if descriptor_config
and descriptor_config
.get("juju"):
1157 member_vnf_index
= None
1161 # Get additional parameters
1163 if db_nsr
.get("additionalParamsForNs"):
1164 deploy_params
= db_nsr
["additionalParamsForNs"].copy()
1165 for k
, v
in deploy_params
.items():
1166 if isinstance(v
, str) and v
.startswith("!!yaml "):
1167 deploy_params
[k
] = yaml
.safe_load(v
[7:])
1168 base_folder
= nsd
["_admin"]["storage"]
1171 # Wait until all tasks of "task_instantiation_list" have been finished
1173 # while time() <= start_deploy + self.total_deploy_timeout:
1175 timeout
= 3600 # time() - start_deploy
1176 task_instantiation_set
= set(task_instantiation_list
)
1177 done
, pending
= await asyncio
.wait(task_instantiation_set
, timeout
=timeout
)
1178 # TODO return_when=asyncio.FIRST_COMPLETED)
1180 error_text
= "timeout"
1182 if task
.cancelled():
1184 error_text
= "cancelled"
1186 exc
= task
.exception()
1188 error_text
= str(exc
)
1191 db_nsr_update
["config-status"] = "failed"
1192 error_text
= "fail configuring " + error_text
1193 db_nsr_update
["detailed-status"] = error_text
1194 db_nslcmop_update
["operationState"] = nslcmop_operation_state
= "FAILED_TEMP"
1195 db_nslcmop_update
["detailed-status"] = error_text
1196 db_nslcmop_update
["statusEnteredTime"] = time()
1199 db_nslcmop_update
["operationState"] = nslcmop_operation_state
= "COMPLETED"
1200 db_nslcmop_update
["statusEnteredTime"] = time()
1201 db_nslcmop_update
["detailed-status"] = "done"
1202 db_nsr_update
["config-status"] = "configured"
1203 db_nsr_update
["detailed-status"] = "done"
1207 except (ROclient
.ROClientException
, DbException
, LcmException
) as e
:
1208 self
.logger
.error(logging_text
+ "Exit Exception while '{}': {}".format(step
, e
))
1210 except asyncio
.CancelledError
:
1211 self
.logger
.error(logging_text
+ "Cancelled Exception while '{}'".format(step
))
1212 exc
= "Operation was cancelled"
1213 except Exception as e
:
1214 exc
= traceback
.format_exc()
1215 self
.logger
.critical(logging_text
+ "Exit Exception {} while '{}': {}".format(type(e
).__name
__, step
, e
),
1220 db_nsr_update
["detailed-status"] = "ERROR {}: {}".format(step
, exc
)
1221 db_nsr_update
["operational-status"] = "failed"
1223 db_nslcmop_update
["detailed-status"] = "FAILED {}: {}".format(step
, exc
)
1224 db_nslcmop_update
["operationState"] = nslcmop_operation_state
= "FAILED"
1225 db_nslcmop_update
["statusEnteredTime"] = time()
1228 db_nsr_update
["_admin.nslcmop"] = None
1229 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1230 if db_nslcmop_update
:
1231 self
.update_db_2("nslcmops", nslcmop_id
, db_nslcmop_update
)
1232 except DbException
as e
:
1233 self
.logger
.error(logging_text
+ "Cannot update database: {}".format(e
))
1234 if nslcmop_operation_state
:
1236 await self
.msg
.aiowrite("ns", "instantiated", {"nsr_id": nsr_id
, "nslcmop_id": nslcmop_id
,
1237 "operationState": nslcmop_operation_state
},
1239 except Exception as e
:
1240 self
.logger
.error(logging_text
+ "kafka_write notification Exception {}".format(e
))
1242 self
.logger
.debug(logging_text
+ "Exit")
1243 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_instantiate")
1245 async def _destroy_charm(self
, model
, application
):
1247 Order N2VC destroy a charm
1250 :return: True if charm does not exist. False if it exist
1252 if not await self
.n2vc
.HasApplication(model
, application
):
1253 return True # Already removed
1254 await self
.n2vc
.RemoveCharms(model
, application
)
1257 async def _wait_charm_destroyed(self
, model
, application
, timeout
):
1259 Wait until charm does not exist
1263 :return: True if not exist, False if timeout
1266 if not await self
.n2vc
.HasApplication(model
, application
):
1270 await asyncio
.sleep(10, loop
=self
.loop
)
1273 # Check if this VNFD has a configured terminate action
1274 def _has_terminate_config_primitive(self
, vnfd
):
1275 vnf_config
= vnfd
.get("vnf-configuration")
1276 if vnf_config
and vnf_config
.get("terminate-config-primitive"):
1282 def _get_terminate_config_primitive_seq_list(vnfd
):
1283 """ Get a numerically sorted list of the sequences for this VNFD's terminate action """
1284 # No need to check for existing primitive twice, already done before
1285 vnf_config
= vnfd
.get("vnf-configuration")
1286 seq_list
= vnf_config
.get("terminate-config-primitive")
1287 # Get all 'seq' tags in seq_list, order sequences numerically, ascending.
1288 seq_list_sorted
= sorted(seq_list
, key
=lambda x
: int(x
['seq']))
1289 return seq_list_sorted
1292 def _create_nslcmop(nsr_id
, operation
, params
):
1294 Creates a ns-lcm-opp content to be stored at database.
1295 :param nsr_id: internal id of the instance
1296 :param operation: instantiate, terminate, scale, action, ...
1297 :param params: user parameters for the operation
1298 :return: dictionary following SOL005 format
1300 # Raise exception if invalid arguments
1301 if not (nsr_id
and operation
and params
):
1303 "Parameters 'nsr_id', 'operation' and 'params' needed to create primitive not provided")
1309 # COMPLETED,PARTIALLY_COMPLETED,FAILED_TEMP,FAILED,ROLLING_BACK,ROLLED_BACK
1310 "operationState": "PROCESSING",
1311 "statusEnteredTime": now
,
1312 "nsInstanceId": nsr_id
,
1313 "lcmOperationType": operation
,
1315 "isAutomaticInvocation": False,
1316 "operationParams": params
,
1317 "isCancelPending": False,
1319 "self": "/osm/nslcm/v1/ns_lcm_op_occs/" + _id
,
1320 "nsInstance": "/osm/nslcm/v1/ns_instances/" + nsr_id
,
1325 def _get_terminate_primitive_params(self
, seq
, vnf_index
):
1326 primitive
= seq
.get('name')
1327 primitive_params
= {}
1329 "member_vnf_index": vnf_index
,
1330 "primitive": primitive
,
1331 "primitive_params": primitive_params
,
1334 return self
._map
_primitive
_params
(seq
, params
, desc_params
)
1338 def _reintent_or_skip_suboperation(self
, db_nslcmop
, op_index
):
1339 op
= db_nslcmop
.get('_admin', {}).get('operations', [])[op_index
]
1340 if (op
.get('operationState') == 'COMPLETED'):
1341 # b. Skip sub-operation
1342 # _ns_execute_primitive() or RO.create_action() will NOT be executed
1343 return self
.SUBOPERATION_STATUS_SKIP
1345 # c. Reintent executing sub-operation
1346 # The sub-operation exists, and operationState != 'COMPLETED'
1347 # Update operationState = 'PROCESSING' to indicate a reintent.
1348 operationState
= 'PROCESSING'
1349 detailed_status
= 'In progress'
1350 self
._update
_suboperation
_status
(
1351 db_nslcmop
, op_index
, operationState
, detailed_status
)
1352 # Return the sub-operation index
1353 # _ns_execute_primitive() or RO.create_action() will be called from scale()
1354 # with arguments extracted from the sub-operation
1357 # Find a sub-operation where all keys in a matching dictionary must match
1358 # Returns the index of the matching sub-operation, or SUBOPERATION_STATUS_NOT_FOUND if no match
1359 def _find_suboperation(self
, db_nslcmop
, match
):
1360 if (db_nslcmop
and match
):
1361 op_list
= db_nslcmop
.get('_admin', {}).get('operations', [])
1362 for i
, op
in enumerate(op_list
):
1363 if all(op
.get(k
) == match
[k
] for k
in match
):
1365 return self
.SUBOPERATION_STATUS_NOT_FOUND
1367 # Update status for a sub-operation given its index
1368 def _update_suboperation_status(self
, db_nslcmop
, op_index
, operationState
, detailed_status
):
1369 # Update DB for HA tasks
1370 q_filter
= {'_id': db_nslcmop
['_id']}
1371 update_dict
= {'_admin.operations.{}.operationState'.format(op_index
): operationState
,
1372 '_admin.operations.{}.detailed-status'.format(op_index
): detailed_status
}
1373 self
.db
.set_one("nslcmops",
1375 update_dict
=update_dict
,
1376 fail_on_empty
=False)
1378 # Add sub-operation, return the index of the added sub-operation
1379 # Optionally, set operationState, detailed-status, and operationType
1380 # Status and type are currently set for 'scale' sub-operations:
1381 # 'operationState' : 'PROCESSING' | 'COMPLETED' | 'FAILED'
1382 # 'detailed-status' : status message
1383 # 'operationType': may be any type, in the case of scaling: 'PRE-SCALE' | 'POST-SCALE'
1384 # Status and operation type are currently only used for 'scale', but NOT for 'terminate' sub-operations.
1385 def _add_suboperation(self
, db_nslcmop
, vnf_index
, vdu_id
, vdu_count_index
,
1386 vdu_name
, primitive
, mapped_primitive_params
,
1387 operationState
=None, detailed_status
=None, operationType
=None,
1388 RO_nsr_id
=None, RO_scaling_info
=None):
1389 if not (db_nslcmop
):
1390 return self
.SUBOPERATION_STATUS_NOT_FOUND
1391 # Get the "_admin.operations" list, if it exists
1392 db_nslcmop_admin
= db_nslcmop
.get('_admin', {})
1393 op_list
= db_nslcmop_admin
.get('operations')
1394 # Create or append to the "_admin.operations" list
1395 new_op
= {'member_vnf_index': vnf_index
,
1397 'vdu_count_index': vdu_count_index
,
1398 'primitive': primitive
,
1399 'primitive_params': mapped_primitive_params
}
1401 new_op
['operationState'] = operationState
1403 new_op
['detailed-status'] = detailed_status
1405 new_op
['lcmOperationType'] = operationType
1407 new_op
['RO_nsr_id'] = RO_nsr_id
1409 new_op
['RO_scaling_info'] = RO_scaling_info
1411 # No existing operations, create key 'operations' with current operation as first list element
1412 db_nslcmop_admin
.update({'operations': [new_op
]})
1413 op_list
= db_nslcmop_admin
.get('operations')
1415 # Existing operations, append operation to list
1416 op_list
.append(new_op
)
1418 db_nslcmop_update
= {'_admin.operations': op_list
}
1419 self
.update_db_2("nslcmops", db_nslcmop
['_id'], db_nslcmop_update
)
1420 op_index
= len(op_list
) - 1
1423 # Helper methods for scale() sub-operations
1425 # pre-scale/post-scale:
1426 # Check for 3 different cases:
1427 # a. New: First time execution, return SUBOPERATION_STATUS_NEW
1428 # b. Skip: Existing sub-operation exists, operationState == 'COMPLETED', return SUBOPERATION_STATUS_SKIP
1429 # c. Reintent: Existing sub-operation exists, operationState != 'COMPLETED', return op_index to re-execute
1430 def _check_or_add_scale_suboperation(self
, db_nslcmop
, vnf_index
,
1431 vnf_config_primitive
, primitive_params
, operationType
,
1432 RO_nsr_id
=None, RO_scaling_info
=None):
1433 # Find this sub-operation
1434 if (RO_nsr_id
and RO_scaling_info
):
1435 operationType
= 'SCALE-RO'
1437 'member_vnf_index': vnf_index
,
1438 'RO_nsr_id': RO_nsr_id
,
1439 'RO_scaling_info': RO_scaling_info
,
1443 'member_vnf_index': vnf_index
,
1444 'primitive': vnf_config_primitive
,
1445 'primitive_params': primitive_params
,
1446 'lcmOperationType': operationType
1448 op_index
= self
._find
_suboperation
(db_nslcmop
, match
)
1449 if (op_index
== self
.SUBOPERATION_STATUS_NOT_FOUND
):
1450 # a. New sub-operation
1451 # The sub-operation does not exist, add it.
1452 # _ns_execute_primitive() will be called from scale() as usual, with non-modified arguments
1453 # The following parameters are set to None for all kind of scaling:
1455 vdu_count_index
= None
1457 if (RO_nsr_id
and RO_scaling_info
):
1458 vnf_config_primitive
= None
1459 primitive_params
= None
1462 RO_scaling_info
= None
1463 # Initial status for sub-operation
1464 operationState
= 'PROCESSING'
1465 detailed_status
= 'In progress'
1466 # Add sub-operation for pre/post-scaling (zero or more operations)
1467 self
._add
_suboperation
(db_nslcmop
,
1472 vnf_config_primitive
,
1479 return self
.SUBOPERATION_STATUS_NEW
1481 # Return either SUBOPERATION_STATUS_SKIP (operationState == 'COMPLETED'),
1482 # or op_index (operationState != 'COMPLETED')
1483 return self
._reintent
_or
_skip
_suboperation
(db_nslcmop
, op_index
)
1485 # Helper methods for terminate()
1487 async def _terminate_action(self
, db_nslcmop
, nslcmop_id
, nsr_id
):
1488 """ Create a primitive with params from VNFD
1489 Called from terminate() before deleting instance
1490 Calls action() to execute the primitive """
1491 logging_text
= "Task ns={} _terminate_action={} ".format(nsr_id
, nslcmop_id
)
1492 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
1495 for vnfr
in db_vnfrs_list
:
1496 vnfd_id
= vnfr
["vnfd-id"]
1497 vnf_index
= vnfr
["member-vnf-index-ref"]
1498 if vnfd_id
not in db_vnfds
:
1499 step
= "Getting vnfd={} id='{}' from db".format(vnfd_id
, vnfd_id
)
1500 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
1501 db_vnfds
[vnfd_id
] = vnfd
1502 vnfd
= db_vnfds
[vnfd_id
]
1503 if not self
._has
_terminate
_config
_primitive
(vnfd
):
1505 # Get the primitive's sorted sequence list
1506 seq_list
= self
._get
_terminate
_config
_primitive
_seq
_list
(vnfd
)
1507 for seq
in seq_list
:
1508 # For each sequence in list, get primitive and call _ns_execute_primitive()
1509 step
= "Calling terminate action for vnf_member_index={} primitive={}".format(
1510 vnf_index
, seq
.get("name"))
1511 self
.logger
.debug(logging_text
+ step
)
1512 # Create the primitive for each sequence, i.e. "primitive": "touch"
1513 primitive
= seq
.get('name')
1514 mapped_primitive_params
= self
._get
_terminate
_primitive
_params
(seq
, vnf_index
)
1515 # The following 3 parameters are currently set to None for 'terminate':
1516 # vdu_id, vdu_count_index, vdu_name
1517 vdu_id
= db_nslcmop
["operationParams"].get("vdu_id")
1518 vdu_count_index
= db_nslcmop
["operationParams"].get("vdu_count_index")
1519 vdu_name
= db_nslcmop
["operationParams"].get("vdu_name")
1521 self
._add
_suboperation
(db_nslcmop
,
1528 mapped_primitive_params
)
1529 # Sub-operations: Call _ns_execute_primitive() instead of action()
1530 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
1531 nsr_deployed
= db_nsr
["_admin"]["deployed"]
1532 result
, result_detail
= await self
._ns
_execute
_primitive
(
1533 nsr_deployed
, vnf_index
, vdu_id
, vdu_name
, vdu_count_index
, primitive
,
1534 mapped_primitive_params
)
1536 # nslcmop_operation_state, nslcmop_operation_state_detail = await self.action(
1537 # nsr_id, nslcmop_terminate_action_id)
1538 # Launch Exception if action() returns other than ['COMPLETED', 'PARTIALLY_COMPLETED']
1539 result_ok
= ['COMPLETED', 'PARTIALLY_COMPLETED']
1540 if result
not in result_ok
:
1542 "terminate_primitive_action for vnf_member_index={}",
1543 " primitive={} fails with error {}".format(
1544 vnf_index
, seq
.get("name"), result_detail
))
1546 async def terminate(self
, nsr_id
, nslcmop_id
):
1548 # Try to lock HA task here
1549 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA('ns', 'nslcmops', nslcmop_id
)
1550 if not task_is_locked_by_me
:
1553 logging_text
= "Task ns={} terminate={} ".format(nsr_id
, nslcmop_id
)
1554 self
.logger
.debug(logging_text
+ "Enter")
1558 failed_detail
= [] # annotates all failed error messages
1559 vca_time_destroy
= None # time of where destroy charm order
1560 db_nsr_update
= {"_admin.nslcmop": nslcmop_id
}
1561 db_nslcmop_update
= {}
1562 nslcmop_operation_state
= None
1563 autoremove
= False # autoremove after terminated
1565 # wait for any previous tasks in process
1566 await self
.lcm_tasks
.waitfor_related_HA("ns", 'nslcmops', nslcmop_id
)
1568 step
= "Getting nslcmop={} from db".format(nslcmop_id
)
1569 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
1570 step
= "Getting nsr={} from db".format(nsr_id
)
1571 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
1572 # nsd = db_nsr["nsd"]
1573 nsr_deployed
= deepcopy(db_nsr
["_admin"].get("deployed"))
1574 if db_nsr
["_admin"]["nsState"] == "NOT_INSTANTIATED":
1576 # #TODO check if VIM is creating and wait
1577 # RO_vim_id = db_vim["_admin"]["deployed"]["RO"]
1578 # Call internal terminate action
1579 await self
._terminate
_action
(db_nslcmop
, nslcmop_id
, nsr_id
)
1581 db_nsr_update
["operational-status"] = "terminating"
1582 db_nsr_update
["config-status"] = "terminating"
1584 if nsr_deployed
and nsr_deployed
.get("VCA-model-name"):
1585 vca_model_name
= nsr_deployed
["VCA-model-name"]
1586 step
= "deleting VCA model name '{}' and all charms".format(vca_model_name
)
1587 self
.logger
.debug(logging_text
+ step
)
1589 await self
.n2vc
.DestroyNetworkService(vca_model_name
)
1590 except NetworkServiceDoesNotExist
:
1592 db_nsr_update
["_admin.deployed.VCA-model-name"] = None
1593 if nsr_deployed
.get("VCA"):
1594 for vca_index
in range(0, len(nsr_deployed
["VCA"])):
1595 db_nsr_update
["_admin.deployed.VCA.{}".format(vca_index
)] = None
1596 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1597 # for backward compatibility if charm have been created with "default" model name delete one by one
1598 elif nsr_deployed
and nsr_deployed
.get("VCA"):
1600 step
= "Scheduling configuration charms removing"
1601 db_nsr_update
["detailed-status"] = "Deleting charms"
1602 self
.logger
.debug(logging_text
+ step
)
1603 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1604 # for backward compatibility
1605 if isinstance(nsr_deployed
["VCA"], dict):
1606 nsr_deployed
["VCA"] = list(nsr_deployed
["VCA"].values())
1607 db_nsr_update
["_admin.deployed.VCA"] = nsr_deployed
["VCA"]
1608 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1610 for vca_index
, vca_deployed
in enumerate(nsr_deployed
["VCA"]):
1612 if await self
._destroy
_charm
(vca_deployed
['model'], vca_deployed
["application"]):
1613 vca_deployed
.clear()
1614 db_nsr
["_admin.deployed.VCA.{}".format(vca_index
)] = None
1616 vca_time_destroy
= time()
1617 except Exception as e
:
1618 self
.logger
.debug(logging_text
+ "Failed while deleting charms: {}".format(e
))
1624 RO_nsr_id
= RO_delete_action
= None
1625 if nsr_deployed
and nsr_deployed
.get("RO"):
1626 RO_nsr_id
= nsr_deployed
["RO"].get("nsr_id")
1627 RO_delete_action
= nsr_deployed
["RO"].get("nsr_delete_action_id")
1630 step
= db_nsr_update
["detailed-status"] = db_nslcmop_update
["detailed-status"] = \
1631 "Deleting ns from VIM"
1632 self
.update_db_2("nslcmops", nslcmop_id
, db_nslcmop_update
)
1633 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1634 self
.logger
.debug(logging_text
+ step
)
1635 desc
= await self
.RO
.delete("ns", RO_nsr_id
)
1636 RO_delete_action
= desc
["action_id"]
1637 db_nsr_update
["_admin.deployed.RO.nsr_delete_action_id"] = RO_delete_action
1638 db_nsr_update
["_admin.deployed.RO.nsr_id"] = None
1639 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
1640 if RO_delete_action
:
1641 # wait until NS is deleted from VIM
1642 step
= detailed_status
= "Waiting ns deleted from VIM. RO_id={} RO_delete_action={}".\
1643 format(RO_nsr_id
, RO_delete_action
)
1644 detailed_status_old
= None
1645 self
.logger
.debug(logging_text
+ step
)
1647 delete_timeout
= 20 * 60 # 20 minutes
1648 while delete_timeout
> 0:
1649 desc
= await self
.RO
.show("ns", item_id_name
=RO_nsr_id
, extra_item
="action",
1650 extra_item_id
=RO_delete_action
)
1651 ns_status
, ns_status_info
= self
.RO
.check_action_status(desc
)
1652 if ns_status
== "ERROR":
1653 raise ROclient
.ROClientException(ns_status_info
)
1654 elif ns_status
== "BUILD":
1655 detailed_status
= step
+ "; {}".format(ns_status_info
)
1656 elif ns_status
== "ACTIVE":
1657 db_nsr_update
["_admin.deployed.RO.nsr_delete_action_id"] = None
1658 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
1661 assert False, "ROclient.check_action_status returns unknown {}".format(ns_status
)
1662 if detailed_status
!= detailed_status_old
:
1663 detailed_status_old
= db_nslcmop_update
["detailed-status"] = \
1664 db_nsr_update
["detailed-status"] = detailed_status
1665 self
.update_db_2("nslcmops", nslcmop_id
, db_nslcmop_update
)
1666 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1667 await asyncio
.sleep(5, loop
=self
.loop
)
1669 else: # delete_timeout <= 0:
1670 raise ROclient
.ROClientException("Timeout waiting ns deleted from VIM")
1672 except ROclient
.ROClientException
as e
:
1673 if e
.http_code
== 404: # not found
1674 db_nsr_update
["_admin.deployed.RO.nsr_id"] = None
1675 db_nsr_update
["_admin.deployed.RO.nsr_status"] = "DELETED"
1676 db_nsr_update
["_admin.deployed.RO.nsr_delete_action_id"] = None
1677 self
.logger
.debug(logging_text
+ "RO_ns_id={} already deleted".format(RO_nsr_id
))
1678 elif e
.http_code
== 409: # conflict
1679 failed_detail
.append("RO_ns_id={} delete conflict: {}".format(RO_nsr_id
, e
))
1680 self
.logger
.debug(logging_text
+ failed_detail
[-1])
1683 failed_detail
.append("RO_ns_id={} delete error: {}".format(RO_nsr_id
, e
))
1684 self
.logger
.error(logging_text
+ failed_detail
[-1])
1688 if not RO_fail
and nsr_deployed
and nsr_deployed
.get("RO") and nsr_deployed
["RO"].get("nsd_id"):
1689 RO_nsd_id
= nsr_deployed
["RO"]["nsd_id"]
1691 step
= db_nsr_update
["detailed-status"] = db_nslcmop_update
["detailed-status"] =\
1692 "Deleting nsd from RO"
1693 await self
.RO
.delete("nsd", RO_nsd_id
)
1694 self
.logger
.debug(logging_text
+ "RO_nsd_id={} deleted".format(RO_nsd_id
))
1695 db_nsr_update
["_admin.deployed.RO.nsd_id"] = None
1696 except ROclient
.ROClientException
as e
:
1697 if e
.http_code
== 404: # not found
1698 db_nsr_update
["_admin.deployed.RO.nsd_id"] = None
1699 self
.logger
.debug(logging_text
+ "RO_nsd_id={} already deleted".format(RO_nsd_id
))
1700 elif e
.http_code
== 409: # conflict
1701 failed_detail
.append("RO_nsd_id={} delete conflict: {}".format(RO_nsd_id
, e
))
1702 self
.logger
.debug(logging_text
+ failed_detail
[-1])
1705 failed_detail
.append("RO_nsd_id={} delete error: {}".format(RO_nsd_id
, e
))
1706 self
.logger
.error(logging_text
+ failed_detail
[-1])
1709 if not RO_fail
and nsr_deployed
and nsr_deployed
.get("RO") and nsr_deployed
["RO"].get("vnfd"):
1710 for index
, vnf_deployed
in enumerate(nsr_deployed
["RO"]["vnfd"]):
1711 if not vnf_deployed
or not vnf_deployed
["id"]:
1714 RO_vnfd_id
= vnf_deployed
["id"]
1715 step
= db_nsr_update
["detailed-status"] = db_nslcmop_update
["detailed-status"] =\
1716 "Deleting member_vnf_index={} RO_vnfd_id={} from RO".format(
1717 vnf_deployed
["member-vnf-index"], RO_vnfd_id
)
1718 await self
.RO
.delete("vnfd", RO_vnfd_id
)
1719 self
.logger
.debug(logging_text
+ "RO_vnfd_id={} deleted".format(RO_vnfd_id
))
1720 db_nsr_update
["_admin.deployed.RO.vnfd.{}.id".format(index
)] = None
1721 except ROclient
.ROClientException
as e
:
1722 if e
.http_code
== 404: # not found
1723 db_nsr_update
["_admin.deployed.RO.vnfd.{}.id".format(index
)] = None
1724 self
.logger
.debug(logging_text
+ "RO_vnfd_id={} already deleted ".format(RO_vnfd_id
))
1725 elif e
.http_code
== 409: # conflict
1726 failed_detail
.append("RO_vnfd_id={} delete conflict: {}".format(RO_vnfd_id
, e
))
1727 self
.logger
.debug(logging_text
+ failed_detail
[-1])
1729 failed_detail
.append("RO_vnfd_id={} delete error: {}".format(RO_vnfd_id
, e
))
1730 self
.logger
.error(logging_text
+ failed_detail
[-1])
1732 # wait until charm deleted
1733 if vca_time_destroy
:
1734 db_nsr_update
["detailed-status"] = db_nslcmop_update
["detailed-status"] = step
= \
1735 "Waiting for deletion of configuration charms"
1736 self
.update_db_2("nslcmops", nslcmop_id
, db_nslcmop_update
)
1737 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1738 for vca_index
, vca_deployed
in enumerate(nsr_deployed
["VCA"]):
1739 if not vca_deployed
:
1741 step
= "Waiting for deletion of charm application_name={}".format(vca_deployed
["application"])
1742 timeout
= self
.timeout_charm_delete
- int(time() - vca_time_destroy
)
1743 if not await self
._wait
_charm
_destroyed
(vca_deployed
['model'], vca_deployed
["application"],
1745 failed_detail
.append("VCA[application_name={}] Deletion timeout".format(
1746 vca_deployed
["application"]))
1748 db_nsr
["_admin.deployed.VCA.{}".format(vca_index
)] = None
1751 self
.logger
.error(logging_text
+ " ;".join(failed_detail
))
1752 db_nsr_update
["operational-status"] = "failed"
1753 db_nsr_update
["detailed-status"] = "Deletion errors " + "; ".join(failed_detail
)
1754 db_nslcmop_update
["detailed-status"] = "; ".join(failed_detail
)
1755 db_nslcmop_update
["operationState"] = nslcmop_operation_state
= "FAILED"
1756 db_nslcmop_update
["statusEnteredTime"] = time()
1758 db_nsr_update
["operational-status"] = "terminated"
1759 db_nsr_update
["detailed-status"] = "Done"
1760 db_nsr_update
["_admin.nsState"] = "NOT_INSTANTIATED"
1761 db_nslcmop_update
["detailed-status"] = "Done"
1762 db_nslcmop_update
["operationState"] = nslcmop_operation_state
= "COMPLETED"
1763 db_nslcmop_update
["statusEnteredTime"] = time()
1764 if db_nslcmop
["operationParams"].get("autoremove"):
1767 except (ROclient
.ROClientException
, DbException
, LcmException
) as e
:
1768 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
1770 except asyncio
.CancelledError
:
1771 self
.logger
.error(logging_text
+ "Cancelled Exception while '{}'".format(step
))
1772 exc
= "Operation was cancelled"
1773 except Exception as e
:
1774 exc
= traceback
.format_exc()
1775 self
.logger
.critical(logging_text
+ "Exit Exception {}".format(e
), exc_info
=True)
1777 if exc
and db_nslcmop
:
1778 db_nslcmop_update
["detailed-status"] = "FAILED {}: {}".format(step
, exc
)
1779 db_nslcmop_update
["operationState"] = nslcmop_operation_state
= "FAILED"
1780 db_nslcmop_update
["statusEnteredTime"] = time()
1782 if db_nslcmop
and db_nslcmop_update
:
1783 self
.update_db_2("nslcmops", nslcmop_id
, db_nslcmop_update
)
1785 db_nsr_update
["_admin.nslcmop"] = None
1786 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1787 except DbException
as e
:
1788 self
.logger
.error(logging_text
+ "Cannot update database: {}".format(e
))
1789 if nslcmop_operation_state
:
1791 await self
.msg
.aiowrite("ns", "terminated", {"nsr_id": nsr_id
, "nslcmop_id": nslcmop_id
,
1792 "operationState": nslcmop_operation_state
,
1793 "autoremove": autoremove
},
1795 except Exception as e
:
1796 self
.logger
.error(logging_text
+ "kafka_write notification Exception {}".format(e
))
1797 self
.logger
.debug(logging_text
+ "Exit")
1798 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_terminate")
1801 def _map_primitive_params(primitive_desc
, params
, instantiation_params
):
1803 Generates the params to be provided to charm before executing primitive. If user does not provide a parameter,
1804 The default-value is used. If it is between < > it look for a value at instantiation_params
1805 :param primitive_desc: portion of VNFD/NSD that describes primitive
1806 :param params: Params provided by user
1807 :param instantiation_params: Instantiation params provided by user
1808 :return: a dictionary with the calculated params
1810 calculated_params
= {}
1811 for parameter
in primitive_desc
.get("parameter", ()):
1812 param_name
= parameter
["name"]
1813 if param_name
in params
:
1814 calculated_params
[param_name
] = params
[param_name
]
1815 elif "default-value" in parameter
or "value" in parameter
:
1816 if "value" in parameter
:
1817 calculated_params
[param_name
] = parameter
["value"]
1819 calculated_params
[param_name
] = parameter
["default-value"]
1820 if isinstance(calculated_params
[param_name
], str) and calculated_params
[param_name
].startswith("<") \
1821 and calculated_params
[param_name
].endswith(">"):
1822 if calculated_params
[param_name
][1:-1] in instantiation_params
:
1823 calculated_params
[param_name
] = instantiation_params
[calculated_params
[param_name
][1:-1]]
1825 raise LcmException("Parameter {} needed to execute primitive {} not provided".
1826 format(calculated_params
[param_name
], primitive_desc
["name"]))
1828 raise LcmException("Parameter {} needed to execute primitive {} not provided".
1829 format(param_name
, primitive_desc
["name"]))
1831 if isinstance(calculated_params
[param_name
], (dict, list, tuple)):
1832 calculated_params
[param_name
] = yaml
.safe_dump(calculated_params
[param_name
], default_flow_style
=True,
1834 elif isinstance(calculated_params
[param_name
], str) and calculated_params
[param_name
].startswith("!!yaml "):
1835 calculated_params
[param_name
] = calculated_params
[param_name
][7:]
1836 return calculated_params
1838 async def _ns_execute_primitive(self
, db_deployed
, member_vnf_index
, vdu_id
, vdu_name
, vdu_count_index
,
1839 primitive
, primitive_params
, retries
=0, retries_interval
=30):
1840 start_primitive_time
= time()
1842 for vca_deployed
in db_deployed
["VCA"]:
1843 if not vca_deployed
:
1845 if member_vnf_index
!= vca_deployed
["member-vnf-index"] or vdu_id
!= vca_deployed
["vdu_id"]:
1847 if vdu_name
and vdu_name
!= vca_deployed
["vdu_name"]:
1849 if vdu_count_index
and vdu_count_index
!= vca_deployed
["vdu_count_index"]:
1853 raise LcmException("charm for member_vnf_index={} vdu_id={} vdu_name={} vdu_count_index={} is not "
1854 "deployed".format(member_vnf_index
, vdu_id
, vdu_name
, vdu_count_index
))
1855 model_name
= vca_deployed
.get("model")
1856 application_name
= vca_deployed
.get("application")
1857 if not model_name
or not application_name
:
1858 raise LcmException("charm for member_vnf_index={} vdu_id={} vdu_name={} vdu_count_index={} has not "
1859 "model or application name" .format(member_vnf_index
, vdu_id
, vdu_name
,
1861 # if vca_deployed["operational-status"] != "active":
1862 # raise LcmException("charm for member_vnf_index={} vdu_id={} operational_status={} not 'active'".format(
1863 # member_vnf_index, vdu_id, vca_deployed["operational-status"]))
1864 callback
= None # self.n2vc_callback
1865 callback_args
= () # [db_nsr, db_nslcmop, member_vnf_index, None]
1866 await self
.n2vc
.login()
1867 if primitive
== "config":
1868 primitive_params
= {"params": primitive_params
}
1870 primitive_id
= await self
.n2vc
.ExecutePrimitive(
1878 while time() - start_primitive_time
< self
.timeout_primitive
:
1879 primitive_result_
= await self
.n2vc
.GetPrimitiveStatus(model_name
, primitive_id
)
1880 if primitive_result_
in ("completed", "failed"):
1881 primitive_result
= "COMPLETED" if primitive_result_
== "completed" else "FAILED"
1882 detailed_result
= await self
.n2vc
.GetPrimitiveOutput(model_name
, primitive_id
)
1884 elif primitive_result_
is None and primitive
== "config":
1885 primitive_result
= "COMPLETED"
1886 detailed_result
= None
1888 else: # ("running", "pending", None):
1890 await asyncio
.sleep(5, loop
=self
.loop
)
1892 raise LcmException("timeout after {} seconds".format(self
.timeout_primitive
))
1893 if primitive_result
== "COMPLETED":
1897 await asyncio
.sleep(retries_interval
, loop
=self
.loop
)
1899 return primitive_result
, detailed_result
1900 except (N2VCPrimitiveExecutionFailed
, LcmException
) as e
:
1901 return "FAILED", str(e
)
1903 async def action(self
, nsr_id
, nslcmop_id
):
1905 # Try to lock HA task here
1906 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA('ns', 'nslcmops', nslcmop_id
)
1907 if not task_is_locked_by_me
:
1910 logging_text
= "Task ns={} action={} ".format(nsr_id
, nslcmop_id
)
1911 self
.logger
.debug(logging_text
+ "Enter")
1912 # get all needed from database
1915 db_nsr_update
= {"_admin.nslcmop": nslcmop_id
}
1916 db_nslcmop_update
= {}
1917 nslcmop_operation_state
= None
1918 nslcmop_operation_state_detail
= None
1921 # wait for any previous tasks in process
1922 await self
.lcm_tasks
.waitfor_related_HA('ns', 'nslcmops', nslcmop_id
)
1924 step
= "Getting information from database"
1925 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
1926 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
1928 nsr_deployed
= db_nsr
["_admin"].get("deployed")
1929 vnf_index
= db_nslcmop
["operationParams"].get("member_vnf_index")
1930 vdu_id
= db_nslcmop
["operationParams"].get("vdu_id")
1931 vdu_count_index
= db_nslcmop
["operationParams"].get("vdu_count_index")
1932 vdu_name
= db_nslcmop
["operationParams"].get("vdu_name")
1935 step
= "Getting vnfr from database"
1936 db_vnfr
= self
.db
.get_one("vnfrs", {"member-vnf-index-ref": vnf_index
, "nsr-id-ref": nsr_id
})
1937 step
= "Getting vnfd from database"
1938 db_vnfd
= self
.db
.get_one("vnfds", {"_id": db_vnfr
["vnfd-id"]})
1940 if db_nsr
.get("nsd"):
1941 db_nsd
= db_nsr
.get("nsd") # TODO this will be removed
1943 step
= "Getting nsd from database"
1944 db_nsd
= self
.db
.get_one("nsds", {"_id": db_nsr
["nsd-id"]})
1946 # for backward compatibility
1947 if nsr_deployed
and isinstance(nsr_deployed
.get("VCA"), dict):
1948 nsr_deployed
["VCA"] = list(nsr_deployed
["VCA"].values())
1949 db_nsr_update
["_admin.deployed.VCA"] = nsr_deployed
["VCA"]
1950 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
1952 primitive
= db_nslcmop
["operationParams"]["primitive"]
1953 primitive_params
= db_nslcmop
["operationParams"]["primitive_params"]
1955 # look for primitive
1956 config_primitive_desc
= None
1958 for vdu
in get_iterable(db_vnfd
, "vdu"):
1959 if vdu_id
== vdu
["id"]:
1960 for config_primitive
in vdu
.get("vdu-configuration", {}).get("config-primitive", ()):
1961 if config_primitive
["name"] == primitive
:
1962 config_primitive_desc
= config_primitive
1965 for config_primitive
in db_vnfd
.get("vnf-configuration", {}).get("config-primitive", ()):
1966 if config_primitive
["name"] == primitive
:
1967 config_primitive_desc
= config_primitive
1970 for config_primitive
in db_nsd
.get("ns-configuration", {}).get("config-primitive", ()):
1971 if config_primitive
["name"] == primitive
:
1972 config_primitive_desc
= config_primitive
1975 if not config_primitive_desc
:
1976 raise LcmException("Primitive {} not found at [ns|vnf|vdu]-configuration:config-primitive ".
1981 if db_vnfr
.get("additionalParamsForVnf"):
1982 desc_params
.update(db_vnfr
["additionalParamsForVnf"])
1984 if db_nsr
.get("additionalParamsForVnf"):
1985 desc_params
.update(db_nsr
["additionalParamsForNs"])
1987 # TODO check if ns is in a proper status
1988 result
, result_detail
= await self
._ns
_execute
_primitive
(
1989 nsr_deployed
, vnf_index
, vdu_id
, vdu_name
, vdu_count_index
, primitive
,
1990 self
._map
_primitive
_params
(config_primitive_desc
, primitive_params
, desc_params
))
1991 db_nslcmop_update
["detailed-status"] = nslcmop_operation_state_detail
= result_detail
1992 db_nslcmop_update
["operationState"] = nslcmop_operation_state
= result
1993 db_nslcmop_update
["statusEnteredTime"] = time()
1994 self
.logger
.debug(logging_text
+ " task Done with result {} {}".format(result
, result_detail
))
1995 return # database update is called inside finally
1997 except (DbException
, LcmException
) as e
:
1998 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
2000 except asyncio
.CancelledError
:
2001 self
.logger
.error(logging_text
+ "Cancelled Exception while '{}'".format(step
))
2002 exc
= "Operation was cancelled"
2003 except Exception as e
:
2004 exc
= traceback
.format_exc()
2005 self
.logger
.critical(logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
), exc_info
=True)
2007 if exc
and db_nslcmop
:
2008 db_nslcmop_update
["detailed-status"] = nslcmop_operation_state_detail
= \
2009 "FAILED {}: {}".format(step
, exc
)
2010 db_nslcmop_update
["operationState"] = nslcmop_operation_state
= "FAILED"
2011 db_nslcmop_update
["statusEnteredTime"] = time()
2013 if db_nslcmop_update
:
2014 self
.update_db_2("nslcmops", nslcmop_id
, db_nslcmop_update
)
2016 db_nsr_update
["_admin.nslcmop"] = None
2017 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2018 except DbException
as e
:
2019 self
.logger
.error(logging_text
+ "Cannot update database: {}".format(e
))
2020 self
.logger
.debug(logging_text
+ "Exit")
2021 if nslcmop_operation_state
:
2023 await self
.msg
.aiowrite("ns", "actioned", {"nsr_id": nsr_id
, "nslcmop_id": nslcmop_id
,
2024 "operationState": nslcmop_operation_state
},
2026 except Exception as e
:
2027 self
.logger
.error(logging_text
+ "kafka_write notification Exception {}".format(e
))
2028 self
.logger
.debug(logging_text
+ "Exit")
2029 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_action")
2030 return nslcmop_operation_state
, nslcmop_operation_state_detail
2032 async def scale(self
, nsr_id
, nslcmop_id
):
2034 # Try to lock HA task here
2035 task_is_locked_by_me
= self
.lcm_tasks
.lock_HA('ns', 'nslcmops', nslcmop_id
)
2036 if not task_is_locked_by_me
:
2039 logging_text
= "Task ns={} scale={} ".format(nsr_id
, nslcmop_id
)
2040 self
.logger
.debug(logging_text
+ "Enter")
2041 # get all needed from database
2044 db_nslcmop_update
= {}
2045 nslcmop_operation_state
= None
2046 db_nsr_update
= {"_admin.nslcmop": nslcmop_id
}
2048 # in case of error, indicates what part of scale was failed to put nsr at error status
2049 scale_process
= None
2050 old_operational_status
= ""
2051 old_config_status
= ""
2054 # wait for any previous tasks in process
2055 await self
.lcm_tasks
.waitfor_related_HA('ns', 'nslcmops', nslcmop_id
)
2057 step
= "Getting nslcmop from database"
2058 self
.logger
.debug(step
+ " after having waited for previous tasks to be completed")
2059 db_nslcmop
= self
.db
.get_one("nslcmops", {"_id": nslcmop_id
})
2060 step
= "Getting nsr from database"
2061 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
2063 old_operational_status
= db_nsr
["operational-status"]
2064 old_config_status
= db_nsr
["config-status"]
2065 step
= "Parsing scaling parameters"
2066 # self.logger.debug(step)
2067 db_nsr_update
["operational-status"] = "scaling"
2068 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2069 nsr_deployed
= db_nsr
["_admin"].get("deployed")
2070 RO_nsr_id
= nsr_deployed
["RO"]["nsr_id"]
2071 vnf_index
= db_nslcmop
["operationParams"]["scaleVnfData"]["scaleByStepData"]["member-vnf-index"]
2072 scaling_group
= db_nslcmop
["operationParams"]["scaleVnfData"]["scaleByStepData"]["scaling-group-descriptor"]
2073 scaling_type
= db_nslcmop
["operationParams"]["scaleVnfData"]["scaleVnfType"]
2074 # scaling_policy = db_nslcmop["operationParams"]["scaleVnfData"]["scaleByStepData"].get("scaling-policy")
2076 # for backward compatibility
2077 if nsr_deployed
and isinstance(nsr_deployed
.get("VCA"), dict):
2078 nsr_deployed
["VCA"] = list(nsr_deployed
["VCA"].values())
2079 db_nsr_update
["_admin.deployed.VCA"] = nsr_deployed
["VCA"]
2080 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2082 step
= "Getting vnfr from database"
2083 db_vnfr
= self
.db
.get_one("vnfrs", {"member-vnf-index-ref": vnf_index
, "nsr-id-ref": nsr_id
})
2084 step
= "Getting vnfd from database"
2085 db_vnfd
= self
.db
.get_one("vnfds", {"_id": db_vnfr
["vnfd-id"]})
2087 step
= "Getting scaling-group-descriptor"
2088 for scaling_descriptor
in db_vnfd
["scaling-group-descriptor"]:
2089 if scaling_descriptor
["name"] == scaling_group
:
2092 raise LcmException("input parameter 'scaleByStepData':'scaling-group-descriptor':'{}' is not present "
2093 "at vnfd:scaling-group-descriptor".format(scaling_group
))
2096 # for scaling_policy_descriptor in scaling_descriptor.get("scaling-policy", ()):
2097 # cooldown_time = scaling_policy_descriptor.get("cooldown-time", 0)
2098 # if scaling_policy and scaling_policy == scaling_policy_descriptor.get("name"):
2101 # TODO check if ns is in a proper status
2102 step
= "Sending scale order to VIM"
2104 if not db_nsr
["_admin"].get("scaling-group"):
2105 self
.update_db_2("nsrs", nsr_id
, {"_admin.scaling-group": [{"name": scaling_group
, "nb-scale-op": 0}]})
2106 admin_scale_index
= 0
2108 for admin_scale_index
, admin_scale_info
in enumerate(db_nsr
["_admin"]["scaling-group"]):
2109 if admin_scale_info
["name"] == scaling_group
:
2110 nb_scale_op
= admin_scale_info
.get("nb-scale-op", 0)
2112 else: # not found, set index one plus last element and add new entry with the name
2113 admin_scale_index
+= 1
2114 db_nsr_update
["_admin.scaling-group.{}.name".format(admin_scale_index
)] = scaling_group
2115 RO_scaling_info
= []
2116 vdu_scaling_info
= {"scaling_group_name": scaling_group
, "vdu": []}
2117 if scaling_type
== "SCALE_OUT":
2118 # count if max-instance-count is reached
2119 max_instance_count
= scaling_descriptor
.get("max-instance-count", 10)
2120 # self.logger.debug("MAX_INSTANCE_COUNT is {}".format(max_instance_count))
2121 if nb_scale_op
>= max_instance_count
:
2122 raise LcmException("reached the limit of {} (max-instance-count) "
2123 "scaling-out operations for the "
2124 "scaling-group-descriptor '{}'".format(nb_scale_op
, scaling_group
))
2127 vdu_scaling_info
["scaling_direction"] = "OUT"
2128 vdu_scaling_info
["vdu-create"] = {}
2129 for vdu_scale_info
in scaling_descriptor
["vdu"]:
2130 RO_scaling_info
.append({"osm_vdu_id": vdu_scale_info
["vdu-id-ref"], "member-vnf-index": vnf_index
,
2131 "type": "create", "count": vdu_scale_info
.get("count", 1)})
2132 vdu_scaling_info
["vdu-create"][vdu_scale_info
["vdu-id-ref"]] = vdu_scale_info
.get("count", 1)
2134 elif scaling_type
== "SCALE_IN":
2135 # count if min-instance-count is reached
2136 min_instance_count
= 0
2137 if "min-instance-count" in scaling_descriptor
and scaling_descriptor
["min-instance-count"] is not None:
2138 min_instance_count
= int(scaling_descriptor
["min-instance-count"])
2139 if nb_scale_op
<= min_instance_count
:
2140 raise LcmException("reached the limit of {} (min-instance-count) scaling-in operations for the "
2141 "scaling-group-descriptor '{}'".format(nb_scale_op
, scaling_group
))
2143 vdu_scaling_info
["scaling_direction"] = "IN"
2144 vdu_scaling_info
["vdu-delete"] = {}
2145 for vdu_scale_info
in scaling_descriptor
["vdu"]:
2146 RO_scaling_info
.append({"osm_vdu_id": vdu_scale_info
["vdu-id-ref"], "member-vnf-index": vnf_index
,
2147 "type": "delete", "count": vdu_scale_info
.get("count", 1)})
2148 vdu_scaling_info
["vdu-delete"][vdu_scale_info
["vdu-id-ref"]] = vdu_scale_info
.get("count", 1)
2150 # update VDU_SCALING_INFO with the VDUs to delete ip_addresses
2151 vdu_create
= vdu_scaling_info
.get("vdu-create")
2152 vdu_delete
= copy(vdu_scaling_info
.get("vdu-delete"))
2153 if vdu_scaling_info
["scaling_direction"] == "IN":
2154 for vdur
in reversed(db_vnfr
["vdur"]):
2155 if vdu_delete
.get(vdur
["vdu-id-ref"]):
2156 vdu_delete
[vdur
["vdu-id-ref"]] -= 1
2157 vdu_scaling_info
["vdu"].append({
2158 "name": vdur
["name"],
2159 "vdu_id": vdur
["vdu-id-ref"],
2162 for interface
in vdur
["interfaces"]:
2163 vdu_scaling_info
["vdu"][-1]["interface"].append({
2164 "name": interface
["name"],
2165 "ip_address": interface
["ip-address"],
2166 "mac_address": interface
.get("mac-address"),
2168 vdu_delete
= vdu_scaling_info
.pop("vdu-delete")
2171 step
= "Executing pre-scale vnf-config-primitive"
2172 if scaling_descriptor
.get("scaling-config-action"):
2173 for scaling_config_action
in scaling_descriptor
["scaling-config-action"]:
2174 if (scaling_config_action
.get("trigger") == "pre-scale-in" and scaling_type
== "SCALE_IN") \
2175 or (scaling_config_action
.get("trigger") == "pre-scale-out" and scaling_type
== "SCALE_OUT"):
2176 vnf_config_primitive
= scaling_config_action
["vnf-config-primitive-name-ref"]
2177 step
= db_nslcmop_update
["detailed-status"] = \
2178 "executing pre-scale scaling-config-action '{}'".format(vnf_config_primitive
)
2180 # look for primitive
2181 for config_primitive
in db_vnfd
.get("vnf-configuration", {}).get("config-primitive", ()):
2182 if config_primitive
["name"] == vnf_config_primitive
:
2186 "Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:scaling-config-action"
2187 "[vnf-config-primitive-name-ref='{}'] does not match any vnf-configuration:config-"
2188 "primitive".format(scaling_group
, config_primitive
))
2190 vnfr_params
= {"VDU_SCALE_INFO": vdu_scaling_info
}
2191 if db_vnfr
.get("additionalParamsForVnf"):
2192 vnfr_params
.update(db_vnfr
["additionalParamsForVnf"])
2193 scale_process
= "VCA"
2194 db_nsr_update
["config-status"] = "configuring pre-scaling"
2195 primitive_params
= self
._map
_primitive
_params
(config_primitive
, {}, vnfr_params
)
2197 # Pre-scale reintent check: Check if this sub-operation has been executed before
2198 op_index
= self
._check
_or
_add
_scale
_suboperation
(
2199 db_nslcmop
, nslcmop_id
, vnf_index
, vnf_config_primitive
, primitive_params
, 'PRE-SCALE')
2200 if (op_index
== self
.SUBOPERATION_STATUS_SKIP
):
2201 # Skip sub-operation
2202 result
= 'COMPLETED'
2203 result_detail
= 'Done'
2204 self
.logger
.debug(logging_text
+
2205 "vnf_config_primitive={} Skipped sub-operation, result {} {}".format(
2206 vnf_config_primitive
, result
, result_detail
))
2208 if (op_index
== self
.SUBOPERATION_STATUS_NEW
):
2209 # New sub-operation: Get index of this sub-operation
2210 op_index
= len(db_nslcmop
.get('_admin', {}).get('operations')) - 1
2211 self
.logger
.debug(logging_text
+ "vnf_config_primitive={} New sub-operation".
2212 format(vnf_config_primitive
))
2214 # Reintent: Get registered params for this existing sub-operation
2215 op
= db_nslcmop
.get('_admin', {}).get('operations', [])[op_index
]
2216 vnf_index
= op
.get('member_vnf_index')
2217 vnf_config_primitive
= op
.get('primitive')
2218 primitive_params
= op
.get('primitive_params')
2219 self
.logger
.debug(logging_text
+ "vnf_config_primitive={} Sub-operation reintent".
2220 format(vnf_config_primitive
))
2221 # Execute the primitive, either with new (first-time) or registered (reintent) args
2222 result
, result_detail
= await self
._ns
_execute
_primitive
(
2223 nsr_deployed
, vnf_index
, None, None, None, vnf_config_primitive
, primitive_params
)
2224 self
.logger
.debug(logging_text
+ "vnf_config_primitive={} Done with result {} {}".format(
2225 vnf_config_primitive
, result
, result_detail
))
2226 # Update operationState = COMPLETED | FAILED
2227 self
._update
_suboperation
_status
(
2228 db_nslcmop
, op_index
, result
, result_detail
)
2230 if result
== "FAILED":
2231 raise LcmException(result_detail
)
2232 db_nsr_update
["config-status"] = old_config_status
2233 scale_process
= None
2237 # Should this block be skipped if 'RO_nsr_id' == None ?
2238 # if (RO_nsr_id and RO_scaling_info):
2240 scale_process
= "RO"
2241 # Scale RO reintent check: Check if this sub-operation has been executed before
2242 op_index
= self
._check
_or
_add
_scale
_suboperation
(
2243 db_nslcmop
, vnf_index
, None, None, 'SCALE-RO', RO_nsr_id
, RO_scaling_info
)
2244 if (op_index
== self
.SUBOPERATION_STATUS_SKIP
):
2245 # Skip sub-operation
2246 result
= 'COMPLETED'
2247 result_detail
= 'Done'
2248 self
.logger
.debug(logging_text
+ "Skipped sub-operation RO, result {} {}".format(
2249 result
, result_detail
))
2251 if (op_index
== self
.SUBOPERATION_STATUS_NEW
):
2252 # New sub-operation: Get index of this sub-operation
2253 op_index
= len(db_nslcmop
.get('_admin', {}).get('operations')) - 1
2254 self
.logger
.debug(logging_text
+ "New sub-operation RO")
2256 # Reintent: Get registered params for this existing sub-operation
2257 op
= db_nslcmop
.get('_admin', {}).get('operations', [])[op_index
]
2258 RO_nsr_id
= op
.get('RO_nsr_id')
2259 RO_scaling_info
= op
.get('RO_scaling_info')
2260 self
.logger
.debug(logging_text
+ "Sub-operation RO reintent".format(
2261 vnf_config_primitive
))
2263 RO_desc
= await self
.RO
.create_action("ns", RO_nsr_id
, {"vdu-scaling": RO_scaling_info
})
2264 db_nsr_update
["_admin.scaling-group.{}.nb-scale-op".format(admin_scale_index
)] = nb_scale_op
2265 db_nsr_update
["_admin.scaling-group.{}.time".format(admin_scale_index
)] = time()
2267 RO_nslcmop_id
= RO_desc
["instance_action_id"]
2268 db_nslcmop_update
["_admin.deploy.RO"] = RO_nslcmop_id
2270 RO_task_done
= False
2271 step
= detailed_status
= "Waiting RO_task_id={} to complete the scale action.".format(RO_nslcmop_id
)
2272 detailed_status_old
= None
2273 self
.logger
.debug(logging_text
+ step
)
2275 deployment_timeout
= 1 * 3600 # One hour
2276 while deployment_timeout
> 0:
2277 if not RO_task_done
:
2278 desc
= await self
.RO
.show("ns", item_id_name
=RO_nsr_id
, extra_item
="action",
2279 extra_item_id
=RO_nslcmop_id
)
2280 ns_status
, ns_status_info
= self
.RO
.check_action_status(desc
)
2281 if ns_status
== "ERROR":
2282 raise ROclient
.ROClientException(ns_status_info
)
2283 elif ns_status
== "BUILD":
2284 detailed_status
= step
+ "; {}".format(ns_status_info
)
2285 elif ns_status
== "ACTIVE":
2287 step
= detailed_status
= "Waiting ns ready at RO. RO_id={}".format(RO_nsr_id
)
2288 self
.logger
.debug(logging_text
+ step
)
2290 assert False, "ROclient.check_action_status returns unknown {}".format(ns_status
)
2292 if ns_status
== "ERROR":
2293 raise ROclient
.ROClientException(ns_status_info
)
2294 elif ns_status
== "BUILD":
2295 detailed_status
= step
+ "; {}".format(ns_status_info
)
2296 elif ns_status
== "ACTIVE":
2297 step
= detailed_status
= \
2298 "Waiting for management IP address reported by the VIM. Updating VNFRs"
2300 self
.scale_vnfr(db_vnfr
, vdu_create
=vdu_create
, vdu_delete
=vdu_delete
)
2303 desc
= await self
.RO
.show("ns", RO_nsr_id
)
2304 # nsr_deployed["nsr_ip"] = RO.get_ns_vnf_info(desc)
2305 self
.ns_update_vnfr({db_vnfr
["member-vnf-index-ref"]: db_vnfr
}, desc
)
2307 except LcmExceptionNoMgmtIP
:
2310 assert False, "ROclient.check_ns_status returns unknown {}".format(ns_status
)
2311 if detailed_status
!= detailed_status_old
:
2312 self
._update
_suboperation
_status
(
2313 db_nslcmop
, op_index
, 'COMPLETED', detailed_status
)
2314 detailed_status_old
= db_nslcmop_update
["detailed-status"] = detailed_status
2315 self
.update_db_2("nslcmops", nslcmop_id
, db_nslcmop_update
)
2317 await asyncio
.sleep(5, loop
=self
.loop
)
2318 deployment_timeout
-= 5
2319 if deployment_timeout
<= 0:
2320 self
._update
_suboperation
_status
(
2321 db_nslcmop
, nslcmop_id
, op_index
, 'FAILED', "Timeout when waiting for ns to get ready")
2322 raise ROclient
.ROClientException("Timeout waiting ns to be ready")
2324 # update VDU_SCALING_INFO with the obtained ip_addresses
2325 if vdu_scaling_info
["scaling_direction"] == "OUT":
2326 for vdur
in reversed(db_vnfr
["vdur"]):
2327 if vdu_scaling_info
["vdu-create"].get(vdur
["vdu-id-ref"]):
2328 vdu_scaling_info
["vdu-create"][vdur
["vdu-id-ref"]] -= 1
2329 vdu_scaling_info
["vdu"].append({
2330 "name": vdur
["name"],
2331 "vdu_id": vdur
["vdu-id-ref"],
2334 for interface
in vdur
["interfaces"]:
2335 vdu_scaling_info
["vdu"][-1]["interface"].append({
2336 "name": interface
["name"],
2337 "ip_address": interface
["ip-address"],
2338 "mac_address": interface
.get("mac-address"),
2340 del vdu_scaling_info
["vdu-create"]
2342 self
._update
_suboperation
_status
(db_nslcmop
, op_index
, 'COMPLETED', 'Done')
2345 scale_process
= None
2347 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2350 # execute primitive service POST-SCALING
2351 step
= "Executing post-scale vnf-config-primitive"
2352 if scaling_descriptor
.get("scaling-config-action"):
2353 for scaling_config_action
in scaling_descriptor
["scaling-config-action"]:
2354 if (scaling_config_action
.get("trigger") == "post-scale-in" and scaling_type
== "SCALE_IN") \
2355 or (scaling_config_action
.get("trigger") == "post-scale-out" and scaling_type
== "SCALE_OUT"):
2356 vnf_config_primitive
= scaling_config_action
["vnf-config-primitive-name-ref"]
2357 step
= db_nslcmop_update
["detailed-status"] = \
2358 "executing post-scale scaling-config-action '{}'".format(vnf_config_primitive
)
2360 vnfr_params
= {"VDU_SCALE_INFO": vdu_scaling_info
}
2361 if db_vnfr
.get("additionalParamsForVnf"):
2362 vnfr_params
.update(db_vnfr
["additionalParamsForVnf"])
2364 # look for primitive
2365 for config_primitive
in db_vnfd
.get("vnf-configuration", {}).get("config-primitive", ()):
2366 if config_primitive
["name"] == vnf_config_primitive
:
2369 raise LcmException("Invalid vnfd descriptor at scaling-group-descriptor[name='{}']:"
2370 "scaling-config-action[vnf-config-primitive-name-ref='{}'] does not "
2371 "match any vnf-configuration:config-primitive".format(scaling_group
,
2373 scale_process
= "VCA"
2374 db_nsr_update
["config-status"] = "configuring post-scaling"
2375 primitive_params
= self
._map
_primitive
_params
(config_primitive
, {}, vnfr_params
)
2377 # Post-scale reintent check: Check if this sub-operation has been executed before
2378 op_index
= self
._check
_or
_add
_scale
_suboperation
(
2379 db_nslcmop
, nslcmop_id
, vnf_index
, vnf_config_primitive
, primitive_params
, 'POST-SCALE')
2380 if (op_index
== self
.SUBOPERATION_STATUS_SKIP
):
2381 # Skip sub-operation
2382 result
= 'COMPLETED'
2383 result_detail
= 'Done'
2384 self
.logger
.debug(logging_text
+
2385 "vnf_config_primitive={} Skipped sub-operation, result {} {}".
2386 format(vnf_config_primitive
, result
, result_detail
))
2388 if (op_index
== self
.SUBOPERATION_STATUS_NEW
):
2389 # New sub-operation: Get index of this sub-operation
2390 op_index
= len(db_nslcmop
.get('_admin', {}).get('operations')) - 1
2391 self
.logger
.debug(logging_text
+ "vnf_config_primitive={} New sub-operation".
2392 format(vnf_config_primitive
))
2394 # Reintent: Get registered params for this existing sub-operation
2395 op
= db_nslcmop
.get('_admin', {}).get('operations', [])[op_index
]
2396 vnf_index
= op
.get('member_vnf_index')
2397 vnf_config_primitive
= op
.get('primitive')
2398 primitive_params
= op
.get('primitive_params')
2399 self
.logger
.debug(logging_text
+ "vnf_config_primitive={} Sub-operation reintent".
2400 format(vnf_config_primitive
))
2401 # Execute the primitive, either with new (first-time) or registered (reintent) args
2402 result
, result_detail
= await self
._ns
_execute
_primitive
(
2403 nsr_deployed
, vnf_index
, None, None, None, vnf_config_primitive
, primitive_params
)
2404 self
.logger
.debug(logging_text
+ "vnf_config_primitive={} Done with result {} {}".format(
2405 vnf_config_primitive
, result
, result_detail
))
2406 # Update operationState = COMPLETED | FAILED
2407 self
._update
_suboperation
_status
(
2408 db_nslcmop
, op_index
, result
, result_detail
)
2410 if result
== "FAILED":
2411 raise LcmException(result_detail
)
2412 db_nsr_update
["config-status"] = old_config_status
2413 scale_process
= None
2416 db_nslcmop_update
["operationState"] = nslcmop_operation_state
= "COMPLETED"
2417 db_nslcmop_update
["statusEnteredTime"] = time()
2418 db_nslcmop_update
["detailed-status"] = "done"
2419 db_nsr_update
["detailed-status"] = "" # "scaled {} {}".format(scaling_group, scaling_type)
2420 db_nsr_update
["operational-status"] = "running" if old_operational_status
== "failed" \
2421 else old_operational_status
2422 db_nsr_update
["config-status"] = old_config_status
2424 except (ROclient
.ROClientException
, DbException
, LcmException
) as e
:
2425 self
.logger
.error(logging_text
+ "Exit Exception {}".format(e
))
2427 except asyncio
.CancelledError
:
2428 self
.logger
.error(logging_text
+ "Cancelled Exception while '{}'".format(step
))
2429 exc
= "Operation was cancelled"
2430 except Exception as e
:
2431 exc
= traceback
.format_exc()
2432 self
.logger
.critical(logging_text
+ "Exit Exception {} {}".format(type(e
).__name
__, e
), exc_info
=True)
2436 db_nslcmop_update
["detailed-status"] = "FAILED {}: {}".format(step
, exc
)
2437 db_nslcmop_update
["operationState"] = nslcmop_operation_state
= "FAILED"
2438 db_nslcmop_update
["statusEnteredTime"] = time()
2440 db_nsr_update
["operational-status"] = old_operational_status
2441 db_nsr_update
["config-status"] = old_config_status
2442 db_nsr_update
["detailed-status"] = ""
2443 db_nsr_update
["_admin.nslcmop"] = None
2445 if "VCA" in scale_process
:
2446 db_nsr_update
["config-status"] = "failed"
2447 if "RO" in scale_process
:
2448 db_nsr_update
["operational-status"] = "failed"
2449 db_nsr_update
["detailed-status"] = "FAILED scaling nslcmop={} {}: {}".format(nslcmop_id
, step
,
2452 if db_nslcmop
and db_nslcmop_update
:
2453 self
.update_db_2("nslcmops", nslcmop_id
, db_nslcmop_update
)
2455 db_nsr_update
["_admin.nslcmop"] = None
2456 self
.update_db_2("nsrs", nsr_id
, db_nsr_update
)
2457 except DbException
as e
:
2458 self
.logger
.error(logging_text
+ "Cannot update database: {}".format(e
))
2459 if nslcmop_operation_state
:
2461 await self
.msg
.aiowrite("ns", "scaled", {"nsr_id": nsr_id
, "nslcmop_id": nslcmop_id
,
2462 "operationState": nslcmop_operation_state
},
2465 # await asyncio.sleep(cooldown_time, loop=self.loop)
2466 # await self.msg.aiowrite("ns","scaled-cooldown-time", {"nsr_id": nsr_id, "nslcmop_id": nslcmop_id})
2467 except Exception as e
:
2468 self
.logger
.error(logging_text
+ "kafka_write notification Exception {}".format(e
))
2469 self
.logger
.debug(logging_text
+ "Exit")
2470 self
.lcm_tasks
.remove("ns", nsr_id
, nslcmop_id
, "ns_scale")