[APACHE] Add Apache CNF use case files
[demo.git] / tutorials / ApacheCNF / templates / cba2dd.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 #   COPYRIGHT NOTICE STARTS HERE
5
6 #   Copyright 2020 . Samsung Electronics Co., Ltd.
7 #
8 #   Licensed under the Apache License, Version 2.0 (the "License");
9 #   you may not use this file except in compliance with the License.
10 #   You may obtain a copy of the License at
11 #
12 #       http://www.apache.org/licenses/LICENSE-2.0
13 #
14 #   Unless required by applicable law or agreed to in writing, software
15 #   distributed under the License is distributed on an "AS IS" BASIS,
16 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 #   See the License for the specific language governing permissions and
18 #   limitations under the License.
19
20 #   COPYRIGHT NOTICE ENDS HERE
21
22 import os
23 import argparse
24 import json
25
26 #
27 # Script to convert given Enriched CDS CBA model to Data Dictionary output
28 # Usage:
29 #   ./cba2dd.py --cba_dir <path to cba main directory> | python3 -m json.tool
30 #
31
32 def get_resources_definition_file(cba_dir):
33     definitions_dir = cba_dir + os.sep + "Definitions"
34     resource_definition_file = definitions_dir + os.sep + "resources_definition_types.json"
35     if not os.path.exists(definitions_dir):
36         raise RuntimeError("'%s' directory does not exists or is not CBA directory" % cba_dir)
37     if not os.path.exists(resource_definition_file):
38         raise RuntimeError("'%s' file does not exists in CBA Definitions directory. CBA is not Enriched!" % resource_definition_file)
39     return resource_definition_file
40
41 def create_dd(cba_dir):
42     with open(get_resources_definition_file(cba_dir)) as f:
43         output_json = json.load(f)
44     dd = []
45     for _, entry in output_json.items():
46         dd.append(build_dd_entry(entry))
47     print(json.dumps(dd))
48
49 def build_dd_entry(definition_entry):
50     """Builds Data Dictionary entry from given dictionary entry. Given entry
51     itself is added as value for "definition" key.
52     {
53         "name": "",
54         "tags": "",
55         "data_type": "",
56         "description": "",
57         "entry_schema": "",
58         "updatedBy": "",
59         "definition": definition_entry
60     }
61     """
62     out_dict = {}
63     out_dict["name"] = definition_entry["name"]
64     out_dict["tags"] = definition_entry["tags"]
65     out_dict["data_type"] = definition_entry["property"]["type"]
66     out_dict["description"] = definition_entry["property"]["description"]
67     out_dict["entry_schema"] = definition_entry["property"]["type"]
68     out_dict["updatedBy"] = definition_entry["updated-by"]
69     out_dict["definition"] = definition_entry
70     return out_dict
71
72 def main():
73     description = """Script to convert given Enriched CDS CBA model to Data Dictionary output.
74 Example:
75   ./cba2dd.py --cba_dir cba | python3 -m json.tool
76     """
77     parser = argparse.ArgumentParser(description=description,
78         formatter_class=argparse.RawTextHelpFormatter)
79     parser.add_argument('--cba_dir',
80                         help='Path to CDS CBA model main directory',
81                         default='')
82     args = parser.parse_args()
83     try:
84         create_dd(args.cba_dir)
85     except Exception as e:
86         print(e)
87         parser.print_help()
88         exit(1)
89
90 if __name__ == '__main__':
91     main()