|
| 1 | +#!/usr/bin/env python |
| 2 | +"""Copyright 2019 Cisco Systems |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | +"""Extremely ugly migration.""" |
| 17 | +import logging |
| 18 | +import json |
| 19 | +import yang_parser |
| 20 | + |
| 21 | +def parse_repository(repo_path): |
| 22 | + modules = yang_parser.parse_repository(repo_path) |
| 23 | + parsed_modules = {} |
| 24 | + for module_key, module_revision in modules.items(): |
| 25 | + revision_with_data = set() |
| 26 | + for revision_key, module in module_revision.items(): |
| 27 | + revision_data = None |
| 28 | + try: |
| 29 | + revision_data = __parse_node_attrs(module, module) |
| 30 | + except Exception: |
| 31 | + logging.exception("Failure while parsing %s!", module_key) |
| 32 | + if not revision_data: |
| 33 | + logging.debug("%s@%s is empty.", module_key, revision_key) |
| 34 | + continue |
| 35 | + revision_with_data.add("%s@%s" % (module_key, revision_key)) |
| 36 | + if module_key in parsed_modules.keys() and parsed_modules[module_key]: |
| 37 | + logging.warn( |
| 38 | + "%s being replaced with %s@%s! Only one revision should be used.", |
| 39 | + module_key, |
| 40 | + module_key, |
| 41 | + revision_key, |
| 42 | + ) |
| 43 | + parsed_modules[module_key] = revision_data |
| 44 | + if revision_with_data: |
| 45 | + logging.debug("%s have data.", ", ".join(revision_with_data)) |
| 46 | + else: |
| 47 | + logging.debug("%s has no revisions with data.", module_key) |
| 48 | + return parsed_modules |
| 49 | + |
| 50 | + |
| 51 | +def __parse_node_attrs(node, base_module): |
| 52 | + if not hasattr(node, "i_children"): |
| 53 | + return {} |
| 54 | + children = ( |
| 55 | + child |
| 56 | + for child in node.i_children |
| 57 | + if child.keyword in yang_parser.statements.data_definition_keywords |
| 58 | + ) |
| 59 | + parsed_children = {} |
| 60 | + multi_map = {} |
| 61 | + for child in children: |
| 62 | + qualified_xpath = yang_parser.get_xpath( |
| 63 | + child, qualified=True, prefix_to_module=True |
| 64 | + ) |
| 65 | + if qualified_xpath in parsed_children.keys(): |
| 66 | + logging.debug("%s encountered more than once! Muxing." % qualified_xpath) |
| 67 | + if qualified_xpath not in multi_map.keys(): |
| 68 | + multi_map[qualified_xpath] = 0 |
| 69 | + multi_map[qualified_xpath] += 1 |
| 70 | + qualified_xpath += "_%i" % multi_map[qualified_xpath] |
| 71 | + attr_dict = { |
| 72 | + '1_machine_id': '%s%s' % (base_module.arg, yang_parser.old_get_xpath(child, with_prefixes=True)), |
| 73 | + '2_machine_id': '/%s' % ('/'.join(map(lambda x: ':'.join(x), yang_parser.mk_path_list(child)))), |
| 74 | + 'children': __parse_node_attrs(child, base_module) |
| 75 | + } |
| 76 | + parsed_children[qualified_xpath] = attr_dict |
| 77 | + return parsed_children |
| 78 | + |
| 79 | +logging.basicConfig(level=logging.INFO) |
| 80 | + |
| 81 | +upgrade_map = {} |
| 82 | + |
| 83 | +# Any paths come out missing, go to instance of TDM |
| 84 | +# Datapath Direct the path and add path to version folder |
| 85 | +repository_paths = [ |
| 86 | + 'yang/vendor/cisco/nx/9.3-1', |
| 87 | + 'yang/vendor/cisco/xe/16111', |
| 88 | + 'yang/vendor/cisco/xr/602', |
| 89 | + 'yang/vendor/cisco/xr/631', |
| 90 | + 'yang/vendor/cisco/xr/662' |
| 91 | +] |
| 92 | + |
| 93 | +found = 0 |
| 94 | +missing = 0 |
| 95 | + |
| 96 | +def upgrade(key): |
| 97 | + if key in upgrade_map.keys(): |
| 98 | + global found |
| 99 | + found += 1 |
| 100 | + return upgrade_map[key] |
| 101 | + else: |
| 102 | + global missing |
| 103 | + if not key.startswith('1.'): |
| 104 | + missing += 1 |
| 105 | + logging.warning('%s not in upgrade map!', key) |
| 106 | + return key |
| 107 | + |
| 108 | +for repo in repository_paths: |
| 109 | + logging.info('Parsing %s ...', repo) |
| 110 | + modules = parse_repository(repo) |
| 111 | + def recurse_nodes(node_tree): |
| 112 | + for node_element in node_tree.values(): |
| 113 | + upgrade_map[node_element['1_machine_id']] = node_element['2_machine_id'] |
| 114 | + recurse_nodes(node_element['children']) |
| 115 | + module_top_children = {} |
| 116 | + for top_children in modules.values(): |
| 117 | + module_top_children.update(top_children) |
| 118 | + recurse_nodes(module_top_children) |
| 119 | +tdm_mappings = None |
| 120 | +with open('tdm_mappings.json', 'r') as mappings_fd: |
| 121 | + tdm_mappings = json.load(mappings_fd) |
| 122 | +logging.info('Upgrading DataPathMatch ...') |
| 123 | +for match in tdm_mappings['DataPathMatch']: |
| 124 | + match['_from'] = upgrade(match['_from']) |
| 125 | + match['_to'] = upgrade(match['_to']) |
| 126 | +logging.info('%i found, %i missing.', found, missing) |
| 127 | +found = 0 |
| 128 | +missing = 0 |
| 129 | +logging.info('Upgrading Calculation ...') |
| 130 | +for calculation in tdm_mappings['Calculation']: |
| 131 | + new_in_calculation = [] |
| 132 | + for path in calculation['InCalculation']: |
| 133 | + new_in_calculation.append(upgrade(path)) |
| 134 | + calculation['InCalculation'] = new_in_calculation |
| 135 | + new_result = [] |
| 136 | + for path in calculation['CalculationResult']: |
| 137 | + new_result.append(upgrade(path)) |
| 138 | + calculation['CalculationResult'] = new_result |
| 139 | +logging.info('%i found, %i missing.', found, missing) |
| 140 | +with open('upgraded_tdm_mappings.json', 'w') as upgraded_fd: |
| 141 | + json.dump(tdm_mappings, upgraded_fd, indent=4, sort_keys=True) |
0 commit comments