+ def onboard(self):
+ """
+ Do all steps to prepare this service to be instantiated
+ :return:
+ """
+ # 1. extract the contents of the package and store them in our catalog
+ self._unpack_service_package()
+ # 2. read in all descriptor files
+ self._load_package_descriptor()
+ self._load_nsd()
+ self._load_vnfd()
+ self._load_docker_files()
+ # 3. prepare container images (e.g. download or build Dockerfile)
+ self._build_images_from_dockerfiles()
+ self._download_predefined_dockerimages()
+
+ LOG.info("On-boarded service: %r" % self.manifest.get("package_name"))
+
+ def start_service(self):
+ """
+ This methods creates and starts a new service instance.
+ It computes placements, iterates over all VNFDs, and starts
+ each VNFD as a Docker container in the data center selected
+ by the placement algorithm.
+ :return:
+ """
+ LOG.info("Starting service %r" % self.uuid)
+ # 1. each service instance gets a new uuid to identify it
+ instance_uuid = str(uuid.uuid4())
+ # build a instances dict (a bit like a NSR :))
+ self.instances[instance_uuid] = dict()
+ self.instances[instance_uuid]["vnf_instances"] = list()
+ # 2. compute placement of this service instance (adds DC names to VNFDs)
+ if not GK_STANDALONE_MODE:
+ self._calculate_placement(FirstDcPlacement)
+ # iterate over all vnfds that we have to start
+ for vnfd in self.vnfds.itervalues():
+ vnfi = None
+ if not GK_STANDALONE_MODE:
+ vnfi = self._start_vnfd(vnfd)
+ self.instances[instance_uuid]["vnf_instances"].append(vnfi)
+ LOG.info("Service started. Instance id: %r" % instance_uuid)
+ return instance_uuid
+
+ def _start_vnfd(self, vnfd):
+ """
+ Start a single VNFD of this service
+ :param vnfd: vnfd descriptor dict
+ :return:
+ """
+ # iterate over all deployment units within each VNFDs
+ for u in vnfd.get("virtual_deployment_units"):
+ # 1. get the name of the docker image to start and the assigned DC
+ docker_name = u.get("vm_image")
+ target_dc = vnfd.get("dc")
+ # 2. perform some checks to ensure we can start the container
+ assert(docker_name is not None)
+ assert(target_dc is not None)
+ if not self._check_docker_image_exists(docker_name):
+ raise Exception("Docker image %r not found. Abort." % docker_name)
+ # 3. do the dc.startCompute(name="foobar") call to run the container
+ # TODO consider flavors, and other annotations
+ vnfi = target_dc.startCompute(GK.get_next_vnf_name(), image=docker_name, flavor_name="small")
+ # 6. store references to the compute objects in self.instances
+ return vnfi
+
+ def _unpack_service_package(self):
+ """
+ unzip *.son file and store contents in CATALOG_FOLDER/services/<service_uuid>/
+ """
+ with zipfile.ZipFile(self.package_file_path, "r") as z:
+ z.extractall(self.package_content_path)
+
+ def _load_package_descriptor(self):
+ """
+ Load the main package descriptor YAML and keep it as dict.
+ :return:
+ """
+ self.manifest = load_yaml(
+ os.path.join(
+ self.package_content_path, "META-INF/MANIFEST.MF"))
+
+ def _load_nsd(self):
+ """
+ Load the entry NSD YAML and keep it as dict.
+ :return:
+ """
+ if "entry_service_template" in self.manifest:
+ nsd_path = os.path.join(
+ self.package_content_path,
+ make_relative_path(self.manifest.get("entry_service_template")))
+ self.nsd = load_yaml(nsd_path)
+ LOG.debug("Loaded NSD: %r" % self.nsd.get("ns_name"))
+
+ def _load_vnfd(self):
+ """
+ Load all VNFD YAML files referenced in MANIFEST.MF and keep them in dict.
+ :return:
+ """
+ if "package_content" in self.manifest:
+ for pc in self.manifest.get("package_content"):
+ if pc.get("content-type") == "application/sonata.function_descriptor":
+ vnfd_path = os.path.join(
+ self.package_content_path,
+ make_relative_path(pc.get("name")))
+ vnfd = load_yaml(vnfd_path)
+ self.vnfds[vnfd.get("vnf_name")] = vnfd
+ LOG.debug("Loaded VNFD: %r" % vnfd.get("vnf_name"))
+
+ def _load_docker_files(self):
+ """
+ Get all paths to Dockerfiles from MANIFEST.MF and store them in dict.
+ :return:
+ """
+ if "package_content" in self.manifest:
+ for df in self.manifest.get("package_content"):
+ if df.get("content-type") == "application/sonata.docker_files":
+ docker_path = os.path.join(
+ self.package_content_path,
+ make_relative_path(df.get("name")))
+ # FIXME: Mapping to docker image names is hardcoded because of the missing mapping in the example package
+ self.local_docker_files[helper_map_docker_name(df.get("name"))] = docker_path
+ LOG.debug("Found Dockerfile: %r" % docker_path)
+
+ def _build_images_from_dockerfiles(self):
+ """
+ Build Docker images for each local Dockerfile found in the package: self.local_docker_files
+ """
+ if GK_STANDALONE_MODE:
+ return # do not build anything in standalone mode
+ dc = DockerClient()
+ LOG.info("Building %d Docker images (this may take several minutes) ..." % len(self.local_docker_files))
+ for k, v in self.local_docker_files.iteritems():
+ for line in dc.build(path=v.replace("Dockerfile", ""), tag=k, rm=False, nocache=False):
+ LOG.debug("DOCKER BUILD: %s" % line)
+ LOG.info("Docker image created: %s" % k)
+
+ def _download_predefined_dockerimages(self):
+ """
+ If the package contains URLs to pre-build Docker images, we download them with this method.
+ """
+ # TODO implement this if we want to be able to download docker images instead of building them