[CORD-1360] Using new Config in synchronizers

Change-Id: Iaa7c3394971286f4823a906fc184b0b83ad7ebbd
diff --git a/lib/xos-config/xosconfig/config.py b/lib/xos-config/xosconfig/config.py
index 608beac..3e0482d 100644
--- a/lib/xos-config/xosconfig/config.py
+++ b/lib/xos-config/xosconfig/config.py
@@ -6,6 +6,7 @@
 from pykwalify.core import Core as PyKwalify
 
 DEFAULT_CONFIG_FILE = "/opt/xos/xos_config.yaml"
+DEFAULT_CONFIG_SCHEMA = 'xos-config-schema.yaml'
 INITIALIZED = False
 CONFIG = {}
 
@@ -15,7 +16,12 @@
     """
 
     @staticmethod
-    def init(config_file=DEFAULT_CONFIG_FILE):
+    def init(config_file=DEFAULT_CONFIG_FILE, config_schema=DEFAULT_CONFIG_SCHEMA):
+
+        # make schema relative to this directory
+        # TODO give the possibility to specify an absolute path
+        config_schema = Config.get_abs_path(config_schema)
+
         global INITIALIZED
         global CONFIG
         # the config module can be initialized only one
@@ -23,16 +29,22 @@
             raise Exception('[XOS-Config] Module already initialized')
         INITIALIZED = True
 
-        # if XOS-CONFIG is defined override the config_file
-        if os.environ.get('XOS-CONFIG'):
-            config_file = os.environ['XOS-CONFIG']
+        # if XOS_CONFIG_FILE is defined override the config_file
+        # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
+        if os.environ.get('XOS_CONFIG_FILE'):
+            config_file = os.environ['XOS_CONFIG_FILE']
+
+        # if XOS_CONFIG_SCHEMA is defined override the config_schema
+        # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
+        if os.environ.get('XOS_CONFIG_SCHEMA'):
+            config_schema = Config.get_abs_path(os.environ['XOS_CONFIG_SCHEMA'])
 
         # if a -C parameter is set in the cli override the config_file
         # FIXME shouldn't this stay in whatever module call this one? and then just pass the file to the init method
         if Config.get_cli_param(sys.argv):
-            config_file = Config.get_cli_param(sys.argv)
+            config_schema = Config.get_cli_param(sys.argv)
 
-        CONFIG = Config.read_config(config_file)
+        CONFIG = Config.read_config(config_file, config_schema)
 
     @staticmethod
     def clear():
@@ -40,8 +52,14 @@
         INITIALIZED = False
 
     @staticmethod
-    def validate_config_format(config_file):
-        schema = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + '/config-schema.yaml')
+    def get_abs_path(path):
+        if os.path.isabs(path):
+            return path
+        return os.path.dirname(os.path.realpath(__file__)) + '/' + path
+
+    @staticmethod
+    def validate_config_format(config_file, config_schema):
+        schema = os.path.abspath(config_schema)
         c = PyKwalify(source_file=config_file, schema_files=[schema])
         c.validate(raise_exception=True)
 
@@ -54,7 +72,7 @@
             last = arg
 
     @staticmethod
-    def read_config(config_file):
+    def read_config(config_file, config_schema):
         """
         Read the configuration file and return a dictionary
         :param config_file: string
@@ -63,8 +81,11 @@
         if not os.path.exists(config_file):
             raise Exception('[XOS-Config] Config file not found at: %s' % config_file)
 
+        if not os.path.exists(config_schema):
+            raise Exception('[XOS-Config] Config schema not found at: %s' % config_schema)
+
         try:
-            Config.validate_config_format(config_file)
+            Config.validate_config_format(config_file, config_schema)
         except Exception, e:
             raise Exception('[XOS-Config] The config format is wrong: %s' % e.msg)
 
@@ -88,7 +109,9 @@
         if not val:
             val = Config.get_param(query, default.DEFAULT_VALUES)
         if not val:
-            raise Exception('[XOS-Config] Config does not have a value (or a default) parameter %s' % query)
+            # TODO if no val return none
+            # raise Exception('[XOS-Config] Config does not have a value (or a default) parameter %s' % query)
+            return None
         return val
 
     @staticmethod
@@ -166,7 +189,5 @@
         service = Config.get_service_info(service_name)
         return 'http://%s:%s' % (service['url'], service['port'])
 
