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
27 from jinja2
import Environment
, PackageLoader
28 from osm_im
.validation
import Validation
as validation_im
29 from osm_im
.validation
import ValidationException
30 from osm_im
import im_translation
31 from osmclient
.common
.exceptions
import ClientException
35 class PackageTool(object):
36 def __init__(self
, client
=None):
38 self
._logger
= logging
.getLogger("osmclient")
39 self
._validator
= validation_im()
60 **Create a package descriptor**
63 - package_type: [vnf, ns, nst]
64 - base directory: path of destination folder
65 - package_name: is the name of the package to be created
66 - image: specify the image of the vdu
67 - vcpu: number of virtual cpus of the vdu
68 - memory: amount of memory in MB pf the vdu
69 - storage: amount of storage in GB of the vdu
70 - interfaces: number of interfaces besides management interface
71 - vendor: vendor name of the vnf/ns
72 - detailed: include all possible values for NSD, VNFD, NST
73 - netslice_subnets: number of netslice_subnets for the NST
74 - netslice_vlds: number of virtual link descriptors for the NST
75 - old: flag to create a package using the IM of OSM<9
79 self
._logger
.debug("")
80 # print("location: {}".format(osmclient.__path__))
81 file_loader
= PackageLoader("osmclient")
82 env
= Environment(loader
=file_loader
)
83 if package_type
== "ns":
84 template
= env
.get_template("nsd.yaml.j2" if not old
else "nsd_old.yaml.j2")
90 "interfaces": interfaces
,
93 elif package_type
== "vnf":
94 template
= env
.get_template("vnfd.yaml.j2" if not old
else "vnfd_old.yaml.j2")
100 "interfaces": interfaces
,
105 "detailed": detailed
,
107 elif package_type
== "nst":
108 # TODO: repo-index did not support nst in OSM<9, no changes in template
109 template
= env
.get_template("nst.yaml.j2")
111 "name": package_name
,
113 "interfaces": interfaces
,
114 "netslice_subnets": netslice_subnets
,
115 "netslice_vlds": netslice_vlds
,
116 "detailed": detailed
,
119 raise ClientException(
120 "Wrong descriptor type {}. Options: ns, vnf, nst".format(package_type
)
123 self
._logger
.debug("To be rendered: {}".format(content
))
124 output
= template
.render(content
)
125 self
._logger
.debug(output
)
127 structure
= self
.discover_folder_structure(
128 base_directory
, package_name
, override
130 if structure
.get("folders"):
131 self
.create_folders(structure
["folders"], package_type
)
132 if structure
.get("files"):
133 self
.create_files(structure
["files"], output
, package_type
)
136 def validate(self
, base_directory
, recursive
=True, old_format
=False):
138 **Validate OSM Descriptors given a path**
141 - base_directory is the root path for all descriptors
143 :return: List of dict of validated descriptors. keys: type, path, valid, error
145 self
._logger
.debug("")
148 descriptors_paths
= [
149 f
for f
in glob
.glob(base_directory
+ "/**/*.yaml", recursive
=recursive
)
152 descriptors_paths
= [
153 f
for f
in glob
.glob(base_directory
+ "/*.yaml", recursive
=recursive
)
155 self
._logger
.info("Base directory: {}".format(base_directory
))
156 self
._logger
.info("{} Descriptors found to validate".format(len(descriptors_paths
)))
157 for desc_path
in descriptors_paths
:
158 with
open(desc_path
) as descriptor_file
:
159 descriptor_data
= descriptor_file
.read()
162 # TODO: refactor validation_im.yaml_validation to @staticmethod
163 desc_type
, descriptor_data
= validation_im
.yaml_validation(
164 self
, descriptor_data
166 self
._logger
.debug(f
"Validate {desc_type} {descriptor_data}")
168 if desc_type
== "vnfd" or desc_type
== "nsd":
170 "OSM descriptor '{}' written in an unsupported format. Please update to ETSI SOL006 format".format(
174 self
._logger
.warning(
175 "Package validation skipped. It can still be done with 'osm package-validate --old'"
177 self
._logger
.warning(
178 "Package build can still be done with 'osm package-build --skip-validation'"
180 raise Exception("Not SOL006 format")
181 validation_im
.pyangbind_validation(self
, desc_type
, descriptor_data
)
183 {"type": desc_type
, "path": desc_path
, "valid": "OK", "error": "-"}
185 except Exception as e
:
186 self
._logger
.error(f
"Validation error: {e}", exc_info
=True)
195 self
._logger
.debug(table
[-1])
198 def translate(self
, base_directory
, recursive
=True, dryrun
=False):
200 **Translate OSM Packages given a path**
203 - base_directory is the root path for all packages
205 :return: List of dict of translated packages. keys: current type, new type, path, valid, translated, error
207 self
._logger
.debug("")
210 descriptors_paths
= [
211 f
for f
in glob
.glob(base_directory
+ "/**/*.yaml", recursive
=recursive
)
214 descriptors_paths
= [
215 f
for f
in glob
.glob(base_directory
+ "/*.yaml", recursive
=recursive
)
217 print("Base directory: {}".format(base_directory
))
218 print("{} Descriptors found to validate".format(len(descriptors_paths
)))
219 for desc_path
in descriptors_paths
:
220 with
open(desc_path
) as descriptor_file
:
221 descriptor_data
= descriptor_file
.read()
224 desc_type
, descriptor_data
= validation_im
.yaml_validation(
225 self
, descriptor_data
227 self
._logger
.debug("desc_type: {}".format(desc_type
))
228 self
._logger
.debug("descriptor_data:\n{}".format(descriptor_data
))
229 self
._validator
.pyangbind_validation(desc_type
, descriptor_data
)
230 if not (desc_type
== "vnfd" or desc_type
== "nsd"):
233 "current type": desc_type
,
234 "new type": desc_type
,
242 new_desc_type
= desc_type
244 sol006_model
= yaml
.safe_dump(
245 im_translation
.translate_im_model_to_sol006(
249 default_flow_style
=False,
254 ) = self
._validator
.yaml_validation(sol006_model
)
255 self
._validator
.pyangbind_validation(
256 new_desc_type
, new_descriptor_data
259 with
open(desc_path
, "w") as descriptor_file
:
260 descriptor_file
.write(sol006_model
)
263 "current type": desc_type
,
264 "new type": new_desc_type
,
271 except ValidationException
as ve2
:
274 "current type": desc_type
,
275 "new type": new_desc_type
,
278 "translated": "ERROR",
279 "error": "Error in the post-validation: {}".format(
284 except Exception as e2
:
287 "current type": desc_type
,
288 "new type": new_desc_type
,
291 "translated": "ERROR",
292 "error": "Error in the translation: {}".format(str(e2
)),
295 except ValidationException
as ve
:
298 "current type": desc_type
,
303 "error": "Error in the pre-validation: {}".format(str(ve
)),
306 except Exception as e
:
309 "current type": desc_type
,
319 def descriptor_translate(self
, descriptor_file
):
321 **Translate input descriptor file from Rel EIGHT OSM to SOL006**
324 - base_directory is the root path for all packages
326 :return: YAML descriptor in the new format
328 self
._logger
.debug("")
329 with
open(descriptor_file
, "r") as df
:
330 im_model
= yaml
.safe_load(df
.read())
331 sol006_model
= im_translation
.translate_im_model_to_sol006(im_model
)
332 return yaml
.safe_dump(sol006_model
, indent
=4, default_flow_style
=False)
334 def build(self
, package_folder
, skip_validation
=False, skip_charm_build
=False):
336 **Creates a .tar.gz file given a package_folder**
339 - package_folder: is the name of the folder to be packaged
340 - skip_validation: is the flag to validate or not the descriptors on the folder before build
342 :returns: message result for the build process
344 self
._logger
.debug("")
345 package_folder
= package_folder
.rstrip("/")
346 if not os
.path
.exists("{}".format(package_folder
)):
347 return "Fail, package is not in the specified path"
348 if not skip_validation
:
349 print("Validating package {}".format(package_folder
))
350 results
= self
.validate(package_folder
, recursive
=False)
352 for result
in results
:
353 if result
["valid"] != "OK":
354 raise ClientException(
355 "There was an error validating the file {} with error: {}".format(
356 result
["path"], result
["error"]
359 print("Validation OK")
361 raise ClientException(
362 "No descriptor file found in: {}".format(package_folder
)
364 charm_list
= self
.build_all_charms(package_folder
, skip_charm_build
)
365 return self
.build_tarfile(package_folder
, charm_list
)
367 def calculate_checksum(self
, package_folder
):
369 **Function to calculate the checksum given a folder**
372 - package_folder: is the folder where we have the files to calculate the checksum
375 self
._logger
.debug("")
378 for f
in glob
.glob(package_folder
+ "/**/*.*", recursive
=True)
381 with
open("{}/checksums.txt".format(package_folder
), "w+") as checksum
:
382 for file_item
in files
:
383 if "checksums.txt" in file_item
:
385 # from https://www.quickprogrammingtips.com/python/how-to-calculate-md5-hash-of-a-file-in-python.html
386 md5_hash
= hashlib
.md5()
387 with
open(file_item
, "rb") as f
:
388 # Read and update hash in chunks of 4K
389 for byte_block
in iter(lambda: f
.read(4096), b
""):
390 md5_hash
.update(byte_block
)
391 checksum
.write("{}\t{}\n".format(md5_hash
.hexdigest(), file_item
))
393 def create_folders(self
, folders
, package_type
):
395 **Create folder given a list of folders**
398 - folders: [List] list of folders paths to be created
399 - package_type: is the type of package to be created
402 self
._logger
.debug("")
403 for folder
in folders
:
405 # print("Folder {} == package_type {}".format(folder[1], package_type))
406 if folder
[1] == package_type
:
407 print("Creating folder:\t{}".format(folder
[0]))
408 os
.makedirs(folder
[0])
409 except FileExistsError
:
412 def save_file(self
, file_name
, file_body
):
414 **Create a file given a name and the content**
417 - file_name: is the name of the file with the relative route
418 - file_body: is the content of the file
421 self
._logger
.debug("")
422 print("Creating file: \t{}".format(file_name
))
424 with
open(file_name
, "w+") as f
:
426 except Exception as e
:
427 raise ClientException(e
)
429 def generate_readme(self
):
431 **Creates the README content**
433 :returns: readme content
435 self
._logger
.debug("")
436 return """# Descriptor created by OSM descriptor package generated\n\n**Created on {} **""".format(
437 time
.strftime("%m/%d/%Y, %H:%M:%S", time
.localtime())
440 def generate_cloud_init(self
):
442 **Creates the cloud-init content**
444 :returns: cloud-init content
446 self
._logger
.debug("")
447 return "---\n#cloud-config"
449 def create_files(self
, files
, file_content
, package_type
):
451 **Creates the files given the file list and type**
454 - files: is the list of files structure
455 - file_content: is the content of the descriptor rendered by the template
456 - package_type: is the type of package to filter the creation structure
460 self
._logger
.debug("")
461 for file_item
, file_package
, file_type
in files
:
462 if package_type
== file_package
:
463 if file_type
== "descriptor":
464 self
.save_file(file_item
, file_content
)
465 elif file_type
== "readme":
466 self
.save_file(file_item
, self
.generate_readme())
467 elif file_type
== "cloud_init":
468 self
.save_file(file_item
, self
.generate_cloud_init())
470 def check_files_folders(self
, path_list
, override
):
472 **Find files and folders missing given a directory structure {"folders": [], "files": []}**
475 - path_list: is the list of files and folders to be created
476 - override: is the flag used to indicate the creation of the list even if the file exist to override it
478 :return: Missing paths Dict
480 self
._logger
.debug("")
484 for folder
in path_list
.get("folders"):
485 if not os
.path
.exists(folder
[0]):
486 folders
.append(folder
)
487 missing_paths
["folders"] = folders
489 for file_item
in path_list
.get("files"):
490 if not os
.path
.exists(file_item
[0]) or override
is True:
491 files
.append(file_item
)
492 missing_paths
["files"] = files
496 def build_all_charms(self
, package_folder
, skip_charm_build
):
498 **Read the descriptor file, check that the charms referenced are in the folder and compiles them**
501 - packet_folder: is the location of the package
502 :return: Files and Folders not found. In case of override, it will return all file list
504 self
._logger
.debug("")
506 descriptor_file
= False
507 descriptors_paths
= [f
for f
in glob
.glob(package_folder
+ "/*.yaml")]
508 for file in descriptors_paths
:
509 if file.endswith("nfd.yaml"):
510 descriptor_file
= True
511 listCharms
= self
.charms_search(file, "vnf")
512 if file.endswith("nsd.yaml"):
513 descriptor_file
= True
514 listCharms
= self
.charms_search(file, "ns")
515 print("List of charms in the descriptor: {}".format(listCharms
))
516 if not descriptor_file
:
517 raise ClientException(
518 'Descriptor filename is not correct in: {}. It should end with "nfd.yaml" or "nsd.yaml"'.format(
522 if listCharms
and not skip_charm_build
:
523 for charmName
in listCharms
:
525 "{}/charms/layers/{}".format(package_folder
, charmName
)
528 "Building charm {}/charms/layers/{}".format(
529 package_folder
, charmName
532 self
.charm_build(package_folder
, charmName
)
533 print("Charm built: {}".format(charmName
))
535 "{}/charms/ops/{}".format(package_folder
, charmName
)
537 self
.charmcraft_build(package_folder
, charmName
)
539 if not os
.path
.isdir(
540 "{}/charms/{}".format(package_folder
, charmName
)
542 raise ClientException(
543 "The charm: {} referenced in the descriptor file "
544 "is not present either in {}/charms or in {}/charms/layers".format(
545 charmName
, package_folder
, package_folder
548 self
._logger
.debug("Return list of charms: {}".format(listCharms
))
551 def discover_folder_structure(self
, base_directory
, name
, override
):
553 **Discover files and folders structure for OSM descriptors given a base_directory and name**
556 - base_directory: is the location of the package to be created
557 - name: is the name of the package
558 - override: is the flag used to indicate the creation of the list even if the file exist to override it
559 :return: Files and Folders not found. In case of override, it will return all file list
561 self
._logger
.debug("")
562 prefix
= "{}/{}".format(base_directory
, name
)
565 ("{}_ns".format(prefix
), "ns"),
566 ("{}_ns/icons".format(prefix
), "ns"),
567 ("{}_ns/charms".format(prefix
), "ns"),
568 ("{}_vnf".format(name
), "vnf"),
569 ("{}_vnf/charms".format(prefix
), "vnf"),
570 ("{}_vnf/cloud_init".format(prefix
), "vnf"),
571 ("{}_vnf/images".format(prefix
), "vnf"),
572 ("{}_vnf/icons".format(prefix
), "vnf"),
573 ("{}_vnf/scripts".format(prefix
), "vnf"),
574 ("{}_nst".format(prefix
), "nst"),
575 ("{}_nst/icons".format(prefix
), "nst"),
578 ("{}_ns/{}_nsd.yaml".format(prefix
, name
), "ns", "descriptor"),
579 ("{}_ns/README.md".format(prefix
), "ns", "readme"),
580 ("{}_vnf/{}_vnfd.yaml".format(prefix
, name
), "vnf", "descriptor"),
582 "{}_vnf/cloud_init/cloud-config.txt".format(prefix
),
586 ("{}_vnf/README.md".format(prefix
), "vnf", "readme"),
587 ("{}_nst/{}_nst.yaml".format(prefix
, name
), "nst", "descriptor"),
588 ("{}_nst/README.md".format(prefix
), "nst", "readme"),
591 missing_files_folders
= self
.check_files_folders(files_folders
, override
)
592 # print("Missing files and folders: {}".format(missing_files_folders))
593 return missing_files_folders
595 def charm_build(self
, charms_folder
, build_name
):
597 Build the charms inside the package.
598 params: package_folder is the name of the folder where is the charms to compile.
599 build_name is the name of the layer or interface
601 self
._logger
.debug("")
602 os
.environ
["JUJU_REPOSITORY"] = "{}/charms".format(charms_folder
)
603 os
.environ
["CHARM_LAYERS_DIR"] = "{}/layers".format(
604 os
.environ
["JUJU_REPOSITORY"]
606 os
.environ
["CHARM_INTERFACES_DIR"] = "{}/interfaces".format(
607 os
.environ
["JUJU_REPOSITORY"]
609 os
.environ
["CHARM_BUILD_DIR"] = "{}/charms/builds".format(charms_folder
)
610 if not os
.path
.exists(os
.environ
["CHARM_BUILD_DIR"]):
611 os
.makedirs(os
.environ
["CHARM_BUILD_DIR"])
612 src_folder
= "{}/{}".format(os
.environ
["CHARM_LAYERS_DIR"], build_name
)
613 result
= subprocess
.run(["charm", "build", "{}".format(src_folder
)])
614 if result
.returncode
== 1:
615 raise ClientException("failed to build the charm: {}".format(src_folder
))
616 self
._logger
.verbose("charm {} built".format(src_folder
))
618 def charmcraft_build(self
, package_folder
, charm_name
):
620 Build the charms inside the package (new operator framework charms)
621 params: package_folder is the name of the folder where is the charms to compile.
622 build_name is the name of the layer or interface
624 self
._logger
.debug("Building charm {}".format(charm_name
))
625 src_folder
= f
"{package_folder}/charms/ops/{charm_name}"
626 current_directory
= os
.getcwd()
629 result
= subprocess
.run(["charmcraft", "build"])
630 if result
.returncode
== 1:
631 raise ClientException(
632 "failed to build the charm: {}".format(src_folder
)
634 subprocess
.run(["rm", "-rf", f
"../../{charm_name}"])
635 subprocess
.run(["mv", "build", f
"../../{charm_name}"])
636 self
._logger
.verbose("charm {} built".format(src_folder
))
638 os
.chdir(current_directory
)
640 def build_tarfile(self
, package_folder
, charm_list
=None):
642 Creates a .tar.gz file given a package_folder
643 params: package_folder is the name of the folder to be packaged
644 returns: .tar.gz name
646 self
._logger
.debug("")
649 directory_name
, package_name
= self
.create_temp_dir(
650 package_folder
, charm_list
653 os
.chdir(directory_name
)
654 self
.calculate_checksum(package_name
)
655 with tarfile
.open("{}.tar.gz".format(package_name
), mode
="w:gz") as archive
:
656 print("Adding File: {}".format(package_name
))
657 archive
.add("{}".format(package_name
), recursive
=True)
658 # return "Created {}.tar.gz".format(package_folder)
659 # self.build("{}".format(os.path.basename(package_folder)))
662 created_package
= "{}/{}.tar.gz".format(
663 os
.path
.dirname(package_folder
) or ".", package_name
666 "{}/{}.tar.gz".format(directory_name
, package_name
), created_package
669 "{}/{}/checksums.txt".format(directory_name
, package_name
),
670 "{}/checksums.txt".format(package_folder
),
672 print("Package created: {}".format(created_package
))
673 return created_package
674 except Exception as exc
:
675 raise ClientException(
676 "failure during build of targz file (create temp dir, calculate checksum, "
677 "tar.gz file): {}".format(exc
)
682 shutil
.rmtree(os
.path
.join(package_folder
, "tmp"))
684 def create_temp_dir(self
, package_folder
, charm_list
=None):
686 Method to create a temporary folder where we can move the files in package_folder
688 self
._logger
.debug("")
689 ignore_patterns
= ".gitignore"
690 ignore
= shutil
.ignore_patterns(ignore_patterns
)
691 directory_name
= os
.path
.abspath(package_folder
)
692 package_name
= os
.path
.basename(directory_name
)
693 directory_name
+= "/tmp"
694 os
.makedirs("{}/{}".format(directory_name
, package_name
), exist_ok
=True)
695 self
._logger
.debug("Makedirs DONE: {}/{}".format(directory_name
, package_name
))
696 for item
in os
.listdir(package_folder
):
697 self
._logger
.debug("Item: {}".format(item
))
699 s
= os
.path
.join(package_folder
, item
)
700 d
= os
.path
.join(os
.path
.join(directory_name
, package_name
), item
)
703 os
.makedirs(d
, exist_ok
=True)
704 s_builds
= os
.path
.join(s
, "builds")
705 for charm
in charm_list
:
706 self
._logger
.debug("Copying charm {}".format(charm
))
707 if charm
in os
.listdir(s
):
708 s_charm
= os
.path
.join(s
, charm
)
709 elif charm
in os
.listdir(s_builds
):
710 s_charm
= os
.path
.join(s_builds
, charm
)
712 raise ClientException(
713 "The charm {} referenced in the descriptor file "
714 "could not be found in {}/charms or in {}/charms/builds".format(
715 charm
, package_folder
, package_folder
718 d_temp
= os
.path
.join(d
, charm
)
720 "Copying tree: {} -> {}".format(s_charm
, d_temp
)
723 s_charm
, d_temp
, symlinks
=True, ignore
=ignore
725 self
._logger
.debug("DONE")
727 self
._logger
.debug("Copying tree: {} -> {}".format(s
, d
))
728 shutil
.copytree(s
, d
, symlinks
=True, ignore
=ignore
)
729 self
._logger
.debug("DONE")
731 if item
in ignore_patterns
:
733 self
._logger
.debug("Copying file: {} -> {}".format(s
, d
))
735 self
._logger
.debug("DONE")
736 return directory_name
, package_name
738 def charms_search(self
, descriptor_file
, desc_type
):
740 "descriptor_file: {}, desc_type: {}".format(descriptor_file
, desc_type
)
742 with
open("{}".format(descriptor_file
)) as yaml_desc
:
743 descriptor_dict
= yaml
.safe_load(yaml_desc
)
744 # self._logger.debug("\n"+yaml.safe_dump(descriptor_dict, indent=4, default_flow_style=False))
749 "vnfd:vnfd-catalog" in descriptor_dict
750 or "vnfd-catalog" in descriptor_dict
755 "nsd:nsd-catalog" in descriptor_dict
756 or "nsd-catalog" in descriptor_dict
759 charms_list
= self
._charms
_search
_on
_osm
_im
_dict
(
760 descriptor_dict
, desc_type
763 if desc_type
== "ns":
764 get_charm_list
= self
._charms
_search
_on
_nsd
_sol
006_dict
765 elif desc_type
== "vnf":
766 get_charm_list
= self
._charms
_search
_on
_vnfd
_sol
006_dict
768 raise Exception("Bad descriptor type")
769 charms_list
= get_charm_list(descriptor_dict
)
772 def _charms_search_on_osm_im_dict(self
, osm_im_dict
, desc_type
):
773 self
._logger
.debug("")
775 for k1
, v1
in osm_im_dict
.items():
776 for k2
, v2
in v1
.items():
778 if "{}-configuration".format(desc_type
) in entry
:
779 vnf_config
= entry
["{}-configuration".format(desc_type
)]
780 for k3
, v3
in vnf_config
.items():
782 charms_list
.append((v3
["charm"]))
786 if "vdu-configuration" in vdu
:
787 for k4
, v4
in vdu
["vdu-configuration"].items():
789 charms_list
.append((v4
["charm"]))
792 def _charms_search_on_vnfd_sol006_dict(self
, sol006_dict
):
793 self
._logger
.debug("")
795 dfs
= sol006_dict
.get("vnfd", {}).get("df", [])
798 df
.get("lcm-operations-configuration", {})
799 .get("operate-vnf-op-config", {})
802 if day_1_2s
is not None:
803 for day_1_2
in day_1_2s
:
804 exec_env_list
= day_1_2
.get("execution-environment-list", [])
805 for exec_env
in exec_env_list
:
806 if "juju" in exec_env
and "charm" in exec_env
["juju"]:
807 charms_list
.append(exec_env
["juju"]["charm"])
810 def _charms_search_on_nsd_sol006_dict(self
, sol006_dict
):
811 self
._logger
.debug("")
813 nsd_list
= sol006_dict
.get("nsd", {}).get("nsd", [])
815 charm
= nsd
.get("ns-configuration", {}).get("juju", {}).get("charm")
817 charms_list
.append(charm
)