* YANG to TOSCA translator
[osm/SO.git] / common / python / rift / mano / yang_translator / compare_desc.py
1 #
2 # Copyright 2016 RIFT.io Inc
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
17
18 import argparse
19 import json
20 import logging
21 import logging.config
22 import pprint
23
24 from deepdiff import DeepDiff
25
26 from toscaparser.utils.gettextutils import _
27
28
29 class CompareDescShell(object):
30
31 SUPPORTED_TYPES = ['json']
32 INDENT = 2
33 DIFF_KEYS = (REMOVED_ITEMS, ADDED_ITEMS, TYPE_CHANGES, VALUES_CHANGED) = \
34 ('dic_item_removed', 'dic_item_added', 'type_changes',
35 'values_changed')
36 DIFF_MAP = {REMOVED_ITEMS: 'Items removed',
37 ADDED_ITEMS: 'Items added',
38 TYPE_CHANGES: 'Changes in types',
39 VALUES_CHANGED: 'Changes in values'}
40 # Currently considering changes in removed keys or type changes
41 # as error.
42 ERROR_ITEMS = [REMOVED_ITEMS, TYPE_CHANGES]
43
44 def main(self, log, args):
45 self.log = log
46 print("Args: {}".format(args))
47 self.log.debug(_("Args: {0}").format(args))
48 if args.type not in self.SUPPORTED_TYPES:
49 self.log.error(_("Unsupported file type {0}").
50 format(args.type))
51 exit(1)
52
53 with open(args.generated_file) as g:
54 gen_data = g.read()
55 json_gen = json.loads(gen_data)
56 self.log.debug(_("Generated: {0}").format(json_gen))
57
58 with open(args.expected_file) as e:
59 exp_data = e.read()
60 json_exp = json.loads(exp_data)
61 self.log.debug(_("Expected: {0}").format(json_exp))
62
63 diff = DeepDiff(json_exp, json_gen)
64 self.log.debug(_("Keys in diff: {0}").format(diff.keys()))
65 self.log.info(_("Differences:\n"))
66
67 d = pprint.pformat(diff, indent=self.INDENT)
68 self.log.info("Differences:\n{0}".format(d))
69
70 if len(set(self.ERROR_ITEMS).intersection(diff.keys())):
71 diff_str = pprint.pformat(diff)
72 msg = _("Found item changes: {0}").format(diff_str)
73 self.log.error(msg)
74 raise ValueError(msg)
75
76
77 def main(args=None):
78 parser = argparse.ArgumentParser(
79 description='Validate descriptors by comparing')
80 parser.add_argument(
81 "-g",
82 "--generated-file",
83 required=True,
84 help="Generated descriptor file")
85 parser.add_argument(
86 "-e",
87 "--expected-file",
88 required=True,
89 help="Descriptor to compare")
90 parser.add_argument(
91 "-t",
92 "--type",
93 default='json',
94 help="File type. Default json")
95 parser.add_argument(
96 "--debug",
97 help="Enable debug logging",
98 action="store_true")
99 if args:
100 args = parser.parse_args(args)
101 else:
102 args = parser.parse_args()
103
104 if args.debug:
105 logging.basicConfig(level=logging.DEBUG)
106 else:
107 logging.basicConfig(level=logging.ERROR)
108 log = logging.getLogger("rwmano-translator")
109
110 CompareDescShell().main(log, args)
111
112
113 if __name__ == '__main__':
114 main()