Remove config template from use in ping-pong
[osm/SO.git] / examples / ping_pong_ns / rift / mano / examples / ping_pong_nsd.py
index 5348e00..e0a6848 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python3
 
-# 
+#
 #   Copyright 2016 RIFT.IO Inc
 #
 #   Licensed under the Apache License, Version 2.0 (the "License");
@@ -120,6 +120,7 @@ class VirtualNetworkFunction(ManoDescriptor):
         self.vnfd = None
         self.instance_count = instance_count
         self._placement_groups = []
+        self.use_vnf_init_conf = False
         super(VirtualNetworkFunction, self).__init__(name)
 
     def add_placement_group(self, group):
@@ -169,6 +170,10 @@ class VirtualNetworkFunction(ManoDescriptor):
             monp.http_endpoint_ref = endpoint
             vnfd.monitoring_param.append(monp)
 
+        if 'pong_' in self.name:
+            self.add_pong_config()
+        else:
+            self.add_ping_config() 
 
         for i in range(num_vms):
             # VDU Specification
@@ -214,12 +219,18 @@ class VirtualNetworkFunction(ManoDescriptor):
                 node = vdu.guest_epa.numa_node_policy.node.add()
                 node.id = 0
                 node.memory_mb = 512
-                node.vcpu = [0, 1]
+                vcpu = node.vcpu.add()
+                vcpu.id = 0
+                vcpu = node.vcpu.add()
+                vcpu.id = 1
 
                 node = vdu.guest_epa.numa_node_policy.node.add()
                 node.id = 1
                 node.memory_mb = 512
-                node.vcpu = [2, 3]
+                vcpu = node.vcpu.add()
+                vcpu.id = 2
+                vcpu = node.vcpu.add()
+                vcpu.id = 3
 
                 # specify the vswitch EPA
                 vdu.vswitch_epa.ovs_acceleration = 'DISABLED'
@@ -254,7 +265,8 @@ class VirtualNetworkFunction(ManoDescriptor):
                     internal_cp.name = cp_name + "/icp{}".format(i)
                     internal_cp.id = cp_name + "/icp{}".format(i)
                     internal_cp.type_yang = 'VPORT'
-                    internal_vlds[i].internal_connection_point_ref.append(internal_cp.id)
+                    ivld_cp = internal_vlds[i].internal_connection_point_ref.add()
+                    ivld_cp.id_ref = internal_cp.id
 
                     internal_interface = vdu.internal_interface.add()
                     internal_interface.name = 'fab%d' % i
@@ -290,6 +302,46 @@ class VirtualNetworkFunction(ManoDescriptor):
                     member_vdu.member_vdu_ref = vdu.id
 
 
+    def add_ping_config(self):
+        vnfd = self.descriptor.vnfd[0]
+        # Add vnf configuration
+        vnf_config = vnfd.vnf_configuration
+
+        # vnf_config.config_attributes.config_delay = 10
+
+        # Select "script" configuration
+        vnf_config.script.script_type = 'bash'
+
+        # Add initial config primitive
+        vnf_config.create_initial_config_primitive()
+        init_config = VnfdYang.InitialConfigPrimitive.from_dict(
+              {
+                  "seq": 1,
+                  "name": "Ping config",
+                  "user_defined_script": "ping_initial_config.py",
+              }
+        )
+        vnf_config.initial_config_primitive.append(init_config)
+
+    def add_pong_config(self):
+        vnfd = self.descriptor.vnfd[0]
+        # Add vnf configuration
+        vnf_config = vnfd.vnf_configuration
+
+        # Select "script" configuration
+        vnf_config.script.script_type = 'bash'
+
+        # Add initial config primitive
+        vnf_config.create_initial_config_primitive()
+        init_config = VnfdYang.InitialConfigPrimitive.from_dict(
+              {
+                  "seq": 1,
+                  "name": "Pong config",
+                  "user_defined_script": "pong_initial_config.py",
+              }
+        )
+        vnf_config.initial_config_primitive.append(init_config)
+
     def write_to_file(self, outdir, output_format):
         dirpath = "%s/%s" % (outdir, self.name)
         if not os.path.exists(dirpath):
@@ -317,212 +369,58 @@ class VirtualNetworkFunction(ManoDescriptor):
         with open(script_file, "w") as f:
             f.write("{}".format(cfg))
 
