1 #==================================================================================
3 # Copyright (c) 2018-2019 AT&T Intellectual Property.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #==================================================================================
19 # Author : Ashwin Sridharan
24 # This initialization script reads in a json from the specified config map path
25 # to set up the initializations (route config map, variables etc) for the main
33 def parseConfigJson(config):
35 print("Processing ", key);
36 if key in ParseSection:
37 result = ParseSection[key](config);
42 def getRMRTable(config):
44 if myKey not in config:
45 print(("Error ! No information found for {0} in config\n".format(myKey)));
48 # Get the rmr routing table
49 if "file_path" not in config[myKey]:
50 print(("Warning ! No file path specified to store seed routing table. Choosing default = {1}\n".format(default_routing_file)));
51 route_file = default_routing_file;
53 route_file = config[myKey]["file_path"];
55 # Get the rmr routing table contents
56 if "contents" not in config[myKey]:
57 print("No contents for routing table found in config");
60 route_contents = config[myKey]["contents"];
62 # Get directory : if not exists create it
63 directory = os.path.dirname(route_file);
64 if not os.path.exists(directory):
69 print(("Error making directory {0}. Reason = {1}\n".format(directory, oe)));
72 # Write contents to file
74 with open(route_file, "w") as f :
75 f.write(config[myKey]["contents"]);
77 except Exception as e:
78 print(("Error writing contents to file {0}. Reason = {1}\n".format(route_file, e)));
81 # Set the environment variable
82 os.environ["RMR_SEED_RT"] = route_file;
85 myKey = "service_ports";
86 if myKey not in config:
87 print(("Error ! No information found for {0} in config\n".format(myKey)));
89 port_config = config[myKey];
90 if "xapp_port" in port_config:
92 xapp_port = int(port_config["xapp_port"]);
94 raise Exception("Port must be > 1024");
95 except Exception as e:
96 print(("Error processing xapp port {0}. Reason = {1}\n".format(port_config["xapp_port"], e)));
104 if myKey not in config:
105 print(("Error ! No information found for {0} in config\n".format(myKey)));
108 env_config = config[myKey];
110 for env_key in env_config:
111 os.environ[env_key] = env_config[env_key];
112 print("Set environment variable {0} = {1}\n".format(env_key, os.environ[env_key]));
118 ParseSection["rmr"] = getRMRTable;
119 ParseSection["envs"] = getEnvs;
121 default_routing_file = "/tmp/routeinfo/routes.txt";
124 #================================================================
125 if __name__ == "__main__":
128 cmd = ["/usr/local/bin/adm-ctrl-xapp"];
131 cmd.append(xapp_port);
133 if len(sys.argv) > 1:
134 config_file = sys.argv[1];
136 print("Error! No configuration file specified\n");
139 if len(sys.argv) > 2:
142 with open(config_file, 'r') as f:
144 config = json.load(f);
145 except Exception as e:
146 print(("Error loading json file from {0}. Reason = {1}\n".format(config_file, e)));
149 result = parseConfigJson(config);
152 print("Error parsing json. Not executing xAPP");
156 print("Executing xAPP ....");
157 p = subprocess.check_call(cmd);