Commit 5336c3a4 authored by Pavel Spirek's avatar Pavel Spirek

YANG library now fetched from backend package resource

parent b415e6df
......@@ -6,20 +6,19 @@ import sys
import signal
from importlib import import_module
from pkg_resources import resource_string
from colorlog import error, info
from yaml.parser import ParserError
from yangson.enumerations import ContentType, ValidationScope
from yangson.exceptions import YangsonException
from yangson.schemanode import SchemaError, SemanticError
from yangson.datamodel import DataModel
from . import op_internal
from .rest_server import RestServer
from .config import CONFIG_GLOBAL, CONFIG_NACM, load_config, print_config
from .helpers import DataHelpers, ErrorHelpers
# from jetconf_jukebox import usr_state_data_handlers, usr_conf_data_handlers, usr_op_handlers
# from jetconf_jukebox.usr_datastore import UserDatastore
from .config import CONFIG_GLOBAL, CONFIG_NACM, load_config, validate_config, print_config
from .helpers import ErrorHelpers
def main():
......@@ -46,6 +45,13 @@ def main():
print("Configuration syntax error: " + str(e))
# Validate configuration
except ValueError as e:
print("Error: " + str(e))
# Set logging level
log_level = {
"error": logging.ERROR,
......@@ -142,17 +148,14 @@ def main():
# Load data model
yang_lib_file = os.path.join(CONFIG_GLOBAL["YANG_LIB_DIR"], "yang-library-data.json")
datamodel = DataHelpers.load_data_model(
yang_mod_dir = CONFIG_GLOBAL["YANG_LIB_DIR"]
yang_lib_str = resource_string(backend_package, "yang-library-data.json").decode("utf-8")
datamodel = DataModel(yang_lib_str, [yang_mod_dir])
# Datastore init
datastore = usr_datastore.UserDatastore(datamodel, CONFIG_GLOBAL["DATA_JSON_FILE"], with_nacm=CONFIG_NACM["ENABLED"])
except (FileNotFoundError, YangsonException) as e:
error("Could not load JSON datastore " + CONFIG_GLOBAL["DATA_JSON_FILE"])
......@@ -3,6 +3,8 @@ import yaml
from colorlog import info
_yang_mod_dir_env = os.environ.get("YANG_MODPATH")
"LOGFILE": "-",
......@@ -10,7 +12,7 @@ CONFIG_GLOBAL = {
"LOG_LEVEL": "info",
"YANG_LIB_DIR": "yang-data/",
"YANG_LIB_DIR": _yang_mod_dir_env,
"DATA_JSON_FILE": "data.json",
"BACKEND_PACKAGE": "jetconf_jukebox"
......@@ -75,5 +77,10 @@ def load_config(filename: str) -> bool:
API_ROOT_ylv = os.path.join(CONFIG_HTTP["API_ROOT"], "yang-library-version")
def validate_config():
raise ValueError("YANG module directory must be specified (in config file or YANG_MODPATH env variable)")
def print_config():
info("Using config:\n" + yaml.dump(CONFIG, default_flow_style=False))
......@@ -5,6 +5,7 @@ from enum import Enum
from colorlog import error, warning as warn, info
from typing import List, Any, Dict, Callable, Optional, Tuple
from datetime import datetime
from pkg_resources import resource_string
from yangson.datamodel import DataModel
from yangson.enumerations import ContentType, ValidationScope
......@@ -253,6 +254,9 @@ class BaseDatastore:
if with_nacm:
self.nacm = NacmConfig(self, self._dm)
yang_lib_str = resource_string(CONFIG["GLOBAL"]["BACKEND_PACKAGE"], "yang-library-data.json").decode("utf-8")
self._yang_lib_data = self._dm.from_raw(json.loads(yang_lib_str))
# Returns DataModel object
def get_dm(self) -> DataModel:
return self._dm
......@@ -880,11 +884,6 @@ class JsonDatastore(BaseDatastore):
if self.nacm is not None:
def load_yl_data(self, filename: str):
self._yang_lib_data = None
with open(filename, "rt") as fp:
self._yang_lib_data = self._dm.from_raw(json.load(fp))
def save(self):
with open(self.json_file, "w") as jfd:
json.dump(self._data.raw_value(), jfd, indent=4)
......@@ -6,7 +6,6 @@ from typing import List, Dict, Union, Any, Iterable
from datetime import datetime
from pytz import timezone
from yangson.instance import InstanceRoute, MemberName, EntryKeys, InstanceNode, ArrayValue, NonexistentInstance
from yangson.datamodel import DataModel
from yangson.schemanode import ListNode, ContainerNode
from .config import CONFIG_GLOBAL, CONFIG_HTTP
......@@ -58,13 +57,6 @@ class DataHelpers:
ns1 = ""
return ns1
def load_data_model(module_dir: str, yang_library_file: str) -> DataModel:
with open(yang_library_file) as ylfile:
yl =
dm = DataModel(yl, [module_dir])
return dm
# Convert InstanceRoute or List[InstanceSelector] to string
def ii2str(ii: Iterable) -> str:
......@@ -114,7 +114,7 @@ class H2Protocol(asyncio.Protocol):
if http_method in ("GET", "DELETE", "OPTIONS", "HEAD"):
self.run_request_handler(headers, event.stream_id, None)
elif http_method in ("PUT", "POST"):
body ='utf-8')
body ="utf-8")
self.run_request_handler(headers, event.stream_id, body)
warn("Unknown http method \"{}\"".format(headers[":method"]))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment