1 # ==================================================================================
3 # Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
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.
17 # ==================================================================================
20 # This initialization script reads in a json from the specified config map path
21 # to set up the initializations (route config map, variables etc) for the main
30 default_routing_file = "/opt/route/test_route.rt"
34 def signal_handler(signum, frame):
35 print("Received signal {0}\n".format(signum))
36 if xapp_subprocess is None or xapp_pid is None:
37 print("No xapp running. Quiting without sending signal to xapp\n", flush=True)
39 print("Sending signal {0} to xapp ...".format(signum), flush=True)
40 xapp_subprocess.send_signal(signum)
43 def parseConfigJson(config):
44 for k1 in config.keys():
45 if k1 in ParseSection:
46 result = ParseSection[k1](config)
51 def getMessagingInfo(config):
53 if 'messaging' in config.keys() and 'ports' in config['messaging'].keys():
54 port_list = config['messaging']['ports']
55 for portdesc in port_list:
56 if 'port' in portdesc.keys() and 'name' in portdesc.keys() and portdesc['name'] == 'rmr-data':
57 lport = portdesc['port']
58 # Set the environment variable
59 os.environ["HW_PORT"] = str(lport)
62 print("Error! No valid listening port", flush=True)
66 def getXappName(config):
68 if myKey not in config.keys():
69 print(("Error ! No information found for {0} in config\n".format(myKey)), flush=True)
72 xapp_name = config[myKey]
73 print("Xapp Name is: " + xapp_name)
74 os.environ["XAPP_NAME"] = xapp_name
78 ParseSection["xapp_name"] = getXappName
79 ParseSection["messaging"] = getMessagingInfo
81 # ================================================================
82 if __name__ == "__main__":
86 cmd = ["/usr/local/bin/run-hw-python.py"]
87 config_file = os.getenv("CONFIG_FILE", None)
89 if config_file is None:
90 print("Error! No configuration file specified\n", flush=True)
93 with open(config_file, 'r') as f:
96 except Exception as e:
97 print(("Error loading json file from {0}. Reason = {1}\n".format(config_file, e)), flush=True)
100 result = parseConfigJson(config)
102 print("Error parsing config json. Not executing xAPP", flush=True)
107 print("Config read successfully", flush=True)
109 # Register signal handlers
110 signal.signal(signal.SIGINT, signal_handler)
111 signal.signal(signal.SIGTERM, signal_handler)
114 print("Executing xAPP ....", flush=True)
115 xapp_subprocess = subprocess.Popen(cmd, shell=False, stdin=None, stdout=None, stderr=None)
116 xapp_pid = xapp_subprocess.pid
118 # Periodically poll the process every 5 seconds to check if still alive
120 xapp_status = xapp_subprocess.poll()
121 if xapp_status is None:
124 print("XaPP terminated via signal {0}\n".format(-1 * xapp_status), flush=True)