Updated documentation for mock a1 tool
[ric-app/admin.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
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     for key in config:
46         print("Processing ", key);
47         if key in ParseSection:
48             result = ParseSection[key](config);
49             if result == False:
50                 return False;
51
52         
53 def getRMRTable(config):
54     myKey= "rmr";
55     if myKey not in config:
56         print(("Error ! No information found for {0} in config\n".format(myKey)));
57         return False;
58
59     # Get the rmr routing table
60     if "file_path" not in config[myKey]:
61         print(("Warning ! No file path specified to store seed routing table. Choosing default = {1}\n".format(default_routing_file)));
62         route_file = default_routing_file;
63     else:
64         route_file = config[myKey]["file_path"];
65
66     # Get the rmr routing table contents
67     if "contents" not in config[myKey]:
68         print("No contents for routing table found in config");
69         return False;
70     else:
71         route_contents = config[myKey]["contents"];
72         
73     # Get directory : if not exists create it
74     directory = os.path.dirname(route_file);
75     if not os.path.exists(directory):
76         # create directory
77         try:
78             os.mkdir(directory);
79         except OSError as oe:
80             print(("Error making directory {0}. Reason = {1}\n".format(directory, oe)));
81             return False;
82
83     # Write contents to file
84     try:
85         with open(route_file, "w") as f :
86             f.write(config[myKey]["contents"]);
87             f.close();
88     except Exception as e:
89         print(("Error writing contents to file {0}. Reason = {1}\n".format(route_file, e)));
90         return False;
91
92     # Set the environment variable
93     os.environ["RMR_SEED_RT"] = route_file;
94
95 def getPort(config):
96     myKey = "service_ports";
97     if myKey not in config:
98         print(("Error ! No information found for {0} in config\n".format(myKey)));
99         return False;
100     port_config = config[myKey];
101     if "xapp_port" in port_config:
102         try:
103             xapp_port = int(port_config["xapp_port"]);
104             if xapp_port < 1024:
105                 raise Exception("Port must be > 1024");
106         except Exception as e:
107             print(("Error processing xapp port {0}. Reason = {1}\n".format(port_config["xapp_port"], e)));
108             return False;
109     else:
110         xapp_port = 0;
111         
112
113 def getEnvs(config):
114     myKey = "envs";
115     if myKey not in config:
116         print(("Error ! No information found for {0} in config\n".format(myKey)));
117         return False;
118     
119     env_config = config[myKey];
120     
121     for env_key in env_config:
122         os.environ[env_key] = env_config[env_key];
123         print("Set environment variable {0} = {1}\n".format(env_key, os.environ[env_key]));
124
125     return True;
126
127
128 # Global variables ...
129 xapp_subprocess = None;
130 xapp_pid = None;
131 ParseSection = {};
132 xapp_port = 0;
133 ParseSection["rmr"] = getRMRTable;
134 ParseSection["envs"] = getEnvs;
135
136 default_routing_file = "/tmp/routeinfo/routes.txt";
137
138
139 #================================================================
140 if __name__ == "__main__":
141
142     import subprocess;
143     cmd = ["/usr/local/bin/adm-ctrl-xapp"];
144     if xapp_port > 0:
145         cmd.append("-p");
146         cmd.append(xapp_port);
147         
148     if len(sys.argv) > 1:
149         config_file = sys.argv[1];
150     else:
151         print("Error! No configuration file specified\n");
152         sys.exit(1);
153         
154     if len(sys.argv) > 2:
155         cmd[0] = sys.argv[2];
156
157     with open(config_file, 'r') as f:
158          try:
159              config = json.load(f);
160          except Exception as e:
161              print(("Error loading json file from {0}. Reason = {1}\n".format(config_file, e)));
162              sys.exit(1);
163              
164     result = parseConfigJson(config);
165
166     if result == False:
167         print("Error parsing json. Not executing xAPP");
168         sys.exit(1);
169
170     else:
171
172         # Register signal handlers
173         signal.signal(signal.SIGINT, signal_handler);
174         signal.signal(signal.SIGTERM, signal_handler);
175
176         # Start the xAPP
177         #print("Executing xAPP ....");
178         xapp_subprocess = subprocess.Popen(cmd, shell = False, stdin=None, stdout=None, stderr = None);
179         xapp_pid = xapp_subprocess.pid;
180
181         # Periodically poll the process every 5 seconds to check if still alive
182         while(1):
183             xapp_status = xapp_subprocess.poll();
184             if xapp_status == None:
185                 time.sleep(5);
186             else:
187                 print("XaPP terminated via signal {0}\n".format(-1 * xapp_status));
188                 break;
189