summaryrefslogtreecommitdiffstats
path: root/src/main/scripts/resend-dmaap-events.sh
blob: 2afa3a76ae00046dd7891f593a20e32c1b3c920c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/bin/bash

###
# ============LICENSE_START=======================================================
# org.onap.aai
# ================================================================================
# Copyright (C) 2017-18 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#      http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=========================================================
###

#
# resend-dmaap-events.sh  -- This tool is used to resend dmaap events.
#    On certain scenarios due to dns or other networking issue, if A&AI fails to publish events
#       We need a mechanism to resend the dmaap events for objects that haven't modified since
#       So if a pserver object event was supposed to be sent but got lost and a later dmaap event
#       was sent out then we shouldn't be sending dmaap messages
#       It identifies if a dmaap message was already sent by looking at the resource version
#       of the dmaap object that was failed to sendand checks the snapshot and see if they are the same
#
# Parameters:
#
# -b,  (required) <string> the base url for the dmaap server 
# -e,  (required) <file>   filename containing the missed events
# -l,  (optional)          indicating that the script should be run it debug mode
#                          it will not send the dmaap messages to dmaap server 
#                          but it will write to a file named resend_dmaap_server.out
# -p,  (required) <string> the password for the dmaap server 
# -s,  (required) <file>   containing the data snapshot graphson file to compare the resource versions against
# -u,  (required) <string> the username for the dmaap server 
#
# An example of how to use the tool:
# Please use right credentials and right dmaap server in the cluster
#
#  ./resend-dmaap-events.sh -e example_events.txt -s dataSnapshot.graphSON.201808091545 -u username -p example_pass -b https://localhost:3905
#
# For each dmaap message in the example_events.txt, it will check 
# against graphson and try to send it to the dmaap server
# If the example_events.txt contains two events one that wasn't sent to dmaap
# and the other that was already updated by another PUT/DELETE
# and the output of the run will look something like this:
# 
# Output:
# Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f was sent
# Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f not sent
# 
# If lets say, there was a username password issue, you will see something like this:
# Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f was not sent due to dmaap error, please check logs
# Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f not sent
# 
# From the directory in which you are executing the script (not where the script is located)
# You can have it be located and executed in the same place 
# Check for a file called resend_dmaap_error.log as it will give you more details on the error
#
# For testing purposes, if you are trying to run this script and don't want to actually
# send it to a dmaap server, then you can run either of the following:
#
# ./resend-dmaap-events.sh -l -e example_events.txt -s dataSnapshot.graphSON.201808091545
# or
# ./resend-dmaap-events.sh -l -e example_events.txt -s dataSnapshot.graphSON.201808091545 -u username -p example_pass -b https://localhost:3905
#
# Following will output what would have been sent out based on checking the datasnapshot with example_events.txt
#
# Output:
# Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f was sent
# Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f not sent
# 
# Also it will write the dmaap events to a file called dmaap_messages.out that 
# would have been sent out in the current directory where you are executing this script
# 

current_directory=$( cd "$(dirname "$0")" ; pwd -P );
resend_error_log=${current_directory}/resend_dmaap_error.log
resend_output=${current_directory}/dmaap_messages.out

