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
35 def signal_handler(signum, frame):
36 print("Received signal {0}\n".format(signum));
37 if(xapp_subprocess == None or xapp_pid == None):
38 print("No xapp running. Quiting without sending signal to xapp\n");
40 print("Sending signal {0} to xapp ...".format(signum));
41 xapp_subprocess.send_signal(signum);
44 def parseConfigJson(config):
46 print("Processing ", key);
47 if key in ParseSection:
48 result = ParseSection[key](config);
53 def getRMRTable(config):
55 if myKey not in config:
56 print(("Error ! No information found for {0} in config\n".format(myKey)));
59 # Get the rmr routing table
60 if "file_path" not in config[myKey]:
61 print(("Warning ! No file path specified to store seed routing table. Choosing default = {1}\n".format(default_routing_file)));
62 route_file = default_routing_file;
64 route_file = config[myKey]["file_path"];
66 # Get the rmr routing table contents
67 if "contents" not in config[myKey]:
68 print("No contents for routing table found in config");
71 route_contents = config[myKey]["contents"];
73 # Get directory : if not exists create it
74 directory = os.path.dirname(route_file);
75 if not os.path.exists(directory):
80 print(("Error making directory {0}. Reason = {1}\n".format(directory, oe)));
83 # Write contents to file
85 with open(route_file, "w") as f :
86 f.write(config[myKey]["contents"]);
88 except Exception as e:
89 print(("Error writing contents to file {0}. Reason = {1}\n".format(route_file, e)));
92 # Set the environment variable
93 os.environ["RMR_SEED_RT"] = route_file;
96 myKey = "service_ports";
97 if myKey not in config:
98 print(("Error ! No information found for {0} in config\n".format(myKey)));
100 port_config = config[myKey];
101 if "xapp_port" in port_config:
103 xapp_port = int(port_config["xapp_port"]);
105 raise Exception("Port must be > 1024");
106 except Exception as e:
107 print(("Error processing xapp port {0}. Reason = {1}\n".format(port_config["xapp_port"], e)));
115 if myKey not in config:
116 print(("Error ! No information found for {0} in config\n".format(myKey)));
119 env_config = config[myKey];
121 for env_key in env_config:
122 os.environ[env_key] = env_config[env_key];
123 print("Set environment variable {0} = {1}\n".format(env_key, os.environ[env_key]));
128 # Global variables ...
129 xapp_subprocess = None;
133 ParseSection["rmr"] = getRMRTable;
134 ParseSection["envs"] = getEnvs;
136 default_routing_file = "../src/routes.txt";
139 #================================================================
140 if __name__ == "__main__":
143 cmd = ["../src/hw_xapp_main"];
146 cmd.append(xapp_port);
148 if len(sys.argv) > 1:
149 config_file = sys.argv[1];
151 print("Error! No configuration file specified\n");
154 if len(sys.argv) > 2:
155 cmd[0] = sys.argv[2];
157 with open(config_file, 'r') as f:
159 config = json.load(f);
160 except Exception as e:
161 print(("Error loading json file from {0}. Reason = {1}\n".format(config_file, e)));
164 result = parseConfigJson(config);
167 print("Error parsing json. Not executing xAPP");
172 # Register signal handlers
173 signal.signal(signal.SIGINT, signal_handler);
174 signal.signal(signal.SIGTERM, signal_handler);
177 #print("Executing xAPP ....");
178 xapp_subprocess = subprocess.Popen(cmd, shell = False, stdin=None, stdout=None, stderr = None);
179 xapp_pid = xapp_subprocess.pid;
181 # Periodically poll the process every 5 seconds to check if still alive
183 xapp_status = xapp_subprocess.poll();
184 if xapp_status == None:
187 print("XaPP terminated via signal {0}\n".format(-1 * xapp_status));