New options for nfpkg-create: override-epa, override-nonepa, override-paravirt 94/8794/3
authorgarciadeblas <gerardo.garciadeblas@telefonica.com>
Thu, 16 Apr 2020 22:17:28 +0000 (22:17 +0000)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Mon, 20 Apr 2020 08:16:17 +0000 (08:16 +0000)
Change-Id: I8277b2f3734936090155bd5dd0b805b0a4410c23
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
osmclient/common/package_tool.py
osmclient/scripts/osm.py
osmclient/sol005/vnfd.py

index f35a7f3..652dbc2 100644 (file)
@@ -276,10 +276,10 @@ class PackageTool(object):
         descriptor_file = False
         descriptors_paths = [f for f in glob.glob(package_folder + "/*.yaml")]
         for file in descriptors_paths:
-            if 'nfd.yaml' in file:
+            if file.endswith('nfd.yaml'):
                 descriptor_file = True
                 listCharms = self.charms_search(file, 'vnf')
-            if 'nsd.yaml' in file:
+            if file.endswith('nsd.yaml'):
                 descriptor_file = True
                 listCharms = self.charms_search(file, 'ns')
         print("List of charms in the descriptor: {}".format(listCharms))
index d405d6d..23b1ee4 100755 (executable)
@@ -1446,11 +1446,13 @@ def nsd_create2(ctx, filename, overwrite, skip_charm_build):
     nsd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
 
 
-def vnfd_create(ctx, filename, overwrite, skip_charm_build):
+def vnfd_create(ctx, filename, overwrite, skip_charm_build, override_epa, override_nonepa, override_paravirt):
     logger.debug("")
     # try:
     check_client_version(ctx.obj, ctx.command.name)
-    ctx.obj.vnfd.create(filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
+    ctx.obj.vnfd.create(filename, overwrite=overwrite, skip_charm_build=skip_charm_build,
+                        override_epa=override_epa, override_nonepa=override_nonepa,
+                        override_paravirt=override_paravirt)
     # except ClientException as e:
     #     print(str(e))
     #     exit(1)
@@ -1465,14 +1467,21 @@ def vnfd_create(ctx, filename, overwrite, skip_charm_build):
                    '"key1.key2...=value[;key3...=value;...]"')
 @click.option('--skip-charm-build', default=False, is_flag=True,
               help='The charm will not be compiled, it is assumed to already exist')
-@click.pass_context
-def vnfd_create1(ctx, filename, overwrite, skip_charm_build):
+@click.option('--override-epa', required=False, default=False, is_flag=True,
+              help='adds guest-epa parameters to all VDU')
+@click.option('--override-nonepa', required=False, default=False, is_flag=True,
+              help='removes all guest-epa parameters from all VDU')
+@click.option('--override-paravirt', required=False, default=False, is_flag=True,
+              help='overrides all VDU interfaces to PARAVIRT')
+@click.pass_context
+def vnfd_create1(ctx, filename, overwrite, skip_charm_build, override_epa, override_nonepa, override_paravirt):
     """creates a new VNFD/VNFpkg
 
     FILENAME: VNFD yaml file or VNFpkg tar.gz file
     """
     logger.debug("")
-    vnfd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
+    vnfd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build,
+                override_epa=override_epa, override_nonepa=override_nonepa, override_paravirt=override_paravirt)
 
 
 @cli_osm.command(name='vnfpkg-create', short_help='creates a new VNFD/VNFpkg')
@@ -1484,14 +1493,21 @@ def vnfd_create1(ctx, filename, overwrite, skip_charm_build):
                    '"key1.key2...=value[;key3...=value;...]"')
 @click.option('--skip-charm-build', default=False, is_flag=True,
               help='The charm will not be compiled, it is assumed to already exist')
