2 #----------------------------------------------------------------------------------
4 # Copyright (c) 2021 AT&T Intellectual Property.
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 #---------------------------------------------------------------------------------
20 # --------------------------------------------------------------------------------------------------------------
21 # Abstract: This will send an application registration message to
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
27 # field:[field:...]:{*|var-name}
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
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.
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.
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.
46 # Date: 28 January 2021
47 # Author: E. Scott Daniels
48 # --------------------------------------------------------------------------------------------------------------
50 # quote the two parms passed in making json style output quoting less cumbersome inline.
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
59 if [[ -d /playpen/bin ]] # where python things live since python doesn't use PATH :(
61 PATH=/playpen/bin:$PATH
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 :(
71 df="${XAPP_DESCRIPTOR_PATH:-/opt/ric/config/config-file.json}" # default is where helm/kubernetes seems to deposit it
74 if [[ -d $df ]] # we got a bloody directory... must play hide and go seek
76 echo "[INFO] df ($df) is a directory, sussing out a config file from its depths" >&2
77 if [[ -e $df/config-file.json ]]
79 echo "[INFO] df found: $df/config-file.json" >&2
80 df=$df/config-file.json
82 cf=$( ls $df/*.json|head -1 )
85 echo "[FAIL] no json file found in $df" >&2
89 echo "[INFO] df is a directory, using: $df" >&2
92 echo "[INFO] descriptor file: $df" >&2
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 :(
98 echo "[INFO] xam_register: parsing descriptor: $df" >&2
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:*"
106 python3 $bin_dir/j2src.py debug $df $config_fields >$sf
108 echo "[WARN] descriptor file isn't there or is empty: $df" >&2
111 echo "[INFO] sourcing info from config" >&2
113 echo "[INFO] end sourced data" >&2
115 # -------------------------------------------------------------------------------
117 unregister=0 # -U to turn this into an unregister
118 forreal="" # set when no-exec mode enabled
120 # commandline parms override what we find in the config, so parsed late
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
128 -V) version=$2; shift;;
130 *) echo "[FAIL] unrecognised option: $1" >&2
138 if [[ $xam_url != "http"* ]]
140 echo "[FAIL] url for xapp manager is not close to being valid: $xam_url" >&2
144 if [[ -z $xapp_name || -z $version ]]
146 echo "[FAIL] could not find xapp name and/or version in the config; not supplied on the command line" >&2
152 echo "[INFO] sending unregister to xAPP mgr: $xam_url" >&2
153 app_name=$( quote appName "$xapp_name" )
154 app_in_name=$( quote appInstanceName "?????" )
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 }"
160 rm -fr /tmp/PID$$.* # clean things (more important in test env than container)
166 config_junk=$( encode_json.py $df ) # squish the config and escape quotes etc
168 echo "[FAIL] no descriptor file (config) found, or file had no size" >&2
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" )
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
187 # tidy the space before going