Skip to content

Commit

Permalink
Merge pull request #11 from vitiwari/master
Browse files Browse the repository at this point in the history
PLUG-226 fix, clean exit fix, $ check added in validation
  • Loading branch information
vitiwari authored Jul 13, 2017
2 parents ea5c25d + 2f6caed commit fbb3dcd
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 104 deletions.
Binary file modified lib/remedy-tsi-integration-lib-0.1.1.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ public void setDispatcher(CollectorDispatcher dispatcher) {
@Override
public void run() {
ArrayList<RemedyPluginConfigurationItem> items = configuration.getItems();
if (items.size() > 0) {
items.forEach((config) -> {
boolean isValidTemplate = true;
if (items != null && items.size() > 0) {
for (RemedyPluginConfigurationItem config : items) {
boolean isTemplateParsingSuccessful = false;
boolean isTemplateValidationSuccessful = false;
//PARSING THE JSON STRING
//System.err.println("parsing param.json data");
TemplateParser templateParser = new GenericTemplateParser();
Expand All @@ -96,29 +97,37 @@ public void run() {
defaultTemplate = templatePreParser.loadDefaults(ARServerForm.CHANGE_FORM);
}
template = templateParser.readParseConfigJson(defaultTemplate, Util.getFieldValues(config.getFields()));
isTemplateParsingSuccessful = true;
} catch (ParsingException ex) {
System.err.println("Parsing failed - " + ex.getMessage());
eventOutput.emit(Util.eventMeterTSI(Constants.REMEDY_PLUGIN_TITLE_MSG, ex.getMessage(), Event.EventSeverity.ERROR.toString()));
isValidTemplate = false;
} catch (Exception ex) {
System.err.println("Parsing failed - " + ex.getMessage());
}
TemplateValidator templateValidator = new PluginTemplateValidator();
try {
templateValidator.validate(template);

} catch (ValidationException ex) {
System.err.println("Validation failed - " + ex.getMessage());
eventOutput.emit(Util.eventMeterTSI(Constants.REMEDY_PLUGIN_TITLE_MSG, ex.getMessage(), Event.EventSeverity.ERROR.toString()));
isValidTemplate = false;
if (isTemplateParsingSuccessful) {
TemplateValidator templateValidator = new PluginTemplateValidator();
try {
templateValidator.validate(template);
isTemplateValidationSuccessful = true;
} catch (ValidationException ex) {
System.err.println("Validation failed - " + ex.getMessage());
} catch (Exception ex) {
System.err.println("Validation failed - " + ex.getMessage());
}
} else {
System.exit(1);
}
if (isValidTemplate) {

if (isTemplateValidationSuccessful) {
if (config.getRequestType().equalsIgnoreCase(RequestType.CM.getValues())) {
dispatcher.addCollector(new RemedyTicketsCollector(config, template, ARServerForm.CHANGE_FORM));
} else if (config.getRequestType().equalsIgnoreCase(RequestType.IM.getValues())) {
dispatcher.addCollector(new RemedyTicketsCollector(config, template, ARServerForm.INCIDENT_FORM));
}

} else {
System.exit(1);
}
});
}
dispatcher.run();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,104 +56,107 @@ public void run() {
EventSinkAPI eventSinkAPI = new EventSinkAPI();
EventSinkStandardOutput eventSinkAPIstd = new EventSinkStandardOutput();
while (true) {
RemedyReader reader = new GenericRemedyReader();
ARServerUser arServerContext = reader.createARServerContext(template.getConfig().getRemedyHostName(), template.getConfig().getRemedyPort(), template.getConfig().getRemedyUserName(), template.getConfig().getRemedyPassword());
boolean isConnectionOpen = false;
List<TSIEvent> eventList = null;
try {
RemedyReader reader = new GenericRemedyReader();
ARServerUser arServerContext = reader.createARServerContext(template.getConfig().getRemedyHostName(), template.getConfig().getRemedyPort(), template.getConfig().getRemedyUserName(), template.getConfig().getRemedyPassword());
boolean isConnectionOpen = false;
List<TSIEvent> eventList = null;
try {
reader.login(arServerContext);
int chunkSize = template.getConfig().getChunkSize();
int startFrom = 0;
int iteration = 1;
int totalRecordsRead = 0;
OutputInteger nMatches = new OutputInteger();
boolean readNext = true;
Long currentMili = Calendar.getInstance().getTimeInMillis();
Long pastMili = currentMili - (config.getPollInterval() * 60 * 1000);
template.getConfig().setStartDateTime(new Date(pastMili));
template.getConfig().setEndDateTime(new Date(currentMili));
boolean exceededMaxServerEntries = false;
System.err.println("Starting event reading & ingestion to tsi for (DateTime:" + Util.dateToString(template.getConfig().getStartDateTime()) + " to DateTime:" + Util.dateToString(template.getConfig().getEndDateTime()) + ")");
isConnectionOpen = eventSinkAPI.openConnection();
if (isConnectionOpen) {
System.err.println("JSON RPC Socket connection successful");
} else {
System.err.println("JSON RPC Socket connection failed");
}
int totalSuccessfulIngestion = 0;
if (isConnectionOpen) {
while (readNext) {
System.err.println("Iteration : " + iteration);
eventList = reader.readRemedyTickets(arServerContext, arServerForm, template, startFrom, chunkSize, nMatches, remedyEntryEventAdapter);
exceededMaxServerEntries = reader.exceededMaxServerEntries(arServerContext);
totalRecordsRead += eventList.size();
if (eventList.size() < chunkSize && totalRecordsRead < nMatches.intValue()
&& exceededMaxServerEntries) {
System.err.println("Request Sent to remedy (startFrom:" + startFrom + ",chunkSize:" + chunkSize + "), Response Got(RecordsRead:" + eventList.size()
+ ", totalRecordsRead:" + totalRecordsRead + ", recordsAvailable:" + nMatches.intValue() + ")");
System.err.println("Based on exceededMaxServerEntries response as(" + exceededMaxServerEntries + "), adjusting the chunk Size as " + eventList.size());
chunkSize = eventList.size();
} else if (eventList.size() <= chunkSize) {
System.err.println("Request Sent to remedy (startFrom:" + startFrom + ", chunkSize:" + chunkSize + "), Response Got (RecordsRead:" + eventList.size() + ", totalRecordsRead:" + totalRecordsRead + ", recordsAvailable:" + nMatches.intValue() + ")");
}

if (eventList.size() > 0) {
List<String> eventsList = new ArrayList<>();
eventList.forEach(event -> {
Gson gson = new Gson();
String eventJson = gson.toJson(event, Object.class);
StringBuilder sendEventToTSI = new StringBuilder();
sendEventToTSI.append(Constants.REMEDY_PROXY_EVENT_JSON_START_STRING).append(eventJson).append(Constants.REMEDY_PROXY_EVENT_JSON_END_STRING);
// eventSinkAPI.emit(sendEventToTSI.toString());
eventsList.add(sendEventToTSI.toString());
});
int succ = eventSinkAPI.emit(eventsList);
totalSuccessfulIngestion += succ;
System.err.println(succ + " Events successfuly ingested out of total " + nMatches + " events in iteration " + iteration);
} else {
System.err.println(eventList.size() + " Events found for the interval, DateTime:" + Util.dateToString(template.getConfig().getStartDateTime()) + " to DateTime:" + Util.dateToString(template.getConfig().getEndDateTime()));
eventSinkAPIstd.emit(Util.eventMeterTSI(Constants.REMEDY_PLUGIN_TITLE_MSG, Constants.REMEDY_IM_NO_DATA_AVAILABLE, Event.EventSeverity.INFO.toString()));
}
reader.login(arServerContext);
int chunkSize = template.getConfig().getChunkSize();
int startFrom = 0;
int iteration = 1;
int totalRecordsRead = 0;
OutputInteger nMatches = new OutputInteger();
boolean readNext = true;
Long currentMili = Calendar.getInstance().getTimeInMillis();
Long pastMili = currentMili - (config.getPollInterval() * 60 * 1000);
template.getConfig().setStartDateTime(new Date(pastMili));
template.getConfig().setEndDateTime(new Date(currentMili));
boolean exceededMaxServerEntries = false;
System.err.println("Starting event reading & ingestion to tsi for (DateTime:" + Util.dateToString(template.getConfig().getStartDateTime()) + " to DateTime:" + Util.dateToString(template.getConfig().getEndDateTime()) + ")");
isConnectionOpen = eventSinkAPI.openConnection();
if (isConnectionOpen) {
System.err.println("JSON RPC Socket connection successful");
} else {
System.err.println("JSON RPC Socket connection failed");
break;
}
int totalSuccessfulIngestion = 0;
if (isConnectionOpen) {
while (readNext) {
System.err.println("Iteration : " + iteration);
eventList = reader.readRemedyTickets(arServerContext, arServerForm, template, startFrom, chunkSize, nMatches, remedyEntryEventAdapter);
exceededMaxServerEntries = reader.exceededMaxServerEntries(arServerContext);
totalRecordsRead += eventList.size();
if (eventList.size() < chunkSize && totalRecordsRead < nMatches.intValue()
&& exceededMaxServerEntries) {
System.err.println("Request Sent to remedy (startFrom:" + startFrom + ",chunkSize:" + chunkSize + "), Response Got(RecordsRead:" + eventList.size()
+ ", totalRecordsRead:" + totalRecordsRead + ", recordsAvailable:" + nMatches.intValue() + ")");
System.err.println("Based on exceededMaxServerEntries response as(" + exceededMaxServerEntries + "), adjusting the chunk Size as " + eventList.size());
chunkSize = eventList.size();
} else if (eventList.size() <= chunkSize) {
System.err.println("Request Sent to remedy (startFrom:" + startFrom + ", chunkSize:" + chunkSize + "), Response Got (RecordsRead:" + eventList.size() + ", totalRecordsRead:" + totalRecordsRead + ", recordsAvailable:" + nMatches.intValue() + ")");
}

if (totalRecordsRead < nMatches.longValue()
&& (totalRecordsRead + chunkSize) > nMatches.longValue()) {
// assuming the long value would be in int range
// always
chunkSize = (int) (nMatches.longValue() - totalRecordsRead);
} else if (totalRecordsRead >= nMatches.longValue()) {
readNext = false;
}
iteration++;
startFrom = totalRecordsRead;
}//each chunk iteration
System.err.println("__________ [Total successful ingestion: " + totalSuccessfulIngestion + ", Total Records from Remedy :" + nMatches.longValue() + ", total iteration " + (iteration - 1) + " ]_____________");
}
} catch (RemedyLoginFailedException e) {
System.err.println("Remedy login failed :" + e.getMessage());
} catch (RemedyReadFailedException e) {
System.err.println("Reading records from Remedy Failed, Reason :" + e.getMessage());
} catch (Exception e) {
System.err.println("Exception occured while fetching the data" + e.getMessage());
e.printStackTrace();
eventSinkAPIstd.emit(Util.eventMeterTSI(Constants.REMEDY_PLUGIN_TITLE_MSG, e.getMessage(), Event.EventSeverity.ERROR.toString()));
} finally {
reader.logout(arServerContext);
if (isConnectionOpen) {
boolean isConnectionClosed = eventSinkAPI.closeConnection();
if (isConnectionClosed) {
System.err.println("JSON RPC Socket connection successfuly closed");
if (eventList.size() > 0) {
List<String> eventsList = new ArrayList<>();
eventList.forEach(event -> {
Gson gson = new Gson();
String eventJson = gson.toJson(event, Object.class);
StringBuilder sendEventToTSI = new StringBuilder();
sendEventToTSI.append(Constants.REMEDY_PROXY_EVENT_JSON_START_STRING).append(eventJson).append(Constants.REMEDY_PROXY_EVENT_JSON_END_STRING);
// eventSinkAPI.emit(sendEventToTSI.toString());
eventsList.add(sendEventToTSI.toString());
});
int succ = eventSinkAPI.emit(eventsList);
totalSuccessfulIngestion += succ;
System.err.println(succ + " Events successfuly ingested out of total " + nMatches + " events in iteration " + iteration);
} else {
System.err.println("Closing JSON RPC Socket connection failed");
System.err.println(eventList.size() + " Events found for the interval, DateTime:" + Util.dateToString(template.getConfig().getStartDateTime()) + " to DateTime:" + Util.dateToString(template.getConfig().getEndDateTime()));
eventSinkAPIstd.emit(Util.eventMeterTSI(Constants.REMEDY_PLUGIN_TITLE_MSG, Constants.REMEDY_IM_NO_DATA_AVAILABLE, Event.EventSeverity.INFO.toString()));
}

if (totalRecordsRead < nMatches.longValue()
&& (totalRecordsRead + chunkSize) > nMatches.longValue()) {
// assuming the long value would be in int range
// always
chunkSize = (int) (nMatches.longValue() - totalRecordsRead);
} else if (totalRecordsRead >= nMatches.longValue()) {
readNext = false;
}
iteration++;
startFrom = totalRecordsRead;
}//each chunk iteration
System.err.println("__________ [Total successful ingestion: " + totalSuccessfulIngestion + ", Total Records from Remedy :" + nMatches.longValue() + ", total iteration " + (iteration - 1) + " ]_____________");
}
} catch (RemedyLoginFailedException e) {
System.err.println("Remedy login failed :" + e.getMessage());
break;
} catch (RemedyReadFailedException e) {
System.err.println("Reading records from Remedy Failed, Reason :" + e.getMessage());
break;
} catch (Exception e) {
System.err.println("Exception occured while fetching the data" + e.getMessage());
eventSinkAPIstd.emit(Util.eventMeterTSI(Constants.REMEDY_PLUGIN_TITLE_MSG, e.getMessage(), Event.EventSeverity.ERROR.toString()));
break;
} finally {
reader.logout(arServerContext);
if (isConnectionOpen) {
boolean isConnectionClosed = eventSinkAPI.closeConnection();
if (isConnectionClosed) {
System.err.println("JSON RPC Socket connection successfuly closed");
} else {
System.err.println("Closing JSON RPC Socket connection failed");
}
}

}
try {
Thread.sleep((config.getPollInterval() * 60 * 1000));
} catch (InterruptedException ex) {
System.err.println("Interrupted Exception :" + ex.getMessage());
eventSinkAPIstd.emit(Util.eventMeterTSI(Constants.REMEDY_PLUGIN_TITLE_MSG, ex.getMessage(), Event.EventSeverity.ERROR.toString()));
}
}
}//infinite while loop end
}//infinite while loop end
}

}
Binary file modified target/meter-plugin-remedy-0.9.0_2.jar
Binary file not shown.

0 comments on commit fbb3dcd

Please sign in to comment.