+ sb.append(")").toString();
+ putRuletoJson(tempJsonNode, i, sb, parseKey, primarykeyAlarm);
+ sigRules = new HashMap<>();
+ }
+ } else {
+ parseData(triggerSig, parseKey);
+ }
+ }
+ policyAdapter.setJsonBody(finalJson.toString());
+ return policyAdapter;
+ }
+
+ private JsonNode putRuletoJson(JsonNode tmpJsonNode, int item, StringBuilder sb, String parseKey,
+ String primaryKey) {
+ JsonNode tmp = tmpJsonNode;
+ ObjectNode objectNode = (ObjectNode) tmp;
+ JsonNode jsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
+ JsonNode tempRuleJsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
+ String[] tempSt = parseKey.split("\\.");
+ for (String value : tempSt) {
+ if (value.contains("[")) {
+ if (tempRuleJsonNode instanceof ArrayNode) {
+ JsonNode tempRuleNode = tempRuleJsonNode.get(item);
+ ((ArrayNode) tempRuleJsonNode).removeAll();
+ ((ArrayNode) tempRuleJsonNode).add(tempRuleNode);
+ objectNode = (ObjectNode) tempRuleJsonNode.get(item);
+ }
+ String key = value.substring(0, value.indexOf('['));
+ objectNode.remove(key);
+ objectNode.put(key, sb.toString());
+ return tmp;
+ } else {
+ jsonNode = jsonNode.get(value);
+ if (jsonNode instanceof ArrayNode) {
+ tempRuleJsonNode = jsonNode;
+ jsonNode = jsonNode.get(item);
+ }
+ }
+ }
+ return tmp;
+ }
+
+ public boolean checkPattern(String patternString) {
+ return PATTERN.matcher(patternString).find();
+ }
+
+ /**
+ * Construct rule.
+ *
+ * @param sb the sb
+ * @param jsonNode the json node
+ * @param sigRules2 the sig rules 2
+ */
+ public void constructRule(StringBuilder sb, String jsonNode, Map<String, String> sigRules2) {
+ int count = 0;
+ String cleanJsonNode = jsonNode.replace("\"\"", " ");
+ cleanJsonNode = cleanJsonNode.replaceAll("\"", "");
+ cleanJsonNode = cleanJsonNode.replaceAll("\\(", "");
+ cleanJsonNode = cleanJsonNode.replaceAll("\\)", "");
+ boolean flag = false;
+ if (cleanJsonNode.contains("OR")) {
+ sb.append("(");
+ flag = true;
+ }
+ for (String rowValue : cleanJsonNode.split(" ")) {
+ if (checkPattern(rowValue)) {
+ String value = sigRules2.get(rowValue);
+ LOGGER.info(" Value is:" + value);
+ constructRule(sb, value, sigRules2);
+ } else {
+ if ((count == 0) && (!("AND").equals(rowValue)) && (!("OR").equals(rowValue))) {
+ sb.append("(");
+ }
+ count++;
+ LOGGER.info(" " + rowValue + " ");
+ sb.append(" " + rowValue + " ");
+ if (count % 3 == 0) {
+ sb.append(")");
+ count = 0;
+ }
+ }
+ }
+ if (flag) {
+ sb.append(")");
+ }
+ }
+
+ /**
+ * Parses the data.
+ *
+ * @param jsonNode the json node
+ * @param string the string
+ */
+ public void parseData(JsonNode jsonNode, String string) {
+ if (string.contains(".")) {
+ String firstIndex = string.substring(0, string.indexOf('.'));
+ JsonNode signtures = jsonNode.get(firstIndex);
+ String subIndex = string.substring(firstIndex.length() + 1);
+ if (signtures instanceof ArrayNode) {
+ for (int i = 0; i < signtures.size(); i++) {
+ parseData(signtures.get(i), subIndex);
+ }
+ } else {
+ parseData(signtures, subIndex);
+ }
+ } else {
+ if (string.contains("[")) {
+ String ruleIndex = string.substring(0, string.indexOf('['));
+ String[] keys = string.substring(string.indexOf('[') + 1, string.lastIndexOf(']')).split(",");
+ String key = "A" + Integer.valueOf(sigRules.size() + 1);
+ JsonNode node = jsonNode.get(ruleIndex);
+ StringBuilder sb = new StringBuilder("(");
+ for (int i = 0; i < keys.length; i++) {
+ sb.append(node.get(keys[i].trim()));
+ }
+ sb.append(")");
+ sigRules.put(key, sb.toString());
+ }
+ }
+ }
+
+ /**
+ * removeNullAttributes.
+ *
+ * @param cleanJson String
+ * @returnString
+ */
+ public String removeNullAttributes(String cleanJson) {
+ ObjectMapper mapper = new ObjectMapper();
+
+ try {
+ JsonNode rootNode = mapper.readTree(cleanJson);
+ JsonNode returnNode = mapper.readTree(cleanJson);
+ Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
+ boolean remove = false;
+ JsonObject removed = null;
+ boolean contentChanged = false;
+ while (fieldsIterator.hasNext()) {
+ Map.Entry<String, JsonNode> field = fieldsIterator.next();
+ final String key = field.getKey();
+ final JsonNode value = field.getValue();
+ if ("content".equalsIgnoreCase(key)) {
+ String contentStr = value.toString();
+ try (JsonReader reader = Json.createReader(new StringReader(contentStr))) {
+ JsonObject jsonContent = reader.readObject();
+ removed = removeNull(jsonContent);
+ if (!jsonContent.toString().equals(removed.toString())) {
+ contentChanged = true;
+ }
+ }
+
+ if (value == null || value.isNull()) {
+ ((ObjectNode) returnNode).remove(key);
+ remove = true;
+ }
+ }
+ if (remove) {
+ cleanJson = returnNode.toString();
+ }
+ if (value == null || value.isNull()) {
+ ((ObjectNode) returnNode).remove(key);
+ remove = true;
+ }
+ }
+ if (remove) {
+ cleanJson = returnNode.toString();
+ }
+
+ if (contentChanged) {
+ // set modified content to cleanJson
+ JSONObject jsonObject = new JSONObject(cleanJson);
+ jsonObject.put("content", removed.toString());
+ cleanJson = cleanUpJson(jsonObject.toString());
+ }
+
+ } catch (IOException e) {
+ LOGGER.error("Error writing out the JsonNode", e);
+ }
+ return cleanJson;
+ }
+
+ /**
+ * To verify if it is a JSON string. If it is, then return its original value.
+ *
+ * @param key holds the values
+ * @return
+ */
+ private String getOriginalValue(String key) {
+ for (String k : jsonStringValues.keySet()) {
+ if (k.contains("@")) {
+ String[] arrOfKeys = k.split("@");
+ for (int i = 0; i < arrOfKeys.length; i++) {
+ if (arrOfKeys[i].contains(".")) {
+ arrOfKeys[i] = arrOfKeys[i].substring(arrOfKeys[i].indexOf(".") + 1);
+ if (arrOfKeys[i].equals(key)) {
+ return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""},
+ new String[] {"\\\""});
+ }
+ }
+ }
+ }
+ if (k.endsWith(key)) {
+ return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""}, new String[] {"\\\""});
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * removeNull.
+ *
+ * @param array JsonArray
+ * @return JsonArray
+ */
+ public JsonArray removeNull(JsonArray array) {
+ JsonArrayBuilder builder = Json.createArrayBuilder();
+ int index = 0;
+ for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++index) {
+ JsonValue value = it.next();
+ switch (value.getValueType()) {
+ case ARRAY:
+ JsonArray tempArray = removeNull(array.getJsonArray(index));
+ if (!tempArray.isEmpty()) {
+ builder.add(tempArray);
+ }
+ break;
+ case OBJECT:
+ JsonObject object = removeNull(array.getJsonObject(index));
+ if (!object.isEmpty()) {
+ builder.add(object);
+ }
+ break;
+ case STRING:
+ String str = array.getString(index);
+ if (str != null && !str.isEmpty()) {
+ builder.add(str);
+ }
+ break;
+ case NUMBER:
+ builder.add(array.getJsonNumber(index));
+ break;
+ case TRUE:
+ case FALSE:
+ builder.add(array.getBoolean(index));
+ break;
+ case NULL:
+ default:
+ break;
+ }
+ }
+ return builder.build();
+ }
+
+ /**
+ * removeNull.
+ *
+ * @param obj JsonObject
+ * @return JsonObject
+ */
+ public JsonObject removeNull(JsonObject obj) {
+ JsonObjectBuilder builder = Json.createObjectBuilder();
+ for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
+ Entry<String, JsonValue> entry = it.next();
+ String key = entry.getKey();
+ JsonValue value = entry.getValue();
+ switch (value.getValueType()) {
+ case ARRAY:
+ JsonArray array = removeNull(obj.getJsonArray(key));
+ if (!array.isEmpty()) {
+ builder.add(key, array);
+ }
+ break;
+ case OBJECT:
+ JsonObject object = removeNull(obj.getJsonObject(key));
+ if (!object.isEmpty()) {
+ if (!jsonStringValues.isEmpty()) {
+ String originalValue = getOriginalValue(key);
+ if (originalValue != null) {
+ builder.add(key, object.toString());
+ break;
+ }
+ }
+ builder.add(key, object);
+ }
+ break;
+ case STRING:
+ String str = obj.getString(key);
+ if (str != null && !str.isEmpty()) {
+ if (!jsonStringValues.isEmpty()) {
+ String originalValue = getOriginalValue(key);
+ if (originalValue != null) {
+ str = getOriginalValue(key);
+ }
+ }
+ builder.add(key, str);
+ }
+ break;
+ case NUMBER:
+ builder.add(key, obj.getJsonNumber(key));
+ break;
+ case TRUE:
+ case FALSE:
+ builder.add(key, obj.getBoolean(key));
+ break;
+ case NULL:
+ default:
+ break;
+ }
+ }
+ return builder.build();
+ }
+
+ /**
+ * cleanUpJson.
+ *
+ * @param json String
+ * @return String
+ */
+ public String cleanUpJson(String json) {
+ String cleanJson = StringUtils.replaceEach(json, new String[] {"\\\\", "\\\\\\", "\\\\\\\\"},
+ new String[] {"\\", "\\", "\\"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\\"}, new String[] {"\\"});
+ cleanJson =
+ StringUtils.replaceEach(cleanJson, new String[] {"\\\\", "[[", "]]"}, new String[] {"\\", "[", "]"});
+
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\"", "\\\"", "\"[{", "}]\""},
+ new String[] {"\"", "\"", "[{", "}]"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[{", "}]\""}, new String[] {"[{", "}]"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[", "]\""}, new String[] {"[", "]"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"{", "}\""}, new String[] {"{", "}"});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\"\"", "\"\""}, new String[] {"\"", "\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\""}, new String[] {""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\""}, new String[] {"\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\\\\\\"}, new String[] {"\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\\\""}, new String[] {"\""});
+ cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[", "]\""}, new String[] {"[", "]"});
+ return cleanJson;
+ }
+
+ /**
+ * decodeContent.
+ *
+ * @param jsonNode JsonNode
+ * @return JsonNode
+ */
+ public JSONObject decodeContent(JsonNode jsonNode) {
+ Iterator<JsonNode> jsonElements = jsonNode.elements();
+ Iterator<String> jsonKeys = jsonNode.fieldNames();
+ Map<String, String> element = new TreeMap<>();
+ while (jsonElements.hasNext() && jsonKeys.hasNext()) {
+ element.put(jsonKeys.next(), jsonElements.next().toString());
+ }
+ JSONObject jsonResult = new JSONObject();
+ JSONArray jsonArray = null;
+ String oldValue = null;
+ String nodeKey = null;
+ String arryKey = null;
+ Boolean isArray = false;
+ JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
+ ObjectNode node = nodeFactory.objectNode();
+ String prevKey = null;
+ String presKey;
+ for (Entry<String, String> entry : element.entrySet()) {
+ String key = entry.getKey();
+ String value = entry.getValue();
+ if (key.contains(".")) {
+ presKey = key.substring(0, key.indexOf('.'));
+ } else if (key.contains("@")) {
+ presKey = key.substring(0, key.indexOf('@'));
+ } else {
+ presKey = key;
+ }
+ // first check if we are different from old.
+ LOGGER.info(key + "\n");
+ if (jsonArray != null && jsonArray.length() > 0 && key.contains("@") && !key.contains(".")
+ && oldValue != null) {
+ if (!oldValue.equals(key.substring(0, key.indexOf('@')))) {
+ jsonResult.put(oldValue, jsonArray);
+ jsonArray = new JSONArray();
+ }
+ } else if (jsonArray != null && jsonArray.length() > 0 && !presKey.equals(prevKey) && oldValue != null) {
+ jsonResult.put(oldValue, jsonArray);
+ isArray = false;
+ jsonArray = new JSONArray();
+ }
+
+ prevKey = presKey;
+ //
+ if (key.contains(".")) {
+ if (nodeKey == null) {
+ nodeKey = key.substring(0, key.indexOf('.'));
+ }
+ if (nodeKey.equals(key.substring(0, key.indexOf('.')))) {
+ node.put(key.substring(key.indexOf('.') + 1), value);
+ } else {
+ if (node.size() != 0) {
+ if (nodeKey.contains("@")) {
+ if (arryKey == null) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ if (nodeKey.endsWith("@0")) {
+ isArray = true;
+ jsonArray = new JSONArray();
+ }
+ if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
+ jsonArray.put(decodeContent(node));
+ }
+ if ((key.contains("@") && !arryKey.equals(key.substring(0, nodeKey.indexOf('@'))))
+ || !key.contains("@")) {
+ jsonResult.put(arryKey, jsonArray);
+ jsonArray = new JSONArray();
+ }
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ } else {
+ isArray = false;
+ jsonResult.put(nodeKey, decodeContent(node));
+ }
+ node = nodeFactory.objectNode();
+ }
+ nodeKey = key.substring(0, key.indexOf('.'));
+ if (nodeKey.contains("@")) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ node.put(key.substring(key.indexOf('.') + 1), value);
+ }
+ } else {
+ if (node.size() != 0) {
+ if (nodeKey.contains("@")) {
+ if (arryKey == null) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ if (nodeKey.endsWith("@0")) {
+ isArray = true;
+ jsonArray = new JSONArray();
+ }
+ if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
+ jsonArray.put(decodeContent(node));
+ }
+ jsonResult.put(arryKey, jsonArray);
+ jsonArray = new JSONArray();
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ } else {
+ isArray = false;
+ jsonResult.put(nodeKey, decodeContent(node));
+ }
+ node = nodeFactory.objectNode();
+ }
+ if (key.contains("@")) {
+ isArray = true;
+ if (key.endsWith("@0") || jsonArray == null) {
+ jsonArray = new JSONArray();
+ }
+ } else if (!key.contains("@")) {
+ isArray = false;
+ }
+ if (isArray) {
+ if (oldValue == null) {
+ oldValue = key.substring(0, key.indexOf('@'));
+ }
+ if (oldValue != prevKey) {
+ oldValue = key.substring(0, key.indexOf('@'));
+ }
+ if (oldValue.equals(key.substring(0, key.indexOf('@')))) {
+ jsonArray.put(value);
+ } else {
+ jsonResult.put(oldValue, jsonArray);
+ jsonArray = new JSONArray();
+ }
+ oldValue = key.substring(0, key.indexOf('@'));
+ } else {
+ jsonResult.put(key, value);
+ }
+ }
+ }
+ if (node.size() > 0) {
+ if (nodeKey.contains("@")) {
+ if (jsonArray == null) {
+ jsonArray = new JSONArray();
+ }
+ if (arryKey == null) {
+ arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
+ }
+ jsonArray.put(decodeContent(node));
+ jsonResult.put(arryKey, jsonArray);
+ isArray = false;
+ } else {
+ jsonResult.put(nodeKey, decodeContent(node));
+ }
+ }
+ if (isArray && jsonArray.length() > 0) {
+ jsonResult.put(oldValue, jsonArray);
+ }
+ return jsonResult;
+ }
+
+ @RequestMapping(
+ value = {"/policyController/getDCAEMSTemplateData.htm"},
+ method = {org.springframework.web.bind.annotation.RequestMethod.POST})
+ public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response)
+ throws IOException {
+ // TreeSet is used to ensure that individual items appear before their containing collection.
+ allManyTrueKeys = new TreeSet<>();
+ ObjectMapper mapper = new ObjectMapper();
+ mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+ JsonNode root = mapper.readTree(request.getReader());
+
+ String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
+ String servicename = value.split("-v")[0];
+ String version = null;
+ if (value.contains("-v")) {
+ version = value.split("-v")[1];
+ }
+ MicroServiceModels returnModel = getAttributeObject(servicename, version);
+
+ MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value);
+ JSONObject jsonHdDefaultObj = null;
+ if (returnHeaderDefauls != null) {
+ jsonHdDefaultObj = new JSONObject();
+ jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName());
+ jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard());
+ jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel());
+ jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType());
+ jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority());
+ }
+ String headDefautlsData = "";
+ if (jsonHdDefaultObj != null) {
+ headDefautlsData = jsonHdDefaultObj.toString();
+ } else {
+ headDefautlsData = "null";
+ }
+
+ // Get all keys with "MANY-true" defined in their value from subAttribute
+ Set<String> allkeys = null;
+ if (returnModel.getSub_attributes() != null && !returnModel.getSub_attributes().isEmpty()) {
+ JSONObject json = new JSONObject(returnModel.getSub_attributes());
+ getAllKeys(json);
+ allkeys = allManyTrueKeys;
+ allManyTrueKeys = new TreeSet<>();
+ LOGGER.info("allkeys : " + allkeys);
+ }
+
+ // Get element order info
+ String dataOrderInfo = returnModel.getDataOrderInfo();
+ if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
+ dataOrderInfo = "\"" + dataOrderInfo + "\"";
+ }
+ LOGGER.info("dataOrderInfo : " + dataOrderInfo);
+
+ String allMnyTrueKeys = "";
+ if (allkeys != null) {
+ allMnyTrueKeys = allkeys.toString();
+ }
+
+ String jsonModel = createMicroSeriveJson(returnModel);
+
+ JSONObject jsonObject = new JSONObject(jsonModel);