|
5 | 5 | from generic_config_updater.generic_updater import GenericUpdater, ConfigFormat
|
6 | 6 | from generic_config_updater.gu_common import EmptyTableError, genericUpdaterLogging
|
7 | 7 |
|
8 |
| -def ValidatedConfigDBConnector(config_db_connector): |
9 |
| - yang_enabled = device_info.is_yang_config_validation_enabled(config_db_connector) |
10 |
| - if yang_enabled: |
11 |
| - config_db_connector.set_entry = validated_set_entry |
12 |
| - config_db_connector.delete_table = validated_delete_table |
13 |
| - return config_db_connector |
14 |
| - |
15 |
| -def make_path_value_jsonpatch_compatible(table, key, value): |
16 |
| - if type(key) == tuple: |
17 |
| - path = JsonPointer.from_parts([table, '|'.join(key)]).path |
18 |
| - else: |
19 |
| - path = JsonPointer.from_parts([table, key]).path |
20 |
| - if value == {"NULL" : "NULL"}: |
21 |
| - value = {} |
22 |
| - return path, value |
23 |
| - |
24 |
| -def create_gcu_patch(op, table, key=None, value=None): |
25 |
| - if key: |
26 |
| - path, value = make_path_value_jsonpatch_compatible(table, key, value) |
27 |
| - else: |
28 |
| - path = "/{}".format(table) |
29 |
| - |
30 |
| - gcu_json_input = [] |
31 |
| - gcu_json = {"op": "{}".format(op), |
32 |
| - "path": "{}".format(path)} |
33 |
| - if op == "add": |
34 |
| - gcu_json["value"] = value |
35 |
| - |
36 |
| - gcu_json_input.append(gcu_json) |
37 |
| - gcu_patch = jsonpatch.JsonPatch(gcu_json_input) |
38 |
| - return gcu_patch |
39 |
| - |
40 |
| -def validated_delete_table(table): |
41 |
| - gcu_patch = create_gcu_patch("remove", table) |
42 |
| - format = ConfigFormat.CONFIGDB.name |
43 |
| - config_format = ConfigFormat[format.upper()] |
44 |
| - try: |
45 |
| - GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None) |
46 |
| - except ValueError as e: |
47 |
| - logger = genericUpdaterLogging.get_logger(title="Patch Applier", print_all_to_console=True) |
48 |
| - logger.log_notice("Unable to remove entry, as doing so will result in invalid config. Error: {}".format(e)) |
49 |
| - |
50 |
| -def validated_set_entry(table, key, value): |
51 |
| - if value is not None: |
52 |
| - op = "add" |
53 |
| - else: |
54 |
| - op = "remove" |
| 8 | +class ValidatedConfigDBConnector(object): |
55 | 9 |
|
56 |
| - gcu_patch = create_gcu_patch(op, table, key, value) |
57 |
| - format = ConfigFormat.CONFIGDB.name |
58 |
| - config_format = ConfigFormat[format.upper()] |
59 |
| - |
60 |
| - try: |
61 |
| - GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None) |
62 |
| - except EmptyTableError: |
63 |
| - validated_delete_table(table) |
| 10 | + def __init__(self, config_db_connector): |
| 11 | + self.connector = config_db_connector |
| 12 | + self.yang_enabled = device_info.is_yang_config_validation_enabled(self.connector) |
| 13 | + |
| 14 | + def __getattr__(self, name): |
| 15 | + if self.yang_enabled: |
| 16 | + if name == "set_entry": |
| 17 | + return self.validated_set_entry |
| 18 | + if name == "delete_table": |
| 19 | + return self.validated_delete_table |
| 20 | + return self.connector.__getattribute__(name) |
| 21 | + |
| 22 | + def make_path_value_jsonpatch_compatible(self, table, key, value): |
| 23 | + if type(key) == tuple: |
| 24 | + path = JsonPointer.from_parts([table, '|'.join(key)]).path |
| 25 | + else: |
| 26 | + path = JsonPointer.from_parts([table, key]).path |
| 27 | + if value == {"NULL" : "NULL"}: |
| 28 | + value = {} |
| 29 | + return path, value |
| 30 | + |
| 31 | + def create_gcu_patch(self, op, table, key=None, value=None): |
| 32 | + if key: |
| 33 | + path, value = self.make_path_value_jsonpatch_compatible(table, key, value) |
| 34 | + else: |
| 35 | + path = "/{}".format(table) |
| 36 | + |
| 37 | + gcu_json_input = [] |
| 38 | + gcu_json = {"op": "{}".format(op), |
| 39 | + "path": "{}".format(path)} |
| 40 | + if op == "add": |
| 41 | + gcu_json["value"] = value |
| 42 | + |
| 43 | + gcu_json_input.append(gcu_json) |
| 44 | + gcu_patch = jsonpatch.JsonPatch(gcu_json_input) |
| 45 | + return gcu_patch |
| 46 | + |
| 47 | + def validated_delete_table(self, table): |
| 48 | + gcu_patch = self.create_gcu_patch("remove", table) |
| 49 | + format = ConfigFormat.CONFIGDB.name |
| 50 | + config_format = ConfigFormat[format.upper()] |
| 51 | + try: |
| 52 | + GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None) |
| 53 | + except ValueError as e: |
| 54 | + logger = genericUpdaterLogging.get_logger(title="Patch Applier", print_all_to_console=True) |
| 55 | + logger.log_notice("Unable to remove entry, as doing so will result in invalid config. Error: {}".format(e)) |
| 56 | + |
| 57 | + def validated_set_entry(self, table, key, value): |
| 58 | + if value is not None: |
| 59 | + op = "add" |
| 60 | + else: |
| 61 | + op = "remove" |
| 62 | + |
| 63 | + gcu_patch = self.create_gcu_patch(op, table, key, value) |
| 64 | + format = ConfigFormat.CONFIGDB.name |
| 65 | + config_format = ConfigFormat[format.upper()] |
| 66 | + |
| 67 | + try: |
| 68 | + GenericUpdater().apply_patch(patch=gcu_patch, config_format=config_format, verbose=False, dry_run=False, ignore_non_yang_tables=False, ignore_paths=None) |
| 69 | + except EmptyTableError: |
| 70 | + self.validated_delete_table(table) |
0 commit comments