1 # -*- coding: utf-8 -*-
4 # Copyright 2020 Telefonica Investigacion y Desarrollo, S.A.U.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
21 from traceback
import format_exc
as traceback_format_exc
22 from osm_ng_ro
.ns_thread
import NsWorker
, NsWorkerException
, deep_get
23 from osm_ng_ro
.validation
import validate_input
, deploy_schema
24 from osm_common
import (
31 version
as common_version
,
33 from osm_common
.dbbase
import DbException
34 from osm_common
.fsbase
import FsException
35 from osm_common
.msgbase
import MsgException
36 from http
import HTTPStatus
37 from uuid
import uuid4
38 from threading
import Lock
39 from random
import choice
as random_choice
48 from cryptography
.hazmat
.primitives
import serialization
as crypto_serialization
49 from cryptography
.hazmat
.primitives
.asymmetric
import rsa
50 from cryptography
.hazmat
.backends
import default_backend
as crypto_default_backend
52 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
53 min_common_version
= "0.1.16"
56 class NsException(Exception):
57 def __init__(self
, message
, http_code
=HTTPStatus
.BAD_REQUEST
):
58 self
.http_code
= http_code
59 super(Exception, self
).__init
__(message
)
64 Obtain a unique ID for this process. If running from inside docker, it will get docker ID. If not it
65 will provide a random one
68 # Try getting docker id. If fails, get pid
70 with
open("/proc/self/cgroup", "r") as f
:
71 text_id_
= f
.readline()
72 _
, _
, text_id
= text_id_
.rpartition("/")
73 text_id
= text_id
.replace("\n", "")[:12]
81 return "".join(random_choice("0123456789abcdef") for _
in range(12))
85 """utility for compare dot separate versions. Fills with zeros to proper number comparison"""
88 for point
in v
.split("."):
89 filled
.append(point
.zfill(8))
100 # self.operations = None
102 # ^ Getting logger inside method self.start because parent logger (ro) is not available yet.
103 # If done now it will not be linked to parent not getting its handler and level
105 self
.write_lock
= None
106 self
.vims_assigned
= {}
111 def init_db(self
, target_version
):
114 def start(self
, config
):
116 Connect to database, filesystem storage, and messaging
117 :param config: two level dictionary with configuration. Top level should contain 'database', 'storage',
118 :param config: Configuration of db, storage, etc
122 self
.config
["process_id"] = get_process_id() # used for HA identity
123 self
.logger
= logging
.getLogger("ro.ns")
125 # check right version of common
126 if versiontuple(common_version
) < versiontuple(min_common_version
):
128 "Not compatible osm/common version '{}'. Needed '{}' or higher".format(
129 common_version
, min_common_version
135 if config
["database"]["driver"] == "mongo":
136 self
.db
= dbmongo
.DbMongo()
137 self
.db
.db_connect(config
["database"])
138 elif config
["database"]["driver"] == "memory":
139 self
.db
= dbmemory
.DbMemory()
140 self
.db
.db_connect(config
["database"])
143 "Invalid configuration param '{}' at '[database]':'driver'".format(
144 config
["database"]["driver"]
149 if config
["storage"]["driver"] == "local":
150 self
.fs
= fslocal
.FsLocal()
151 self
.fs
.fs_connect(config
["storage"])
152 elif config
["storage"]["driver"] == "mongo":
153 self
.fs
= fsmongo
.FsMongo()
154 self
.fs
.fs_connect(config
["storage"])
155 elif config
["storage"]["driver"] is None:
159 "Invalid configuration param '{}' at '[storage]':'driver'".format(
160 config
["storage"]["driver"]
165 if config
["message"]["driver"] == "local":
166 self
.msg
= msglocal
.MsgLocal()
167 self
.msg
.connect(config
["message"])
168 elif config
["message"]["driver"] == "kafka":
169 self
.msg
= msgkafka
.MsgKafka()
170 self
.msg
.connect(config
["message"])
173 "Invalid configuration param '{}' at '[message]':'driver'".format(
174 config
["message"]["driver"]
178 # TODO load workers to deal with exising database tasks
180 self
.write_lock
= Lock()
181 except (DbException
, FsException
, MsgException
) as e
:
182 raise NsException(str(e
), http_code
=e
.http_code
)
184 def get_assigned_vims(self
):
185 return list(self
.vims_assigned
.keys())
190 self
.db
.db_disconnect()
193 self
.fs
.fs_disconnect()
196 self
.msg
.disconnect()
198 self
.write_lock
= None
199 except (DbException
, FsException
, MsgException
) as e
:
200 raise NsException(str(e
), http_code
=e
.http_code
)
202 for worker
in self
.workers
:
203 worker
.insert_task(("terminate",))
205 def _create_worker(self
):
207 Look for a worker thread in idle status. If not found it creates one unless the number of threads reach the
208 limit of 'server.ns_threads' configuration. If reached, it just assigns one existing thread
209 return the index of the assigned worker thread. Worker threads are storead at self.workers
211 # Look for a thread in idle status
215 for i
in range(len(self
.workers
))
216 if self
.workers
[i
] and self
.workers
[i
].idle
221 if worker_id
is not None:
222 # unset idle status to avoid race conditions
223 self
.workers
[worker_id
].idle
= False
225 worker_id
= len(self
.workers
)
227 if worker_id
< self
.config
["global"]["server.ns_threads"]:
228 # create a new worker
230 NsWorker(worker_id
, self
.config
, self
.plugins
, self
.db
)
232 self
.workers
[worker_id
].start()
234 # reached maximum number of threads, assign VIM to an existing one
235 worker_id
= self
.next_worker
236 self
.next_worker
= (self
.next_worker
+ 1) % self
.config
["global"][
242 def assign_vim(self
, target_id
):
243 with self
.write_lock
:
244 return self
._assign
_vim
(target_id
)
246 def _assign_vim(self
, target_id
):
247 if target_id
not in self
.vims_assigned
:
248 worker_id
= self
.vims_assigned
[target_id
] = self
._create
_worker
()
249 self
.workers
[worker_id
].insert_task(("load_vim", target_id
))
251 def reload_vim(self
, target_id
):
252 # send reload_vim to the thread working with this VIM and inform all that a VIM has been changed,
253 # this is because database VIM information is cached for threads working with SDN
254 with self
.write_lock
:
255 for worker
in self
.workers
:
256 if worker
and not worker
.idle
:
257 worker
.insert_task(("reload_vim", target_id
))
259 def unload_vim(self
, target_id
):
260 with self
.write_lock
:
261 return self
._unload
_vim
(target_id
)
263 def _unload_vim(self
, target_id
):
264 if target_id
in self
.vims_assigned
:
265 worker_id
= self
.vims_assigned
[target_id
]
266 self
.workers
[worker_id
].insert_task(("unload_vim", target_id
))
267 del self
.vims_assigned
[target_id
]
269 def check_vim(self
, target_id
):
270 with self
.write_lock
:
271 if target_id
in self
.vims_assigned
:
272 worker_id
= self
.vims_assigned
[target_id
]
274 worker_id
= self
._create
_worker
()
276 worker
= self
.workers
[worker_id
]
277 worker
.insert_task(("check_vim", target_id
))
279 def unload_unused_vims(self
):
280 with self
.write_lock
:
283 for target_id
in self
.vims_assigned
:
284 if not self
.db
.get_one(
287 "target_id": target_id
,
288 "tasks.status": ["SCHEDULED", "BUILD", "DONE", "FAILED"],
292 vims_to_unload
.append(target_id
)
294 for target_id
in vims_to_unload
:
295 self
._unload
_vim
(target_id
)
297 def _get_cloud_init(self
, where
):
299 Not used as cloud init content is provided in the http body. This method reads cloud init from a file
300 :param where: can be 'vnfr_id:file:file_name' or 'vnfr_id:vdu:vdu_idex'
303 vnfd_id
, _
, other
= where
.partition(":")
304 _type
, _
, name
= other
.partition(":")
305 vnfd
= self
.db
.get_one("vnfds", {"_id": vnfd_id
})
308 base_folder
= vnfd
["_admin"]["storage"]
309 cloud_init_file
= "{}/{}/cloud_init/{}".format(
310 base_folder
["folder"], base_folder
["pkg-dir"], name
315 "Cannot read file '{}'. Filesystem not loaded, change configuration at storage.driver".format(
320 with self
.fs
.file_open(cloud_init_file
, "r") as ci_file
:
321 cloud_init_content
= ci_file
.read()
323 cloud_init_content
= vnfd
["vdu"][int(name
)]["cloud-init"]
325 raise NsException("Mismatch descriptor for cloud init: {}".format(where
))
327 return cloud_init_content
329 def _parse_jinja2(self
, cloud_init_content
, params
, context
):
331 env
= Environment(undefined
=StrictUndefined
)
332 template
= env
.from_string(cloud_init_content
)
334 return template
.render(params
or {})
335 except UndefinedError
as e
:
337 "Variable '{}' defined at vnfd='{}' must be provided in the instantiation parameters"
338 "inside the 'additionalParamsForVnf' block".format(e
, context
)
340 except (TemplateError
, TemplateNotFound
) as e
:
342 "Error parsing Jinja2 to cloud-init content at vnfd='{}': {}".format(
347 def _create_db_ro_nsrs(self
, nsr_id
, now
):
349 key
= rsa
.generate_private_key(
350 backend
=crypto_default_backend(), public_exponent
=65537, key_size
=2048
352 private_key
= key
.private_bytes(
353 crypto_serialization
.Encoding
.PEM
,
354 crypto_serialization
.PrivateFormat
.PKCS8
,
355 crypto_serialization
.NoEncryption(),
357 public_key
= key
.public_key().public_bytes(
358 crypto_serialization
.Encoding
.OpenSSH
,
359 crypto_serialization
.PublicFormat
.OpenSSH
,
361 private_key
= private_key
.decode("utf8")
362 # Change first line because Paramiko needs a explicit start with 'BEGIN RSA PRIVATE KEY'
363 i
= private_key
.find("\n")
364 private_key
= "-----BEGIN RSA PRIVATE KEY-----" + private_key
[i
:]
365 public_key
= public_key
.decode("utf8")
366 except Exception as e
:
367 raise NsException("Cannot create ssh-keys: {}".format(e
))
369 schema_version
= "1.1"
370 private_key_encrypted
= self
.db
.encrypt(
371 private_key
, schema_version
=schema_version
, salt
=nsr_id
378 "schema_version": schema_version
,
380 "public_key": public_key
,
381 "private_key": private_key_encrypted
,
384 self
.db
.create("ro_nsrs", db_content
)
388 def deploy(self
, session
, indata
, version
, nsr_id
, *args
, **kwargs
):
389 self
.logger
.debug("ns.deploy nsr_id={} indata={}".format(nsr_id
, indata
))
390 validate_input(indata
, deploy_schema
)
391 action_id
= indata
.get("action_id", str(uuid4()))
393 # get current deployment
394 db_nsr_update
= {} # update operation on nsrs
396 db_vnfrs
= {} # vnf's info indexed by _id
397 nb_ro_tasks
= 0 # for logging
398 vdu2cloud_init
= indata
.get("cloud_init_content") or {}
400 logging_text
= "Task deploy nsr_id={} action_id={} ".format(nsr_id
, action_id
)
401 self
.logger
.debug(logging_text
+ "Enter")
404 step
= "Getting ns and vnfr record from db"
405 db_nsr
= self
.db
.get_one("nsrs", {"_id": nsr_id
})
407 tasks_by_target_record_id
= {}
408 # read from db: vnf's of this ns
409 step
= "Getting vnfrs from db"
410 db_vnfrs_list
= self
.db
.get_list("vnfrs", {"nsr-id-ref": nsr_id
})
412 if not db_vnfrs_list
:
413 raise NsException("Cannot obtain associated VNF for ns")
415 for vnfr
in db_vnfrs_list
:
416 db_vnfrs
[vnfr
["_id"]] = vnfr
417 db_vnfrs_update
[vnfr
["_id"]] = {}
420 db_ro_nsr
= self
.db
.get_one("ro_nsrs", {"_id": nsr_id
}, fail_on_empty
=False)
423 db_ro_nsr
= self
._create
_db
_ro
_nsrs
(nsr_id
, now
)
425 ro_nsr_public_key
= db_ro_nsr
["public_key"]
427 # check that action_id is not in the list of actions. Suffixed with :index
428 if action_id
in db_ro_nsr
["actions"]:
432 new_action_id
= "{}:{}".format(action_id
, index
)
434 if new_action_id
not in db_ro_nsr
["actions"]:
435 action_id
= new_action_id
438 + "Changing action_id in use to {}".format(action_id
)
457 "target_id": target_id
, # it will be removed before pushing at database
458 "action_id": action_id
,
460 "task_id": "{}:{}".format(action_id
, task_index
),
461 "status": "SCHEDULED",
464 "target_record": target_record
,
465 "target_record_id": target_record_id
,
469 task
.update(extra_dict
) # params, find_params, depends_on
475 def _create_ro_task(target_id
, task
):
480 _id
= task
["task_id"]
485 "target_id": target_id
,
488 "created_items": None,
503 def _process_image_params(target_image
, vim_info
, target_record_id
):
506 if target_image
.get("image"):
507 find_params
["filter_dict"] = {"name": target_image
.get("image")}
509 if target_image
.get("vim_image_id"):
510 find_params
["filter_dict"] = {
511 "id": target_image
.get("vim_image_id")
514 if target_image
.get("image_checksum"):
515 find_params
["filter_dict"] = {
516 "checksum": target_image
.get("image_checksum")
519 return {"find_params": find_params
}
521 def _process_flavor_params(target_flavor
, vim_info
, target_record_id
):
522 def _get_resource_allocation_params(quota_descriptor
):
524 read the quota_descriptor from vnfd and fetch the resource allocation properties from the
526 :param quota_descriptor: cpu/mem/vif/disk-io quota descriptor
527 :return: quota params for limit, reserve, shares from the descriptor object
531 if quota_descriptor
.get("limit"):
532 quota
["limit"] = int(quota_descriptor
["limit"])
534 if quota_descriptor
.get("reserve"):
535 quota
["reserve"] = int(quota_descriptor
["reserve"])
537 if quota_descriptor
.get("shares"):
538 quota
["shares"] = int(quota_descriptor
["shares"])
545 "disk": int(target_flavor
["storage-gb"]),
546 "ram": int(target_flavor
["memory-mb"]),
547 "vcpus": int(target_flavor
["vcpu-count"]),
553 for vnf
in indata
.get("vnf", []):
554 for vdur
in vnf
.get("vdur", []):
555 if vdur
.get("ns-flavor-id") == target_flavor
["id"]:
558 for storage
in target_vdur
.get("virtual-storages", []):
560 storage
.get("type-of-storage")
561 == "etsi-nfv-descriptors:ephemeral-storage"
563 flavor_data
["ephemeral"] = int(
564 storage
.get("size-of-storage", 0)
567 if target_flavor
.get("guest-epa"):
571 if target_flavor
["guest-epa"].get("numa-node-policy"):
572 numa_node_policy
= target_flavor
["guest-epa"].get(
576 if numa_node_policy
.get("node"):
577 numa_node
= numa_node_policy
["node"][0]
579 if numa_node
.get("num-cores"):
580 numa
["cores"] = numa_node
["num-cores"]
583 if numa_node
.get("paired-threads"):
584 if numa_node
["paired-threads"].get(
587 numa
["paired-threads"] = int(
588 numa_node
["paired-threads"][
595 numa_node
["paired-threads"].get("paired-thread-ids")
597 numa
["paired-threads-id"] = []
599 for pair
in numa_node
["paired-threads"][
602 numa
["paired-threads-id"].append(
604 str(pair
["thread-a"]),
605 str(pair
["thread-b"]),
609 if numa_node
.get("num-threads"):
610 numa
["threads"] = int(numa_node
["num-threads"])
613 if numa_node
.get("memory-mb"):
614 numa
["memory"] = max(
615 int(numa_node
["memory-mb"] / 1024), 1
618 if target_flavor
["guest-epa"].get("mempage-size"):
619 extended
["mempage-size"] = target_flavor
["guest-epa"].get(
624 target_flavor
["guest-epa"].get("cpu-pinning-policy")
628 target_flavor
["guest-epa"]["cpu-pinning-policy"]
632 target_flavor
["guest-epa"].get(
633 "cpu-thread-pinning-policy"
635 and target_flavor
["guest-epa"][
636 "cpu-thread-pinning-policy"
640 numa
["cores"] = max(flavor_data
["vcpus"], 1)
642 numa
["threads"] = max(flavor_data
["vcpus"], 1)
646 if target_flavor
["guest-epa"].get("cpu-quota") and not epa_vcpu_set
:
647 cpuquota
= _get_resource_allocation_params(
648 target_flavor
["guest-epa"].get("cpu-quota")
652 extended
["cpu-quota"] = cpuquota
654 if target_flavor
["guest-epa"].get("mem-quota"):
655 vduquota
= _get_resource_allocation_params(
656 target_flavor
["guest-epa"].get("mem-quota")
660 extended
["mem-quota"] = vduquota
662 if target_flavor
["guest-epa"].get("disk-io-quota"):
663 diskioquota
= _get_resource_allocation_params(
664 target_flavor
["guest-epa"].get("disk-io-quota")
668 extended
["disk-io-quota"] = diskioquota
670 if target_flavor
["guest-epa"].get("vif-quota"):
671 vifquota
= _get_resource_allocation_params(
672 target_flavor
["guest-epa"].get("vif-quota")
676 extended
["vif-quota"] = vifquota
679 extended
["numas"] = [numa
]
682 flavor_data
["extended"] = extended
684 extra_dict
= {"find_params": {"flavor_data": flavor_data
}}
685 flavor_data_name
= flavor_data
.copy()
686 flavor_data_name
["name"] = target_flavor
["name"]
687 extra_dict
["params"] = {"flavor_data": flavor_data_name
}
691 def _ip_profile_2_ro(ip_profile
):
697 if "v4" in ip_profile
.get("ip-version", "ipv4")
699 "subnet_address": ip_profile
.get("subnet-address"),
700 "gateway_address": ip_profile
.get("gateway-address"),
701 "dhcp_enabled": ip_profile
.get("dhcp-params", {}).get(
704 "dhcp_start_address": ip_profile
.get("dhcp-params", {}).get(
705 "start-address", None
707 "dhcp_count": ip_profile
.get("dhcp-params", {}).get("count", None),
710 if ip_profile
.get("dns-server"):
711 ro_ip_profile
["dns_address"] = ";".join(
712 [v
["address"] for v
in ip_profile
["dns-server"]]
715 if ip_profile
.get("security-group"):
716 ro_ip_profile
["security_group"] = ip_profile
["security-group"]
720 def _process_net_params(target_vld
, vim_info
, target_record_id
):
724 if vim_info
.get("sdn"):
725 # vnf_preffix = "vnfrs:{}".format(vnfr_id)
726 # ns_preffix = "nsrs:{}".format(nsr_id)
727 # remove the ending ".sdn
728 vld_target_record_id
, _
, _
= target_record_id
.rpartition(".")
729 extra_dict
["params"] = {
731 for k
in ("sdn-ports", "target_vim", "vlds", "type")
735 # TODO needed to add target_id in the dependency.
736 if vim_info
.get("target_vim"):
737 extra_dict
["depends_on"] = [
738 vim_info
.get("target_vim") + " " + vld_target_record_id
743 if vim_info
.get("vim_network_name"):
744 extra_dict
["find_params"] = {
745 "filter_dict": {"name": vim_info
.get("vim_network_name")}
747 elif vim_info
.get("vim_network_id"):
748 extra_dict
["find_params"] = {
749 "filter_dict": {"id": vim_info
.get("vim_network_id")}
751 elif target_vld
.get("mgmt-network"):
752 extra_dict
["find_params"] = {"mgmt": True, "name": target_vld
["id"]}
755 extra_dict
["params"] = {
756 "net_name": "{}-{}".format(
758 target_vld
.get("name", target_vld
["id"])[:16],
760 "ip_profile": _ip_profile_2_ro(vim_info
.get("ip_profile")),
761 "provider_network_profile": vim_info
.get("provider_network"),
764 if not target_vld
.get("underlay"):
765 extra_dict
["params"]["net_type"] = "bridge"
767 extra_dict
["params"]["net_type"] = (
768 "ptp" if target_vld
.get("type") == "ELINE" else "data"
773 def _process_vdu_params(target_vdu
, vim_info
, target_record_id
):
778 nonlocal vdu2cloud_init
779 nonlocal tasks_by_target_record_id
781 vnf_preffix
= "vnfrs:{}".format(vnfr_id
)
782 ns_preffix
= "nsrs:{}".format(nsr_id
)
783 image_text
= ns_preffix
+ ":image." + target_vdu
["ns-image-id"]
784 flavor_text
= ns_preffix
+ ":flavor." + target_vdu
["ns-flavor-id"]
785 extra_dict
= {"depends_on": [image_text
, flavor_text
]}
788 for iface_index
, interface
in enumerate(target_vdu
["interfaces"]):
789 if interface
.get("ns-vld-id"):
790 net_text
= ns_preffix
+ ":vld." + interface
["ns-vld-id"]
791 elif interface
.get("vnf-vld-id"):
792 net_text
= vnf_preffix
+ ":vld." + interface
["vnf-vld-id"]
795 "Interface {} from vdu {} not connected to any vld".format(
796 iface_index
, target_vdu
["vdu-name"]
800 continue # interface not connected to any vld
802 extra_dict
["depends_on"].append(net_text
)
804 if "port-security-enabled" in interface
:
805 interface
["port_security"] = interface
.pop(
806 "port-security-enabled"
809 if "port-security-disable-strategy" in interface
:
810 interface
["port_security_disable_strategy"] = interface
.pop(
811 "port-security-disable-strategy"
816 for x
, v
in interface
.items()
822 "port_security_disable_strategy",
826 net_item
["net_id"] = "TASK-" + net_text
827 net_item
["type"] = "virtual"
829 # TODO mac_address: used for SR-IOV ifaces #TODO for other types
830 # TODO floating_ip: True/False (or it can be None)
831 if interface
.get("type") in ("SR-IOV", "PCI-PASSTHROUGH"):
832 # mark the net create task as type data
834 tasks_by_target_record_id
, net_text
, "params", "net_type"
836 tasks_by_target_record_id
[net_text
]["params"][
840 net_item
["use"] = "data"
841 net_item
["model"] = interface
["type"]
842 net_item
["type"] = interface
["type"]
844 interface
.get("type") == "OM-MGMT"
845 or interface
.get("mgmt-interface")
846 or interface
.get("mgmt-vnf")
848 net_item
["use"] = "mgmt"
850 # if interface.get("type") in ("VIRTIO", "E1000", "PARAVIRT"):
851 net_item
["use"] = "bridge"
852 net_item
["model"] = interface
.get("type")
854 if interface
.get("ip-address"):
855 net_item
["ip_address"] = interface
["ip-address"]
857 if interface
.get("mac-address"):
858 net_item
["mac_address"] = interface
["mac-address"]
860 net_list
.append(net_item
)
862 if interface
.get("mgmt-vnf"):
863 extra_dict
["mgmt_vnf_interface"] = iface_index
864 elif interface
.get("mgmt-interface"):
865 extra_dict
["mgmt_vdu_interface"] = iface_index
870 if target_vdu
.get("cloud-init"):
871 if target_vdu
["cloud-init"] not in vdu2cloud_init
:
872 vdu2cloud_init
[target_vdu
["cloud-init"]] = self
._get
_cloud
_init
(
873 target_vdu
["cloud-init"]
876 cloud_content_
= vdu2cloud_init
[target_vdu
["cloud-init"]]
877 cloud_config
["user-data"] = self
._parse
_jinja
2(
879 target_vdu
.get("additionalParams"),
880 target_vdu
["cloud-init"],
883 if target_vdu
.get("boot-data-drive"):
884 cloud_config
["boot-data-drive"] = target_vdu
.get("boot-data-drive")
888 if target_vdu
.get("ssh-keys"):
889 ssh_keys
+= target_vdu
.get("ssh-keys")
891 if target_vdu
.get("ssh-access-required"):
892 ssh_keys
.append(ro_nsr_public_key
)
895 cloud_config
["key-pairs"] = ssh_keys
898 if target_vdu
.get("virtual-storages"):
900 {"size": disk
["size-of-storage"]}
901 for disk
in target_vdu
["virtual-storages"]
902 if disk
.get("type-of-storage")
903 == "persistent-storage:persistent-storage"
906 extra_dict
["params"] = {
907 "name": "{}-{}-{}-{}".format(
909 vnfr
["member-vnf-index-ref"][:16],
910 target_vdu
["vdu-name"][:32],
911 target_vdu
.get("count-index") or 0,
913 "description": target_vdu
["vdu-name"],
915 "image_id": "TASK-" + image_text
,
916 "flavor_id": "TASK-" + flavor_text
,
917 "net_list": net_list
,
918 "cloud_config": cloud_config
or None,
919 "disk_list": disk_list
,
920 "availability_zone_index": None, # TODO
921 "availability_zone_list": None, # TODO
935 nonlocal db_new_tasks
936 nonlocal tasks_by_target_record_id
939 # ensure all the target_list elements has an "id". If not assign the index as id
940 for target_index
, tl
in enumerate(target_list
):
941 if tl
and not tl
.get("id"):
942 tl
["id"] = str(target_index
)
944 # step 1 items (networks,vdus,...) to be deleted/updated
945 for item_index
, existing_item
in enumerate(existing_list
):
947 (t
for t
in target_list
if t
["id"] == existing_item
["id"]), None
950 for target_vim
, existing_viminfo
in existing_item
.get(
953 if existing_viminfo
is None:
957 target_viminfo
= target_item
.get("vim_info", {}).get(
961 target_viminfo
= None
963 if target_viminfo
is None:
965 self
._assign
_vim
(target_vim
)
966 target_record_id
= "{}.{}".format(
967 db_record
, existing_item
["id"]
971 if target_vim
.startswith("sdn"):
972 # item must be sdn-net instead of net if target_vim is a sdn
974 target_record_id
+= ".sdn"
980 target_record
="{}.{}.vim_info.{}".format(
981 db_record
, item_index
, target_vim
983 target_record_id
=target_record_id
,
985 tasks_by_target_record_id
[target_record_id
] = task
986 db_new_tasks
.append(task
)
988 # TODO check one by one the vims to be created/deleted
990 # step 2 items (networks,vdus,...) to be created
991 for target_item
in target_list
:
994 for item_index
, existing_item
in enumerate(existing_list
):
995 if existing_item
["id"] == target_item
["id"]:
999 db_update
[db_path
+ ".{}".format(item_index
)] = target_item
1000 existing_list
.append(target_item
)
1001 existing_item
= None
1003 for target_vim
, target_viminfo
in target_item
.get(
1006 existing_viminfo
= None
1009 existing_viminfo
= existing_item
.get("vim_info", {}).get(
1013 # TODO check if different. Delete and create???
1014 # TODO delete if not exist
1015 if existing_viminfo
is not None:
1018 target_record_id
= "{}.{}".format(db_record
, target_item
["id"])
1021 if target_vim
.startswith("sdn"):
1022 # item must be sdn-net instead of net if target_vim is a sdn
1024 target_record_id
+= ".sdn"
1026 extra_dict
= process_params(
1027 target_item
, target_viminfo
, target_record_id
1029 self
._assign
_vim
(target_vim
)
1030 task
= _create_task(
1034 target_record
="{}.{}.vim_info.{}".format(
1035 db_record
, item_index
, target_vim
1037 target_record_id
=target_record_id
,
1038 extra_dict
=extra_dict
,
1040 tasks_by_target_record_id
[target_record_id
] = task
1041 db_new_tasks
.append(task
)
1043 if target_item
.get("common_id"):
1044 task
["common_id"] = target_item
["common_id"]
1046 db_update
[db_path
+ ".{}".format(item_index
)] = target_item
1048 def _process_action(indata
):
1049 nonlocal db_new_tasks
1054 if indata
["action"]["action"] == "inject_ssh_key":
1055 key
= indata
["action"].get("key")
1056 user
= indata
["action"].get("user")
1057 password
= indata
["action"].get("password")
1059 for vnf
in indata
.get("vnf", ()):
1060 if vnf
["_id"] not in db_vnfrs
:
1061 raise NsException("Invalid vnf={}".format(vnf
["_id"]))
1063 db_vnfr
= db_vnfrs
[vnf
["_id"]]
1065 for target_vdu
in vnf
.get("vdur", ()):
1066 vdu_index
, vdur
= next(
1069 for i_v
in enumerate(db_vnfr
["vdur"])
1070 if i_v
[1]["id"] == target_vdu
["id"]
1077 "Invalid vdu vnf={}.{}".format(
1078 vnf
["_id"], target_vdu
["id"]
1082 target_vim
, vim_info
= next(
1083 k_v
for k_v
in vdur
["vim_info"].items()
1085 self
._assign
_vim
(target_vim
)
1086 target_record
= "vnfrs:{}:vdur.{}.ssh_keys".format(
1087 vnf
["_id"], vdu_index
1091 "vnfrs:{}:vdur.{}".format(vnf
["_id"], vdur
["id"])
1094 "ip_address": vdur
.get("ip-address"),
1097 "password": password
,
1098 "private_key": db_ro_nsr
["private_key"],
1099 "salt": db_ro_nsr
["_id"],
1100 "schema_version": db_ro_nsr
["_admin"][
1105 task
= _create_task(
1109 target_record
=target_record
,
1110 target_record_id
=None,
1111 extra_dict
=extra_dict
,
1113 db_new_tasks
.append(task
)
1115 with self
.write_lock
:
1116 if indata
.get("action"):
1117 _process_action(indata
)
1119 # compute network differences
1121 step
= "process NS VLDs"
1123 target_list
=indata
["ns"]["vld"] or [],
1124 existing_list
=db_nsr
.get("vld") or [],
1125 db_record
="nsrs:{}:vld".format(nsr_id
),
1126 db_update
=db_nsr_update
,
1129 process_params
=_process_net_params
,
1132 step
= "process NS images"
1134 target_list
=indata
.get("image") or [],
1135 existing_list
=db_nsr
.get("image") or [],
1136 db_record
="nsrs:{}:image".format(nsr_id
),
1137 db_update
=db_nsr_update
,
1140 process_params
=_process_image_params
,
1143 step
= "process NS flavors"
1145 target_list
=indata
.get("flavor") or [],
1146 existing_list
=db_nsr
.get("flavor") or [],
1147 db_record
="nsrs:{}:flavor".format(nsr_id
),
1148 db_update
=db_nsr_update
,
1151 process_params
=_process_flavor_params
,
1155 for vnfr_id
, vnfr
in db_vnfrs
.items():
1156 # vnfr_id need to be set as global variable for among others nested method _process_vdu_params
1157 step
= "process VNF={} VLDs".format(vnfr_id
)
1161 for vnf
in indata
.get("vnf", ())
1162 if vnf
["_id"] == vnfr_id
1166 target_list
= target_vnf
.get("vld") if target_vnf
else None
1168 target_list
=target_list
or [],
1169 existing_list
=vnfr
.get("vld") or [],
1170 db_record
="vnfrs:{}:vld".format(vnfr_id
),
1171 db_update
=db_vnfrs_update
[vnfr
["_id"]],
1174 process_params
=_process_net_params
,
1177 target_list
= target_vnf
.get("vdur") if target_vnf
else None
1178 step
= "process VNF={} VDUs".format(vnfr_id
)
1180 target_list
=target_list
or [],
1181 existing_list
=vnfr
.get("vdur") or [],
1182 db_record
="vnfrs:{}:vdur".format(vnfr_id
),
1183 db_update
=db_vnfrs_update
[vnfr
["_id"]],
1186 process_params
=_process_vdu_params
,
1189 for db_task
in db_new_tasks
:
1190 step
= "Updating database, Appending tasks to ro_tasks"
1191 target_id
= db_task
.pop("target_id")
1192 common_id
= db_task
.get("common_id")
1198 "target_id": target_id
,
1199 "tasks.common_id": common_id
,
1201 update_dict
={"to_check_at": now
, "modified_at": now
},
1202 push
={"tasks": db_task
},
1203 fail_on_empty
=False,
1207 if not self
.db
.set_one(
1210 "target_id": target_id
,
1211 "tasks.target_record": db_task
["target_record"],
1213 update_dict
={"to_check_at": now
, "modified_at": now
},
1214 push
={"tasks": db_task
},
1215 fail_on_empty
=False,
1218 step
= "Updating database, Creating ro_tasks"
1219 db_ro_task
= _create_ro_task(target_id
, db_task
)
1221 self
.db
.create("ro_tasks", db_ro_task
)
1223 step
= "Updating database, nsrs"
1225 self
.db
.set_one("nsrs", {"_id": nsr_id
}, db_nsr_update
)
1227 for vnfr_id
, db_vnfr_update
in db_vnfrs_update
.items():
1229 step
= "Updating database, vnfrs={}".format(vnfr_id
)
1230 self
.db
.set_one("vnfrs", {"_id": vnfr_id
}, db_vnfr_update
)
1234 + "Exit. Created {} ro_tasks; {} tasks".format(
1235 nb_ro_tasks
, len(db_new_tasks
)
1240 {"status": "ok", "nsr_id": nsr_id
, "action_id": action_id
},
1244 except Exception as e
:
1245 if isinstance(e
, (DbException
, NsException
)):
1247 logging_text
+ "Exit Exception while '{}': {}".format(step
, e
)
1250 e
= traceback_format_exc()
1251 self
.logger
.critical(
1252 logging_text
+ "Exit Exception while '{}': {}".format(step
, e
),
1256 raise NsException(e
)
1258 def delete(self
, session
, indata
, version
, nsr_id
, *args
, **kwargs
):
1259 self
.logger
.debug("ns.delete version={} nsr_id={}".format(version
, nsr_id
))
1260 # self.db.del_list({"_id": ro_task["_id"], "tasks.nsr_id.ne": nsr_id})
1262 with self
.write_lock
:
1264 NsWorker
.delete_db_tasks(self
.db
, nsr_id
, None)
1265 except NsWorkerException
as e
:
1266 raise NsException(e
)
1268 return None, None, True
1270 def status(self
, session
, indata
, version
, nsr_id
, action_id
, *args
, **kwargs
):
1271 # self.logger.debug("ns.status version={} nsr_id={}, action_id={} indata={}"
1272 # .format(version, nsr_id, action_id, indata))
1276 ro_tasks
= self
.db
.get_list("ro_tasks", {"tasks.action_id": action_id
})
1277 global_status
= "DONE"
1280 for ro_task
in ro_tasks
:
1281 for task
in ro_task
["tasks"]:
1282 if task
and task
["action_id"] == action_id
:
1283 task_list
.append(task
)
1286 if task
["status"] == "FAILED":
1287 global_status
= "FAILED"
1288 error_text
= "Error at {} {}: {}".format(
1289 task
["action"].lower(),
1291 ro_task
["vim_info"].get("vim_details") or "unknown",
1293 details
.append(error_text
)
1294 elif task
["status"] in ("SCHEDULED", "BUILD"):
1295 if global_status
!= "FAILED":
1296 global_status
= "BUILD"
1301 "status": global_status
,
1302 "details": ". ".join(details
)
1304 else "progress {}/{}".format(done
, total
),
1306 "action_id": action_id
,
1310 return return_data
, None, True
1312 def cancel(self
, session
, indata
, version
, nsr_id
, action_id
, *args
, **kwargs
):
1314 "ns.cancel session={} indata={} version={} nsr_id={}, action_id={}".format(
1315 session
, indata
, version
, nsr_id
, action_id
1319 return None, None, True
1321 def get_deploy(self
, session
, indata
, version
, nsr_id
, action_id
, *args
, **kwargs
):
1322 nsrs
= self
.db
.get_list("nsrs", {})
1326 return_data
.append({"_id": ns
["_id"], "name": ns
["name"]})
1328 return return_data
, None, True
1330 def get_actions(self
, session
, indata
, version
, nsr_id
, action_id
, *args
, **kwargs
):
1331 ro_tasks
= self
.db
.get_list("ro_tasks", {"tasks.nsr_id": nsr_id
})
1334 for ro_task
in ro_tasks
:
1335 for task
in ro_task
["tasks"]:
1336 if task
["action_id"] not in return_data
:
1337 return_data
.append(task
["action_id"])
1339 return return_data
, None, True