Merge 'base' and 'controller/network_generator'
[oam.git] / code / network-generator / network_generation / cli.py
1 # Copyright 2023 highstreet technologies GmbH
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # inspired by https://github.com/rochacbruno/python-project-template
16
17 """CLI interface for network_generation project.
18
19 Be creative! do whatever you want!
20
21 - Install click or typer and create a CLI app
22 - Use builtin argparse
23 - Start a web application
24 - Import things from your .base module
25 """
26
27
28
29 """
30 Module as entry point to generate an ietf topology json
31 """
32 import os
33 import sys
34
35 from network_generation.view.network_viewer import NetworkViewer
36 from network_generation.base import NetworkGenerator
37 from network_generation.parameter_validator import ParameterValidator
38
39 def main():  # pragma: no cover
40     """
41     The main function executes on commands:
42     `python -m network_generation`.
43
44     """
45     validator: ParameterValidator = ParameterValidator(sys.argv)
46
47     if validator.is_valid():
48         configuration = validator.configuration()
49         generator = NetworkGenerator(configuration['network'])
50         network = generator.generate()
51         viewer = NetworkViewer(network)
52
53         output_folder:str = configuration['output-folder']
54         # If folder doesn't exist, then create it.
55         if not os.path.isdir(output_folder):
56             os.makedirs(output_folder)
57             
58         name: str = configuration['network']['name']
59
60         # topology json
61         if configuration['generation-tasks']['topology'] is True:
62             filename: str = output_folder + "/" + name + "-operational.json"
63             viewer.json().save(filename)
64
65         # svg xml
66         if configuration['generation-tasks']['svg'] is True:
67             filename: str = output_folder + "/" + name + ".svg"
68             viewer.svg(filename)
69
70         # kml xml
71         if configuration['generation-tasks']['kml'] is True:
72             filename: str = output_folder + "/" + name + ".kml"
73             viewer.kml(filename)
74
75     else:
76         print(validator.error_message())
77