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.Level;
import org.apache.log4j.Logger;
import org.jasypt.util.text.BasicTextEncryptor;
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);
String topicname = "";
String mechid = "";
String password = "";
+ TopicUtil topicUtil = new TopicUtil();
+ public boolean exitLoop = false;
private static String secret = "utdfpWlgyDQ2ZB8SLVRtmN834I1JcT9J";
public static void main(String[] args) {
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);
+ 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" + e);
+ logger.error(" IOException occers " + e);
}
}
}
loadProperties();
input = null;
try {
- /*input = new FileInputStream(kafkahome + "/bin/kafka-run-class.sh");*/
- if(false) {
+ /*
+ * input = new FileInputStream(kafkahome +
+ * "/bin/kafka-run-class.sh");
+ */
+ if (false) {
throw new IOException();
}
logger.info("kakahome is set :" + kafkahome);
}
}
}
- String response = publishTopic("{\"test\":\"test\"}");
+ String response = topicUtil.publishTopic(topicURL, topicname, mechid, password, "{\"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");
+ response = topicUtil.subscribeTopic(topicURL, topicname, "1", response, response);
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);
}
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.store(out, "");
} catch (Exception e) {
- logger.error("Exception at checkPropertiesFile " +e);
+ logger.error("Exception at checkPropertiesFile " + e);
}
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
- logger.error("Exception occurred is " +e);
+ logger.error("Exception occurred is " + e);
}
}
if (out != null) {
out.close();
} catch (IOException e) {
e.printStackTrace();
- logger.error("Exception is : "+e);
+ logger.error("Exception is : " + e);
}
}
}
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");
+ 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);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
}
mirrorMakers.getListMirrorMaker().set(i, mm);
} else {
// 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() {
+ public void readAgentTopic() {
try {
int connectionattempt = 0;
while (true) {
logger.info("--------------------------------");
logger.info("Waiting for Messages for 60 secs");
- String topicMessage = subscribeTopic("60000");
+ String topicMessage = topicUtil.subscribeTopic(topicURL, topicname, "60000", mechid, password);
Gson g = new Gson();
LinkedTreeMap<?, ?> object = null;
if (topicMessage != null) {
// 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");
- }
+ readAgent(object, topicMessage);
} catch (Exception ex) {
connectionattempt++;
if (connectionattempt > 5) {
- logger.info("Can't connect to the topic, mmagent shutting down , " + topicMessage);
+ logger.info("Can't connect to the topic, mmagent shutting down , " + topicMessage + ex);
return;
}
logger.info("Can't connect to the topic, " + topicMessage + " Retrying " + connectionattempt
connectionattempt = 0;
checkAgentProcess();
}
-
+ if (exitLoop) {
+ break;
+ }
}
} catch (Exception e) {
logger.error("Exception at readAgentTopic : " + e);
}
+ public void readAgent(LinkedTreeMap<?, ?> object, String topicMessage) {
+
+ Gson g = new Gson();
+
+ 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());
+ topicUtil.publishTopic(topicMessage, topicMessage, topicMessage, topicMessage, 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());
+ topicUtil.publishTopic(topicMessage, topicMessage, topicMessage, topicMessage, 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());
+ topicUtil.publishTopic(topicMessage, topicMessage, topicMessage, topicMessage, 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"));
+ topicUtil.publishTopic(topicMessage, topicMessage, topicMessage, topicMessage, g.toJson(mirrorMakers));
+ } 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());
+ topicUtil.publishTopic(topicMessage, topicMessage, topicMessage, topicMessage, 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");
+ }
+
+ }
+
protected void createMirrorMaker(MirrorMaker newMirrorMaker) {
boolean exists = false;
if (mirrorMakers != null) {
out = new FileOutputStream(mmagenthome + "/etc/mmagent.config");
mirrorMakerProperties.store(out, "");
} catch (IOException ex) {
- ex.printStackTrace();
+ logger.error(" IOException Occered " + ex);
} finally {
if (out != null) {
try {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
+ logger.log(Level.WARN, "Interrupted!", e);
+ Thread.currentThread().interrupt();
}
} catch (IOException ex) {
- ex.printStackTrace();
+ logger.error(" IOException Occered " + ex);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
- e.printStackTrace();
+ logger.error(" IOException Occered " + e);
}
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
+ logger.log(Level.WARN, "Interrupted!", e);
+ Thread.currentThread().interrupt();
}
} catch (IOException ex) {
- ex.printStackTrace();
+ logger.error("Exception at updateWhiteList : " + ex);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
+ logger.error("IOException occered " + e);
}
}
}
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(secret);
- //this.password = textEncryptor.decrypt(mirrorMakerProperties.getProperty("password"));
+ // this.password =
+ // textEncryptor.decrypt(mirrorMakerProperties.getProperty("password"));
this.password = mirrorMakerProperties.getProperty("password");
} catch (IOException ex) {
// ex.printStackTrace();