+        self.add_config_scripts(outdir)
 
-class NetworkService(ManoDescriptor):
-    def __init__(self, name):
-        super(NetworkService, self).__init__(name)
-        self._scale_groups = []
-        self.vnfd_config = {}
-        self._placement_groups = []
+        # Copy the vnf_init_config script
+        if self.use_vnf_init_conf and ('ping' in self.name):
+            script_name = 'ping_set_rate.py'
 
-    def ping_config(self, mano_ut, use_ns_init_conf):
-        suffix = ''
-        if mano_ut:
-            ping_cfg = r'''
-#!/bin/bash
+            src_path = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
+            script_src = os.path.join(src_path, script_name)
+            if not os.path.exists(script_src):
+                src_path = os.path.join(os.environ['RIFT_ROOT'],
+                                        'modules/core/mano/examples/ping_pong_ns/rift/mano/examples')
+                script_src = os.path.join(src_path, script_name)
+
+            dest_path = os.path.join(outdir, self.name, 'scripts')
+            os.makedirs(dest_path, exist_ok=True)
+
+            shutil.copy2(script_src, dest_path)
+   
+    def add_config_scripts(self, outdir):
+        dest_path = os.path.join(outdir, self.name, 'scripts')
+        try:
+            os.makedirs(dest_path)
+        except OSError:
+            if not os.path.isdir(dest_path):
+                raise
 
-echo "!!!!!!!! Executed ping Configuration !!!!!!!!!"
-            '''
+        if 'pong_' in self.name:
+            scripts = ['pong_initial_config.py']
         else:
-            ping_cfg = r'''
-#!/bin/bash
-
-# Rest API config
-ping_mgmt_ip='<rw_mgmt_ip>'
-ping_mgmt_port=18888
-
-# VNF specific configuration
-pong_server_ip='<rw_connection_point_name pong_vnfd%s/cp0>'
-ping_rate=5
-server_port=5555
-
-# Make rest API calls to configure VNF
-curl -D /dev/stdout \
-    -H "Accept: application/vnd.yang.data+xml" \
-    -H "Content-Type: application/vnd.yang.data+json" \
-    -X POST \
-    -d "{\"ip\":\"$pong_server_ip\", \"port\":$server_port}" \
-    http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/server
-rc=$?
-if [ $rc -ne 0 ]
-then
-    echo "Failed to set server info for ping!"
-    exit $rc
-fi
-
-curl -D /dev/stdout \
-    -H "Accept: application/vnd.yang.data+xml" \
-    -H "Content-Type: application/vnd.yang.data+json" \
-    -X POST \
-    -d "{\"rate\":$ping_rate}" \
-    http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/rate
-rc=$?
-if [ $rc -ne 0 ]
-then
-    echo "Failed to set ping rate!"
-    exit $rc
-fi
-
-''' % suffix
-            if use_ns_init_conf:
-                ping_cfg += "exit 0\n"
-            else:
-                ping_cfg +='''
-output=$(curl -D /dev/stdout \
-    -H "Accept: application/vnd.yang.data+xml" \
-    -H "Content-Type: application/vnd.yang.data+json" \
-    -X POST \
-    -d "{\"enable\":true}" \
-    http://${ping_mgmt_ip}:${ping_mgmt_port}/api/v1/ping/adminstatus/state)
-if [[ $output == *"Internal Server Error"* ]]
-then
-    echo $output
-    exit 3
-else
-    echo $output
-fi
-
-exit 0
-'''
-        return ping_cfg
+            scripts = ['ping_initial_config.py']
 