-@click.pass_context
-def vnfd_create2(ctx, filename, overwrite, skip_charm_build):
+@click.option('--override-epa', required=False, default=False, is_flag=True,
+              help='adds guest-epa parameters to all VDU')
+@click.option('--override-nonepa', required=False, default=False, is_flag=True,
+              help='removes all guest-epa parameters from all VDU')
+@click.option('--override-paravirt', required=False, default=False, is_flag=True,
+              help='overrides all VDU interfaces to PARAVIRT')
+@click.pass_context
+def vnfd_create2(ctx, filename, overwrite, skip_charm_build, override_epa, override_nonepa, override_paravirt):
     """creates a new VNFD/VNFpkg
 
     FILENAME: NF Package Folder, NF Descriptor yaml file or NFpkg tar.gz file
     """
     logger.debug("")
-    vnfd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
+    vnfd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build,
+                override_epa=override_epa, override_nonepa=override_nonepa, override_paravirt=override_paravirt)
 
 
 @cli_osm.command(name='nfpkg-create', short_help='creates a new NFpkg')
@@ -1503,14 +1519,21 @@ def vnfd_create2(ctx, filename, overwrite, skip_charm_build):
                    '"key1.key2...=value[;key3...=value;...]"')
 @click.option('--skip-charm-build', default=False, is_flag=True,
               help='The charm will not be compiled, it is assumed to already exist')
-@click.pass_context
-def nfpkg_create(ctx, filename, overwrite, skip_charm_build):
+@click.option('--override-epa', required=False, default=False, is_flag=True,
+              help='adds guest-epa parameters to all VDU')
+@click.option('--override-nonepa', required=False, default=False, is_flag=True,
+              help='removes all guest-epa parameters from all VDU')
+@click.option('--override-paravirt', required=False, default=False, is_flag=True,
+              help='overrides all VDU interfaces to PARAVIRT')
+@click.pass_context
+def nfpkg_create(ctx, filename, overwrite, skip_charm_build, override_epa, override_nonepa, override_paravirt):
     """creates a new NFpkg
 
     FILENAME: NF Package Folder, NF Descriptor yaml file or NFpkg tar.gz filems to build
     """
     logger.debug("")
-    vnfd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build)
+    vnfd_create(ctx, filename, overwrite=overwrite, skip_charm_build=skip_charm_build,
+                override_epa=override_epa, override_nonepa=override_nonepa, override_paravirt=override_paravirt)
 
 
 @cli_osm.command(name='ns-create', short_help='creates a new Network Service instance')
index 606fe64..8307088 100644 (file)
@@ -22,11 +22,14 @@ from osmclient.common.exceptions import NotFound
 from osmclient.common.exceptions import ClientException
 from osmclient.common import utils
 import json
+import yaml
 import magic
 from os.path import basename
 import logging
 import os.path
-#from os import stat
+from urllib.parse import quote
+import tarfile
+from osm_im.validation import Validation as validation_im
 
 
 class Vnfd(object):
@@ -137,13 +140,16 @@ class Vnfd(object):
             #         msg = resp
             raise ClientException("failed to delete vnfd {} - {}".format(name, msg))
 
-    def create(self, filename, overwrite=None, update_endpoint=None, skip_charm_build=False):
+    def create(self, filename, overwrite=None, update_endpoint=None, skip_charm_build=False,
+               override_epa=False, override_nonepa=False, override_paravirt=False):
         self._logger.debug("")
         if os.path.isdir(filename):
             filename = filename.rstrip('/')
             filename = self._client.package_tool.build(filename, skip_validation=False, skip_charm_build=skip_charm_build)
             print('Uploading package {}'.format(filename))
-            self.create(filename, overwrite=overwrite, update_endpoint=update_endpoint)
+            self.create(filename, overwrite=overwrite, update_endpoint=update_endpoint,
+                        override_epa=override_epa, override_nonepa=override_nonepa,
+                        override_paravirt=override_paravirt)
         else:
             self._client.get_token()
             mime_type = magic.from_file(filename, mime=True)
@@ -152,7 +158,7 @@ class Vnfd(object):
                     "Unexpected MIME type for file {}: MIME type {}".format(
                         filename, mime_type)
                 )
-            headers= self._client._headers
+            headers = self._client._headers
             headers['Content-Filename'] = basename(filename)
             if mime_type in ['application/yaml', 'text/plain', 'application/json']:
                 headers['Content-Type'] = 'text/plain'
