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
363
364
365
366
367
368
369
370
371
|
#!/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 in debug mode
# it will not send the dmaap messages to dmaap server
# but it will write to a file named resend_dmaap_server.out
# -x (optional) skip resource version check
# -p, (required) <string> the password for the dmaap server
# -s, (required) <file> containing the data snapshot graphson file to compare the resource versions against
# partial snapshots should be concatenated into a full snapshot file
# before running the script
# -u, (required) <string> the username for the dmaap server
# -t, (required) <string> the dmaap topic
#
# 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 -t AAI-EVENT
#
# 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 -t <other-dmaap-topic>
# or
# ./resend-dmaap-events.sh -l -e example_events.txt -s dataSnapshot.graphSON.201808091545 -u username -p example_pass -b https://localhost:3905 -t <other-dmaap-topic>
# or, to skip the resource-version check
# ./resend-dmaap-events.sh -l -x -e example_events.txt -s dataSnapshot.graphSON.201808091545 -u username -p example_pass -b https://localhost:3905 -t <other-dmaap-topic>
#
# 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 in 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 " -x, (optional) indicating that the script will skip the resource-version check"
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 " partial snapshots should be concatenated into a full snapshot file";
echo " before running the script";
echo " -u, <string> the username for the dmaap server";
echo " -t, <string> the dmaap topic";
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 -t AAI-EVENT";
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 -t AAI-EVENT";
echo " or, to skip the resource-version check";
echo " ./resend-dmaap-events.sh -l -x -e example_events.txt -s dataSnapshot.graphSON.201808091545 -u username -p example_pass -b https://localhost:3905 -t AAI-EVENT";
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(){
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;
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(){
snapshot_file=$1;
entity_link=$2;
resource_version=$3;
action=$4;
topic=$5;
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]+
if [ "${topic}" = "<other-dmaap-topic>" ]; then
aai_uri=$(echo $entity_link | sed 's/\/<other-base>\/v[0-9][0-9]*//g');
else
aai_uri=$(echo $entity_link | sed 's/\/aai\/v[0-9][0-9]*//g');
fi
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;
}
# Send the dmaap event to the host based on
# the line that was send to the function
send_dmaap(){
local_mode=$1;
line=$2;
username=$3;
password=$4;
baseurl=$5;
topic=$6;
resp_code=0;
generated_file=$(uuidgen);
json_file=/tmp/${generated_file}.json;
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/${topic}"\
);
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;
versioncheck=true
while getopts ":e:s:u:xlp:b:t:h" opt; do
case ${opt} in
l ) # Specify that the application will not send messages to dmaap but save it a file
local_mode=true
;;
e ) # Specify the file for missed events
missed_events_file=$OPTARG
;;
s ) # Specify the file for snapshot
snapshot_file=$OPTARG
;;
u ) # Specify the username to dmaap
username=$OPTARG
;;
p ) # Specify the password to dmaap
password=$OPTARG
;;
t ) # Specify the dmaap topic
topic=$OPTARG
;;
x ) # Specify whether to skip version check
versioncheck=false
;;
b ) # Specify the baseurl to dmaap
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');
#smallest_resource_version=$(echo $dmaap_event | python -m json.tool | grep 'resource-version' | sort | tail -1 | sed 's/[^0-9]//g');
match=0;
if [ "$versioncheck" = true ]; then
resource_version_matches_snapshot_file "${snapshot_file}" "${entity_link}" "${smallest_resource_version}" "${action}" "${topic}" && {
match=0;
} || {
match=1;
}
fi;
if [ $match -eq 0 ]; then
send_dmaap "${local_mode}" "$dmaap_event" "$username" "$password" "$hostname" "$topic" && {
echo "Dmaap Event with Id $id was sent";
} || {
echo "Dmaap Event with Id $id was not sent due to dmaap error, please check logs";
}
else
echo "Dmaap Event with Id $id not sent";
fi;
done < ${missed_events_file};
}
main $@
|