Old ASN1 Decoding/Encoding files removal
[ric-app/hw.git] / init / init_script.py
1 #==================================================================================
2
3 #        Copyright (c) 2018-2019 AT&T Intellectual Property.
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 # Author : Ashwin Sridharan
20 #   Date    : Feb 2019
21 #
22
23
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
26 # xapp process
27
28 import json;
29 import sys;
30 import os;
31 import signal;
32 import time;
33 import ast;
34
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");
39     else:
40         print("Sending signal {0} to xapp ...".format(signum));
41         xapp_subprocess.send_signal(signum);
42         
43
44 def parseConfigJson(config):
45     
46     for k1 in config.keys():
47         if k1 in ParseSection:
48             result = ParseSection[k1](config);
49             if result == False:
50                     return False;
51
52         
53 #        for k2 in config[k1].keys():
54             #print(k2);
55 #            if k2 in ParseSection:
56 #                result = ParseSection[k2](config[k1]);
57 #                if result == False:
58 #                    return False;
59
60
61         
62 def getMessagingInfo(config):
63      if 'messaging' in config.keys() and 'ports' in config['messaging'].keys():
64         port_list = config['messaging']['ports']
65         for portdesc in port_list :
66             if 'port' in portdesc.keys() and 'name' in portdesc.keys() and portdesc['name'] == 'rmr-data':
67                 lport = portdesc['port']
68                 # Set the environment variable
69                 os.environ["HW_PORT"] = str(lport)
70                 return True;
71      if lport == 0:
72          print("Error! No valid listening port");
73          return False;
74
75 def getXappName(config):
76     myKey = "xapp_name";
77     if myKey not in config.keys():
78         print(("Error ! No information found for {0} in config\n".format(myKey)));
79         return False;
80
81     xapp_name = config[myKey];
82     print("Xapp Name is: " + xapp_name); 
83     os.environ["XAPP_NAME"] = xapp_name;
84
85 default_routing_file = "/tmp/routeinfo/routes.txt";
86 lport = 0;
87 ParseSection = {};
88 ParseSection["xapp_name"] = getXappName;
89 ParseSection["messaging"] = getMessagingInfo;
90
91
92 #================================================================
93 if __name__ == "__main__":
94
95     import subprocess;
96 #    cmd = ["../src/hw_xapp_main"];
97     cmd = ["/usr/local/bin/hw_xapp_main"];
98         
99     if len(sys.argv) > 1:
100         config_file = sys.argv[1];
101     else:
102         print("Error! No configuration file specified\n");
103         sys.exit(1);
104         
105     if len(sys.argv) > 2:
106         cmd[0] = sys.argv[2];
107
108     with open(config_file, 'r') as f:
109          try:
110              config = json.load(f);
111          except Exception as e:
112              print(("Error loading json file from {0}. Reason = {1}\n".format(config_file, e)));
113              sys.exit(1);
114              
115     result = parseConfigJson(config);
116     time.sleep(10);
117     if result == False:
118         print("Error parsing json. Not executing xAPP");
119         sys.exit(1);
120
121     else:
122
123         # Register signal handlers
124         signal.signal(signal.SIGINT, signal_handler);
125         signal.signal(signal.SIGTERM, signal_handler);
126
127         # Start the xAPP
128         #print("Executing xAPP ....");
129         xapp_subprocess = subprocess.Popen(cmd, shell = False, stdin=None, stdout=None, stderr = None);
130         xapp_pid = xapp_subprocess.pid;
131
132         # Periodically poll the process every 5 seconds to check if still alive
133         while(1):
134             xapp_status = xapp_subprocess.poll();
135             if xapp_status == None:
136                 time.sleep(5);
137             else:
138                 print("XaPP terminated via signal {0}\n".format(-1 * xapp_status));
139                 break;
140