Added A1 policy handler, healthcheck handler, sdl handler and alarm
[ric-app/hw-python.git] / init / init_script.py
1 # ==================================================================================
2 #
3 #       Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved.
4 #
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
8 #
9 #          http://www.apache.org/licenses/LICENSE-2.0
10 #
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 #
17 # ==================================================================================
18
19
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
22 # xapp process
23
24 import json
25 import sys
26 import os
27 import signal
28 import time
29
30 default_routing_file = "/opt/route/test_route.rt"
31 lport = 0
32
33
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)
38     else:
39         print("Sending signal {0} to xapp ...".format(signum), flush=True)
40         xapp_subprocess.send_signal(signum)
41
42
43 def parseConfigJson(config):
44     for k1 in config.keys():
45         if k1 in ParseSection:
46             result = ParseSection[k1](config)
47             if not result:
48                 return False
49
50
51 def getMessagingInfo(config):
52     global lport
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)
60                 return True
61     if lport == 0:
62         print("Error! No valid listening port", flush=True)
63         return False
64
65
66 def getXappName(config):
67     myKey = "xapp_name"
68     if myKey not in config.keys():
69         print(("Error ! No information found for {0} in config\n".format(myKey)), flush=True)
70         return False
71
72     xapp_name = config[myKey]
73     print("Xapp Name is: " + xapp_name)
74     os.environ["XAPP_NAME"] = xapp_name
75
76
77 ParseSection = dict()
78 ParseSection["xapp_name"] = getXappName
79 ParseSection["messaging"] = getMessagingInfo
80
81 # ================================================================
82 if __name__ == "__main__":
83
84     import subprocess
85
86     cmd = ["/usr/local/bin/run-hw-python.py"]
87     config_file = os.getenv("CONFIG_FILE", None)
88
89     if config_file is None:
90         print("Error! No configuration file specified\n", flush=True)
91         sys.exit(1)
92
93     with open(config_file, 'r') as f:
94         try:
95             config = json.load(f)
96         except Exception as e:
97             print(("Error loading json file from {0}. Reason = {1}\n".format(config_file, e)), flush=True)
98             sys.exit(1)
99
100     result = parseConfigJson(config)
101     if not result:
102         print("Error parsing config json. Not executing xAPP", flush=True)
103         sys.exit(1)
104
105     else:
106
107         print("Config read successfully", flush=True)
108
109         # Register signal handlers
110         signal.signal(signal.SIGINT, signal_handler)
111         signal.signal(signal.SIGTERM, signal_handler)
112
113         # Start the xAPP
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
117
118         # Periodically poll the process every 5 seconds to check if still alive
119         while 1:
120             xapp_status = xapp_subprocess.poll()
121             if xapp_status is None:
122                 time.sleep(5)
123             else:
124                 print("XaPP terminated via signal {0}\n".format(-1 * xapp_status), flush=True)
125                 break