config.py 3.12 KB
Newer Older
Pavel Spirek's avatar
Pavel Spirek committed
1 2
import os
import yaml
3

4
from colorlog import info
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
from yaml.parser import ParserError

# For backward compatibility
CONFIG_GLOBAL = {}
CONFIG_HTTP = {}
CONFIG_NACM = {}
CONFIG = {}

CFG = None  # type: JcConfig


class JcConfig:
    def __init__(self):
        yang_mod_dir_env = os.environ.get("YANG_MODPATH")

        glob_def = {
            "TIMEZONE": "GMT",
            "LOGFILE": "-",
            "PIDFILE": "/tmp/jetconf.pid",
            "PERSISTENT_CHANGES": True,
            "LOG_LEVEL": "info",
            "LOG_DBG_MODULES": ["*"],
            "YANG_LIB_DIR": yang_mod_dir_env,
            "DATA_JSON_FILE": "data.json",
            "VALIDATE_TRANSACTIONS": True,
            "BACKEND_PACKAGE": "jetconf_jukebox"
        }

        http_def = {
            "DOC_ROOT": "doc-root",
            "DOC_DEFAULT_NAME": "index.html",
            "API_ROOT": "/restconf",
            "API_ROOT_RUNNING": "/restconf_running",
            "SERVER_NAME": "jetconf-h2",
            "UPLOAD_SIZE_LIMIT": 1,
            "LISTEN_LOCALHOST_ONLY": False,
            "PORT": 8443,

            "SERVER_SSL_CERT": "server.crt",
            "SERVER_SSL_PRIVKEY": "server.key",
            "CA_CERT": "ca.pem",
            "DBG_DISABLE_CERTS": False
        }

        nacm_def = {
            "ENABLED": True,
            "ALLOWED_USERS": []
        }

        root_def = {
            "GLOBAL": glob_def,
            "HTTP_SERVER": http_def,
            "NACM": nacm_def
        }

        self.glob = glob_def
        self.http = http_def
        self.nacm = nacm_def
        self.root = root_def

        # Shortcuts
        self.api_root_data = None
        self.api_root_running_data = None
        self.api_root_ops = None
        self.api_root_ylv = None

        self._gen_shortcuts()

    def _gen_shortcuts(self):
        global CONFIG_GLOBAL
        global CONFIG_HTTP
        global CONFIG_NACM
        global CONFIG

        CONFIG_GLOBAL.update(self.glob)
        CONFIG_HTTP.update(self.http)
        CONFIG_NACM.update(self.nacm)
        CONFIG.update(self.root)

        api_root = self.http["API_ROOT"]
        api_root_running = self.http["API_ROOT_RUNNING"]
        self.api_root_data = os.path.join(api_root, "data")
        self.api_root_running_data = os.path.join(api_root_running, "data")
        self.api_root_ops = os.path.join(api_root, "operations")
        self.api_root_ylv = os.path.join(api_root, "yang-library-version")

    def load_file(self, file_path: str) -> bool:
        with open(file_path) as conf_fd:
93
            try:
94 95 96
                conf_yaml = yaml.load(conf_fd)
            except ParserError as e:
                raise ValueError(str(e))
Pavel Spirek's avatar
Pavel Spirek committed
97

98 99 100 101 102
            for conf_key in self.root.keys():
                try:
                    self.root[conf_key].update(conf_yaml[conf_key])
                except KeyError:
                    pass
Pavel Spirek's avatar
Pavel Spirek committed
103

104
        self._gen_shortcuts()
105

106 107 108
    def validate(self):
        if self.glob["YANG_LIB_DIR"] is None:
            raise ValueError("YANG module directory must be specified (in config file or YANG_MODPATH env variable)")
109

110 111
    def print(self):
        info("Using config:\n" + yaml.dump(self.root, default_flow_style=False))