Report: Remove DNV testbeds
[csit.git] / resources / tools / presentation / pal.py
1 # Copyright (c) 2023 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """CSIT Presentation and Analytics Layer.
15 """
16
17 import sys
18 import argparse
19 import logging
20
21 from pal_errors import PresentationError
22 from specification_parser import Specification
23 from environment import Environment, clean_environment
24 from static_content import prepare_static_content
25 from input_data_parser import InputData
26 from generator_tables import generate_tables
27 from generator_plots import generate_plots
28 from generator_files import generate_files
29 from generator_report import generate_report
30 from generator_cpta import generate_cpta
31 from generator_alerts import Alerting, AlertingError
32 from convert_xml_json import convert_xml_to_json
33
34
35 OUTPUTS = ("none", "report", "trending", "convert-xml-to-json")
36
37
38 def parse_args():
39     """Parse arguments from cmd line.
40
41     :returns: Parsed arguments.
42     :rtype: ArgumentParser
43     """
44
45     parser = argparse.ArgumentParser(
46         description=__doc__,
47         formatter_class=argparse.RawDescriptionHelpFormatter
48     )
49     parser.add_argument(
50         "-s", "--specification",
51         required=True,
52         type=str,
53         help="Specification YAML file."
54     )
55     parser.add_argument(
56         "-r", "--release",
57         default="master",
58         type=str,
59         help="Release string of the product."
60     )
61     parser.add_argument(
62         "-w", "--week",
63         default="1",
64         type=str,
65         help="Calendar week when the report is published."
66     )
67     parser.add_argument(
68         "-l", "--logging",
69         choices=[
70             "NOTSET", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"
71         ],
72         default="ERROR",
73         help="Logging level."
74     )
75     parser.add_argument(
76         "-f", "--force",
77         action="store_true",
78         help="Force removing the old build(s) if present."
79     )
80     parser.add_argument(
81         "-o", "--print-all-oper-data",
82         action="store_true",
83         help="Print all operational data to console. Be careful, the output "
84              "can be really long."
85     )
86     parser.add_argument(
87         "-i", "--input-file",
88         type=str,
89         default="",
90         help="XML file generated by RobotFramework which will be processed "
91              "instead of downloading the data from Nexus and/or Jenkins. In "
92              "this case, the section 'input' in the specification file is "
93              "ignored."
94     )
95     parser.add_argument(
96         "-d", "--input-directory",
97         type=str,
98         default="",
99         help="Directory with XML file(s) generated by RobotFramework or with "
100              "sub-directories with XML file(s) which will be processed "
101              "instead of downloading the data from Nexus and/or Jenkins. In "
102              "this case, the section 'input' in the specification file is "
103              "ignored."
104     )
105
106     return parser.parse_args()
107
108
109 def main():
110     """Main function."""
111
112     log_levels = {
113         "NOTSET": logging.NOTSET,
114         "DEBUG": logging.DEBUG,
115         "INFO": logging.INFO,
116         "WARNING": logging.WARNING,
117         "ERROR": logging.ERROR,
118         "CRITICAL": logging.CRITICAL
119     }
120
121     args = parse_args()
122     logging.basicConfig(
123         format="%(asctime)s: %(levelname)s: %(message)s",
124         datefmt="%Y/%m/%d %H:%M:%S",
125         level=log_levels[args.logging]
126     )
127
128     logging.info("Application started.")
129
130     try:
131         spec = Specification(args.specification)
132         spec.read_specification()
133     except PresentationError as err:
134         logging.critical("Finished with error.")
135         logging.critical(repr(err))
136         return 1
137
138     if spec.output["output"] not in OUTPUTS:
139         logging.critical(
140             f"The output {spec.output[u'output']} is not supported."
141         )
142         return 1
143
144     return_code = 1
145     try:
146         env = Environment(spec.environment, args.force)
147         env.set_environment()
148
149         prepare_static_content(spec)
150
151         data = InputData(spec, spec.output["output"])
152         if args.input_file:
153             data.process_local_file(args.input_file)
154         elif args.input_directory:
155             data.process_local_directory(args.input_directory)
156         else:
157             data.download_and_parse_data(repeat=1)
158
159         if args.print_all_oper_data:
160             data.print_all_oper_data()
161
162         generate_tables(spec, data)
163         generate_plots(spec, data)
164         generate_files(spec, data)
165
166         if spec.output["output"] == "report":
167             generate_report(args.release, spec, args.week)
168         elif spec.output["output"] == "trending":
169             sys.stdout.write(generate_cpta(spec, data))
170             try:
171                 alert = Alerting(spec)
172                 alert.generate_alerts()
173             except AlertingError as err:
174                 logging.warning(repr(err))
175         elif spec.output["output"] == "convert-xml-to-json":
176             convert_xml_to_json(spec, data)
177         else:
178             logging.info("No output will be generated.")
179
180         logging.info("Successfully finished.")
181         return_code = 0
182
183     except AlertingError as err:
184         logging.critical(f"Finished with an alerting error.\n{repr(err)}")
185     except PresentationError as err:
186         logging.critical(f"Finished with a PAL error.\n{str(err)}")
187     except (KeyError, ValueError) as err:
188         logging.critical(f"Finished with an error.\n{repr(err)}")
189     finally:
190         if spec is not None:
191             clean_environment(spec.environment)
192     return return_code
193
194
195 if __name__ == "__main__":
196     sys.exit(main())