Add chlorine to release dict
[integration/test.git] / csit / libraries / ConfGen.py
1 """
2 Library generating configuration/initial files.
3
4 The purpose of this library is about to customize conf file creation for special
5 use cases while testing, e.g. non replicated shards.
6 """
7
8
9 import pyhocon
10
11
12 def _parse_input(file_name):
13     """Parses given file and returns it's pyhocon object.
14
15     Args:
16         :param file_name: path to the filename
17
18     Returns:
19         :returns pyhocon_obj: parsed content of the given file
20     """
21     return pyhocon.ConfigFactory.parse_file(file_name)
22
23
24 def generate_akka(original_file, node_idx=1, nodes_ip_list=["127.0.0.1"]):
25     """Generates akka.conf content.
26
27     Args:
28         :param original_file: path to the filename, normally expecting file from system/org/opendaylight...
29                           sal-clustering-config-<version>-akkaconf.xml
30         :param node_idx: cluster node index for which the file is generated
31         :param nodes_ip_list: list of luster nodes ip addresses
32
33     Returns:
34         :returns str: akka.conf content
35     """
36
37     conf = _parse_input(original_file)
38     conf["odl-cluster-data"]["akka"]["remote"]["netty"]["tcp"][
39         "hostname"
40     ] = nodes_ip_list[node_idx - 1]
41     seed_nodes = [
42         u"akka.tcp://opendaylight-cluster-data@{}:2550".format(ip)
43         for ip in nodes_ip_list
44     ]
45     conf["odl-cluster-data"]["akka"]["cluster"]["seed-nodes"] = seed_nodes
46     conf["odl-cluster-data"]["akka"]["cluster"]["roles"] = [
47         "member-{}".format(node_idx)
48     ]
49     return pyhocon.tool.HOCONConverter.to_hocon(conf)
50
51
52 def generate_modules(original_file, name="", namespace=""):
53     """Generates modules.conf content.
54
55     If name and namespace parameters are filled, exactly one module item is added to the content of orginal file.
56     If more modules needed, then use this keyword more times with storing temp file after each addition and use
57     it as <original_file> in the next step.
58
59     Args:
60         :param original_files: path to the filename, normally expecting file from system/org/opendaylight...
61                           sal-clustering-config-<version>-moduleconf.xml
62         :param name: name of the new, addional shard
63         :param namespace: namespace of the new, addional shard
64
65     Returns:
66         :returns str: modules.conf content
67     """
68     conf = _parse_input(original_file)
69     if name != "" and namespace != "":
70         conf["modules"].append(
71             pyhocon.ConfigTree(
72                 [("name", name), ("namespace", namespace), ("shard-strategy", "module")]
73             )
74         )
75     return pyhocon.tool.HOCONConverter.to_hocon(conf)
76
77
78 def generate_module_shards(original_file, nodes=1, shard_name="", replicas=[]):
79     """Generates module-shards.conf content.
80
81     If shard_name and replicas parameters are filled, exactly one shard item is added to the content of orginal file.
82     If more shards needed, then use this keyword more times with storing temp file after each addition and use it as
83     <original_file> in the next step.
84
85     Args:
86         :param file_name: path to the filename, normally expecting file from system/org/opendaylight...
87                           sal-clustering-config-<version>-moduleshardconf.xml
88         :param: nodes: number of nodes in the cluster
89         :param shard_name: new name of the additional shard
90         :param replicas: list of member indexes which should keep shard replicas
91
92     Returns:
93         :returns str: module-shards.conf content
94     """
95     conf = _parse_input(original_file)
96     for module_shard in conf["module-shards"]:
97         module_shard["shards"][0]["replicas"] = [
98             "member-{}".format(i + 1) for i in range(int(nodes))
99         ]
100     if shard_name != "" and replicas != []:
101         conf["module-shards"].append(
102             pyhocon.ConfigTree(
103                 [
104                     ("name", shard_name),
105                     (
106                         "shards",
107                         [
108                             pyhocon.ConfigTree(
109                                 [
110                                     ("name", shard_name),
111                                     (
112                                         "replicas",
113                                         ["member-{}".format(i) for i in replicas],
114                                     ),
115                                 ]
116                             )
117                         ],
118                     ),
119                 ]
120             )
121         )
122     return pyhocon.tool.HOCONConverter.to_hocon(conf)