d002a830854651b4dca97a0ebae3b5c1db7b3720
[ric-app/mc.git] / mc-core / mc / queries / generate_runall.py
1 # ------------------------------------------------
2 #Copyright 2020 AT&T Intellectual Property
3 #   Licensed under the Apache License, Version 2.0 (the "License");
4 #   you may not use this file except in compliance with the License.
5 #   You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #   Unless required by applicable law or agreed to in writing, software
10 #   distributed under the License is distributed on an "AS IS" BASIS,
11 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #   See the License for the specific language governing permissions and
13 #   limitations under the License.
14 # ------------------------------------------- 
15
16 import xml.etree.ElementTree as ET
17 import json
18 from optparse import OptionParser
19 import sys
20 import os
21
22
23
24 optparser = OptionParser(usage="usage: %prog [options] path_file [path_file*]")
25 optparser.add_option("-Q", "--querydir", dest="querydir",
26                  default=".",help="directory with qtree.xml and output_spec.cfg" )
27 (options, args) = optparser.parse_args()
28
29 odoc = ""
30
31 nibflnm = options.querydir+"/nib.json"
32 nibfl = open(nibflnm, "r")
33 nib = json.load(nibfl)
34 nibfl.close()
35
36 osflnm = options.querydir+"/output_spec.cfg"
37 osfl = open(osflnm, "r")
38 oqy = {}
39 for line in osfl:
40         flds = line.split(",")
41         oqy[flds[0]]=1
42
43 runall = """
44 #!/bin/bash
45
46 # -------------------------------------------------------------------------------
47 #    Copyright (c) 2018-2019 AT&T Intellectual Property.
48 #
49 #   Licensed under the Apache License, Version 2.0 (the "License");
50 #   you may not use this file except in compliance with the License.
51 #   You may obtain a copy of the License at
52 #
53 #       http://www.apache.org/licenses/LICENSE-2.0
54 #
55 #   Unless required by applicable law or agreed to in writing, software
56 #   distributed under the License is distributed on an "AS IS" BASIS,
57 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
58 #   See the License for the specific language governing permissions and
59 #   limitations under the License.
60 # -------------------------------------------------------------------------------
61
62 set -m
63
64 FIFO_DIR="/tmp/mcl/fifos"
65
66 SIMULATOR_MODE=`python /mc/extract_params.py ${XAPP_DESCRIPTOR_PATH}/config-file.json simulator_mode`
67
68 DEBUG_MODE=`python /mc/extract_params.py ${XAPP_DESCRIPTOR_PATH}/config-file.json debug_mode`
69
70 WINDOW=`python /mc/extract_params.py ${XAPP_DESCRIPTOR_PATH}/config-file.json measurement_interval`
71
72 RMR_PORT=`python /mc/extract_rmr_port.py ${XAPP_DESCRIPTOR_PATH}/config-file.json rmr-data-out`
73
74 # export DBAAS_SERVICE_HOST=`python /mc/extract_params.py ${XAPP_DESCRIPTOR_PATH}/config-file.json __DBAAS_SERVICE_HOST__`
75 # export DBAAS_SERVICE_PORT=`python /mc/extract_params.py ${XAPP_DESCRIPTOR_PATH}/config-file.json __DBAAS_SERVICE_PORT__`
76
77 if [ "$SIMULATOR_MODE" = "true" ]
78 then
79         mkdir -p $FIFO_DIR
80         python /mc/data_gen/dc_gen.py &
81         python /mc/data_gen/rrcx_gen.py &
82 fi
83
84 if [ "$WINDOW" = "" ]
85 then
86         WINDOW="10000"
87 fi
88
89 VES_COLLECTOR=`python /mc/extract_params.py ${XAPP_DESCRIPTOR_PATH}/config-file.json ves_collector_address`
90
91 VES_NAME=`echo $VES_COLLECTOR | awk 'BEGIN{FS=":"} {print $1}'`
92 VES_PORT=`echo $VES_COLLECTOR | awk 'BEGIN{FS=":"} {print $2}'`
93 VES_IP=`getent ahosts $VES_NAME | awk '{ print $1; exit }'`
94
95 echo "Clearing MC NIB namespace" >&2
96 /mc/gs-lite/bin/mc_clear >&2
97
98 echo "Storing MC NIB schemas" >&2
99 /mc/gs-lite/bin/mc_store_schema >&2
100
101 ./runit
102 sleep 1
103
104 """
105
106 debug_q = ""
107 for q in oqy:
108         if "debug" in q:
109                 debug_q += "\t/mc/gs-lite/bin/gsprintconsole -e `cat gshub.log` default "+q+" window=$WINDOW &\n"
110
111 if len(debug_q)>0:
112         runall += """
113 if [ "$DEBUG_MODE" = "true" ]
114 then
115     # invoke gsprintconsole for all the queries with debug in their name
116 """
117         runall += debug_q
118         runall += """
119 fi
120 """
121
122 runall += """
123
124 if [ "$RMR_PORT" != "" ]
125 then
126     RMR_OPTION="-R $RMR_PORT"
127 fi
128
129 # invoke gsprintconsole_ves gsmcnib for all non-debug queries
130 """
131
132 for q in oqy:
133         if "debug" not in q:
134                 runall += " /mc/gs-lite/bin/gsprintconsole_ves -C $VES_IP:$VES_PORT -U /vescollector/eventListener/v7 -V 7 $RMR_OPTION `cat gshub.log` default "+q+" window=$WINDOW &\n"
135                 keys = nib[q]["keys"]
136                 if len(keys)>0:
137                         keys_str = ",".join(keys)
138                         runall += " /mc/gs-lite/bin/gsmcnib -K "+keys_str+" `cat gshub.log` default "+q+" window=$WINDOW &\n"
139                 else:
140                         runall += " /mc/gs-lite/bin/gsmcnib `cat gshub.log` default "+q+" window=$WINDOW &\n"
141
142 runall += """
143
144 sleep 1
145 bash /mc/gs-lite/bin/start_processing
146 fg %1
147 """
148
149 rflnm = options.querydir+"/runall"
150 rfl = open(rflnm, "w")
151 rfl.write(runall)
152 rfl.close()
153
154 cmd = "chmod +x "+rflnm
155 sys.stderr.write("Executing "+cmd+"\n")
156 ret = os.system(cmd)
157 if ret != 0:
158         sys.stderr.write("Error executing "+cmd+"\n")
159         exit(1)
160 exit(0)