Update traversal from AJSC 2 to Spring Boot
[aai/traversal.git] / aai-traversal / src / main / scripts / putTool.sh
1 #!/bin/ksh
2 #
3 # ============LICENSE_START=======================================================
4 # org.onap.aai
5 # ================================================================================
6 # Copyright © 2017 AT&T Intellectual Property. All rights reserved.
7 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11 #
12 #    http://www.apache.org/licenses/LICENSE-2.0
13 #
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END=========================================================
20 #
21 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 #
23
24 #
25 # The script is called with a resource, filepath, an optional argument to
26 # ignore HTTP failure codes which would otherwise indicate a failure,
27 # and an optional argument to display more data.
28 # It invokes a PUT on the resource with the file using curl
29 # Uses aaiconfig.properties for authorization type and url. The HTTP response
30 # code is checked. Responses between 200 and 299 are considered success.
31 # When the ignore failure code parameter is passed, responses outside of
32 # the 200 to 299 range but matching a sub-string of the parameter are
33 # considered success. For example, a parameter value of 412 will consider
34 # responses in the range of 200 to 299 and 412 successes.
35 #
36 # method checking parameter list for two strings, and determine if
37 # the second string is a sub-string of the first
38 contains() {
39     string="$1"
40     substring="$2"
41     if test "${string#*$substring}" != "$string"
42     then
43         return 0    # $substring is in $string
44     else
45         return 1    # $substring is not in $string
46     fi
47 }
48
49 display_usage() {
50         cat <<EOF
51         Usage: $0 [options]
52
53         1. Usage: putTool.sh <resource-path> <json payload file> <optional HTTP Response code> <optional -display>
54         2. This script requires two arguments, a resource path and a file path to a json file containing the payload.
55         3. Example: query?format=xxxx customquery.json (possible formats are simple, raw, console, count, graphson, id, pathed, resource and resource_and_url)
56         4. Adding the optional HTTP Response code will allow the script to ignore HTTP failure codes that match the input parameter.
57         5. Adding the optional "-display" argument will display all data returned from the request, instead of just a response code.
58                 
59 EOF
60 }
61 if [ $# -eq 0 ]; then
62         display_usage
63         exit 1
64 fi
65
66 # remove leading slash when present
67 RESOURCE=`echo $1 | sed "s,^/,,"`
68 if [ -z $RESOURCE ]; then
69         echo "resource parameter is missing"
70         echo "usage: $0 resource file [expected-failure-codes]"
71         exit 1
72 fi
73 JSONFILE=$2
74 if [ -z $JSONFILE ]; then
75         echo "json file parameter is missing"
76         echo "usage: $0 resource file [expected-failure-codes]"
77         exit 1
78 fi
79 echo `date` "   Starting $0 for resource $RESOURCE"
80 ALLOWHTTPRESPONSES=$3
81
82 XFROMAPPID="AAI-TOOLS"
83 XTRANSID=`uuidgen`
84
85 userid=$( id | cut -f2 -d"(" | cut -f1 -d")" )
86 if [ "${userid}" != "aaiadmin" ]; then
87     echo "You must be aaiadmin to run $0. The id used $userid."
88     exit 1
89 fi
90
91 . /etc/profile.d/aai.sh
92 PROJECT_HOME=/opt/app/aai-traversal
93 prop_file=$PROJECT_HOME/resources/etc/appprops/aaiconfig.properties
94 log_dir=$PROJECT_HOME/logs/misc
95 today=$(date +\%Y-\%m-\%d)
96
97 RETURNRESPONSE=false
98 if [ ${#} -ne 2 ]; then
99     if [ "$3" = "-display" ]; then
100         RETURNRESPONSE=true
101     fi
102 fi
103 if [ ${#} -ne 3 ]; then
104     if [ "$4" = "-display" ]; then
105         RETURNRESPONSE=true
106     fi
107 fi
108
109 MISSING_PROP=false
110 RESTURL=`grep ^aai.server.url= $prop_file |cut -d'=' -f2 |tr -d "\015"`
111 if [ -z $RESTURL ]; then
112         echo "Property [aai.server.url] not found in file $prop_file"
113         MISSING_PROP=true
114 fi
115 USEBASICAUTH=false
116 BASICENABLE=`grep ^aai.tools.enableBasicAuth $prop_file |cut -d'=' -f2 |tr -d "\015"`
117 if [ -z $BASICENABLE ]; then
118         USEBASICAUTH=false
119 else
120         USEBASICAUTH=true
121         CURLUSER=`grep ^aai.tools.username $prop_file |cut -d'=' -f2 |tr -d "\015"`
122         if [ -z $CURLUSER ]; then
123                 echo "Property [aai.tools.username] not found in file $prop_file"
124                 MISSING_PROP=true
125         fi
126         CURLPASSWORD=`grep ^aai.tools.password $prop_file |cut -d'=' -f2 |tr -d "\015"`
127         if [ -z $CURLPASSWORD ]; then
128                 echo "Property [aai.tools.password] not found in file $prop_file"
129                 MISSING_PROP=true
130         fi
131 fi
132
133 if [ $MISSING_PROP = false ]; then
134         if [ $USEBASICAUTH = false ]; then
135                 AUTHSTRING="--cert $PROJECT_HOME/resources/etc/auth/aaiClientPublicCert.pem --key $PROJECT_HOME/resources/etc/auth/aaiClientPrivateKey.pem"
136         else
137                 AUTHSTRING="-u $CURLUSER:$CURLPASSWORD"
138         fi
139
140         if [ $RETURNRESPONSE = true ]; then
141                         curl --request PUT -sL -k $AUTHSTRING -H "Content-Type: application/json" -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID" -H "Accept: application/json" -T $JSONFILE $RESTURL$RESOURCE | python -mjson.tool
142                         RC=$?
143                 else
144                 result=`curl --request PUT -sL -w "%{http_code}" -o /dev/null -k $AUTHSTRING -H "Content-Type: application/json" -H "X-FromAppId: $XFROMAPPID" -H "X-TransactionId: $XTRANSID" -H "Accept: application/json" -T $JSONFILE $RESTURL$RESOURCE`
145                 #echo "result is $result."
146                 RC=0;
147                 if [ $? -eq 0 ]; then
148                 case $result in
149                         +([0-9])?)
150                                 #if [[ "$result" -eq 412 || "$result" -ge 200 && $result -lt 300 ]]
151                                 if [[ "$result" -ge 200 && $result -lt 300 ]]
152                                 then
153                                         echo "PUT result is OK,  $result"
154                                 else
155                                         if [ -z $ALLOWHTTPRESPONSES ]; then
156                                                 echo "PUT request failed, response code was  $result"
157                                                 RC=$result
158                                         else
159                                                 contains $ALLOWHTTPRESPONSES $result
160                                                 if [ $? -ne 0 ]
161                                                 then
162                                                         echo "PUT request failed, unexpected response code was  $result"
163                                                         RC=$result
164                                                 else
165                                                         echo "PUT result is expected,  $result"
166                                                 fi
167                                         fi
168                                 fi
169                                 ;;
170                         *)
171                                 echo "PUT request failed, response was $result"
172                                 RC=-1
173                                 ;;
174
175                 esac
176                 else
177                 echo "FAILED to send request to $RESTURL"
178                 RC=-1
179                 fi
180         fi      
181 else
182         echo "usage: $0 resource file [expected-failure-codes]"
183         RC=-1
184 fi
185
186 echo `date` "   Done $0, returning $RC"
187 exit $RC