Support reading configuration from vcpeconfig.yaml 93/99393/2
authorBartek Grzybowski <b.grzybowski@partner.samsung.com>
Tue, 10 Dec 2019 08:50:09 +0000 (09:50 +0100)
committerBartek Grzybowski <b.grzybowski@partner.samsung.com>
Wed, 8 Jan 2020 10:18:04 +0000 (10:18 +0000)
Settings from config file will be assigned directly
to VcpeCommon class' object attributes.

Change-Id: I3cd31077c04de6bacf6dc2365837405a1aa5f6cc
Signed-off-by: Bartek Grzybowski <b.grzybowski@partner.samsung.com>
Issue-ID: INT-1399

test/vcpe/vcpecommon.py
test/vcpe/vcpeconfig.yaml [new file with mode: 0644]

index 0041a56..9c5e626 100755 (executable)
@@ -12,6 +12,7 @@ import mysql.connector
 import requests
 import commands
 import time
+import yaml
 from novaclient import client as openstackclient
 from kubernetes import client, config
 from netaddr import IPAddress, IPNetwork
@@ -95,6 +96,9 @@ class VcpeCommon:
         self.logger.setLevel(logging.DEBUG)
         self.logger.info('Initializing configuration')
 
+        # Read configuration from config file
+        self._load_config()
+
         # OOM: this is the address that the brg and bng will nat for sdnc access - 10.0.0.x address of k8 host for sdnc-0 container
         self.sdnc_oam_ip = self.get_pod_node_oam_ip(self.sdnc_controller_pod)
         # OOM: this is a k8s host external IP, e.g. oom-k8s-01 IP
@@ -227,6 +231,32 @@ class VcpeCommon:
                             'Content-Type': 'application/json',
                             'X-FromAppId': 'postman', 'X-TransactionId': '9999'}
 
+    def _load_config(self, cfg_file='vcpeconfig.yaml'):
+        """
+        Reads vcpe config file and injects settings as object's attributes
+        :param cfg_file: Configuration file path
+        """
+
+        try:
+            with open(cfg_file, 'r') as cfg:
+                cfg_yml = yaml.full_load(cfg)
+        except Exception as e:
+            self.logger.error('Error loading configuration: ' + str(e))
+            sys.exit(1)
+
+        self.logger.debug('\n' + yaml.dump(cfg_yml))
+
+        # Use setattr to load config file keys as VcpeCommon class' object
+        # attributes
+        try:
+            # Check config isn't empty
+            if cfg_yml is not None:
+                for cfg_key in cfg_yml:
+                    setattr(self, cfg_key, cfg_yml[cfg_key])
+        except TypeError as e:
+            self.logger.error('Unable to parse config file: ' + str(e))
+            sys.exit(1)
+
     def heatbridge(self, openstack_stack_name, svc_instance_uuid):
         """
         Add vserver information to AAI
diff --git a/test/vcpe/vcpeconfig.yaml b/test/vcpe/vcpeconfig.yaml
new file mode 100644 (file)
index 0000000..e69de29