From 9346cb2915128831b4f58854362775f33cbd7415 Mon Sep 17 00:00:00 2001 From: VENKATESH KUMAR Date: Thu, 7 Dec 2017 15:30:01 +0000 Subject: code sync-up updates Issue-ID: DCAEGEN2-212 Change-Id: Id72d2b1851ff4457295088609c355e19d19e3d73 Signed-off-by: VENKATESH KUMAR --- .../onap/dcae/commonFunction/CommonStartup.java | 13 +- .../onap/dcae/commonFunction/ConfigProcessors.java | 1142 +++++++++----------- .../dcae/commonFunction/CustomExceptionLoader.java | 157 +-- .../dcae/commonFunction/DmaapPropertyReader.java | 10 +- .../onap/dcae/commonFunction/EventProcessor.java | 290 +++-- .../onap/dcae/commonFunction/EventPublisher.java | 256 +++-- .../org/onap/dcae/commonFunction/VESLogger.java | 242 +++-- .../onap/dcae/controller/FetchDynamicConfig.java | 159 ++- .../onap/dcae/controller/LoadDynamicConfig.java | 164 ++- 9 files changed, 1162 insertions(+), 1271 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java b/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java index fe3ba325..55a83f7d 100644 --- a/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java +++ b/src/main/java/org/onap/dcae/commonFunction/CommonStartup.java @@ -63,7 +63,6 @@ import java.util.concurrent.Executors; import java.util.concurrent.LinkedBlockingQueue; import javax.servlet.ServletException; - public class CommonStartup extends NsaBaseEndpoint implements Runnable { public static final String KCONFIG = "c"; @@ -194,14 +193,15 @@ public class CommonStartup extends NsaBaseEndpoint implements Runnable { executor = Executors.newFixedThreadPool(20); executor.execute(ep); - } catch (loadException | missingReqdSetting | IOException | invalidSettingValue | ServletException | InterruptedException e) { + } catch (loadException | missingReqdSetting | IOException | invalidSettingValue | ServletException + | InterruptedException e) { CommonStartup.eplog.error("FATAL_STARTUP_ERROR" + e.getMessage()); throw new RuntimeException(e); } catch (Throwable e) { - System.err.println("Uncaught exception - " + e.getMessage()); - CommonStartup.eplog.error("FATAL_ERROR" + e.getMessage() ); - e.printStackTrace(System.err); - } finally { + System.err.println("Uncaught exception - " + e.getMessage()); + CommonStartup.eplog.error("FATAL_ERROR" + e.getMessage()); + e.printStackTrace(System.err); + } finally { // This will make the executor accept no new threads // and finish all existing threads in the queue if (executor != null) { @@ -310,4 +310,3 @@ public class CommonStartup extends NsaBaseEndpoint implements Runnable { private static final Logger log = LoggerFactory.getLogger(CommonStartup.class); } - diff --git a/src/main/java/org/onap/dcae/commonFunction/ConfigProcessors.java b/src/main/java/org/onap/dcae/commonFunction/ConfigProcessors.java index 51158aa7..7e42bb57 100644 --- a/src/main/java/org/onap/dcae/commonFunction/ConfigProcessors.java +++ b/src/main/java/org/onap/dcae/commonFunction/ConfigProcessors.java @@ -21,7 +21,6 @@ package org.onap.dcae.commonFunction; - import java.text.DecimalFormat; import org.json.JSONArray; import org.json.JSONObject; @@ -37,633 +36,546 @@ public class ConfigProcessors { private static final String VALUE = "value"; private static final String REGEX = "\\[\\]"; private static final String OBJECT_NOT_FOUND = "ObjectNotFound"; - - public ConfigProcessors(JSONObject eventJson) - { - event = eventJson; - } - - /** - * - */ - public void getValue(JSONObject J) - { - //log.info("addAttribute"); - final String field = J.getString(FIELD); - //final String value = J.getString(VALUE); - final JSONObject filter = J.optJSONObject(FILTER); - if (filter == null || isFilterMet(filter)) - { - //log.info("field ==" + field); - //log.info("value ==" + value); - getEventObjectVal(field); - } - else - log.info("Filter not met"); - } - - /** - * - */ - public void setValue(JSONObject J) - { - //log.info("addAttribute"); - final String field = J.getString(FIELD); - final String value = J.getString(VALUE); - final JSONObject filter = J.optJSONObject(FILTER); - if (filter == null || isFilterMet(filter)) - { - //log.info("field ==" + field); - //log.info("value ==" + value); - setEventObjectVal(field, value); - } - else - log.info("Filter not met"); - } - - /** - * - */ - public String evaluate(String str) - { - String value = str; - if (str.startsWith("$")) - { - value = (String) getEventObjectVal(str.substring(1)); - - } - return value; - } - - /** - * { "functionName":"suppressEvent", - "args":{} + + public ConfigProcessors(JSONObject eventJson) { + event = eventJson; + } + + /** + * + */ + public void getValue(JSONObject J) { + // log.info("addAttribute"); + final String field = J.getString(FIELD); + // final String value = J.getString(VALUE); + final JSONObject filter = J.optJSONObject(FILTER); + if (filter == null || isFilterMet(filter)) { + // log.info("field ==" + field); + // log.info("value ==" + value); + getEventObjectVal(field); + } else + log.info("Filter not met"); + } + + /** + * + */ + public void setValue(JSONObject J) { + // log.info("addAttribute"); + final String field = J.getString(FIELD); + final String value = J.getString(VALUE); + final JSONObject filter = J.optJSONObject(FILTER); + if (filter == null || isFilterMet(filter)) { + // log.info("field ==" + field); + // log.info("value ==" + value); + setEventObjectVal(field, value); + } else + log.info("Filter not met"); + } + + /** + * + */ + public String evaluate(String str) { + String value = str; + if (str.startsWith("$")) { + value = (String) getEventObjectVal(str.substring(1)); + + } + return value; + } + + /** + * { "functionName":"suppressEvent", "args":{} } + */ + public void suppressEvent(JSONObject J) { + // log.info("addAttribute"); + final JSONObject filter = J.optJSONObject(FILTER); + + if (filter == null || isFilterMet(filter)) { + // log.info("field ==" + field); + // log.info("value ==" + value); + setEventObjectVal("suppressEvent", "true"); + } else + log.info("Filter not met"); + } + + /** + * + */ + public void addAttribute(JSONObject J) { + // log.info("addAttribute begin"); + final String field = J.getString(FIELD); + final String value = evaluate(J.getString(VALUE)); + final JSONObject filter = J.optJSONObject(FILTER); + final String fieldType = J.optString("fieldType", "string").toLowerCase(); + + if (filter == null || isFilterMet(filter)) { + // log.info("field ==" + field); + // log.info("value ==" + value); + setEventObjectVal(field, value, fieldType); + } else + log.info("Filter not met"); + // log.info("addAttribute End"); + } + + /** + * + */ + public void updateAttribute(JSONObject J) { + // log.info("updateAttribute"); + final String field = J.getString(FIELD); + final String value = evaluate(J.getString(VALUE)); + final JSONObject filter = J.optJSONObject(FILTER); + if (filter == null || isFilterMet(filter)) { + // log.info("field ==" + field); + // log.info("value ==" + value); + setEventObjectVal(field, value); + } else + log.info("Filter not met"); + } + + /** + * + */ + public void removeAttribute(JSONObject J) { + // log.info("removeAttribute"); + final String field = J.getString(FIELD); + final JSONObject filter = J.optJSONObject(FILTER); + + if (filter == null || isFilterMet(filter)) { + removeEventKey(field); + } else + log.info("Filter not met"); + } + + /** + * + */ + public void renameArrayInArray(JSONObject J) // map + { + log.info("renameArrayInArray"); + final String field = J.getString(FIELD); + final String oldField = J.getString(OLD_FIELD); + final JSONObject filter = J.optJSONObject(FILTER); + // String value = ""; + if (filter == null || isFilterMet(filter)) { + // log.info("field ==" + field); + final String[] fsplit = field.split(REGEX, field.length()); + final String[] oldfsplit = oldField.split(REGEX, oldField.length()); + /* + * for (int i=0; i< oldfsplit.length; i++ ) { log.info( + * "renameArrayInArray " + i + " ==" + oldfsplit[i]); } + */ + + final String oldValue = getEventObjectVal(oldfsplit[0]).toString(); + if (!oldValue.equals(OBJECT_NOT_FOUND)) { + final String oldArrayName = oldfsplit[1].substring(1); + final String newArrayName = fsplit[1].substring(1); + final String value = oldValue.replaceAll(oldArrayName, newArrayName); + // log.info("oldArrayName ==" + oldArrayName); + // log.info("newArrayName ==" + newArrayName); + log.info("oldValue ==" + oldValue); + log.info("value ==" + value); + JSONArray ja = new JSONArray(value); + removeEventKey(oldfsplit[0]); + setEventObjectVal(fsplit[0], ja); } - */ - public void suppressEvent(JSONObject J) - { - //log.info("addAttribute"); - final JSONObject filter = J.optJSONObject(FILTER); - - if (filter == null || isFilterMet(filter)) - { - //log.info("field ==" + field); - //log.info("value ==" + value); - setEventObjectVal("suppressEvent", "true"); - } - else - log.info("Filter not met"); - } - - /** - * - */ - public void addAttribute(JSONObject J) - { - //log.info("addAttribute begin"); - final String field = J.getString(FIELD); - final String value = evaluate(J.getString(VALUE)); - final JSONObject filter = J.optJSONObject(FILTER); - final String fieldType = J.optString("fieldType", "string").toLowerCase(); - - if (filter == null || isFilterMet(filter)) - { - //log.info("field ==" + field); - //log.info("value ==" + value); - setEventObjectVal(field, value, fieldType); - } - else - log.info("Filter not met"); - //log.info("addAttribute End"); - } - - /** - * - */ - public void updateAttribute(JSONObject J) - { - //log.info("updateAttribute"); - final String field = J.getString(FIELD); - final String value = evaluate(J.getString(VALUE)); - final JSONObject filter = J.optJSONObject(FILTER); - if (filter == null || isFilterMet(filter)) - { - //log.info("field ==" + field); - //log.info("value ==" + value); - setEventObjectVal(field, value); - } - else - log.info("Filter not met"); - } - - /** - * - */ - public void removeAttribute(JSONObject J) - { - //log.info("removeAttribute"); - final String field = J.getString(FIELD); - final JSONObject filter = J.optJSONObject(FILTER); - - if (filter == null || isFilterMet(filter)) - { - removeEventKey(field); - } - else - log.info("Filter not met"); - } - - /** - * - */ - public void renameArrayInArray(JSONObject J) //map - { - log.info("renameArrayInArray"); - final String field = J.getString(FIELD); - final String oldField = J.getString(OLD_FIELD); - final JSONObject filter = J.optJSONObject(FILTER); - //String value = ""; - if (filter == null || isFilterMet(filter)) - { - //log.info("field ==" + field); - final String[] fsplit = field.split(REGEX, field.length()); - final String[] oldfsplit = oldField.split(REGEX, oldField.length()); - /*for (int i=0; i< oldfsplit.length; i++ ) - { - log.info( "renameArrayInArray " + i + " ==" + oldfsplit[i]); - }*/ - - final String oldValue = getEventObjectVal(oldfsplit[0]).toString(); - if (!oldValue.equals(OBJECT_NOT_FOUND)){ - final String oldArrayName = oldfsplit[1].substring(1); - final String newArrayName = fsplit[1].substring(1); - final String value = oldValue.replaceAll(oldArrayName, newArrayName); - //log.info("oldArrayName ==" + oldArrayName); - //log.info("newArrayName ==" + newArrayName); - log.info("oldValue ==" + oldValue); - log.info("value ==" + value); - JSONArray ja = new JSONArray(value); - removeEventKey(oldfsplit[0]); - setEventObjectVal(fsplit[0], ja); - } - } - else - log.info("Filter not met"); - } - - /** - * - */ - public void map(JSONObject J) - { - //log.info("mapAttribute"); - final String field = J.getString(FIELD); - if (field.contains("[]")) - { - if (field.matches(".*\\[\\]\\..*\\[\\]")) - renameArrayInArray(J); - else - mapToJArray(J); - } - else - mapAttribute(J); - } - - /** - * - */ - public String performOperation(String operation, String value) - { - log.info("performOperation"); - if (operation != null) - { - if (operation.equals("convertMBtoKB")) - { - float kbValue = Float.parseFloat(value) * 1024; - value = String.valueOf(kbValue); - } - } - return value; - } - - /** - * - */ - //public void mapAttributeToArrayAttribute(JSONObject J) - public void mapAttribute(JSONObject J) - { - //log.info("mapAttribute"); - final String field = J.getString(FIELD); - final String oldField = J.getString(OLD_FIELD); - final JSONObject filter = J.optJSONObject(FILTER); - final String operation = J.optString("operation"); - String value = ""; - if (filter == null || isFilterMet(filter)) - { - //log.info("field ==" + field); - - value = getEventObjectVal(oldField).toString(); - if (!value.equals(OBJECT_NOT_FOUND)) - { - if (operation != null && !operation.equals("")) - value = performOperation(operation, value); - //log.info("value ==" + value); - setEventObjectVal(field, value); - - removeEventKey(oldField); - } - } - else - log.info("Filter not met"); - } - - /** - * - */ - public void mapToJArray(JSONObject J) - { - log.info("mapToJArray"); - String field = J.getString(FIELD); - String oldField = J.getString(OLD_FIELD); - final JSONObject filter = J.optJSONObject(FILTER); - final JSONObject attrMap = J.optJSONObject("attrMap"); - oldField = oldField.replaceAll(REGEX, ""); - field = field.replaceAll(REGEX, ""); - - //log.info("oldField ==" + field); - if (filter == null || isFilterMet(filter)) - { - //log.info("oldField ==" + field); - String value = getEventObjectVal(oldField).toString(); - if (!value.equals(OBJECT_NOT_FOUND)) - { - log.info("old value ==" + value.toString()); - //update old value based on attrMap - if (attrMap != null) - { - //loop thru attrMap and update attribute name to new name - for (String key : attrMap.keySet()) - { - //log.info("attr key==" + key + " value==" + attrMap.getString(key)); - value = value.replaceAll(key, attrMap.getString(key)); - } - } - - log.info("new value ==" + value); - char c = value.charAt(0); - if (c != '[') - { - //oldfield is JsonObject - JSONObject valueJO = new JSONObject(value); - // if the array already exists - - String existingValue = getEventObjectVal(field).toString(); - if (!existingValue.equals(OBJECT_NOT_FOUND)) - { - JSONArray ja = new JSONArray(existingValue); - JSONObject jo = ja.optJSONObject(0); - if (jo != null) - { - for (String key : valueJO.keySet()) - { - jo.put(key, valueJO.get(key)); - - } - ja.put(0, jo); - //log.info("jarray== " + ja.toString()); - setEventObjectVal(field,ja); - } - } - else //if new array - setEventObjectVal(field + "[0]", new JSONObject(value), "JArray"); - } - else //oldfield is jsonArray - setEventObjectVal(field, new JSONArray(value)); - - removeEventKey(oldField); - } - } - else - log.info("Filter not met"); - } - - /** - * example - - { - "functionName": "concatenateValue", - "args":{ - "filter": {"event.commonEventHeader.event":"heartbeat"}, - FIELD:"event.commonEventHeader.eventName", - "concatenate": ["event.commonEventHeader.domain","event.commonEventHeader.eventType","event.commonEventHeader.alarmCondition"], - "delimiter":"_" - } - } - **/ - public void concatenateValue(JSONObject J) - { - //log.info("concatenateValue"); - final String field = J.getString(FIELD); - final String delimiter = J.getString("delimiter"); - final JSONArray values = J.getJSONArray("concatenate"); - final JSONObject filter = J.optJSONObject(FILTER); - if (filter == null || isFilterMet(filter)) - { - String value = ""; - for (int i=0; i < values.length(); i++) - { - //log.info(values.getString(i)); - String tempVal = evaluate(values.getString(i)); - if (!tempVal.equals(OBJECT_NOT_FOUND)) - { - if (i ==0) - value = value + tempVal; - else - value = value + delimiter + tempVal; - } - } - //log.info("value ==" + value); - setEventObjectVal(field, value); - } - else - log.info("Filter not met"); - } - - public void subtractValue(JSONObject J) - { - //log.info("concatenateValue"); - final String field = J.getString(FIELD); - final JSONArray values = J.getJSONArray("subtract"); - final JSONObject filter = J.optJSONObject(FILTER); - if (filter == null || isFilterMet(filter)) - { - float value = 0; - for (int i=0; i < values.length(); i++) - { - log.info(values.getString(i)); - String tempVal = evaluate(values.getString(i)); - log.info("tempVal==" + tempVal); - if (!tempVal.equals(OBJECT_NOT_FOUND)) - { - if (i ==0) - value = value + Float.valueOf(tempVal); - else - value = value - Float.valueOf(tempVal); - } - } - log.info("value ==" + value ); - setEventObjectVal(field, value, "number"); - } - else - log.info("Filter not met"); - } - - /** - * - */ - private void removeEventKey(String field) - { - String[] keySet = field.split("\\.",field.length()); - JSONObject keySeries = event; - for (int i=0; i<(keySet.length -1); i++ ) - { - //log.info( i + " ==" + keySet[i]); - keySeries = keySeries.getJSONObject(keySet[i]); - } - //log.info(keySet[keySet.length -1]); - - keySeries.remove(keySet[keySet.length -1]); - - } - - /** - * - */ - private boolean checkFilter(JSONObject jo, String key, String logicKey) - { - String filterValue = jo.getString(key); - boolean retVal = true; - - if(filterValue.contains(":")) - { - String[] splitVal = filterValue.split(":"); - //log.info(splitVal[0] + " " + splitVal[1]); - if (splitVal[0].equals("matches")) - { - if (logicKey.equals("not")) - { - //log.info("not"); - //log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "split1==" + splitVal[1]); - if (getEventObjectVal(key).toString().matches(splitVal[1])) - { - log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); - return false; - } - } - else - { - if (!(getEventObjectVal(key).toString().matches(splitVal[1]))) - { - log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); - return false; - } + } else + log.info("Filter not met"); + } + + /** + * + */ + public void map(JSONObject J) { + // log.info("mapAttribute"); + final String field = J.getString(FIELD); + if (field.contains("[]")) { + if (field.matches(".*\\[\\]\\..*\\[\\]")) + renameArrayInArray(J); + else + mapToJArray(J); + } else + mapAttribute(J); + } + + /** + * + */ + public String performOperation(String operation, String value) { + log.info("performOperation"); + if (operation != null) { + if (operation.equals("convertMBtoKB")) { + float kbValue = Float.parseFloat(value) * 1024; + value = String.valueOf(kbValue); + } + } + return value; + } + + /** + * + */ + // public void mapAttributeToArrayAttribute(JSONObject J) + public void mapAttribute(JSONObject J) { + // log.info("mapAttribute"); + final String field = J.getString(FIELD); + final String oldField = J.getString(OLD_FIELD); + final JSONObject filter = J.optJSONObject(FILTER); + final String operation = J.optString("operation"); + String value = ""; + if (filter == null || isFilterMet(filter)) { + // log.info("field ==" + field); + + value = getEventObjectVal(oldField).toString(); + if (!value.equals(OBJECT_NOT_FOUND)) { + if (operation != null && !operation.equals("")) + value = performOperation(operation, value); + // log.info("value ==" + value); + setEventObjectVal(field, value); + + removeEventKey(oldField); + } + } else + log.info("Filter not met"); + } + + /** + * + */ + public void mapToJArray(JSONObject J) { + log.info("mapToJArray"); + String field = J.getString(FIELD); + String oldField = J.getString(OLD_FIELD); + final JSONObject filter = J.optJSONObject(FILTER); + final JSONObject attrMap = J.optJSONObject("attrMap"); + oldField = oldField.replaceAll(REGEX, ""); + field = field.replaceAll(REGEX, ""); + + // log.info("oldField ==" + field); + if (filter == null || isFilterMet(filter)) { + // log.info("oldField ==" + field); + String value = getEventObjectVal(oldField).toString(); + if (!value.equals(OBJECT_NOT_FOUND)) { + log.info("old value ==" + value.toString()); + // update old value based on attrMap + if (attrMap != null) { + // loop thru attrMap and update attribute name to new name + for (String key : attrMap.keySet()) { + // log.info("attr key==" + key + " value==" + + // attrMap.getString(key)); + value = value.replaceAll(key, attrMap.getString(key)); } - } - if (splitVal[0].equals("contains")) - { - if (logicKey.equals("not")) - { - //log.info("not"); - //log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "split1==" + splitVal[1]); - if (getEventObjectVal(key).toString().contains(splitVal[1])) - { - log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); - return false; + + log.info("new value ==" + value); + char c = value.charAt(0); + if (c != '[') { + // oldfield is JsonObject + JSONObject valueJO = new JSONObject(value); + // if the array already exists + + String existingValue = getEventObjectVal(field).toString(); + if (!existingValue.equals(OBJECT_NOT_FOUND)) { + JSONArray ja = new JSONArray(existingValue); + JSONObject jo = ja.optJSONObject(0); + if (jo != null) { + for (String key : valueJO.keySet()) { + jo.put(key, valueJO.get(key)); + + } + ja.put(0, jo); + // log.info("jarray== " + ja.toString()); + setEventObjectVal(field, ja); } - } + } else // if new array + setEventObjectVal(field + "[0]", new JSONObject(value), "JArray"); + } else // oldfield is jsonArray + setEventObjectVal(field, new JSONArray(value)); + + removeEventKey(oldField); + } + } else + log.info("Filter not met"); + } + + /** + * example - { "functionName": "concatenateValue", "args":{ "filter": + * {"event.commonEventHeader.event":"heartbeat"}, + * FIELD:"event.commonEventHeader.eventName", "concatenate": + * ["event.commonEventHeader.domain","event.commonEventHeader.eventType","event.commonEventHeader.alarmCondition"], + * "delimiter":"_" } } + **/ + public void concatenateValue(JSONObject J) { + // log.info("concatenateValue"); + final String field = J.getString(FIELD); + final String delimiter = J.getString("delimiter"); + final JSONArray values = J.getJSONArray("concatenate"); + final JSONObject filter = J.optJSONObject(FILTER); + if (filter == null || isFilterMet(filter)) { + String value = ""; + for (int i = 0; i < values.length(); i++) { + // log.info(values.getString(i)); + String tempVal = evaluate(values.getString(i)); + if (!tempVal.equals(OBJECT_NOT_FOUND)) { + if (i == 0) + value = value + tempVal; else - { - if (!(getEventObjectVal(key).toString().contains(splitVal[1]))) - { - log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); - return false; - } + value = value + delimiter + tempVal; + } + } + // log.info("value ==" + value); + setEventObjectVal(field, value); + } else + log.info("Filter not met"); + } + + public void subtractValue(JSONObject J) { + // log.info("concatenateValue"); + final String field = J.getString(FIELD); + final JSONArray values = J.getJSONArray("subtract"); + final JSONObject filter = J.optJSONObject(FILTER); + if (filter == null || isFilterMet(filter)) { + float value = 0; + for (int i = 0; i < values.length(); i++) { + log.info(values.getString(i)); + String tempVal = evaluate(values.getString(i)); + log.info("tempVal==" + tempVal); + if (!tempVal.equals(OBJECT_NOT_FOUND)) { + if (i == 0) + value = value + Float.valueOf(tempVal); + else + value = value - Float.valueOf(tempVal); + } + } + log.info("value ==" + value); + setEventObjectVal(field, value, "number"); + } else + log.info("Filter not met"); + } + + /** + * + */ + private void removeEventKey(String field) { + String[] keySet = field.split("\\.", field.length()); + JSONObject keySeries = event; + for (int i = 0; i < (keySet.length - 1); i++) { + // log.info( i + " ==" + keySet[i]); + keySeries = keySeries.getJSONObject(keySet[i]); + } + // log.info(keySet[keySet.length -1]); + + keySeries.remove(keySet[keySet.length - 1]); + + } + + /** + * + */ + private boolean checkFilter(JSONObject jo, String key, String logicKey) { + String filterValue = jo.getString(key); + boolean retVal = true; + + if (filterValue.contains(":")) { + String[] splitVal = filterValue.split(":"); + // log.info(splitVal[0] + " " + splitVal[1]); + if (splitVal[0].equals("matches")) { + if (logicKey.equals("not")) { + // log.info("not"); + // log.info(filterValue + "==" + key + "==" + + // getEventObjectVal(key) + "split1==" + splitVal[1]); + if (getEventObjectVal(key).toString().matches(splitVal[1])) { + log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); + return false; + } + } else { + if (!(getEventObjectVal(key).toString().matches(splitVal[1]))) { + log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); + return false; } - } + } - else - { - if (logicKey.equals("not")) - { - if(getEventObjectVal(key).toString().equals(filterValue)) - { + if (splitVal[0].equals("contains")) { + if (logicKey.equals("not")) { + // log.info("not"); + // log.info(filterValue + "==" + key + "==" + + // getEventObjectVal(key) + "split1==" + splitVal[1]); + if (getEventObjectVal(key).toString().contains(splitVal[1])) { log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); return false; } + } else { + if (!(getEventObjectVal(key).toString().contains(splitVal[1]))) { + log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); + return false; + } + } + + } + } else { + if (logicKey.equals("not")) { + if (getEventObjectVal(key).toString().equals(filterValue)) { + log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); + return false; + } + } else { + if (!(getEventObjectVal(key).toString().equals(filterValue))) { + log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); + return false; + } + } + } + return retVal; + } + + /** + * + */ + public boolean isFilterMet(JSONObject jo) { + boolean retval = true; + // log.info("Filter==" + jo.toString()); + for (String key : jo.keySet()) { + if (key.equals("not")) { + JSONObject njo = jo.getJSONObject(key); + for (String njoKey : njo.keySet()) { + // log.info(njoKey); + retval = checkFilter(njo, njoKey, key); + if (retval == false) + return retval; + } + } else { + // log.info(key); + // final String filterKey = key; + retval = checkFilter(jo, key, key); + if (retval == false) + return retval; + } + } + return true; + } + + /** + * returns a string or JSONObject or JSONArray + **/ + public Object getEventObjectVal(String keySeriesStr) { + keySeriesStr = keySeriesStr.replaceAll("\\[", "."); + keySeriesStr = keySeriesStr.replaceAll("\\]", "."); + if (keySeriesStr.contains("..")) { + keySeriesStr = keySeriesStr.replaceAll("\\.\\.", "."); + } + // log.info(Integer.toString(keySeriesStr.lastIndexOf("."))); + // log.info(Integer.toString(keySeriesStr.length() -1)); + if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() - 1) + keySeriesStr = keySeriesStr.substring(0, keySeriesStr.length() - 1); + String[] keySet = keySeriesStr.split("\\.", keySeriesStr.length()); + Object keySeriesObj = event; + for (int i = 0; i < (keySet.length); i++) { + // log.info( "getEventObject " + i + " ==" + keySet[i]); + if (keySeriesObj != null) { + if (keySeriesObj instanceof String) { + // keySeriesObj = keySeriesObj.get(keySet[i]); + log.info("STRING==" + keySeriesObj); + } else if (keySeriesObj instanceof JSONArray) { + keySeriesObj = ((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i])); + // log.info("ARRAY==" + keySeriesObj); + } else if (keySeriesObj instanceof JSONObject) { + keySeriesObj = ((JSONObject) keySeriesObj).opt(keySet[i]); + // log.info("JSONObject==" + keySeriesObj); + } else { + log.info("unknown object==" + keySeriesObj); + } + } + } + + if (keySeriesObj == null) + return OBJECT_NOT_FOUND; + return keySeriesObj; + } + + public void setEventObjectVal(String keySeriesStr, Object value) { + setEventObjectVal(keySeriesStr, value, "string"); + } + + /** + * returns a string or JSONObject or JSONArray + **/ + public void setEventObjectVal(String keySeriesStr, Object value, String fieldType) { + keySeriesStr = keySeriesStr.replaceAll("\\[", "."); + keySeriesStr = keySeriesStr.replaceAll("\\]", "."); + if (keySeriesStr.contains("..")) { + keySeriesStr = keySeriesStr.replaceAll("\\.\\.", "."); + } + log.info("fieldType==" + fieldType); + // log.info(Integer.toString(keySeriesStr.lastIndexOf("."))); + // log.info(Integer.toString(keySeriesStr.length() -1)); + if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() - 1) + keySeriesStr = keySeriesStr.substring(0, keySeriesStr.length() - 1); + String[] keySet = keySeriesStr.split("\\.", keySeriesStr.length()); + Object keySeriesObj = event; + for (int i = 0; i < (keySet.length - 1); i++) { + // log.info( "setEventObject " + i + " ==" + keySet[i]); + if (keySeriesObj instanceof JSONArray) { + // keySeriesObj = ((JSONArray) + // keySeriesObj).optJSONObject(Integer.parseInt(keySet[i])); + if (((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i])) == null) // if + // the + // object + // is + // not + // there + // then + // add + // it + { + log.info("Object is null, must add it"); + if (keySet[i + 1].matches("[0-9]*")) // if index then array + ((JSONArray) keySeriesObj).put(Integer.parseInt(keySet[i]), new JSONArray()); + else + ((JSONArray) keySeriesObj).put(Integer.parseInt(keySet[i]), new JSONObject()); } - else + keySeriesObj = ((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i])); + // log.info("ARRAY==" + keySeriesObj); + } else if (keySeriesObj instanceof JSONObject) { + if (((JSONObject) keySeriesObj).opt(keySet[i]) == null) // if + // the + // object + // is + // not + // there + // then + // add + // it { - if(!(getEventObjectVal(key).toString().equals(filterValue))) - { - log.info(filterValue + "==" + key + "==" + getEventObjectVal(key) + "==false"); - return false; - } + if (keySet[i + 1].matches("[0-9]*")) // if index then array + ((JSONObject) keySeriesObj).put(keySet[i], new JSONArray()); + else + ((JSONObject) keySeriesObj).put(keySet[i], new JSONObject()); + log.info("Object is null, must add it"); } + keySeriesObj = ((JSONObject) keySeriesObj).opt(keySet[i]); + // log.info("JSONObject==" + keySeriesObj); + } else { + log.info("unknown object==" + keySeriesObj); } - return retVal; - } - /** - * - */ - public boolean isFilterMet(JSONObject jo) - { - boolean retval = true; - //log.info("Filter==" + jo.toString()); - for (String key : jo.keySet()) - { - if (key.equals("not")) - { - JSONObject njo = jo.getJSONObject(key); - for (String njoKey : njo.keySet()) - { - //log.info(njoKey); - retval = checkFilter(njo, njoKey, key); - if (retval == false) - return retval; - } - } - else - { - //log.info(key); - //final String filterKey = key; - retval = checkFilter(jo, key, key); - if (retval == false) - return retval; - } - } - return true; - } - - /** - * returns a string or JSONObject or JSONArray - **/ - public Object getEventObjectVal(String keySeriesStr) - { - keySeriesStr = keySeriesStr.replaceAll("\\[", "."); - keySeriesStr = keySeriesStr.replaceAll("\\]", "."); - if (keySeriesStr.contains("..")) - { - keySeriesStr = keySeriesStr.replaceAll("\\.\\.", "."); - } - //log.info(Integer.toString(keySeriesStr.lastIndexOf("."))); - //log.info(Integer.toString(keySeriesStr.length() -1)); - if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() -1 ) - keySeriesStr = keySeriesStr.substring(0,keySeriesStr.length()-1 ); - String[] keySet = keySeriesStr.split("\\.", keySeriesStr.length()); - Object keySeriesObj = event; - for (int i=0; i<(keySet.length); i++ ) - { - //log.info( "getEventObject " + i + " ==" + keySet[i]); - if (keySeriesObj != null) - { - if (keySeriesObj instanceof String) - { - //keySeriesObj = keySeriesObj.get(keySet[i]); - log.info("STRING==" + keySeriesObj); - } - else if (keySeriesObj instanceof JSONArray) { - keySeriesObj = ((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i])); - //log.info("ARRAY==" + keySeriesObj); - } - else if (keySeriesObj instanceof JSONObject) { - keySeriesObj = ( (JSONObject) keySeriesObj).opt(keySet[i]); - //log.info("JSONObject==" + keySeriesObj); - } - else - { - log.info("unknown object==" + keySeriesObj); - } - } - } - - if (keySeriesObj == null) - return OBJECT_NOT_FOUND; - return keySeriesObj; - } - - public void setEventObjectVal(String keySeriesStr, Object value) - { - setEventObjectVal(keySeriesStr, value, "string"); - } - - /** - * returns a string or JSONObject or JSONArray - **/ - public void setEventObjectVal(String keySeriesStr, Object value, String fieldType) - { - keySeriesStr = keySeriesStr.replaceAll("\\[", "."); - keySeriesStr = keySeriesStr.replaceAll("\\]", "."); - if (keySeriesStr.contains("..")) - { - keySeriesStr = keySeriesStr.replaceAll("\\.\\.", "."); - } - log.info("fieldType==" + fieldType); - //log.info(Integer.toString(keySeriesStr.lastIndexOf("."))); - //log.info(Integer.toString(keySeriesStr.length() -1)); - if (keySeriesStr.lastIndexOf(".") == keySeriesStr.length() -1 ) - keySeriesStr = keySeriesStr.substring(0,keySeriesStr.length()-1 ); - String[] keySet = keySeriesStr.split("\\.", keySeriesStr.length()); - Object keySeriesObj = event; - for (int i=0; i<(keySet.length -1); i++ ) - { - //log.info( "setEventObject " + i + " ==" + keySet[i]); - if (keySeriesObj instanceof JSONArray) { - //keySeriesObj = ((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i])); - if (((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i])) == null) //if the object is not there then add it - { - log.info("Object is null, must add it"); - if (keySet[i+1].matches("[0-9]*")) // if index then array - ((JSONArray) keySeriesObj).put(Integer.parseInt(keySet[i]), new JSONArray()); - else - ((JSONArray) keySeriesObj).put(Integer.parseInt(keySet[i]), new JSONObject()); - } - keySeriesObj = ((JSONArray) keySeriesObj).optJSONObject(Integer.parseInt(keySet[i])); - //log.info("ARRAY==" + keySeriesObj); - } - else if (keySeriesObj instanceof JSONObject) { - if (( (JSONObject) keySeriesObj).opt(keySet[i]) == null) //if the object is not there then add it - { - if (keySet[i+1].matches("[0-9]*")) // if index then array - ((JSONObject) keySeriesObj).put(keySet[i], new JSONArray()); - else - ((JSONObject) keySeriesObj).put(keySet[i], new JSONObject()); - log.info("Object is null, must add it"); - } - keySeriesObj = ( (JSONObject) keySeriesObj).opt(keySet[i]); - //log.info("JSONObject==" + keySeriesObj); - } - else - { - log.info("unknown object==" + keySeriesObj); - } - } - if (fieldType.equals("number") ) - { - DecimalFormat df = new DecimalFormat("#.0"); - if (value instanceof String) - ((JSONObject)keySeriesObj).put(keySet[keySet.length -1], Float.valueOf(df.format(Float.valueOf((String) value)))); - else - ((JSONObject)keySeriesObj).put(keySet[keySet.length -1], Float.valueOf(df.format(value))); - } - else if (fieldType.equals("integer") && value instanceof String) - ((JSONObject)keySeriesObj).put(keySet[keySet.length -1], Integer.valueOf((String) value)); - else if (fieldType.equals("JArray")) - ((JSONArray)keySeriesObj).put( value); - else - ((JSONObject)keySeriesObj).put(keySet[keySet.length -1], value); - - } - private JSONObject event = new JSONObject(); -} + } + if (fieldType.equals("number")) { + DecimalFormat df = new DecimalFormat("#.0"); + if (value instanceof String) + ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], + Float.valueOf(df.format(Float.valueOf((String) value)))); + else + ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], Float.valueOf(df.format(value))); + } else if (fieldType.equals("integer") && value instanceof String) + ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], Integer.valueOf((String) value)); + else if (fieldType.equals("JArray")) + ((JSONArray) keySeriesObj).put(value); + else + ((JSONObject) keySeriesObj).put(keySet[keySet.length - 1], value); + } + + private JSONObject event = new JSONObject(); +} diff --git a/src/main/java/org/onap/dcae/commonFunction/CustomExceptionLoader.java b/src/main/java/org/onap/dcae/commonFunction/CustomExceptionLoader.java index fbeba2f6..2aec512f 100644 --- a/src/main/java/org/onap/dcae/commonFunction/CustomExceptionLoader.java +++ b/src/main/java/org/onap/dcae/commonFunction/CustomExceptionLoader.java @@ -39,89 +39,90 @@ import com.google.gson.JsonSyntaxException; public class CustomExceptionLoader { - protected static HashMap map = null; - private static final Logger log = LoggerFactory.getLogger ( CustomExceptionLoader.class ); - - //For standalone test - //LoadMap Invoked from servletSetup - /* - public static void main(String[] args) { - - System.out.println("CustomExceptionLoader.main --> Arguments -- ExceptionConfig file: " + args[0] + "StatusCode:" + args[1]+ " Error Msg:" + args[2]); - CommonStartup.exceptionConfig = args[0]; - - //Read the Custom exception JSON file into map - LoadMap(); - System.out.println("CustomExceptionLoader.main --> Map info post LoadMap:" + map); - - String[] str= LookupMap(args[1],args[2]); - if (! (str==null)) { - System.out.println("CustomExceptionLoader.main --> Return from lookup function" + str[0] + "value:" + str[1]); + protected static HashMap map = null; + private static final Logger log = LoggerFactory.getLogger(CustomExceptionLoader.class); + + // For standalone test + // LoadMap Invoked from servletSetup + /* + * public static void main(String[] args) { + * + * System.out. + * println("CustomExceptionLoader.main --> Arguments -- ExceptionConfig file: " + * + args[0] + "StatusCode:" + args[1]+ " Error Msg:" + args[2]); + * CommonStartup.exceptionConfig = args[0]; + * + * //Read the Custom exception JSON file into map LoadMap(); System.out. + * println("CustomExceptionLoader.main --> Map info post LoadMap:" + map); + * + * String[] str= LookupMap(args[1],args[2]); if (! (str==null)) { + * System.out. + * println("CustomExceptionLoader.main --> Return from lookup function" + + * str[0] + "value:" + str[1]); } + * + * } + */ + + public static void LoadMap() { + + map = new HashMap(); + FileReader fr = null; + try { + JsonElement root = null; + fr = new FileReader(CommonStartup.exceptionConfig); + root = new JsonParser().parse(fr); + JsonObject jsonObject = root.getAsJsonObject().get("code").getAsJsonObject(); + + for (Entry entry : jsonObject.entrySet()) { + map.put(entry.getKey(), (JsonArray) entry.getValue()); + } + + log.debug("CustomExceptionLoader.LoadMap --> Map loaded - " + map); + } catch (JsonIOException | JsonSyntaxException | FileNotFoundException e) { + log.error("Exception in LoadMap:" + e.getMessage()); + map = null; + } finally { + if (fr != null) { + try { + fr.close(); + } catch (IOException e) { + log.error("Error closing file reader stream : " + e.toString()); + map = null; + } + } } - } - */ - - public static void LoadMap () { - - map = new HashMap(); - FileReader fr = null; - try { - JsonElement root = null; - fr = new FileReader(CommonStartup.exceptionConfig); - root = new JsonParser().parse(fr); - JsonObject jsonObject = root.getAsJsonObject().get("code").getAsJsonObject(); - - for (Entry entry : jsonObject.entrySet()) { - map.put(entry.getKey(), (JsonArray) entry.getValue()); + + public static String[] LookupMap(String error, String errormsg) { + + String[] retarray = null; + + log.debug("CustomExceptionLoader.LookupMap -->" + " HTTP StatusCode:" + error + " Msg:" + errormsg); + try { + + JsonArray jarray = map.get(error); + for (int i = 0; i < jarray.size(); i++) { + + JsonElement val = jarray.get(i).getAsJsonObject().get("Reason"); + JsonArray ec = (JsonArray) jarray.get(i).getAsJsonObject().get("ErrorCode"); + log.trace("CustomExceptionLoader.LookupMap Parameter -> Error msg : " + errormsg + + " Reason text being matched:" + val); + if (errormsg.contains(val.toString().replace("\"", ""))) { + log.trace( + "CustomExceptionLoader.LookupMap Successful! Exception matched to error message StatusCode:" + + ec.get(0).toString() + "ErrorMessage:" + ec.get(1).toString()); + retarray = new String[2]; + retarray[0] = ec.get(0).toString(); + retarray[1] = ec.get(1).toString(); + return retarray; } - - log.debug("CustomExceptionLoader.LoadMap --> Map loaded - " + map); - } catch (JsonIOException|JsonSyntaxException|FileNotFoundException e) { - log.error("Exception in LoadMap:" + e.getMessage()); - map = null; } - finally { - if (fr != null) { - try { - fr.close(); - } catch (IOException e) { - log.error("Error closing file reader stream : " +e.toString()); - map = null; - } - } - } - } - public static String[] LookupMap (String error, String errormsg) { - - String[] retarray = null; - - log.debug("CustomExceptionLoader.LookupMap -->" + " HTTP StatusCode:" + error + " Msg:" + errormsg); - try{ - - JsonArray jarray = map.get(error); - for (int i = 0; i < jarray.size(); i++) { - - JsonElement val = jarray.get(i).getAsJsonObject().get("Reason"); - JsonArray ec = (JsonArray) jarray.get(i).getAsJsonObject().get("ErrorCode"); - log.trace("CustomExceptionLoader.LookupMap Parameter -> Error msg : " + errormsg + " Reason text being matched:" + val); - if (errormsg.contains(val.toString().replace("\"", ""))){ - log.trace("CustomExceptionLoader.LookupMap Successful! Exception matched to error message StatusCode:" + ec.get(0).toString() + "ErrorMessage:" + ec.get(1).toString()); - retarray = new String[2]; - retarray[0]=ec.get(0).toString(); - retarray[1]=ec.get(1).toString(); - return retarray; - } - } - - } - catch (Exception e) - { - System.out.println(e.getMessage()); - } - - return retarray; + } catch (Exception e) { + System.out.println(e.getMessage()); + } + + return retarray; } } diff --git a/src/main/java/org/onap/dcae/commonFunction/DmaapPropertyReader.java b/src/main/java/org/onap/dcae/commonFunction/DmaapPropertyReader.java index b10f5882..6ff24abf 100644 --- a/src/main/java/org/onap/dcae/commonFunction/DmaapPropertyReader.java +++ b/src/main/java/org/onap/dcae/commonFunction/DmaapPropertyReader.java @@ -52,7 +52,8 @@ public class DmaapPropertyReader { fr = new FileReader(CambriaConfigFile); root = new JsonParser().parse(fr); - //Check if dmaap config is handled by legacy controller/service manager + // Check if dmaap config is handled by legacy controller/service + // manager if (root.getAsJsonObject().has("channels")) { JsonArray jsonObject = (JsonArray) root.getAsJsonObject().get("channels"); @@ -88,13 +89,14 @@ public class DmaapPropertyReader { } } else { - //Handing new format from controllergen2/config_binding_service + // Handing new format from controllergen2/config_binding_service JsonObject jsonObject = root.getAsJsonObject(); Set> entries = jsonObject.entrySet(); for (Map.Entry entry : entries) { - - JsonElement topicurl = entry.getValue().getAsJsonObject().get("dmaap_info").getAsJsonObject().get("topic_url"); + + JsonElement topicurl = entry.getValue().getAsJsonObject().get("dmaap_info").getAsJsonObject() + .get("topic_url"); String[] urlParts = dmaapUrlSplit(topicurl.toString().replace("\"", "")); String mrTopic = null; diff --git a/src/main/java/org/onap/dcae/commonFunction/EventProcessor.java b/src/main/java/org/onap/dcae/commonFunction/EventProcessor.java index 05e5f0ba..79dea790 100644 --- a/src/main/java/org/onap/dcae/commonFunction/EventProcessor.java +++ b/src/main/java/org/onap/dcae/commonFunction/EventProcessor.java @@ -42,154 +42,150 @@ import java.util.TimeZone; public class EventProcessor implements Runnable { - private static final Logger log = LoggerFactory.getLogger(EventProcessor.class); - private static final String EVENT_LITERAL = "event"; - private static final String COMMON_EVENT_HEADER = "commonEventHeader"; - - private static HashMap streamid_hash = new HashMap(); - public JSONObject event; - - public EventProcessor() { - log.debug("EventProcessor: Default Constructor"); - - String[] list = CommonStartup.streamid.split("\\|"); - for (String aList : list) { - String domain = aList.split("=")[0]; - //String streamIdList[] = list[i].split("=")[1].split(","); - String[] streamIdList = aList.substring(aList.indexOf('=') + 1).split(","); - - log.debug(String.format("Domain: %s streamIdList:%s", domain, - Arrays.toString(streamIdList))); - streamid_hash.put(domain, streamIdList); - } - - } - - @Override - public void run() { - - try { - - event = CommonStartup.fProcessingInputQueue.take(); - log.info("EventProcessor\tRemoving element: " + event); - - //EventPublisher Ep=new EventPublisher(); - while (event != null) { - // As long as the producer is running we remove elements from the queue. - - //UUID uuid = UUID.fromString(event.get("VESuniqueId").toString()); - String uuid = event.get("VESuniqueId").toString(); - LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid); - localLC.put(EcompFields.kBeginTimestampMs, SaClock.now()); - - log.debug("event.VESuniqueId" + event.get("VESuniqueId") - + "event.commonEventHeader.domain:" + event.getJSONObject(EVENT_LITERAL) - .getJSONObject(COMMON_EVENT_HEADER).getString("domain")); - String[] streamIdList = streamid_hash.get( - event.getJSONObject(EVENT_LITERAL).getJSONObject(COMMON_EVENT_HEADER) - .getString("domain")); - log.debug("streamIdList:" + streamIdList); - - if (streamIdList.length == 0) { - log.error("No StreamID defined for publish - Message dropped" + event); - } else { - for (String aStreamIdList : streamIdList) { - log.info("Invoking publisher for streamId:" + aStreamIdList); - this.overrideEvent(); - EventPublisher.getInstance(aStreamIdList).sendEvent(event); - - } - } - log.debug("Message published" + event); - event = CommonStartup.fProcessingInputQueue.take(); - // log.info("EventProcessor\tRemoving element: " + this.queue.remove()); - } - } catch (InterruptedException e) { - log.error("EventProcessor InterruptedException" + e.getMessage()); - } - - } - - - @SuppressWarnings({"unchecked", "rawtypes"}) - public void overrideEvent() { - //Set collector timestamp in event payload before publish - final Date currentTime = new Date(); - final SimpleDateFormat sdf = new SimpleDateFormat("EEE, MM dd yyyy hh:mm:ss z"); - sdf.setTimeZone(TimeZone.getTimeZone("GMT")); - - JSONObject collectorTimeStamp = new JSONObject() - .put("collectorTimeStamp", sdf.format(currentTime)); - JSONObject commonEventHeaderkey = event.getJSONObject(EVENT_LITERAL) - .getJSONObject(COMMON_EVENT_HEADER); - commonEventHeaderkey.put("internalHeaderFields", collectorTimeStamp); - event.getJSONObject(EVENT_LITERAL).put(COMMON_EVENT_HEADER, commonEventHeaderkey); - - if (CommonStartup.eventTransformFlag == 1) { - // read the mapping json file - final JsonParser parser = new JsonParser(); - FileReader fr = null; - try { - fr = new FileReader ( "./etc/eventTransform.json" ); - final JsonArray jo = (JsonArray) parser.parse(fr); - log.info("parse eventTransform.json"); - // now convert to org.json - final String jsonText = jo.toString(); - final JSONArray topLevel = new JSONArray(jsonText); - //log.info("topLevel == " + topLevel); - - Class[] paramJSONObject = new Class[1]; - paramJSONObject[0] = JSONObject.class; - //load VESProcessors class at runtime - Class cls = Class.forName("org.onap.dcae.commonFunction.ConfigProcessors"); - Constructor constr = cls.getConstructor(paramJSONObject); - Object obj = constr.newInstance(event); - - for (int j = 0; j < topLevel.length(); j++) { - JSONObject filterObj = topLevel.getJSONObject(j).getJSONObject("filter"); - Method method = cls.getDeclaredMethod("isFilterMet", paramJSONObject); - boolean filterMet = (boolean) method.invoke(obj, filterObj); - if (filterMet) { - final JSONArray processors = topLevel.getJSONObject(j) - .getJSONArray("processors"); - - //call the processor method - for (int i = 0; i < processors.length(); i++) { - final JSONObject processorList = processors.getJSONObject(i); - final String functionName = processorList.getString("functionName"); - final JSONObject args = processorList.getJSONObject("args"); - //final JSONObject filter = processorList.getJSONObject("filter"); - - log.info(String.format("functionName==%s | args==%s", functionName, - args)); - //reflect method call - method = cls.getDeclaredMethod(functionName, paramJSONObject); - method.invoke(obj, args); - } - } - } - - } catch (Exception e) { - - log.error("EventProcessor Exception" + e.getMessage() + e + e.getCause() ); - } - finally { - //close the file - if (fr != null) { - try { - fr.close(); - } catch (IOException e) { - log.error("Error closing file reader stream : " + e.toString()); - } - - } - } + private static final Logger log = LoggerFactory.getLogger(EventProcessor.class); + private static final String EVENT_LITERAL = "event"; + private static final String COMMON_EVENT_HEADER = "commonEventHeader"; + + private static HashMap streamid_hash = new HashMap(); + public JSONObject event; + + public EventProcessor() { + log.debug("EventProcessor: Default Constructor"); + + String[] list = CommonStartup.streamid.split("\\|"); + for (String aList : list) { + String domain = aList.split("=")[0]; + // String streamIdList[] = list[i].split("=")[1].split(","); + String[] streamIdList = aList.substring(aList.indexOf('=') + 1).split(","); + + log.debug(String.format("Domain: %s streamIdList:%s", domain, Arrays.toString(streamIdList))); + streamid_hash.put(domain, streamIdList); + } + } - //Remove VESversion from event. This field is for internal use and must be removed after use. - if (event.has("VESversion")) - event.remove("VESversion"); - log.debug("Modified event:" + event); + @Override + public void run() { + + try { + + event = CommonStartup.fProcessingInputQueue.take(); + log.info("EventProcessor\tRemoving element: " + event); + + // EventPublisher Ep=new EventPublisher(); + while (event != null) { + // As long as the producer is running we remove elements from + // the queue. + + // UUID uuid = + // UUID.fromString(event.get("VESuniqueId").toString()); + String uuid = event.get("VESuniqueId").toString(); + LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid); + localLC.put(EcompFields.kBeginTimestampMs, SaClock.now()); + + log.debug("event.VESuniqueId" + event.get("VESuniqueId") + "event.commonEventHeader.domain:" + + event.getJSONObject(EVENT_LITERAL).getJSONObject(COMMON_EVENT_HEADER).getString("domain")); + String[] streamIdList = streamid_hash + .get(event.getJSONObject(EVENT_LITERAL).getJSONObject(COMMON_EVENT_HEADER).getString("domain")); + log.debug("streamIdList:" + streamIdList); + + if (streamIdList.length == 0) { + log.error("No StreamID defined for publish - Message dropped" + event); + } else { + for (String aStreamIdList : streamIdList) { + log.info("Invoking publisher for streamId:" + aStreamIdList); + this.overrideEvent(); + EventPublisher.getInstance(aStreamIdList).sendEvent(event); + + } + } + log.debug("Message published" + event); + event = CommonStartup.fProcessingInputQueue.take(); + // log.info("EventProcessor\tRemoving element: " + + // this.queue.remove()); + } + } catch (InterruptedException e) { + log.error("EventProcessor InterruptedException" + e.getMessage()); + } - } + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + public void overrideEvent() { + // Set collector timestamp in event payload before publish + final Date currentTime = new Date(); + final SimpleDateFormat sdf = new SimpleDateFormat("EEE, MM dd yyyy hh:mm:ss z"); + sdf.setTimeZone(TimeZone.getTimeZone("GMT")); + + JSONObject collectorTimeStamp = new JSONObject().put("collectorTimeStamp", sdf.format(currentTime)); + JSONObject commonEventHeaderkey = event.getJSONObject(EVENT_LITERAL).getJSONObject(COMMON_EVENT_HEADER); + commonEventHeaderkey.put("internalHeaderFields", collectorTimeStamp); + event.getJSONObject(EVENT_LITERAL).put(COMMON_EVENT_HEADER, commonEventHeaderkey); + + if (CommonStartup.eventTransformFlag == 1) { + // read the mapping json file + final JsonParser parser = new JsonParser(); + FileReader fr = null; + try { + fr = new FileReader("./etc/eventTransform.json"); + final JsonArray jo = (JsonArray) parser.parse(fr); + log.info("parse eventTransform.json"); + // now convert to org.json + final String jsonText = jo.toString(); + final JSONArray topLevel = new JSONArray(jsonText); + // log.info("topLevel == " + topLevel); + + Class[] paramJSONObject = new Class[1]; + paramJSONObject[0] = JSONObject.class; + // load VESProcessors class at runtime + Class cls = Class.forName("org.onap.dcae.commonFunction.ConfigProcessors"); + Constructor constr = cls.getConstructor(paramJSONObject); + Object obj = constr.newInstance(event); + + for (int j = 0; j < topLevel.length(); j++) { + JSONObject filterObj = topLevel.getJSONObject(j).getJSONObject("filter"); + Method method = cls.getDeclaredMethod("isFilterMet", paramJSONObject); + boolean filterMet = (boolean) method.invoke(obj, filterObj); + if (filterMet) { + final JSONArray processors = topLevel.getJSONObject(j).getJSONArray("processors"); + + // call the processor method + for (int i = 0; i < processors.length(); i++) { + final JSONObject processorList = processors.getJSONObject(i); + final String functionName = processorList.getString("functionName"); + final JSONObject args = processorList.getJSONObject("args"); + // final JSONObject filter = + // processorList.getJSONObject("filter"); + + log.info(String.format("functionName==%s | args==%s", functionName, args)); + // reflect method call + method = cls.getDeclaredMethod(functionName, paramJSONObject); + method.invoke(obj, args); + } + } + } + + } catch (Exception e) { + + log.error("EventProcessor Exception" + e.getMessage() + e + e.getCause()); + } finally { + // close the file + if (fr != null) { + try { + fr.close(); + } catch (IOException e) { + log.error("Error closing file reader stream : " + e.toString()); + } + + } + } + } + // Remove VESversion from event. This field is for internal use and must + // be removed after use. + if (event.has("VESversion")) + event.remove("VESversion"); + + log.debug("Modified event:" + event); + + } } diff --git a/src/main/java/org/onap/dcae/commonFunction/EventPublisher.java b/src/main/java/org/onap/dcae/commonFunction/EventPublisher.java index d76299df..7dbb8f3c 100644 --- a/src/main/java/org/onap/dcae/commonFunction/EventPublisher.java +++ b/src/main/java/org/onap/dcae/commonFunction/EventPublisher.java @@ -34,147 +34,133 @@ import java.security.GeneralSecurityException; import java.util.List; import java.util.concurrent.TimeUnit; - public class EventPublisher { - private static final String VES_UNIQUE_ID = "VESuniqueId"; - private static EventPublisher instance; - private static CambriaBatchingPublisher pub; - - private String streamid = ""; - private String ueburl = ""; - private String topic = ""; - private String authuser = ""; - private String authpwd = ""; - - private static Logger log = LoggerFactory.getLogger(EventPublisher.class); - - - private EventPublisher(String newstreamid) { - - streamid = newstreamid; - try { - ueburl = DmaapPropertyReader.getInstance(CommonStartup.cambriaConfigFile).dmaap_hash - .get(streamid + ".cambria.url"); - - if (ueburl == null) { - ueburl = DmaapPropertyReader.getInstance(CommonStartup.cambriaConfigFile).dmaap_hash - .get(streamid + ".cambria.hosts"); - } - topic = DmaapPropertyReader.getInstance(CommonStartup.cambriaConfigFile) - .getKeyValue(streamid + ".cambria.topic"); - authuser = DmaapPropertyReader.getInstance(CommonStartup.cambriaConfigFile) - .getKeyValue(streamid + ".basicAuthUsername"); - - if (authuser != null) { - authpwd = DmaapPropertyReader - .getInstance(CommonStartup.cambriaConfigFile).dmaap_hash - .get(streamid + ".basicAuthPassword"); - } - } catch (Exception e) { - log.error("CambriaClientBuilders connection reader exception : " + e.getMessage()); - - } - - } - - - /** - * Returns event publisher - * - * @param streamid stream id - * @return event publisher - */ - public static synchronized EventPublisher getInstance(String streamid) { - if (instance == null) { - instance = new EventPublisher(streamid); - } - if (!instance.streamid.equals(streamid)) { - instance.closePublisher(); - instance = new EventPublisher(streamid); - } - return instance; - - } - - - /** - * - * @param event json object for event - */ - public synchronized void sendEvent(JSONObject event) { - - log.debug("EventPublisher.sendEvent: instance for publish is ready"); - - if (event.has(VES_UNIQUE_ID)) { - String uuid = event.get(VES_UNIQUE_ID).toString(); - LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid); - localLC.put(EcompFields.kBeginTimestampMs, SaClock.now()); - log.debug("Removing VESuniqueid object from event"); - event.remove(VES_UNIQUE_ID); - } - - try { - - if (authuser != null) { - log.debug(String.format("URL:%sTOPIC:%sAuthUser:%sAuthpwd:%s", ueburl, topic, - authuser, authpwd)); - pub = new CambriaClientBuilders.PublisherBuilder() - .usingHosts(ueburl) - .onTopic(topic) - .usingHttps() - .authenticatedByHttp(authuser, authpwd) - .logSendFailuresAfter(5) - // .logTo(log) - // .limitBatch(100, 10) - .build(); - } else { - - log.debug(String.format("URL:%sTOPIC:%s", ueburl, topic)); - pub = new CambriaClientBuilders.PublisherBuilder() - .usingHosts(ueburl) - .onTopic(topic) - // .logTo(log) - .logSendFailuresAfter(5) - // .limitBatch(100, 10) - .build(); - - } - - int pendingMsgs = pub.send("MyPartitionKey", event.toString()); - //this.wait(2000); - - if (pendingMsgs > 100) { - log.info("Pending Message Count=" + pendingMsgs); - } - - closePublisher(); - log.info("pub.send invoked - no error"); - CommonStartup.oplog.info(String.format("URL:%sTOPIC:%sEvent Published:%s", - ueburl, topic, event)); - - } catch (IOException | GeneralSecurityException | IllegalArgumentException e) { - log.error("Unable to publish event: {} streamid: {}. Exception: {}", event, streamid, e); - } - finally { - closePublisher(); + private static final String VES_UNIQUE_ID = "VESuniqueId"; + private static EventPublisher instance; + private static CambriaBatchingPublisher pub; + + private String streamid = ""; + private String ueburl = ""; + private String topic = ""; + private String authuser = ""; + private String authpwd = ""; + + private static Logger log = LoggerFactory.getLogger(EventPublisher.class); + + private EventPublisher(String newstreamid) { + + streamid = newstreamid; + try { + ueburl = DmaapPropertyReader.getInstance(CommonStartup.cambriaConfigFile).dmaap_hash + .get(streamid + ".cambria.url"); + + if (ueburl == null) { + ueburl = DmaapPropertyReader.getInstance(CommonStartup.cambriaConfigFile).dmaap_hash + .get(streamid + ".cambria.hosts"); + } + topic = DmaapPropertyReader.getInstance(CommonStartup.cambriaConfigFile) + .getKeyValue(streamid + ".cambria.topic"); + authuser = DmaapPropertyReader.getInstance(CommonStartup.cambriaConfigFile) + .getKeyValue(streamid + ".basicAuthUsername"); + + if (authuser != null) { + authpwd = DmaapPropertyReader.getInstance(CommonStartup.cambriaConfigFile).dmaap_hash + .get(streamid + ".basicAuthPassword"); + } + } catch (Exception e) { + log.error("CambriaClientBuilders connection reader exception : " + e.getMessage()); + + } + + } + + /** + * Returns event publisher + * + * @param streamid + * stream id + * @return event publisher + */ + public static synchronized EventPublisher getInstance(String streamid) { + if (instance == null) { + instance = new EventPublisher(streamid); + } + if (!instance.streamid.equals(streamid)) { + instance.closePublisher(); + instance = new EventPublisher(streamid); + } + return instance; + } - } + /** + * + * @param event + * json object for event + */ + public synchronized void sendEvent(JSONObject event) { + + log.debug("EventPublisher.sendEvent: instance for publish is ready"); + + if (event.has(VES_UNIQUE_ID)) { + String uuid = event.get(VES_UNIQUE_ID).toString(); + LoggingContext localLC = VESLogger.getLoggingContextForThread(uuid); + localLC.put(EcompFields.kBeginTimestampMs, SaClock.now()); + log.debug("Removing VESuniqueid object from event"); + event.remove(VES_UNIQUE_ID); + } + + try { + + if (authuser != null) { + log.debug(String.format("URL:%sTOPIC:%sAuthUser:%sAuthpwd:%s", ueburl, topic, authuser, authpwd)); + pub = new CambriaClientBuilders.PublisherBuilder().usingHosts(ueburl).onTopic(topic).usingHttps() + .authenticatedByHttp(authuser, authpwd).logSendFailuresAfter(5) + // .logTo(log) + // .limitBatch(100, 10) + .build(); + } else { + + log.debug(String.format("URL:%sTOPIC:%s", ueburl, topic)); + pub = new CambriaClientBuilders.PublisherBuilder().usingHosts(ueburl).onTopic(topic) + // .logTo(log) + .logSendFailuresAfter(5) + // .limitBatch(100, 10) + .build(); + + } + + int pendingMsgs = pub.send("MyPartitionKey", event.toString()); + // this.wait(2000); + + if (pendingMsgs > 100) { + log.info("Pending Message Count=" + pendingMsgs); + } + + log.info("pub.send invoked - no error"); + CommonStartup.oplog.info(String.format("URL:%sTOPIC:%sEvent Published:%s", ueburl, topic, event)); + + } catch (IOException | GeneralSecurityException | IllegalArgumentException e) { + log.error("Unable to publish event: {} streamid: {}. Exception: {}", event, streamid, e); + } finally { + closePublisher(); + } + } - public synchronized void closePublisher() { + public synchronized void closePublisher() { - try { - if (pub != null) { - final List stuck = pub.close(20, TimeUnit.SECONDS); - if (!stuck.isEmpty()) { - log.error(stuck.size() + " messages unsent"); - } - } - } catch (InterruptedException | IOException e) { - log.error("Caught Exception on Close event: {}", e); - } + try { + if (pub != null) { + final List stuck = pub.close(20, TimeUnit.SECONDS); + if (!stuck.isEmpty()) { + log.error(stuck.size() + " messages unsent"); + } + } + } catch (InterruptedException | IOException e) { + log.error("Caught Exception on Close event: {}", e); + } - } + } } diff --git a/src/main/java/org/onap/dcae/commonFunction/VESLogger.java b/src/main/java/org/onap/dcae/commonFunction/VESLogger.java index 79108443..8902a47c 100644 --- a/src/main/java/org/onap/dcae/commonFunction/VESLogger.java +++ b/src/main/java/org/onap/dcae/commonFunction/VESLogger.java @@ -33,126 +33,130 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.util.UUID; - public class VESLogger { - public static final String VES_AGENT = "VES_AGENT"; - private static final String REQUEST_ID = "requestId"; - private static final String IP_ADDRESS ="127.0.0.1"; - private static final String HOST_NAME="localhost"; - - public static Logger auditLog; - public static Logger metricsLog; - public static Logger errorLog; - public static Logger debugLog; - - // Common LoggingContext - private static LoggingContext commonLC; - // Thread-specific LoggingContext - private static LoggingContext threadLC; - public LoggingContext lc; - - - /** - * Returns the common LoggingContext instance that is the base context - * for all subsequent instances. - * - * @return the common LoggingContext - */ - public static LoggingContext getCommonLoggingContext() { - if (commonLC == null) { - commonLC = new LoggingContextFactory.Builder().build(); - final UUID uuid = UUID.randomUUID(); - - commonLC.put(REQUEST_ID, uuid.toString()); - } - return commonLC; - } - - /** - * Get a logging context for the current thread that's based on the common logging context. - * Populate the context with context-specific values. - * - * @param aUuid uuid for request id - * @return a LoggingContext for the current thread - */ - public static LoggingContext getLoggingContextForThread(UUID aUuid) { - // note that this operation requires everything from the common context - // to be (re)copied into the target context. That seems slow, but it actually - // helps prevent the thread from overwriting supposedly common data. It also - // should be fairly quick compared with the overhead of handling the actual - // service call. - - threadLC = new LoggingContextFactory.Builder(). - withBaseContext(getCommonLoggingContext()). - build(); - // Establish the request-specific UUID, as long as we are here... - threadLC.put(REQUEST_ID, aUuid.toString()); - threadLC.put(EcompFields.kEndTimestamp, SaClock.now()); - - return threadLC; - } - - /** - * Get a logging context for the current thread that's based on the common logging context. - * Populate the context with context-specific values. - * - * @param aUuid uuid for request id - * @return a LoggingContext for the current thread - */ - public static LoggingContext getLoggingContextForThread(String aUuid) { - // note that this operation requires everything from the common context - // to be (re)copied into the target context. That seems slow, but it actually - // helps prevent the thread from overwriting supposedly common data. It also - // should be fairly quick compared with the overhead of handling the actual - // service call. - - threadLC = new LoggingContextFactory.Builder(). - withBaseContext(getCommonLoggingContext()). - build(); - // Establish the request-specific UUID, as long as we are here... - threadLC.put(REQUEST_ID, aUuid); - threadLC.put("statusCode", "COMPLETE"); - threadLC.put(EcompFields.kEndTimestamp, SaClock.now()); - return threadLC; - } - - public static void setUpEcompLogging() { - - // Create ECOMP Logger instances - auditLog = LoggerFactory.getLogger("com.att.ecomp.audit"); - metricsLog = LoggerFactory.getLogger("com.att.ecomp.metrics"); - debugLog = LoggerFactory.getLogger("com.att.ecomp.debug"); - errorLog = LoggerFactory.getLogger("com.att.ecomp.error"); - - final LoggingContext lc = getCommonLoggingContext(); - - String ipAddr = IP_ADDRESS; - String hostname = HOST_NAME; - try { - final InetAddress ip = InetAddress.getLocalHost(); - hostname = ip.getCanonicalHostName(); - ipAddr = ip.getHostAddress(); - } catch (UnknownHostException x) { - Log.debug(x.getMessage()); - } - - lc.put("serverName", hostname); - lc.put("serviceName", "VESCollecor"); - lc.put("statusCode", "RUNNING"); - lc.put("targetEntity", "NULL"); - lc.put("targetServiceName", "NULL"); - lc.put("server", hostname); - lc.put("serverIpAddress", ipAddr); - - // instance UUID is meaningless here, so we just create a new one each time the - // server starts. One could argue each new instantiation of the service should - // have a new instance ID. - lc.put("instanceUuid", ""); - lc.put("severity", ""); - lc.put(EcompFields.kEndTimestamp, SaClock.now()); - lc.put("EndTimestamp", SaClock.now()); - lc.put("partnerName", "NA"); - } + public static final String VES_AGENT = "VES_AGENT"; + private static final String REQUEST_ID = "requestId"; + private static final String IP_ADDRESS = "127.0.0.1"; + private static final String HOST_NAME = "localhost"; + + public static Logger auditLog; + public static Logger metricsLog; + public static Logger errorLog; + public static Logger debugLog; + + // Common LoggingContext + private static LoggingContext commonLC; + // Thread-specific LoggingContext + private static LoggingContext threadLC; + public LoggingContext lc; + + /** + * Returns the common LoggingContext instance that is the base context for + * all subsequent instances. + * + * @return the common LoggingContext + */ + public static LoggingContext getCommonLoggingContext() { + if (commonLC == null) { + commonLC = new LoggingContextFactory.Builder().build(); + final UUID uuid = UUID.randomUUID(); + + commonLC.put(REQUEST_ID, uuid.toString()); + } + return commonLC; + } + + /** + * Get a logging context for the current thread that's based on the common + * logging context. Populate the context with context-specific values. + * + * @param aUuid + * uuid for request id + * @return a LoggingContext for the current thread + */ + public static LoggingContext getLoggingContextForThread(UUID aUuid) { + // note that this operation requires everything from the common context + // to be (re)copied into the target context. That seems slow, but it + // actually + // helps prevent the thread from overwriting supposedly common data. It + // also + // should be fairly quick compared with the overhead of handling the + // actual + // service call. + + threadLC = new LoggingContextFactory.Builder().withBaseContext(getCommonLoggingContext()).build(); + // Establish the request-specific UUID, as long as we are here... + threadLC.put(REQUEST_ID, aUuid.toString()); + threadLC.put(EcompFields.kEndTimestamp, SaClock.now()); + + return threadLC; + } + + /** + * Get a logging context for the current thread that's based on the common + * logging context. Populate the context with context-specific values. + * + * @param aUuid + * uuid for request id + * @return a LoggingContext for the current thread + */ + public static LoggingContext getLoggingContextForThread(String aUuid) { + // note that this operation requires everything from the common context + // to be (re)copied into the target context. That seems slow, but it + // actually + // helps prevent the thread from overwriting supposedly common data. It + // also + // should be fairly quick compared with the overhead of handling the + // actual + // service call. + + threadLC = new LoggingContextFactory.Builder().withBaseContext(getCommonLoggingContext()).build(); + // Establish the request-specific UUID, as long as we are here... + threadLC.put(REQUEST_ID, aUuid); + threadLC.put("statusCode", "COMPLETE"); + threadLC.put(EcompFields.kEndTimestamp, SaClock.now()); + return threadLC; + } + + public static void setUpEcompLogging() { + + // Create ECOMP Logger instances + auditLog = LoggerFactory.getLogger("com.att.ecomp.audit"); + metricsLog = LoggerFactory.getLogger("com.att.ecomp.metrics"); + debugLog = LoggerFactory.getLogger("com.att.ecomp.debug"); + errorLog = LoggerFactory.getLogger("com.att.ecomp.error"); + + final LoggingContext lc = getCommonLoggingContext(); + + String ipAddr = IP_ADDRESS; + String hostname = HOST_NAME; + try { + final InetAddress ip = InetAddress.getLocalHost(); + hostname = ip.getCanonicalHostName(); + ipAddr = ip.getHostAddress(); + } catch (UnknownHostException x) { + Log.debug(x.getMessage()); + } + + lc.put("serverName", hostname); + lc.put("serviceName", "VESCollecor"); + lc.put("statusCode", "RUNNING"); + lc.put("targetEntity", "NULL"); + lc.put("targetServiceName", "NULL"); + lc.put("server", hostname); + lc.put("serverIpAddress", ipAddr); + + // instance UUID is meaningless here, so we just create a new one each + // time the + // server starts. One could argue each new instantiation of the service + // should + // have a new instance ID. + lc.put("instanceUuid", ""); + lc.put("severity", ""); + lc.put(EcompFields.kEndTimestamp, SaClock.now()); + lc.put("EndTimestamp", SaClock.now()); + lc.put("partnerName", "NA"); + } } diff --git a/src/main/java/org/onap/dcae/controller/FetchDynamicConfig.java b/src/main/java/org/onap/dcae/controller/FetchDynamicConfig.java index 655b9755..82d4bba3 100644 --- a/src/main/java/org/onap/dcae/controller/FetchDynamicConfig.java +++ b/src/main/java/org/onap/dcae/controller/FetchDynamicConfig.java @@ -34,87 +34,82 @@ import java.util.Map; public class FetchDynamicConfig { - private static final Logger log = LoggerFactory.getLogger(FetchDynamicConfig.class); - - static String configFile = "/opt/app/KV-Configuration.json"; - static String url; - static String retString; - - public FetchDynamicConfig() { - } - - public static void main(String[] args) { - Map env = System.getenv(); - for (Map.Entry entry : env.entrySet()) { - log.info("%s=%s%n", entry.getKey(), entry.getValue()); - } - - if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE") - && env.containsKey("HOSTNAME")) { - log.info(">>>Dynamic configuration to be fetched from ConfigBindingService"); - url = env.get("CONSUL_HOST") + ":8500/v1/catalog/service/" + env - .get("CONFIG_BINDING_SERVICE"); - - retString = executecurl(url); - // consul return as array - JSONTokener temp = new JSONTokener(retString); - JSONObject cbsjobj = (JSONObject) new JSONArray(temp).get(0); - - String urlPart1 = null; - if (cbsjobj.has("ServiceAddress") && cbsjobj.has("ServicePort")) { - urlPart1 = - cbsjobj.getString("ServiceAddress") + ":" + cbsjobj.getInt("ServicePort"); - } - - log.info("CONFIG_BINDING_SERVICE DNS RESOLVED:" + urlPart1); - url = urlPart1 + "/service_component/" + env.get("HOSTNAME"); - retString = executecurl(url); - - JSONObject jsonObject = new JSONObject(new JSONTokener(retString)); - try (FileWriter file = new FileWriter(configFile)) { - file.write(jsonObject.toString()); - - log.info( - "Successfully Copied JSON Object to file /opt/app/KV-Configuration.json"); - } catch (IOException e) { - log.error( - "Error in writing configuration into file /opt/app/KV-Configuration.json " - + jsonObject, e); - e.printStackTrace(); - } - } else { - log.info(">>>Static configuration to be used"); - } - - } - - public static String executecurl(String url) { - - String[] command = {"curl", "-v", url}; - ProcessBuilder process = new ProcessBuilder(command); - Process p; - String result = null; - try { - p = process.start(); - InputStreamReader ipr = new InputStreamReader(p.getInputStream()); - BufferedReader reader = new BufferedReader(ipr); - StringBuilder builder = new StringBuilder(); - String line; - - while ((line = reader.readLine()) != null) { - builder.append(line); - } - result = builder.toString(); - log.info(result); - - reader.close(); - ipr.close(); - } catch (IOException e) { - log.error("error", e); - e.printStackTrace(); - } - return result; - - } + private static final Logger log = LoggerFactory.getLogger(FetchDynamicConfig.class); + + static String configFile = "/opt/app/KV-Configuration.json"; + static String url; + static String retString; + + public FetchDynamicConfig() { + } + + public static void main(String[] args) { + Map env = System.getenv(); + for (Map.Entry entry : env.entrySet()) { + log.info("%s=%s%n", entry.getKey(), entry.getValue()); + } + + if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE") + && env.containsKey("HOSTNAME")) { + log.info(">>>Dynamic configuration to be fetched from ConfigBindingService"); + url = env.get("CONSUL_HOST") + ":8500/v1/catalog/service/" + env.get("CONFIG_BINDING_SERVICE"); + + retString = executecurl(url); + // consul return as array + JSONTokener temp = new JSONTokener(retString); + JSONObject cbsjobj = (JSONObject) new JSONArray(temp).get(0); + + String urlPart1 = null; + if (cbsjobj.has("ServiceAddress") && cbsjobj.has("ServicePort")) { + urlPart1 = cbsjobj.getString("ServiceAddress") + ":" + cbsjobj.getInt("ServicePort"); + } + + log.info("CONFIG_BINDING_SERVICE DNS RESOLVED:" + urlPart1); + url = urlPart1 + "/service_component/" + env.get("HOSTNAME"); + retString = executecurl(url); + + JSONObject jsonObject = new JSONObject(new JSONTokener(retString)); + try (FileWriter file = new FileWriter(configFile)) { + file.write(jsonObject.toString()); + + log.info("Successfully Copied JSON Object to file /opt/app/KV-Configuration.json"); + } catch (IOException e) { + log.error("Error in writing configuration into file /opt/app/KV-Configuration.json " + jsonObject, e); + e.printStackTrace(); + } + } else { + log.info(">>>Static configuration to be used"); + } + + } + + public static String executecurl(String url) { + + String[] command = { "curl", "-v", url }; + ProcessBuilder process = new ProcessBuilder(command); + Process p; + String result = null; + try { + p = process.start(); + InputStreamReader ipr = new InputStreamReader(p.getInputStream()); + BufferedReader reader = new BufferedReader(ipr); + StringBuilder builder = new StringBuilder(); + String line; + + while ((line = reader.readLine()) != null) { + builder.append(line); + } + result = builder.toString(); + log.info(result); + + reader.close(); + ipr.close(); + } catch (IOException e) { + log.error("error", e); + e.printStackTrace(); + } + return result; + + } } diff --git a/src/main/java/org/onap/dcae/controller/LoadDynamicConfig.java b/src/main/java/org/onap/dcae/controller/LoadDynamicConfig.java index 1f77751d..2db4ff42 100644 --- a/src/main/java/org/onap/dcae/controller/LoadDynamicConfig.java +++ b/src/main/java/org/onap/dcae/controller/LoadDynamicConfig.java @@ -34,91 +34,87 @@ import java.io.InputStreamReader; import java.util.Iterator; import java.util.Map; - public class LoadDynamicConfig { - private static final Logger log = LoggerFactory.getLogger(LoadDynamicConfig.class); - - public String propFile = "collector.properties"; - public String configFile = "/opt/app/KV-Configuration.json"; - static String url; - static String retString; - - public LoadDynamicConfig() { - - } - - public static void main(String[] args) { - Map env = System.getenv(); - - //Check again to ensure new controller deployment related config - if (env.containsKey("CONSUL_HOST") && - env.containsKey("CONFIG_BINDING_SERVICE") && env.containsKey("HOSTNAME")) { - - try { - - LoadDynamicConfig lc = new LoadDynamicConfig(); - String jsonData = readFile(lc.configFile); - JSONObject jsonObject = new JSONObject(jsonData); - - PropertiesConfiguration conf; - conf = new PropertiesConfiguration(lc.propFile); - conf.setEncoding(null); - - // update properties based on consul dynamic configuration - Iterator keys = jsonObject.keys(); - - while (keys.hasNext()) { - String key = (String) keys.next(); - // check if any configuration is related to dmaap - // and write into dmaapconfig.json - if (key.startsWith("streams_publishes")) { - //VESCollector only have publish streams - try (FileWriter file = new FileWriter("./etc/DmaapConfig.json")) { - file.write(jsonObject.get(key).toString()); - log.info("Successfully written JSON Object to DmaapConfig.json"); - file.close(); - } catch (IOException e) { - log.info( - "Error in writing dmaap configuration into DmaapConfig.json", - e); - } - } else { - conf.setProperty(key, jsonObject.get(key).toString()); - } - - } - conf.save(); - - } catch (ConfigurationException e) { - log.error(e.getLocalizedMessage(), e); - e.printStackTrace(); - - } - - } else { - log.info(">>>Static configuration to be used"); - } - - } - - public static String readFile(String filename) { - String result = ""; - try (BufferedReader br = new BufferedReader(new FileReader(filename))) { - StringBuilder sb = new StringBuilder(); - String line = br.readLine(); - while (line != null) { - sb.append(line); - line = br.readLine(); - } - result = sb.toString(); - br.close(); - } catch (Exception e) { - log.error(e.getLocalizedMessage(), e); - e.printStackTrace(); - } - return result; - } - + private static final Logger log = LoggerFactory.getLogger(LoadDynamicConfig.class); + + public String propFile = "collector.properties"; + public String configFile = "/opt/app/KV-Configuration.json"; + static String url; + static String retString; + + public LoadDynamicConfig() { + + } + + public static void main(String[] args) { + Map env = System.getenv(); + + // Check again to ensure new controller deployment related config + if (env.containsKey("CONSUL_HOST") && env.containsKey("CONFIG_BINDING_SERVICE") + && env.containsKey("HOSTNAME")) { + + try { + + LoadDynamicConfig lc = new LoadDynamicConfig(); + String jsonData = readFile(lc.configFile); + JSONObject jsonObject = new JSONObject(jsonData); + + PropertiesConfiguration conf; + conf = new PropertiesConfiguration(lc.propFile); + conf.setEncoding(null); + + // update properties based on consul dynamic configuration + Iterator keys = jsonObject.keys(); + + while (keys.hasNext()) { + String key = (String) keys.next(); + // check if any configuration is related to dmaap + // and write into dmaapconfig.json + if (key.startsWith("streams_publishes")) { + // VESCollector only have publish streams + try (FileWriter file = new FileWriter("./etc/DmaapConfig.json")) { + file.write(jsonObject.get(key).toString()); + log.info("Successfully written JSON Object to DmaapConfig.json"); + file.close(); + } catch (IOException e) { + log.info("Error in writing dmaap configuration into DmaapConfig.json", e); + } + } else { + conf.setProperty(key, jsonObject.get(key).toString()); + } + + } + conf.save(); + + } catch (ConfigurationException e) { + log.error(e.getLocalizedMessage(), e); + e.printStackTrace(); + + } + + } else { + log.info(">>>Static configuration to be used"); + } + + } + + public static String readFile(String filename) { + String result = ""; + try (BufferedReader br = new BufferedReader(new FileReader(filename))) { + StringBuilder sb = new StringBuilder(); + String line = br.readLine(); + while (line != null) { + sb.append(line); + line = br.readLine(); + } + result = sb.toString(); + br.close(); + } catch (Exception e) { + log.error(e.getLocalizedMessage(), e); + e.printStackTrace(); + } + return result; + } } -- cgit 1.2.3-korg