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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
/*******************************************************************************
* ============LICENSE_START=======================================================
* org.onap.dmaap
* ================================================================================
* Copyright © 2017 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=========================================================
*
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
*
*******************************************************************************/
package com.att.nsa.dmaapMMAgent;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.jasypt.util.text.BasicTextEncryptor;
import com.att.nsa.dmaapMMAgent.dao.CreateMirrorMaker;
import com.att.nsa.dmaapMMAgent.dao.DeleteMirrorMaker;
import com.att.nsa.dmaapMMAgent.dao.ListMirrorMaker;
import com.att.nsa.dmaapMMAgent.dao.MirrorMaker;
import com.att.nsa.dmaapMMAgent.dao.UpdateMirrorMaker;
import com.att.nsa.dmaapMMAgent.dao.UpdateWhiteList;
import com.att.nsa.dmaapMMAgent.utils.MirrorMakerProcessHandler;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.sun.org.apache.xerces.internal.impl.dtd.models.CMAny;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class MirrorMakerAgent {
static final Logger logger = Logger.getLogger(MirrorMakerAgent.class);
Properties mirrorMakerProperties = new Properties();
ListMirrorMaker mirrorMakers = null;
String mmagenthome = "";
String kafkahome = "";
String topicURL = "";
String topicname = "";
String mechid = "";
String password = "";
private static String secret = "utdfpWlgyDQ2ZB8SLVRtmN834I1JcT9J";
public static void main(String[] args) {
if (args != null && args.length == 2) {
if (args[0].equals("-encrypt")) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(secret);
String plainText = textEncryptor.encrypt(args[1]);
System.out.println("Encrypted Password is :" + plainText);
return;
}
} else if (args != null && args.length > 0) {
System.out.println(
"Usage: ./mmagent to run with the configuration \n -encrypt <password> to Encrypt Password for config ");
return;
}
MirrorMakerAgent agent = new MirrorMakerAgent();
if (agent.checkStartup()) {
logger.info("mmagent started, loading properties");
agent.checkAgentProcess();
agent.readAgentTopic();
} else {
System.out.println(
"ERROR: mmagent startup unsuccessful, please make sure the mmagenthome /etc/mmagent.config is set and mechid have the rights to the topic");
}
}
private boolean checkStartup() {
FileInputStream input = null;
try {
this.mmagenthome = System.getProperty("MMAGENTHOME");
input = new FileInputStream(mmagenthome + "/etc/mmagent.config");
logger.info("mmagenthome is set :" + mmagenthome + " loading properties at /etc/mmagent.config");
} catch (IOException ex) {
logger.error(mmagenthome + "/etc/mmagent.config not found. Set -DMMAGENTHOME and check the config file" + ex);
return false;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
logger.error(" IOException occers " + e);
}
}
}
loadProperties();
input = null;
try {
/*input = new FileInputStream(kafkahome + "/bin/kafka-run-class.sh");*/
if(false) {
throw new IOException();
}
logger.info("kakahome is set :" + kafkahome);
} catch (IOException ex) {
logger.error(kafkahome + "/bin/kafka-run-class.sh not found. Make sure kafka home is set correctly" + ex);
return false;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
logger.error("IOException" + e);
}
}
}
String response = publishTopic("{\"test\":\"test\"}");
if (response.startsWith("ERROR:")) {
logger.error("Problem publishing to topic, please verify the config " + this.topicname + " MR URL is:"
+ this.topicURL + " Error is: " + response);
return false;
}
logger.info("Published to Topic :" + this.topicname + " Successfully");
response = subscribeTopic("1");
if (response != null && response.startsWith("ERROR:")) {
logger.error("Problem subscribing to topic, please verify the config " + this.topicname + " MR URL is:"
+ this.topicURL + " Error is: " + response);
return false;
}
logger.info("Subscribed to Topic :" + this.topicname + " Successfully");
return true;
}
private void checkPropertiesFile(String agentName, String propName, String info, boolean refresh) {
InputStream input = null;
OutputStream out = null;
try {
if (refresh) {
throw new IOException();
}
input = new FileInputStream(mmagenthome + "/etc/" + agentName + propName + ".properties");
} catch (IOException ex) {
logger.error(" IOException will be handled " + ex);
try {
input = new FileInputStream(mmagenthome + "/etc/" + propName + ".properties");
Properties prop = new Properties();
prop.load(input);
if (propName.equals("consumer")) {
prop.setProperty("group.id", agentName);
prop.setProperty("zookeeper.connect", info);
} else {
prop.setProperty("metadata.broker.list", info);
}
out = new FileOutputStream(mmagenthome + "/etc/" + agentName + propName + ".properties");
prop.store(out, "");
} catch (Exception e) {
e.printStackTrace();
}
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("IOException" + e);
}
}
}
}
private void checkAgentProcess() {
logger.info("Checking MirrorMaker Process");
if (mirrorMakers != null) {
int mirrorMakersCount = mirrorMakers.getListMirrorMaker().size();
for (int i = 0; i < mirrorMakersCount; i++) {
MirrorMaker mm = mirrorMakers.getListMirrorMaker().get(i);
if (MirrorMakerProcessHandler.checkMirrorMakerProcess(mm.name) == false) {
checkPropertiesFile(mm.name, "consumer", mm.consumer, false);
checkPropertiesFile(mm.name, "producer", mm.producer, false);
if (mm.whitelist != null && !mm.whitelist.equals("")) {
logger.info("MirrorMaker " + mm.name + " is not running, restarting. Check Logs for more Details");
MirrorMakerProcessHandler.startMirrorMaker(this.mmagenthome, this.kafkahome, mm.name,
mmagenthome + "/etc/" + mm.name + "consumer.properties",
mmagenthome + "/etc/" + mm.name + "producer.properties", mm.whitelist);
mm.setStatus("RESTARTING");
} else {
logger.info("MirrorMaker " + mm.name + " is STOPPED");
mm.setStatus("STOPPED");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
mirrorMakers.getListMirrorMaker().set(i, mm);
} else {
logger.info("MirrorMaker " + mm.name + " is running");
mm.setStatus("RUNNING");
mirrorMakers.getListMirrorMaker().set(i, mm);
}
}
}
// Gson g = new Gson();
// System.out.println(g.toJson(mirrorMakers));
}
private String subscribeTopic(String timeout) {
String response = "";
try {
String requestURL = this.topicURL + "/events/" + this.topicname + "/mirrormakeragent/1?timeout=" + timeout
+ "&limit=1";
String authString = this.mechid + ":" + this.password;
String authStringEnc = Base64.encode(authString.getBytes());
URL url = new URL(requestURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Content-Type", "application/json");
InputStream content = (InputStream) connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
response = response + line;
}
Gson g = new Gson();
// get message as JSON String Array
String[] topicMessage = g.fromJson(response, String[].class);
if (topicMessage.length != 0) {
return topicMessage[0];
}
} catch (Exception e) {
return "ERROR:" + e.getMessage() + " Server Response is:" + response;
}
return null;
}
private String publishTopic(String message) {
try {
String requestURL = this.topicURL + "/events/" + this.topicname;
String authString = this.mechid + ":" + this.password;
String authStringEnc = Base64.encode(authString.getBytes());
URL url = new URL(requestURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Authorization", "Basic " + authStringEnc);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Content-Length", Integer.toString(message.length()));
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.write(message.getBytes());
InputStream content = (InputStream) connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
String response = "";
while ((line = in.readLine()) != null) {
response = response + line;
}
return response;
} catch (Exception e) {
return "ERROR:" + e.getLocalizedMessage();
}
}
private void readAgentTopic() {
try {
int connectionattempt = 0;
while (true) {
logger.info("--------------------------------");
logger.info("Waiting for Messages for 60 secs");
String topicMessage = subscribeTopic("60000");
Gson g = new Gson();
LinkedTreeMap<?, ?> object = null;
if (topicMessage != null) {
try {
object = g.fromJson(topicMessage, LinkedTreeMap.class);
// Cast the 1st item (since limit=1 and see the type of
// object
if (object.get("createMirrorMaker") != null) {
logger.info("Received createMirrorMaker request from topic");
CreateMirrorMaker m = g.fromJson(topicMessage, CreateMirrorMaker.class);
createMirrorMaker(m.getCreateMirrorMaker());
checkAgentProcess();
mirrorMakers.setMessageID(m.getMessageID());
publishTopic(g.toJson(mirrorMakers));
mirrorMakers.setMessageID("");
} else if (object.get("updateMirrorMaker") != null) {
logger.info("Received updateMirrorMaker request from topic");
UpdateMirrorMaker m = g.fromJson(topicMessage, UpdateMirrorMaker.class);
updateMirrorMaker(m.getUpdateMirrorMaker());
checkAgentProcess();
mirrorMakers.setMessageID(m.getMessageID());
publishTopic(g.toJson(mirrorMakers));
mirrorMakers.setMessageID("");
} else if (object.get("deleteMirrorMaker") != null) {
logger.info("Received deleteMirrorMaker request from topic");
DeleteMirrorMaker m = g.fromJson(topicMessage, DeleteMirrorMaker.class);
deleteMirrorMaker(m.getDeleteMirrorMaker());
checkAgentProcess();
mirrorMakers.setMessageID(m.getMessageID());
publishTopic(g.toJson(mirrorMakers));
mirrorMakers.setMessageID("");
} else if (object.get("listAllMirrorMaker") != null) {
logger.info("Received listALLMirrorMaker request from topic");
checkAgentProcess();
mirrorMakers.setMessageID((String) object.get("messageID"));
publishTopic(g.toJson(mirrorMakers));
mirrorMakers.setMessageID("");
} else if (object.get("updateWhiteList") != null) {
logger.info("Received updateWhiteList request from topic");
UpdateWhiteList m = g.fromJson(topicMessage, UpdateWhiteList.class);
updateWhiteList(m.getUpdateWhiteList());
checkAgentProcess();
mirrorMakers.setMessageID(m.getMessageID());
publishTopic(g.toJson(mirrorMakers));
mirrorMakers.setMessageID("");
} else if (object.get("listMirrorMaker") != null) {
logger.info("Received listMirrorMaker from topic, skipping messages");
} else {
logger.info("Received unknown request from topic");
}
} catch (Exception ex) {
connectionattempt++;
if (connectionattempt > 5) {
logger.info("Can't connect to the topic, mmagent shutting down , " + topicMessage);
return;
}
logger.info("Can't connect to the topic, " + topicMessage + " Retrying " + connectionattempt
+ " of 5 times in 1 minute" + " Error:" + ex.getLocalizedMessage());
Thread.sleep(60000);
}
} else {
// Check all MirrorMaker every min
connectionattempt = 0;
checkAgentProcess();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void createMirrorMaker(MirrorMaker newMirrorMaker) {
boolean exists = false;
if (mirrorMakers != null) {
int mirrorMakersCount = mirrorMakers.getListMirrorMaker().size();
for (int i = 0; i < mirrorMakersCount; i++) {
MirrorMaker mm = mirrorMakers.getListMirrorMaker().get(i);
if (mm.name.equals(newMirrorMaker.name)) {
exists = true;
logger.info("MirrorMaker already exist for:" + newMirrorMaker.name);
return;
}
}
}
logger.info("Adding new MirrorMaker:" + newMirrorMaker.name);
if (exists == false && mirrorMakers != null) {
mirrorMakers.getListMirrorMaker().add(newMirrorMaker);
} else if (exists == false && mirrorMakers == null) {
mirrorMakers = new ListMirrorMaker();
ArrayList<MirrorMaker> list = mirrorMakers.getListMirrorMaker();
list = new ArrayList<MirrorMaker>();
list.add(newMirrorMaker);
mirrorMakers.setListMirrorMaker(list);
}
checkPropertiesFile(newMirrorMaker.name, "consumer", newMirrorMaker.consumer, true);
checkPropertiesFile(newMirrorMaker.name, "producer", newMirrorMaker.producer, true);
Gson g = new Gson();
mirrorMakerProperties.setProperty("mirrormakers", g.toJson(this.mirrorMakers));
OutputStream out = null;
try {
out = new FileOutputStream(mmagenthome + "/etc/mmagent.config");
mirrorMakerProperties.store(out, "");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void updateMirrorMaker(MirrorMaker newMirrorMaker) {
boolean exists = false;
if (mirrorMakers != null) {
int mirrorMakersCount = mirrorMakers.getListMirrorMaker().size();
for (int i = 0; i < mirrorMakersCount; i++) {
MirrorMaker mm = mirrorMakers.getListMirrorMaker().get(i);
if (mm.name.equals(newMirrorMaker.name)) {
exists = true;
mm.setConsumer(newMirrorMaker.getConsumer());
mm.setProducer(newMirrorMaker.getProducer());
mirrorMakers.getListMirrorMaker().set(i, mm);
logger.info("Updating MirrorMaker:" + newMirrorMaker.name);
}
}
}
if (exists) {
checkPropertiesFile(newMirrorMaker.name, "consumer", newMirrorMaker.consumer, true);
checkPropertiesFile(newMirrorMaker.name, "producer", newMirrorMaker.producer, true);
Gson g = new Gson();
mirrorMakerProperties.setProperty("mirrormakers", g.toJson(this.mirrorMakers));
OutputStream out = null;
try {
out = new FileOutputStream(mmagenthome + "/etc/mmagent.config");
mirrorMakerProperties.store(out, "");
MirrorMakerProcessHandler.stopMirrorMaker(newMirrorMaker.name);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
logger.info("MirrorMaker Not found for:" + newMirrorMaker.name);
}
}
private void updateWhiteList(MirrorMaker newMirrorMaker) {
boolean exists = false;
if (mirrorMakers != null) {
int mirrorMakersCount = mirrorMakers.getListMirrorMaker().size();
for (int i = 0; i < mirrorMakersCount; i++) {
MirrorMaker mm = mirrorMakers.getListMirrorMaker().get(i);
if (mm.name.equals(newMirrorMaker.name)) {
exists = true;
mm.setWhitelist(newMirrorMaker.whitelist);
mirrorMakers.getListMirrorMaker().set(i, mm);
logger.info("Updating MirrorMaker WhiteList:" + newMirrorMaker.name + " WhiteList:"
+ newMirrorMaker.whitelist);
}
}
}
if (exists) {
Gson g = new Gson();
mirrorMakerProperties.setProperty("mirrormakers", g.toJson(this.mirrorMakers));
OutputStream out = null;
try {
out = new FileOutputStream(mmagenthome + "/etc/mmagent.config");
mirrorMakerProperties.store(out, "");
MirrorMakerProcessHandler.stopMirrorMaker(newMirrorMaker.name);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("IOException occered " + e);
}
}
}
} else {
logger.info("MirrorMaker Not found for:" + newMirrorMaker.name);
}
}
private void deleteMirrorMaker(MirrorMaker newMirrorMaker) {
boolean exists = false;
if (mirrorMakers != null) {
int mirrorMakersCount = mirrorMakers.getListMirrorMaker().size();
for (int i = 0; i < mirrorMakersCount; i++) {
MirrorMaker mm = mirrorMakers.getListMirrorMaker().get(i);
if (mm.name.equals(newMirrorMaker.name)) {
exists = true;
mirrorMakers.getListMirrorMaker().remove(i);
logger.info("Removing MirrorMaker:" + newMirrorMaker.name);
i = mirrorMakersCount;
}
}
}
if (exists) {
try {
String path = mmagenthome + "/etc/" + newMirrorMaker.name + "consumer" + ".properties";
File file = new File(path);
file.delete();
} catch (Exception ex) {
}
try {
String path = mmagenthome + "/etc/" + newMirrorMaker.name + "producer" + ".properties";
File file = new File(path);
file.delete();
} catch (Exception ex) {
}
Gson g = new Gson();
mirrorMakerProperties.setProperty("mirrormakers", g.toJson(this.mirrorMakers));
OutputStream out = null;
try {
out = new FileOutputStream(mmagenthome + "/etc/mmagent.config");
mirrorMakerProperties.store(out, "");
MirrorMakerProcessHandler.stopMirrorMaker(newMirrorMaker.name);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} else {
logger.info("MirrorMaker Not found for:" + newMirrorMaker.name);
}
}
private void loadProperties() {
InputStream input = null;
try {
input = new FileInputStream(mmagenthome + "/etc/mmagent.config");
mirrorMakerProperties.load(input);
Gson g = new Gson();
if (mirrorMakerProperties.getProperty("mirrormakers") == null) {
this.mirrorMakers = new ListMirrorMaker();
ArrayList<MirrorMaker> list = this.mirrorMakers.getListMirrorMaker();
list = new ArrayList<MirrorMaker>();
this.mirrorMakers.setListMirrorMaker(list);
} else {
this.mirrorMakers = g.fromJson(mirrorMakerProperties.getProperty("mirrormakers"),
ListMirrorMaker.class);
}
this.kafkahome = mirrorMakerProperties.getProperty("kafkahome");
this.topicURL = mirrorMakerProperties.getProperty("topicURL");
this.topicname = mirrorMakerProperties.getProperty("topicname");
this.mechid = mirrorMakerProperties.getProperty("mechid");
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(secret);
//this.password = textEncryptor.decrypt(mirrorMakerProperties.getProperty("password"));
this.password = mirrorMakerProperties.getProperty("password");
} catch (IOException ex) {
// ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
// e.printStackTrace();
}
}
}
}
}
|