def rosetta_get_driver(platform): if platform == "ios" or "eos": # eos not a platform in rosetta https://github.com/networktocode/ntc-rosetta/blob/develop/ntc_rosetta/__init__.py#L13 ios = get_driver("ios", "openconfig") rosetta_device_driver = ios() if platform == "junos": junos = get_driver("junos", "openconfig") rosetta_device_driver = junos() return rosetta_device_driver
def parse(self, input_args: JsonNodeT, username: str) -> JsonNodeT: validate = input_args.get("ntc-rosetta-conf:validate", True) native = input_args.get("ntc-rosetta-conf:native") if not native: return _fail(f"you need to set native data") platform = self._get_platform(username) if not platform: return _fail(f"you need to set the platform first") device_driver = get_driver(platform.split(":")[-1], "openconfig") processor = device_driver() native = processor.parse(native={"dev_conf": native}, validate=validate) native_raw = native.raw_value() models_to_propagate = ["ntc-rosetta-conf:device"] for m in models_to_propagate: irt = self.ds._dm.parse_resource_id(f"/{m}") data = self.ds._data.goto(irt) native_raw[m] = data.raw_value() self.ds._data = self.ds._dm.from_raw(native_raw) return _succeed({})
def to_yang(file_path, driver="ios"): ios = get_driver(driver, "openconfig") ios_driver = ios() with open(file_path, "r") as f: config = f.read() parsed = ios_driver.parse(native={"dev_conf": config}) return (json.dumps(parsed.raw_value(), indent=4))
def _test_translate(self, driver: str, test_case_path: pathlib.Path, mode: str) -> None: with open(test_case_path.joinpath("data.json"), "r") as f: candidate = json.load(f) with open(test_case_path.joinpath(f"res_{mode}"), "r") as f: expected = f.read() driver_class = ntc_rosetta.get_driver(driver) device = driver_class() config = device.translate(candidate, replace=mode == "replace") assert config == expected
def _test_merge(self, driver: str, test_case_path: pathlib.Path, mode: str) -> None: with open(test_case_path.joinpath("data_candidate.json"), "r") as f: candidate = json.load(f) with open(test_case_path.joinpath("data_running.json"), "r") as f: running = json.load(f) with open(test_case_path.joinpath(f"res_{mode}"), "r") as f: expected = f.read() driver_class = ntc_rosetta.get_driver(driver) device = driver_class() res_merge = device.merge(candidate, running, replace=mode == "replace") assert res_merge == expected
def print_parser(ctx: click.Context, driver: str, model: str, to_json: bool) -> None: """ Prints a tree representation of a parser/translator. Parser/Translator needs to be properly linted for this to work """ d = get_driver(driver, model) dm = get_data_model(model) lint = linter.Linter.lint(d.parser, dm, recursive=True) if to_json: text = json.dumps(lint.to_dict(), indent=4) else: text = lint.to_ascii_tree("") print(text)
def main() -> None: for processor_type in ["parser", "translator"]: linter_result = LinterPlatformResult() for model in models: dm = get_data_model(model) for driver in drivers: d = get_driver(driver, model) r = RootLinter.lint(getattr(d, processor_type), dm, True, set()) linter_result[driver] = _process_linter(r) schema_result = _process_schema(dm.schema, linter_result) filepath = Path(__file__).parent.joinpath( "models", "matrix", f"{model}_{processor_type}.rst") with open(filepath, "w+") as f: f.write( schema_result.to_html_table(f"{model} {processor_type}"))
def merge(self, input_args: JsonNodeT, username: str) -> JsonNodeT: candidate = self._get_instance_node("/", False, username).raw_value() running = self._get_instance_node("/", True, username).raw_value() replace = input_args.get("ntc-rosetta-conf:replace", False) platform = self._get_platform(username) if not platform: return _fail(f"you need to set the platform first") device_driver = get_driver(platform.split(":")[-1], "openconfig") processor = device_driver() remove_ntc_rosetta_conf_specific_data(candidate) remove_ntc_rosetta_conf_specific_data(running) native = processor.merge(candidate=candidate, running=running, replace=replace) return _succeed({"native": native})
def test_parser(self, model: str, driver: str, test_case_path: pathlib.Path) -> None: with open(test_case_path.joinpath("dev_conf"), "r") as f: dev_conf = f.read() with open(test_case_path.joinpath("result.json"), "r") as f: structured = json.load(f) driver_class = ntc_rosetta.get_driver(driver) device = driver_class() parsed_obj = device.parse( native={"dev_conf": dev_conf}, validate=False, include=filters[model]["include"], exclude=filters[model]["exclude"], ) # print(json.dumps(parsed_obj.raw_value())) assert parsed_obj.raw_value() == structured, json.dumps( parsed_obj.raw_value())
def translate(self, input_args: JsonNodeT, username: str) -> JsonNodeT: db = input_args["ntc-rosetta-conf:database"] if db == "candidate": data = self._get_instance_node("/", False, username).raw_value() elif db == "running": data = self._get_instance_node("/", True, username).raw_value() else: return _fail(f"don't know this database: '{db}'") replace = input_args.get("ntc-rosetta-conf:replace", False) platform = self._get_platform(username) if not platform: return _fail(f"you need to set the platform first") device_driver = get_driver(platform.split(":")[-1], "openconfig") processor = device_driver() remove_ntc_rosetta_conf_specific_data(data) native = processor.translate(candidate=data, replace=replace) return _succeed({"native": native})
def load_driver(nos): nos = get_driver(nos, "openconfig") return nos()