# Prints the usage of the shell script
usage(){
    echo "Usage $0 [options...]";
    echo;
    echo "  -b,   <string> the base url for the dmaap server";
    echo "  -e,   <file>   filename containing the missed events";
    echo "  -l, (optional) indicating that the script should be run it debug mode"
    echo "                 it will not send the dmaap messages to dmaap server "
    echo "                 but it will write to a file named resend_dmaap_server.out"
    echo "  -p,   <string> the password for the dmaap server";
    echo "  -s,   <file>   containing the data snapshot graphson file to compare the resource versions against";
    echo "  -u,   <string> the username for the dmaap server";
    echo;
    echo;
    echo " An example of how to use the tool:";
    echo " Please use right credentials and right dmaap server in the cluster";
    echo;
    echo "  ./resend-dmaap-events.sh -e example_events.txt -s dataSnapshot.graphSON.201808091545 -u username -p example_pass -b https://localhost:3905";
    echo;
    echo " For each dmaap message in the example_events.txt, it will check ";
    echo " against graphson and try to send it to the dmaap server";
    echo " If the example_events.txt contains two events one that wasn't sent to dmaap";
    echo " and the other that was already updated by another PUT/DELETE";
    echo " and the output of the run will look something like this:";
    echo;
    echo " Output:";
    echo " Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f was sent";
    echo " Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f not sent";
    echo " ";
    echo " If lets say, there was a username password issue, you will see something like this:";
    echo " Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f was not sent due to dmaap error, please check logs";
    echo " Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f not sent";
    echo;
    echo " From the directory in which you are executing the script (not where the script is located)";
    echo " You can have it be located and executed in the same place ";
    echo " Check for a file called resend_dmaap_error.log as it will give you more details on the error";
    echo;
    echo " For testing purposes, if you are trying to run this script and don't want to actually";
    echo " send it to a dmaap server, then you can run either of the following:";
    echo;
    echo " ./resend-dmaap-events.sh -l -e example_events.txt -s dataSnapshot.graphSON.201808091545";
    echo " or";
    echo " ./resend-dmaap-events.sh -l -e example_events.txt -s dataSnapshot.graphSON.201808091545 -u username -p example_pass -b https://localhost:3905";
    echo;
    echo " Following will output what would have been sent out based on checking the datasnapshot with example_events.txt";
    echo;
    echo " Output:";
    echo " Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f was sent";
    echo " Dmaap Event with Id 7f7d8a7b-4034-46f3-a969-d7e5cbcbf75f not sent";
    echo;
    echo " Also it will write the dmaap events to a file called dmaap_messages.out that ";
    echo " would have been sent out in the current directory where you are executing this script";
    exit;
}