-    def pong_config(self, mano_ut, use_ns_init_conf):
-        suffix = ''
-        if mano_ut:
-            pong_cfg = r'''
-#!/bin/bash
-
-echo "!!!!!!!! Executed pong Configuration !!!!!!!!!"
-            '''
-        else:
-            pong_cfg = r'''
-#!/bin/bash
-
-# Rest API configuration
-pong_mgmt_ip='<rw_mgmt_ip>'
-pong_mgmt_port=18889
-# username=<rw_username>
-# password=<rw_password>
-
-# VNF specific configuration
-pong_server_ip='<rw_connection_point_name pong_vnfd%s/cp0>'
-server_port=5555
-
-# Make Rest API calls to configure VNF
-curl -D /dev/stdout \
-    -H "Accept: application/vnd.yang.data+xml" \
-    -H "Content-Type: application/vnd.yang.data+json" \
-    -X POST \
-    -d "{\"ip\":\"$pong_server_ip\", \"port\":$server_port}" \
-    http://${pong_mgmt_ip}:${pong_mgmt_port}/api/v1/pong/server
-rc=$?
-if [ $rc -ne 0 ]
-then
-    echo "Failed to set server(own) info for pong!"
-    exit $rc
-fi
-
-''' % suffix
-
-            if use_ns_init_conf:
-                pong_cfg += "exit 0\n"
-            else:
-                pong_cfg +='''
-curl -D /dev/stdout \
-    -H "Accept: application/vnd.yang.data+xml" \
-    -H "Content-Type: application/vnd.yang.data+json" \
-    -X POST \
-    -d "{\"enable\":true}" \
-    http://${pong_mgmt_ip}:${pong_mgmt_port}/api/v1/pong/adminstatus/state
-rc=$?
-if [ $rc -ne 0 ]
-then
-    echo "Failed to enable pong service!"
-    exit $rc
-fi
-
-exit 0
-'''
-        return pong_cfg
+        for script_name in scripts:
+            src_path = os.path.dirname(os.path.abspath(
+                os.path.realpath(__file__)))
+            script_src = os.path.join(src_path, script_name)
+            if not os.path.exists(script_src):
+                src_path = os.path.join(os.environ['RIFT_ROOT'],
+                                        'modules/core/mano/examples/'
+                                        'ping_pong_ns/rift/mano/examples')
+                script_src = os.path.join(src_path, script_name)
 
-    def pong_fake_juju_config(self, vnf_config):
+            shutil.copy2(script_src, dest_path) 
 
-        if vnf_config:
-            # Select "script" configuration
-            vnf_config.juju.charm = 'clearwater-aio-proxy'
 
-            # Set the initital-config
-            vnf_config.create_initial_config_primitive()
-            init_config = VnfdYang.InitialConfigPrimitive.from_dict({
-                "seq": 1,
-                "name": "config",
-                "parameter": [
-                    {"name": "proxied_ip", "value": "<rw_mgmt_ip>"},
-                ]
-            })
-            vnf_config.initial_config_primitive.append(init_config)
-
-            init_config_action = VnfdYang.InitialConfigPrimitive.from_dict({
-                "seq": 2,
-                "name": "action1",
-                "parameter": [
-                    {"name": "Pong Connection Point", "value": "pong_vnfd/cp0"},
-                ]
-            })
-            vnf_config.initial_config_primitive.append(init_config_action)
-            init_config_action = VnfdYang.InitialConfigPrimitive.from_dict({
-                "seq": 3,
-                "name": "action2",
-                "parameter": [
-                    {"name": "Ping Connection Point", "value": "ping_vnfd/cp0"},
-                ]
-            })
-            vnf_config.initial_config_primitive.append(init_config_action)
-
-            # Config parameters can be taken from config.yaml and
-            # actions from actions.yaml in the charm
-            # Config to set the home domain
-            vnf_config.create_service_primitive()
-            config = VnfdYang.ServicePrimitive.from_dict({
-                "name": "config",
-                "parameter": [
-                    {"name": "home_domain", "data_type": "STRING"},
-                    {"name": "base_number", "data_type": "STRING"},
-                    {"name": "number_count", "data_type": "INTEGER"},
-                    {"name": "password", "data_type": "STRING"},
-                ]
-            })
-            vnf_config.service_primitive.append(config)
-
-            config = VnfdYang.ServicePrimitive.from_dict({
-                "name": "create-update-user",
-                # "user-defined-script":"/tmp/test.py",
-                "parameter": [
-                    {"name": "number", "data_type": "STRING", "mandatory": True},
-                    {"name": "password", "data_type": "STRING", "mandatory": True},
-                ]
-            })
-            vnf_config.service_primitive.append(config)
-
-            config = VnfdYang.ServicePrimitive.from_dict({
-                "name": "delete-user",
-                "parameter": [
-                    {"name": "number", "data_type": "STRING", "mandatory": True},
-                ]
-            })
-            vnf_config.service_primitive.append(config)
+class NetworkService(ManoDescriptor):
+    def __init__(self, name):
+        super(NetworkService, self).__init__(name)
+        self._scale_groups = []
+        self.vnfd_config = {}
+        self._placement_groups = []
 
