a553cd39d5f9fa5ca968d5af49ebe1d86fd1736e
[ric-app/mc.git] / mc-core / package / xam_register.sh
1 # vim: noet ts=4 :
2 #----------------------------------------------------------------------------------
3 #
4 #   Copyright (c) 2021 AT&T Intellectual Property.
5 #
6 #   Licensed under the Apache License, Version 2.0 (the "License");
7 #   you may not use this file except in compliance with the License.
8 #   You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 #   Unless required by applicable law or agreed to in writing, software
13 #   distributed under the License is distributed on an "AS IS" BASIS,
14 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #   See the License for the specific language governing permissions and
16 #   limitations under the License.
17 #
18 #---------------------------------------------------------------------------------
19
20 # --------------------------------------------------------------------------------------------------------------
21 #       Abstract:       This will send an application registration message to
22 #                               the xAPP manager.
23 #
24 #                               A note about the field descriptions given to j2src programme:
25 #                                       They define what we need from the config file. Input to j2src is
26 #
27 #                                               field:[field:...]:{*|var-name}
28 #
29 #                                       fields are considered to be an "object" if not the last. The last field may be
30 #                                       of  the form  name[]match=value@pull-name
31 #                                       where:
32 #                                               the named field is an array of objects and "match" is a field in each
33 #                                               object. Each element is tested to see if the match field's value is "value"
34 #                                               and when true, the "pull-name" field's value is used.  The description is
35 #                                               used in our list to pull the rmr-data port from the messageing section.
36 #
37 #                               Notes about URLs and DNS names:
38 #                                       According to those in the know, the xAPP manager being a platform component will have
39 #                                       a fixed DNS name, and thus we expect that.
40 #
41 #                                       In the less than obvious world of kubernetes we cannot depend on hostname actually being
42 #                                       the DNS name for our stuff. So, we assume that if the DN name is NOT the host name, the
43 #                                       variable will be set to the DNS name as RMR also depends on this for things.
44 #                                       If it's not set, we'll default to hostname.
45 #
46 #       Date:           28 January 2021
47 #       Author:         E. Scott Daniels
48 # --------------------------------------------------------------------------------------------------------------
49
50 # quote the two parms passed in making json style output quoting less cumbersome inline.
51 function quote {
52         echo "\"$1\": \"$2\""
53 }
54
55 # ------------------------------------------------------------------------------------------------
56 xam_url="http://service-ricplt-appmgr-http:8080/ric/v1"         # where we expect xAPP mgr to be waiting
57 svc_name="${RMR_SRC_ID:-$( hostname )}"                                         # the DNS name of this "host" for RMR
58
59 if [[ -d /playpen/bin ]]                # where python things live since python doesn't use PATH :(
60 then
61         PATH=/playpen/bin:$PATH
62         bin_dir=/playpen/bin
63 else
64         PATH=$PATH:.
65         bin_dir="."
66 fi
67
68 # suss out the descriptor file. The env name _should_ be a direct pointer
69 # to the file, but it might be just a pointer to the directory :(
70 #
71 df="${XAPP_DESCRIPTOR_PATH:-/opt/ric/config/config-file.json}"      # default is where helm/kubernetes seems to deposit it
72
73
74 if [[ -d $df ]]                                                                                                         # we got a bloody directory... must play hide and go seek
75 then
76         echo "[INFO] df ($df) is a directory, sussing out a config file from its depths" >&2
77         if [[ -e $df/config-file.json ]]
78         then
79                 echo "[INFO] df found: $df/config-file.json" >&2
80                 df=$df/config-file.json
81         else
82                 cf=$( ls $df/*.json|head -1 )
83                 if [[ -z $cf ]]
84                 then
85                         echo "[FAIL] no json file found in $df" >&2
86                         exit 1
87                 fi
88                 df=$df/$cf
89                 echo "[INFO] df is  a directory, using: $df" >&2
90         fi
91 fi
92 echo "[INFO] descriptor file: $df" >&2
93
94 sf=/tmp/PID$$.cfg                       # where shell config dump goes
95 touch $sf                                       # must exist
96 if [[ -s $df ]]                         # pull config stuff into src file; python can't handle a nil/empty file :(
97 then
98         echo "[INFO] xam_register: parsing descriptor: $df" >&2
99
100                                                                                         # CAUTION: these must be concatinated with a trailing space!
101         config_fields="xapp_name:* "                    # create space sep list of fields we need
102         config_fields+="version:* "
103         config_fields+="controls:app_man_url:xam_url "  # xapp manager url for registration if there
104         config_fields+="messaging:ports[]name=rmr-data@port:*"
105
106         python3 $bin_dir/j2src.py debug $df $config_fields >$sf
107 else
108         echo "[WARN] descriptor file isn't there or is empty: $df" >&2
109 fi
110
111 echo "[INFO] sourcing info from config" >&2
112 cat $sf
113 echo "[INFO] end sourced data" >&2
114 . $sf
115 # -------------------------------------------------------------------------------
116
117 unregister=0                            # -U to turn this into an unregister
118 forreal=""                                      # set when no-exec mode enabled
119
120 # commandline parms override what we find in the config, so parsed late
121 while [[ $1 == -* ]]
122 do
123         case $1 in
124                 -N)     xapp_name=$2; shift;;
125                 -n)     forreal="echo no-execute mode, would run: ";;
126                 -u)     xam_url="$2"; shift;;                           # mostly for testing, but in a pinch might be useful
127                 -U)     unregister=1;;
128                 -V) version=$2; shift;;
129
130                 *)      echo "[FAIL] unrecognised option: $1" >&2
131                         exit 1
132                         ;;
133         esac
134
135         shift
136 done
137
138 if [[ $xam_url != "http"* ]]
139 then
140         echo "[FAIL] url for xapp manager is not close to being valid: $xam_url" >&2
141         exit 1
142 fi
143
144 if [[ -z $xapp_name || -z $version ]]
145 then
146         echo "[FAIL] could not find xapp name and/or version in the config; not supplied on the command line" >&2
147         exit 1
148 fi
149
150 if (( unregister ))
151 then
152         echo "[INFO] sending unregister to xAPP mgr: $xam_url" >&2
153         app_name=$(    quote appName "$xapp_name" )
154         app_in_name=$( quote appInstanceName  "?????" )
155
156         $forreal curl -X POST "${xam_url:-http://junk-not-supplied}/deregister" -H "accept: application/json" -H "Content-Type: application/json" \
157                 -d "{ $app_name, $app_in_name }"
158         rv=$?
159
160         rm -fr /tmp/PID$$.*                                                     # clean things (more important in test env than container)
161         exit $rv
162 fi
163
164 if [[  -s $df ]]
165 then
166         config_junk=$( encode_json.py $df )                     # squish the config and escape quotes etc
167 else
168         echo "[FAIL] no descriptor file (config) found, or file had no size" >&2
169         exit 1
170 fi
171
172
173 # these are klunky, but makes quoting junk for the curl command a bit less tedious
174 app_name=$(    quote appName "$xapp_name" )
175 config_path=$( quote configPath "" )
176 app_in_name=$( quote appInstanceName  "?????" )
177 http_endpt=$(  quote httpEndpoint "" )
178 rmr_endpt=$(   quote rmrEndpoint "$svc_name:$port" )
179 config=$(      quote config "$config_junk" )
180
181 echo "[INFO] sending register to xAPP mgr: $xam_url" >&2
182 $forreal curl -X POST "${xam_url:-http://junk-not-supplied}/register" -H "accept: application/json" -H "Content-Type: application/json" \
183         -d "{ $app_name, $config_path, $app_in_name, $http_endpt, $rmr_endpt, $config }"
184 rv=$?   # use curl result as exit value not results of cleanup ops
185
186
187 # tidy the space before going
188 rm -fr /tmp/PID$$.*
189
190 exit $rv
191
192