@@ -168,6 +174,59 @@ class Vnfd(object):
                          "Unexpected MIME type for file {}: MIME type {}".format(
                              filename, mime_type)
                       )
+            special_ow_string = ''
+            if override_epa or override_nonepa or override_paravirt:
+                # If override for EPA, non-EPA or paravirt is required, get the descriptor data 
+                descriptor_data = None
+                if mime_type in ['application/yaml', 'text/plain', 'application/json']:
+                    with open(filename) as df:
+                        descriptor_data = df.read()
+                elif mime_type in ['application/gzip', 'application/x-gzip']:
+                    tar_object = tarfile.open(filename, "r:gz")
+                    descriptor_list = []
+                    for member in tar_object:
+                        if member.isreg():
+                            if '/' not in os.path.dirname(member.name) and member.name.endswith('.yaml'):
+                                descriptor_list.append(member.name)
+                    if len(descriptor_list) > 1:
+                        raise ClientException('Found more than one potential descriptor in the tar.gz file')
+                    elif len(descriptor_list) == 0:
+                        raise ClientException('No descriptor was found in the tar.gz file')
+                    with tar_object.extractfile(descriptor_list[0]) as df:
+                        descriptor_data = df.read()
+                    tar_object.close()
+                if not descriptor_data:
+                    raise ClientException('Descriptor could not be read')
+                desc_type, vnfd = validation_im.yaml_validation(self, descriptor_data)
+                validation_im.pyangbind_validation(self, desc_type, vnfd)
+                vnfd = yaml.safe_load(descriptor_data)
+                vdu_list = []
+                for k in vnfd:
+                    # Get only the first descriptor in case there are many in the yaml file
+                    # k can be vnfd:vnfd-catalog or vnfd-catalog. This check is skipped
+                    vdu_list = vnfd[k]['vnfd'][0]['vdu']
+                    break;
+                for vdu_number, vdu in enumerate(vdu_list):
+                    if override_epa:
+                        guest_epa = {}
+                        guest_epa["mempage-size"] = "LARGE"
+                        guest_epa["cpu-pinning-policy"] = "DEDICATED"
+                        guest_epa["cpu-thread-pinning-policy"] = "PREFER"
+                        guest_epa["numa-node-policy"] = {}
+                        guest_epa["numa-node-policy"]["node-cnt"] = 1
+                        guest_epa["numa-node-policy"]["mem-policy"] = "STRICT"
+                        #guest_epa["numa-node-policy"]["node"] = []
+                        #guest_epa["numa-node-policy"]["node"].append({"id": "0", "paired-threads": {"num-paired-threads": 1} })
+                        special_ow_string = "{}vdu.{}.guest-epa={};".format(special_ow_string,vdu_number,quote(yaml.safe_dump(guest_epa)))
+                        headers['Query-String-Format'] = 'yaml'
+                    if override_nonepa:
+                        special_ow_string = "{}vdu.{}.guest-epa=;".format(special_ow_string,vdu_number)
+                    if override_paravirt:
+                        for iface_number in range(len(vdu['interface'])):
+                            special_ow_string = "{}vdu.{}.interface.{}.virtual-interface.type=PARAVIRT;".format(
+                                                special_ow_string,vdu_number,iface_number)
+                special_ow_string = special_ow_string.rstrip(";")
+
             headers["Content-File-MD5"] = utils.md5(filename)
             http_header = ['{}: {}'.format(key,val)
                              for (key,val) in list(headers.items())]
@@ -176,6 +235,11 @@ class Vnfd(object):
                 http_code, resp = self._http.put_cmd(endpoint=update_endpoint, filename=filename)
             else:
                 ow_string = ''
+                if special_ow_string:
+                    if overwrite:
+                        overwrite = "{};{}".format(overwrite,special_ow_string)
+                    else:
+                        overwrite = special_ow_string
                 if overwrite:
                     ow_string = '?{}'.format(overwrite)
                 self._apiResource = '/vnf_packages_content'