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):
45 for k1 in config.keys():
46 for k2 in config[k1].keys():
48 if k2 in ParseSection:
49 result = ParseSection[k2](config[k1]);
55 def getMessagingInfo(config):
56 if 'messaging' in config.keys() and 'ports' in config['messaging'].keys():
57 port_list = config['messaging']['ports']
58 for portdesc in port_list :
59 if 'port' in portdesc.keys() and 'name' in portdesc.keys() and portdesc['name'] == 'rmr-data':
60 lport = portdesc['port']
61 # Set the environment variable
62 os.environ["HW_PORT"] = str(lport)
65 print("Error! No valid listening port");
68 def getXappName(config):
70 if myKey not in config.keys():
71 print(("Error ! No information found for {0} in config\n".format(myKey)));
73 xapp_name = config[myKey];
74 os.environ["XAPP_NAME"] = xapp_name;
76 default_routing_file = "/tmp/routeinfo/routes.txt";
79 ParseSection["xapp_name"] = getXappName;
80 ParseSection["messaging"] = getMessagingInfo;
83 #================================================================
84 if __name__ == "__main__":
87 # cmd = ["../src/hw_xapp_main"];
88 cmd = ["/usr/local/bin/hw_xapp_main"];
91 config_file = sys.argv[1];
93 print("Error! No configuration file specified\n");
99 with open(config_file, 'r') as f:
101 config = json.load(f);
102 except Exception as e:
103 print(("Error loading json file from {0}. Reason = {1}\n".format(config_file, e)));
106 result = parseConfigJson(config);
109 print("Error parsing json. Not executing xAPP");
114 # Register signal handlers
115 signal.signal(signal.SIGINT, signal_handler);
116 signal.signal(signal.SIGTERM, signal_handler);
119 #print("Executing xAPP ....");
120 xapp_subprocess = subprocess.Popen(cmd, shell = False, stdin=None, stdout=None, stderr = None);
121 xapp_pid = xapp_subprocess.pid;
123 # Periodically poll the process every 5 seconds to check if still alive
125 xapp_status = xapp_subprocess.poll();
126 if xapp_status == None:
129 print("XaPP terminated via signal {0}\n".format(-1 * xapp_status));