-# NOTE is this needed if this package is not meant to be execute from the CLI?
 if __name__ == '__main__':
-    config = Config()
-    config.init()
\ No newline at end of file
+    Config.init()
\ No newline at end of file
diff --git a/lib/xos-config/xosconfig/default.py b/lib/xos-config/xosconfig/default.py
index 02abaa6..f5cd761 100644
--- a/lib/xos-config/xosconfig/default.py
+++ b/lib/xos-config/xosconfig/default.py
@@ -1,9 +1,27 @@
 DEFAULT_VALUES = {
     'xos_dir': '/opt/xos',
     'logging': {
-        'file': '/var/log/xos.log',
+        'file': '/var/log/xos.log', # TODO remove me, the new logger will be able to decide on which file to log
         'level': 'info',
         'channels': ['file', 'console'],
         'logstash_hostport': 'cordloghost:5617'
-    }
+    },
+    'accessor': {
+        'endpoint': 'xos-core.cord.lab:50051',
+    },
+    'keep_temp_files': False,
+    'enable_watchers': False,
+    'dependency_graph': '/opt/xos/model-deps',
+    'error_map_path': '/opt/xos/error_map.txt',
+    'feefie': {
+        'client_user': 'pl'
+    },
+    'proxy_ssh': {
+      'enabled': True,
+      'key': '/opt/cord_profile/node_key',
+      'user': 'root'
+    },
+    'node_key': '/opt/cord_profile/node_key',
+    'config_dir': '/etc/xos/sync',
+    'backoff_disabled': True
 }
\ No newline at end of file
diff --git a/lib/xos-config/xosconfig/synchronizer-config-schema.yaml b/lib/xos-config/xosconfig/synchronizer-config-schema.yaml
new file mode 100644
index 0000000..16c5433
--- /dev/null
+++ b/lib/xos-config/xosconfig/synchronizer-config-schema.yaml
@@ -0,0 +1,80 @@
+map:
+  name:
+    type: str
+    required: True
+  xos_dir:
+    type: str
+  logging:
+    type: map
+    map:
+      level:
+        type: str
+      channels:
+        type: seq
+        sequence:
+          - type: str
+            enum: ['file', 'console', 'elkstack']
+  dependency_graph:
+    type: str
+  steps_dir:
+    type: str
+  sys_dir:
+    type: str
+  accessor:
+    type: map
+    required: True
+    map:
+      username:
+        type: str
+        required: True
+      password:
+        type: str
+        required: True
+  required_models:
+    type: seq
+    sequence:
+      - type: str
+  keep_temp_files:
+    type: bool
+  proxy_ssh:
+    type: map
+    map:
+      enabled:
+        type: bool
+        required: True
+      key:
+        type: str
+      user:
+        type: str
+  enable_watchers:
+    type: bool
+  model_policies_dir:
+    type: str
+  error_map_path:
+    type: str
+  feefie:
+    type: map
+    map:
+      client_id:
+        type: str
+      user_id:
+        type: str
+  node_key:
+    type: str
+  config_dir:
+    type: str
+  backoff_disabled:
+    type: bool
+  images_directory:
+    type: str
+  nova:
+    type: map
+    map:
+      enabled:
+        type: bool
+      ca_ssl_cert:
+        type: str
+      default_flavor:
+        type: str
+      default_security_group:
+        type: str
\ No newline at end of file
diff --git a/lib/xos-config/xosconfig/config-schema.yaml b/lib/xos-config/xosconfig/xos-config-schema.yaml
similarity index 84%
rename from lib/xos-config/xosconfig/config-schema.yaml
rename to lib/xos-config/xosconfig/xos-config-schema.yaml
index b79905e..a2b2c75 100644
--- a/lib/xos-config/xosconfig/config-schema.yaml
+++ b/lib/xos-config/xosconfig/xos-config-schema.yaml
@@ -23,8 +23,11 @@
         type: str
       level:
         type: str
+        # TODO add validation [info, debug, warning, error, critical]
       channels:
         type: seq
         sequence:
           - type: str
-            enum: ['file', 'console', 'elkstack']
\ No newline at end of file
+            enum: ['file', 'console', 'elkstack']
+  xos_dir:
+    type: str
\ No newline at end of file