d64522700d58358496c1935c6b5348562e9ca94d
[ric-app/bouncer.git] / Bouncer / init / init_script.py
1 # ==================================================================================
2 # Copyright (c) 2020 HCL Technologies Limited.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ==================================================================================
16
17 # This initialization script reads in a json from the specified config map path
18 # to set up the initializations (route config map, variables etc) for the main
19 # xapp process
20
21 import json;
22 import sys;
23 import os;
24 import signal;
25 import time;
26 import ast;
27
28 def signal_handler(signum, frame):
29     print("Received signal {0}\n".format(signum));
30     if(xapp_subprocess == None or xapp_pid == None):
31         print("No xapp running. Quiting without sending signal to xapp\n");
32     else:
33         print("Sending signal {0} to xapp ...".format(signum));
34         xapp_subprocess.send_signal(signum);
35         
36
37 def parseConfigJson(config):
38     
39     for k1 in config.keys():
40         if k1 in ParseSection:
41             result = ParseSection[k1](config);
42             if result == False:
43                     return False;
44
45         
46 #        for k2 in config[k1].keys():
47             #print(k2);
48 #            if k2 in ParseSection:
49 #                result = ParseSection[k2](config[k1]);
50 #                if result == False:
51 #                    return False;
52
53
54         
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["BOUNCER_PORT"] = str(lport)
63                 return True;
64      if lport == 0:
65          print("Error! No valid listening port");
66          return False;
67
68 def getXappName(config):
69     myKey = "xapp_name";
70     if myKey not in config.keys():
71         print(("Error ! No information found for {0} in config\n".format(myKey)));
72         return False;
73
74     xapp_name = config[myKey];
75     print("Xapp Name is: " + xapp_name); 
76     os.environ["XAPP_NAME"] = xapp_name;
77
78 default_routing_file = "/tmp/routeinfo/routes.txt";
79 lport = 0;
80 ParseSection = {};
81 ParseSection["xapp_name"] = getXappName;
82 ParseSection["messaging"] = getMessagingInfo;
83
84
85 #================================================================
86 if __name__ == "__main__":
87
88     import subprocess;
89     cmd = ["/usr/local/bin/b_xapp_main"];
90         
91     if len(sys.argv) > 1:
92         config_file = sys.argv[1];
93     else:
94         print("Error! No configuration file specified\n");
95         sys.exit(1);
96         
97     if len(sys.argv) > 2:
98         cmd[0] = sys.argv[2];
99
100     with open(config_file, 'r') as f:
101          try:
102              config = json.load(f);
103          except Exception as e:
104              print(("Error loading json file from {0}. Reason = {1}\n".format(config_file, e)));
105              sys.exit(1);
106              
107     result = parseConfigJson(config);
108     time.sleep(10);
109     if result == False:
110         print("Error parsing json. Not executing xAPP");
111         sys.exit(1);
112
113     else:
114
115         # Register signal handlers
116         signal.signal(signal.SIGINT, signal_handler);
117         signal.signal(signal.SIGTERM, signal_handler);
118
119         # Start the xAPP
120         #print("Executing xAPP ....");
121         xapp_subprocess = subprocess.Popen(cmd, shell = False, stdin=None, stdout=None, stderr = None);
122         xapp_pid = xapp_subprocess.pid;
123
124         # Periodically poll the process every 5 seconds to check if still alive
125         while(1):
126             xapp_status = xapp_subprocess.poll();
127             if xapp_status == None:
128                 time.sleep(5);
129             else:
130                 print("XaPP terminated via signal {0}\n".format(-1 * xapp_status));
131                 break;
132