Initial commit of Admission Control xAPP and E2AP/X2AP definitions
[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
32
33 def parseConfigJson(config):
34     for key in config:
35         print("Processing ", key);
36         if key in ParseSection:
37             result = ParseSection[key](config);
38             if result == False:
39                 return False;
40
41         
42 def getRMRTable(config):
43     myKey= "rmr";
44     if myKey not in config:
45         print(("Error ! No information found for {0} in config\n".format(myKey)));
46         return False;
47
48     # Get the rmr routing table
49     if "file_path" not in config[myKey]:
50         print(("Warning ! No file path specified to store seed routing table. Choosing default = {1}\n".format(default_routing_file)));
51         route_file = default_routing_file;
52     else:
53         route_file = config[myKey]["file_path"];
54
55     # Get the rmr routing table contents
56     if "contents" not in config[myKey]:
57         print("No contents for routing table found in config");
58         return False;
59     else:
60         route_contents = config[myKey]["contents"];
61         
62     # Get directory : if not exists create it
63     directory = os.path.dirname(route_file);
64     if not os.path.exists(directory):
65         # create directory
66         try:
67             os.mkdir(directory);
68         except OSError as oe:
69             print(("Error making directory {0}. Reason = {1}\n".format(directory, oe)));
70             return False;
71
72     # Write contents to file
73     try:
74         with open(route_file, "w") as f :
75             f.write(config[myKey]["contents"]);
76             f.close();
77     except Exception as e:
78         print(("Error writing contents to file {0}. Reason = {1}\n".format(route_file, e)));
79         return False;
80
81     # Set the environment variable
82     os.environ["RMR_SEED_RT"] = route_file;
83
84 def getPort(config):
85     myKey = "service_ports";
86     if myKey not in config:
87         print(("Error ! No information found for {0} in config\n".format(myKey)));
88         return False;
89     port_config = config[myKey];
90     if "xapp_port" in port_config:
91         try:
92             xapp_port = int(port_config["xapp_port"]);
93             if xapp_port < 1024:
94                 raise Exception("Port must be > 1024");
95         except Exception as e:
96             print(("Error processing xapp port {0}. Reason = {1}\n".format(port_config["xapp_port"], e)));
97             return False;
98     else:
99         xapp_port = 0;
100         
101
102 def getEnvs(config):
103     myKey = "envs";
104     if myKey not in config:
105         print(("Error ! No information found for {0} in config\n".format(myKey)));
106         return False;
107     
108     env_config = config[myKey];
109     
110     for env_key in env_config:
111         os.environ[env_key] = env_config[env_key];
112         print("Set environment variable {0} = {1}\n".format(env_key, os.environ[env_key]));
113
114     return True;
115
116 ParseSection = {};
117 xapp_port = 0;
118 ParseSection["rmr"] = getRMRTable;
119 ParseSection["envs"] = getEnvs;
120
121 default_routing_file = "/tmp/routeinfo/routes.txt";
122
123
124 #================================================================
125 if __name__ == "__main__":
126
127     import subprocess;
128     cmd = ["/usr/local/bin/adm-ctrl-xapp"];
129     if xapp_port > 0:
130         cmd.append("-p");
131         cmd.append(xapp_port);
132         
133     if len(sys.argv) > 1:
134         config_file = sys.argv[1];
135     else:
136         print("Error! No configuration file specified\n");
137         sys.exit(1);
138         
139
140     with open(config_file, 'r') as f:
141          try:
142              config = json.load(f);
143          except Exception as e:
144              print(("Error loading json file from {0}. Reason = {1}\n".format(config_file, e)));
145              sys.exit(1);
146              
147     result = parseConfigJson(config);
148
149     if result == False:
150         print("Error parsing json. Not executing xAPP");
151         sys.exit(1);
152
153     else:
154         print("Executing xAPP ....");
155         p = subprocess.check_call(cmd);