Remove config template from use in ping-pong
[osm/SO.git] / examples / ping_pong_ns / rift / mano / examples / ping_pong_nsd.py
index ef2dd90..e0a6848 100755 (executable)
@@ -170,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
@@ -298,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):
@@ -325,6 +369,8 @@ class VirtualNetworkFunction(ManoDescriptor):
         with open(script_file, "w") as f:
             f.write("{}".format(cfg))
 
+        self.add_config_scripts(outdir)
+
         # Copy the vnf_init_config script
         if self.use_vnf_init_conf and ('ping' in self.name):
             script_name = 'ping_set_rate.py'
@@ -340,6 +386,31 @@ class VirtualNetworkFunction(ManoDescriptor):
             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
+
+        if 'pong_' in self.name:
+            scripts = ['pong_initial_config.py']
+        else:
+            scripts = ['ping_initial_config.py']
+
+        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)
+
+            shutil.copy2(script_src, dest_path) 
 
 
 class NetworkService(ManoDescriptor):
@@ -349,209 +420,7 @@ class NetworkService(ManoDescriptor):
         self.vnfd_config = {}
         self._placement_groups = []
 
-    def ping_config(self, mano_ut, use_ns_init_conf, use_vnf_init_conf):
-        suffix = ''
-        if mano_ut:
-            ping_cfg = r'''
-#!/bin/bash
-
-echo "!!!!!!!! Executed ping Configuration !!!!!!!!!"
-            '''
-        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
-''' % suffix
-
-            if use_vnf_init_conf is False:
-                 ping_cfg +='''
-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
-
-'''
-            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
-
-    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
-
-    def pong_fake_juju_config(self, vnf_config):
-
-        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)
-
-    def default_config(self, const_vnfd, vnfd, mano_ut,
-                       use_ns_init_conf,
-                       use_vnf_init_conf):
+    def default_config(self, vnfd):
         vnf_config = vnfd.vnfd.vnf_configuration
 
         vnf_config.config_attributes.config_priority = 0
@@ -559,38 +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,
-                                                          use_vnf_init_conf)
-            if use_vnf_init_conf:
-                vnf_config.initial_config_primitive.add().from_dict(
-                    {
-                        "seq": 1,
-                        "name": "set ping rate",
-                        "user_defined_script": "ping_set_rate.py",
-                        "parameter": [
-                            {
-                                'name': 'rate',
-                                'value': '5',
-                            },
-                        ],
-                    }
-                )
 
     def ns_config(self, nsd, vnfd_list, mano_ut):
         # Used by scale group
@@ -719,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, use_vnf_init_conf)
+                self.default_config(vnfd)
                 member_vnf_index += 1
 
         # Enable config primitives if either mano_ut or