# Validate the arguments being passed by user
# Checks if the argument of the string is greater than zero
# Also check if the file actually exists
validate(){
    local type_of_file=$1;

    if [ $# -eq 0 ]; then
        echo "Error expecting the validate method to have at least one argument indicating what type";
        exit -1;
    fi;

    shift;

    local arg=$1;

    if [ -z "$arg" ]; then
        echo "Error missing the expected argument for ${type_of_file}";
        exit -1;
    fi;

    if [ ! -f "$arg" ]; then
        echo "Error: file $arg cannot be found, please check the file again";
        exit -1;
    fi;
}

# Checks if the resource version in dmaap message passed for an aai-uri
# is the same as the value in the snapshot file for that version
# If the resource version is the same it will return 0 for success
# Otherwise it will return non zero to indicate that this method failed
resource_version_matches_snapshot_file(){

    local snapshot_file=$1;
    local entity_link=$2;
    local resource_version=$3;
    local action=$4;
    
    if [ -z ${resource_version} ]; then
        echo "Missing the parameter resource version to be passed";
        return -1;
    fi

    # Modify the entity link passed to remove the /aai/v[0-9]+
    aai_uri=$(echo $entity_link | sed 's/\/aai\/v[0-9][0-9]*//g');

    local line=$(grep '"value":"'${aai_uri}'"' ${snapshot_file} 2> /dev/null);

    if [ -z "${line}" ] ; then
        if [ "${action}" = "DELETE" ]; then
            return 0;
        else
            return -1;
        fi;
    fi;

    cnt=$(echo $line | grep -o '"resource-version":\[{"id":"[^"]*","value":"'$resource_version'"}\]' | wc -l);

    if [ $cnt -eq 1 ]; then
        return 0;
    else
        return -1;
    fi;
}

# From a array being passed, it will determine the smallest element
# and return the index of the smallest element
# If the array length is zero, then it will return -1
retrieve_smallest_index(){

    local elements=("${@}");

    if [ ${#elements} -eq 0 ]; then
        return -1;
    fi;

    local smallest_element=${elements[0]};

    local index=0;
    local smallest_index=0;

    for element in ${elements[@]}; do
        if [ $element -lt $smallest_element ]; then
            smallest_index=${index};
        fi;
        index=$((index+1));
    done;

    return ${smallest_index};
}

# Send the dmaap event to the host based on
# the line that was send to the function
send_dmaap(){

    local local_mode=$1;
    local line=$2;
    local username=$3;
    local password=$4;
    local baseurl=$5;
    local resp_code=0;
    
    generated_file=$(uuidgen);

    local json_file=/tmp/${generated_file}.json;
    local curl_output=/tmp/${generated_file}.txt;

    echo ${line} > ${json_file};
    > ${curl_output};
    id=$(echo $line | grep -o '"id":"[^"]*"' | cut -d":" -f2- | sed 's/"//g');

    if [ "$local_mode" = true ]; then
        echo $line >> ${resend_output};
    else

        response_code=$(curl \
            -k -o ${curl_output} -s -w "%{http_code}\n" \
            -u "${username}:${password}" \
            -X POST \
            -H "Content-Type: application/json" \
            -d "@${json_file}" \
            "${baseurl}/events/AAI-EVENT"\
        );

        if [ "$response_code" -ne "200" ]; then
            echo -n "Response failure for dmaap message with id ${id}," >> ${resend_error_log};
            echo " code: ${response_code} body: $(cat ${curl_output})" >> ${resend_error_log};
            resp_code=-1;
        fi;
    fi;
    
    if [ -f "${json_file}" ]; then
        rm $json_file;
    fi;

    if [ -f "${curl_output}" ]; then
        rm $curl_output;
    fi;

    return ${resp_code};
}

# Validates the events file and the snapshot file
# Goes through each line in the missed events file
# Gets all the resource versions there are 
# Finds the smallest resource version there 
# checks if the smallest resource version for the aai uri
# is what is currently in the last snapshot file provided by user
# If it is, it will send an dmaap event out

main(){

    if [ "${#}" -eq 0 ]; then
        usage;
    fi;

    # Get the first character of the first command line argument
    # If the character doesn't start with dash (-)
    # Then fail the script and display usage

    if [ "${1:0:1}" != "-" ]; then
        echo "Invalid option: $1" >&2
        usage;
    fi;

    while getopts ":e:s:u:lp:b:h" opt; do
        case ${opt} in
            l ) # Specify that the application will not send messages to dmaap but save it a file
                local local_mode=true
                ;;
            e ) # Specify the file for missed events
                local missed_events_file=$OPTARG
                ;;
            s ) # Specify the file for snapshot
                local snapshot_file=$OPTARG
                ;;
            u ) # Specify the username to dmaap
                local username=$OPTARG
                ;;
            p ) # Specify the password to dmaap
                local password=$OPTARG
                ;;
            b ) # Specify the baseurl to dmaap
                local hostname=$OPTARG
                ;;
            h ) 
                usage;
                ;;
            \? ) 
                echo "Invalid option: -$OPTARG" >&2
                usage;
                ;;
        esac
    done;

    validate "events_file" $missed_events_file;
    validate "snapshot_file" $snapshot_file;

    if [ "$local_mode" = true ]; then
        > ${resend_output};
    fi;
    
    while read dmaap_event; do
        entity_link=$(echo $dmaap_event | grep -o '"entity-link":"[^"]*"' | cut -d":" -f2- | sed 's/"//g');
        id=$(echo $dmaap_event | grep -o '"id":"[^"]*"' | cut -d":" -f2- | sed 's/"//g');
        action=$(echo $dmaap_event | grep -o '"action":"[^"]*"' | cut -d":" -f2- | sed 's/"//g');
        smallest_resource_version=$(echo $dmaap_event | jq -M '.' | grep 'resource-version' | sort | tail -1 | sed 's/[^0-9]//g');
        resource_version_matches_snapshot_file "${snapshot_file}" "${entity_link}" "${smallest_resource_version}" "${action}" && {
            send_dmaap "${local_mode}" "$dmaap_event" "$username" "$password" "$hostname" && {
                echo "Dmaap Event with Id $id was sent";
            } || {
                echo "Dmaap Event with Id $id was not sent due to dmaap error, please check logs";
            }
        } || {
            echo "Dmaap Event with Id $id not sent";
        }

    done < ${missed_events_file};

}

main $@