-    def default_config(self, const_vnfd, vnfd, mano_ut, use_ns_init_conf):
+    def default_config(self, vnfd):
         vnf_config = vnfd.vnfd.vnf_configuration
 
         vnf_config.config_attributes.config_priority = 0
@@ -530,22 +428,8 @@ exit 0
 
         # Select "script" configuration
         vnf_config.script.script_type = 'bash'
+        vnf_config.config_template = "#!/bin/bash"
 
-        if vnfd.name == 'pong_vnfd' or vnfd.name == 'pong_vnfd_with_epa' or vnfd.name == 'pong_vnfd_aws':
-            vnf_config.config_attributes.config_priority = 1
-            vnf_config.config_template = self.pong_config(mano_ut, use_ns_init_conf)
-            # First priority config delay will delay the entire NS config delay
-            if mano_ut is False:
-                vnf_config.config_attributes.config_delay = 60
-            else:
-                # This is PONG and inside mano_ut
-                # This is test only
-                vnf_config.config_attributes.config_delay = 10
-                # vnf_config.config_template = self.pong_config(vnf_config, use_ns_init_conf)
-
-        if vnfd.name == 'ping_vnfd' or vnfd.name == 'ping_vnfd_with_epa' or vnfd.name == 'ping_vnfd_aws':
-            vnf_config.config_attributes.config_priority = 2
-            vnf_config.config_template = self.ping_config(mano_ut, use_ns_init_conf)
 
     def ns_config(self, nsd, vnfd_list, mano_ut):
         # Used by scale group
@@ -608,11 +492,14 @@ exit 0
 
 
 
-    def compose(self, vnfd_list, cpgroup_list, mano_ut, use_ns_init_conf=True):
+    def compose(self, vnfd_list, cpgroup_list, mano_ut,
+                use_ns_init_conf=True,
+                use_vnf_init_conf=True,):
 
         if mano_ut:
             # Disable NS initial config primitive
-            use_ns_init_conf=False
+            use_ns_init_conf = False
+            use_vnf_init_conf = False
 
         self.descriptor = RwNsdYang.YangData_Nsd_NsdCatalog()
         self.id = str(uuid.uuid1())
@@ -637,7 +524,7 @@ exit 0
         ip_profile.ip_profile_params.ip_version = "ipv4"
         ip_profile.ip_profile_params.subnet_address = "31.31.31.0/24"
         ip_profile.ip_profile_params.gateway_address = "31.31.31.210"
-        
+
         vld_id = 1
         for cpgroup in cpgroup_list:
             vld = nsd.vld.add()
@@ -671,8 +558,7 @@ exit 0
                     constituent_vnfd.start_by_default = False
 
                 constituent_vnfd.vnfd_id_ref = vnfd.descriptor.vnfd[0].id
-                self.default_config(constituent_vnfd, vnfd, mano_ut,
-                                    use_ns_init_conf,)
+                self.default_config(vnfd)
                 member_vnf_index += 1
 
         # Enable config primitives if either mano_ut or
@@ -885,6 +771,7 @@ def generate_ping_pong_descriptors(fmt="json",
                                    ex_pong_userdata=None,
                                    use_placement_group=True,
                                    use_ns_init_conf=True,
+                                   use_vnf_init_conf=True,
                                    ):
     # List of connection point groups
     # Each connection point group refers to a virtual link
@@ -895,6 +782,7 @@ def generate_ping_pong_descriptors(fmt="json",
 
     suffix = ''
     ping = VirtualNetworkFunction("ping_vnfd%s" % (suffix), pingcount)
+    ping.use_vnf_init_conf = use_vnf_init_conf
 
     if use_placement_group:
         ### Add group name Eris
@@ -1034,7 +922,8 @@ def generate_ping_pong_descriptors(fmt="json",
     nsd_catalog.compose(vnfd_list,
                         cpgroup_list,
                         mano_ut,
-                        use_ns_init_conf=use_ns_init_conf,)
+                        use_ns_init_conf=use_ns_init_conf,
+                        use_vnf_init_conf=use_vnf_init_conf,)
 
     if write_to_file:
         ping.write_to_file(out_dir, ping_fmt if ping_fmt is not None else fmt)