2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controller;
23 import com.att.research.xacml.util.XACMLProperties;
24 import com.fasterxml.jackson.core.JsonProcessingException;
25 import com.fasterxml.jackson.databind.DeserializationFeature;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.ObjectWriter;
29 import com.fasterxml.jackson.databind.node.ArrayNode;
30 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
31 import com.fasterxml.jackson.databind.node.ObjectNode;
32 import com.google.gson.Gson;
33 import java.io.BufferedInputStream;
34 import java.io.BufferedOutputStream;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.OutputStream;
39 import java.io.StringReader;
40 import java.nio.charset.StandardCharsets;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.util.ArrayList;
45 import java.util.Base64;
46 import java.util.Enumeration;
47 import java.util.HashMap;
48 import java.util.HashSet;
49 import java.util.Iterator;
50 import java.util.LinkedHashMap;
51 import java.util.LinkedList;
52 import java.util.List;
54 import java.util.Map.Entry;
56 import java.util.TreeMap;
57 import java.util.TreeSet;
58 import java.util.UUID;
59 import java.util.regex.Pattern;
60 import java.util.zip.ZipEntry;
61 import java.util.zip.ZipFile;
62 import javax.json.Json;
63 import javax.json.JsonArray;
64 import javax.json.JsonArrayBuilder;
65 import javax.json.JsonObject;
66 import javax.json.JsonObjectBuilder;
67 import javax.json.JsonReader;
68 import javax.json.JsonValue;
69 import javax.servlet.http.HttpServletRequest;
70 import javax.servlet.http.HttpServletResponse;
73 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
74 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
75 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
76 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
77 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
78 import org.apache.commons.compress.utils.IOUtils;
79 import org.apache.commons.fileupload.FileItem;
80 import org.apache.commons.fileupload.FileUploadException;
81 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
82 import org.apache.commons.fileupload.servlet.ServletFileUpload;
83 import org.apache.commons.io.FileUtils;
84 import org.apache.commons.lang.StringUtils;
85 import org.apache.commons.lang3.StringEscapeUtils;
86 import org.json.JSONArray;
87 import org.json.JSONObject;
88 import org.onap.policy.common.logging.flexlogger.FlexLogger;
89 import org.onap.policy.common.logging.flexlogger.Logger;
90 import org.onap.policy.rest.XacmlRestProperties;
91 import org.onap.policy.rest.adapter.PolicyRestAdapter;
92 import org.onap.policy.rest.dao.CommonClassDao;
93 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
94 import org.onap.policy.rest.jpa.MicroServiceModels;
95 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
96 import org.onap.policy.rest.jpa.PolicyEntity;
97 import org.onap.policy.rest.util.MsAttributeObject;
98 import org.onap.policy.rest.util.MsModelUtils;
99 import org.onap.policy.rest.util.MsModelUtils.ModelType;
100 import org.onap.policy.utils.PolicyUtils;
101 import org.onap.portalsdk.core.controller.RestrictedBaseController;
102 import org.onap.portalsdk.core.web.support.JsonMessage;
103 import org.springframework.beans.factory.annotation.Autowired;
104 import org.springframework.http.MediaType;
105 import org.springframework.stereotype.Controller;
106 import org.springframework.web.bind.annotation.RequestMapping;
107 import org.springframework.web.servlet.ModelAndView;
111 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
112 private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
114 private static CommonClassDao commonClassDao;
116 public static CommonClassDao getCommonClassDao() {
117 return commonClassDao;
120 public static void setCommonClassDao(CommonClassDao commonClassDao) {
121 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
124 private MicroServiceModels newModel;
125 private String newFile;
126 private String directory;
127 private List<String> modelList = new ArrayList<>();
128 private List<String> dirDependencyList = new ArrayList<>();
129 private LinkedHashMap<String, MsAttributeObject> classMap = new LinkedHashMap<>();
130 String referenceAttributes;
131 String attributeString;
132 Set<String> allManyTrueKeys = null;
133 private Map<String, String> sigRules = null;
135 public static final String DATATYPE = "data_types.policy.data.";
136 public static final String PROPERTIES = ".properties.";
137 public static final String TYPE = ".type";
138 public static final String STRING = "string";
139 public static final String INTEGER = "integer";
140 public static final String LIST = "list";
141 public static final String DEFAULT = ".default";
142 public static final String REQUIRED = ".required";
143 public static final String MATCHABLE = ".matchable";
144 public static final String MANYFALSE = ":MANY-false";
145 private static final Pattern PATTERN = Pattern.compile("[A][0-9]");
146 private static final String POLICYJSON = "policyJSON";
149 private CreateDcaeMicroServiceController(CommonClassDao commonClassDao) {
150 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
153 public CreateDcaeMicroServiceController() {
157 protected PolicyRestAdapter policyAdapter = null;
158 private int priorityCount;
159 private Map<String, String> attributesListRefMap = new HashMap<>();
160 private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
161 private Map<String, String> jsonStringValues = new HashMap<>();
164 * setDataToPolicyRestAdapter.
166 * @param policyData PolicyRestAdapter
167 * @param root JsonNode
168 * @return PolicyRestAdapter
170 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
172 String jsonContent = null;
174 LOGGER.info("policyJSON :" + (root.get(POLICYJSON)).toString());
176 String tempJson = root.get(POLICYJSON).toString();
177 JSONObject policyJson = new JSONObject(root.get(POLICYJSON).toString());
178 tempJson = decodeJsonVal(tempJson, policyJson);
179 // ---replace empty value with the value below before calling decodeContent method.
180 String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
181 LOGGER.info("dummyValue:" + dummyValue);
183 StringUtils.replaceEach(tempJson, new String[] {"\"\""}, new String[] {"\"" + dummyValue + "\""});
184 ObjectMapper mapper = new ObjectMapper();
185 JsonNode tempJsonNode = mapper.readTree(tempJson);
186 jsonContent = decodeContent(tempJsonNode).toString();
187 constructJson(policyData, jsonContent, dummyValue);
188 } catch (Exception e) {
189 LOGGER.error("Error while decoding microservice content", e);
192 // ----Here is the final step to reset the original value back.
193 if (policyData.getJsonBody() != null && jsonStringValues.size() > 0) {
194 String contentBody = policyData.getJsonBody();
195 JSONObject contentJson = new JSONObject(contentBody);
196 JSONObject content = contentJson.getJSONObject("content");
197 content = setOriginalJsonObject(content, jsonStringValues);
198 contentJson.put("content", content);
199 policyData.setJsonBody(StringEscapeUtils.unescapeJava(contentJson.toString()));
205 private String decodeJsonVal(String tempJson, JSONObject policyJson) {
206 if (policyJson != null) {
207 for (Object key : policyJson.keySet()) {
208 String keyStr = (String) key;
209 Object keyvalue = policyJson.get(keyStr);
210 String decodedString = keyvalue.toString();//.replace("\"", "");
211 if(!decodedString.equals("true") && !decodedString.equals("false")) {
212 String decodedJson = new String(Base64.getDecoder().decode(decodedString), StandardCharsets.UTF_8).replace("\"", "\\\"");
213 policyJson.put(keyStr, decodedJson);
216 tempJson = saveOriginalJsonObject(policyJson, jsonStringValues).toString();
221 private JSONObject saveOriginalJsonObject(JSONObject jsonObj, Map<String, String> jsonStringValues) {
222 for (Object key : jsonObj.keySet()) {
223 String keyStr = (String) key;
224 Object keyvalue = jsonObj.get(keyStr);
225 if (keyvalue.toString().contains("{\\\"") || keyvalue.toString().contains("\\\"")) {
226 jsonStringValues.put(keyStr, keyvalue.toString());
227 // --- set default value
228 jsonObj.put(keyStr, "JSON_STRING");
231 // for nested objects iteration if required
232 if (keyvalue instanceof JSONObject) {
233 saveOriginalJsonObject((JSONObject) keyvalue, jsonStringValues);
234 // --- set default value
235 jsonObj.put(keyStr, "JSON_STRING");
238 if (keyvalue instanceof JSONArray) {
239 for (int i = 0; i < ((JSONArray) keyvalue).length(); i++) {
240 JSONObject temp = ((JSONArray) keyvalue).getJSONObject(i);
241 saveOriginalJsonObject(temp, jsonStringValues);
249 private JSONObject setOriginalJsonObject(JSONObject jsonObj, Map<String, String> jsonStringValues) {
250 for (Object key : jsonObj.keySet()) {
251 String keyStr = (String) key;
252 Object keyvalue = jsonObj.get(keyStr);
253 String originalValue = getOriginalValue(keyStr);
254 if (originalValue != null) {
255 jsonObj.put(keyStr, originalValue);
258 // for nested objects iteration if required
259 if (keyvalue instanceof JSONObject) {
260 setOriginalJsonObject((JSONObject) keyvalue, jsonStringValues);
261 jsonObj.put(keyStr, originalValue);
264 if (keyvalue instanceof JSONArray) {
265 for (int i = 0; i < ((JSONArray) keyvalue).length(); i++) {
266 JSONObject temp = ((JSONArray) keyvalue).getJSONObject(i);
267 setOriginalJsonObject(temp, jsonStringValues);
275 private GroupPolicyScopeList getPolicyObject(String policyScope) {
276 return (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
279 private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue)
281 ObjectWriter om = new ObjectMapper().writer();
283 DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
284 MicroServiceModels returnModel = new MicroServiceModels();
285 microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XacmlRestProperties.TEMPLATE_VERSION_MS));
286 if (policyAdapter.getServiceType() != null) {
287 microServiceObject.setService(policyAdapter.getServiceType());
288 microServiceObject.setVersion(policyAdapter.getVersion());
289 returnModel = getAttributeObject(microServiceObject.getService(), microServiceObject.getVersion());
291 if (returnModel.getAnnotation() == null || returnModel.getAnnotation().isEmpty()) {
292 if (policyAdapter.getUuid() != null) {
293 microServiceObject.setUuid(policyAdapter.getUuid());
295 if (policyAdapter.getLocation() != null) {
296 microServiceObject.setLocation(policyAdapter.getLocation());
298 if (policyAdapter.getConfigName() != null) {
299 microServiceObject.setConfigName(policyAdapter.getConfigName());
301 GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
302 if (policyScopeValue != null) {
303 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());
307 if (policyAdapter.getPolicyName() != null) {
308 microServiceObject.setPolicyName(policyAdapter.getPolicyName());
310 if (policyAdapter.getPolicyDescription() != null) {
311 microServiceObject.setDescription(policyAdapter.getPolicyDescription());
313 if (policyAdapter.getPriority() != null) {
314 microServiceObject.setPriority(policyAdapter.getPriority());
316 microServiceObject.setPriority("9999");
319 if (policyAdapter.getRiskLevel() != null) {
320 microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
322 if (policyAdapter.getRiskType() != null) {
323 microServiceObject.setRiskType(policyAdapter.getRiskType());
325 if (policyAdapter.getGuard() != null) {
326 microServiceObject.setGuard(policyAdapter.getGuard());
328 microServiceObject.setContent(jsonContent);
329 String modelName = policyAdapter.getServiceType();
330 String versionName = policyAdapter.getVersion();
331 List<Object> triggerData = commonClassDao.getDataById(MicroServiceModels.class, "modelName:version",
332 modelName + ":" + versionName);
333 MicroServiceModels model = null;
334 boolean ruleCheck = false;
335 if (!triggerData.isEmpty()) {
336 model = (MicroServiceModels) triggerData.get(0);
337 if (model.getRuleFormation() != null) {
338 microServiceObject.setUiContent(jsonContent);
343 json = om.writeValueAsString(microServiceObject);
344 } catch (JsonProcessingException e) {
345 LOGGER.error("Error writing out the object", e);
347 LOGGER.info("input json: " + json);
348 LOGGER.info("input jsonContent: " + jsonContent);
349 String cleanJson = cleanUpJson(json);
350 // --- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
352 StringUtils.replaceEach(cleanJson, new String[] {"\"" + dummyValue + "\""}, new String[] {"\"\""});
353 LOGGER.info("tempJson: " + tempJson);
354 cleanJson = removeNullAttributes(tempJson);
355 if (cleanJson.contains("\\")) {
356 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\"}, new String[] {""});
358 policyAdapter.setJsonBody(cleanJson);
360 ObjectMapper mapper = new ObjectMapper();
361 JsonNode tempJsonNode = mapper.readTree(cleanJson);
363 return policyAdapter;
365 ObjectNode finalJson = (ObjectNode) tempJsonNode;
366 JsonNode object = tempJsonNode.get("content");
367 String primaryKey1 = model.getRuleFormation();
368 String[] primaryKeyForSignatures = primaryKey1.split("@");
369 for (String primaryKeyForSignature : primaryKeyForSignatures) {
370 String primarykeyAlarm = primaryKeyForSignature.substring(0, primaryKeyForSignature.indexOf('.'));
371 JsonNode triggerSig = object.get(primarykeyAlarm);
372 sigRules = new HashMap<>();
373 String parseKey = primaryKeyForSignature.substring(primaryKeyForSignature.indexOf('.') + 1);
374 StringBuilder sb = null;
375 if (triggerSig instanceof ArrayNode) {
376 for (int i = 0; i < triggerSig.size(); i++) {
377 sb = new StringBuilder();
378 parseData(triggerSig.get(i), parseKey);
380 List<?> keyList = new ArrayList<>(sigRules.keySet());
381 for (int j = keyList.size() - 1; j >= 0; j--) {
382 String key = (String) keyList.get(j);
383 String jsonNode = sigRules.get(key);
384 constructRule(sb, jsonNode, sigRules);
386 sb.append(")").toString();
387 putRuletoJson(tempJsonNode, i, sb, parseKey, primarykeyAlarm);
388 sigRules = new HashMap<>();
391 parseData(triggerSig, parseKey);
394 policyAdapter.setJsonBody(finalJson.toString());
395 return policyAdapter;
398 private JsonNode putRuletoJson(JsonNode tmpJsonNode, int item, StringBuilder sb, String parseKey,
400 JsonNode tmp = tmpJsonNode;
401 ObjectNode objectNode = (ObjectNode) tmp;
402 JsonNode jsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
403 JsonNode tempRuleJsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
404 String[] tempSt = parseKey.split("\\.");
405 for (String value : tempSt) {
406 if (value.contains("[")) {
407 if (tempRuleJsonNode instanceof ArrayNode) {
408 JsonNode tempRuleNode = tempRuleJsonNode.get(item);
409 ((ArrayNode) tempRuleJsonNode).removeAll();
410 ((ArrayNode) tempRuleJsonNode).add(tempRuleNode);
411 objectNode = (ObjectNode) tempRuleJsonNode.get(item);
413 String key = value.substring(0, value.indexOf('['));
414 objectNode.remove(key);
415 objectNode.put(key, sb.toString());
418 jsonNode = jsonNode.get(value);
419 if (jsonNode instanceof ArrayNode) {
420 tempRuleJsonNode = jsonNode;
421 jsonNode = jsonNode.get(item);
428 public boolean checkPattern(String patternString) {
429 return PATTERN.matcher(patternString).find();
436 * @param jsonNode the json node
437 * @param sigRules2 the sig rules 2
439 public void constructRule(StringBuilder sb, String jsonNode, Map<String, String> sigRules2) {
441 String cleanJsonNode = jsonNode.replace("\"\"", " ");
442 cleanJsonNode = cleanJsonNode.replaceAll("\"", "");
443 cleanJsonNode = cleanJsonNode.replaceAll("\\(", "");
444 cleanJsonNode = cleanJsonNode.replaceAll("\\)", "");
445 boolean flag = false;
446 if (cleanJsonNode.contains("OR")) {
450 for (String rowValue : cleanJsonNode.split(" ")) {
451 if (checkPattern(rowValue)) {
452 String value = sigRules2.get(rowValue);
453 LOGGER.info(" Value is:" + value);
454 constructRule(sb, value, sigRules2);
456 if ((count == 0) && (!("AND").equals(rowValue)) && (!("OR").equals(rowValue))) {
460 LOGGER.info(" " + rowValue + " ");
461 sb.append(" " + rowValue + " ");
462 if (count % 3 == 0) {
476 * @param jsonNode the json node
477 * @param string the string
479 public void parseData(JsonNode jsonNode, String string) {
480 if (string.contains(".")) {
481 String firstIndex = string.substring(0, string.indexOf('.'));
482 JsonNode signtures = jsonNode.get(firstIndex);
483 String subIndex = string.substring(firstIndex.length() + 1);
484 if (signtures instanceof ArrayNode) {
485 for (int i = 0; i < signtures.size(); i++) {
486 parseData(signtures.get(i), subIndex);
489 parseData(signtures, subIndex);
492 if (string.contains("[")) {
493 String ruleIndex = string.substring(0, string.indexOf('['));
494 String[] keys = string.substring(string.indexOf('[') + 1, string.lastIndexOf(']')).split(",");
495 String key = "A" + Integer.valueOf(sigRules.size() + 1);
496 JsonNode node = jsonNode.get(ruleIndex);
497 StringBuilder sb = new StringBuilder("(");
498 for (int i = 0; i < keys.length; i++) {
499 sb.append(node.get(keys[i].trim()));
502 sigRules.put(key, sb.toString());
508 * removeNullAttributes.
510 * @param cleanJson String
513 public String removeNullAttributes(String cleanJson) {
514 ObjectMapper mapper = new ObjectMapper();
517 JsonNode rootNode = mapper.readTree(cleanJson);
518 JsonNode returnNode = mapper.readTree(cleanJson);
519 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
520 boolean remove = false;
521 JsonObject removed = null;
522 boolean contentChanged = false;
523 while (fieldsIterator.hasNext()) {
524 Map.Entry<String, JsonNode> field = fieldsIterator.next();
525 final String key = field.getKey();
526 final JsonNode value = field.getValue();
527 if ("content".equalsIgnoreCase(key)) {
528 String contentStr = value.toString();
529 try (JsonReader reader = Json.createReader(new StringReader(contentStr))) {
530 JsonObject jsonContent = reader.readObject();
531 removed = removeNull(jsonContent);
532 if (!jsonContent.toString().equals(removed.toString())) {
533 contentChanged = true;
537 if (value == null || value.isNull()) {
538 ((ObjectNode) returnNode).remove(key);
543 cleanJson = returnNode.toString();
545 if (value == null || value.isNull()) {
546 ((ObjectNode) returnNode).remove(key);
551 cleanJson = returnNode.toString();
554 if (contentChanged) {
555 // set modified content to cleanJson
556 JSONObject jsonObject = new JSONObject(cleanJson);
557 jsonObject.put("content", removed.toString());
558 cleanJson = cleanUpJson(jsonObject.toString());
561 } catch (IOException e) {
562 LOGGER.error("Error writing out the JsonNode", e);
568 * To verify if it is a JSON string. If it is, then return its original value.
570 * @param key holds the values
573 private String getOriginalValue(String key) {
574 for (String k : jsonStringValues.keySet()) {
575 if (k.contains("@")) {
576 String[] arrOfKeys = k.split("@");
577 for (int i = 0; i < arrOfKeys.length; i++) {
578 if (arrOfKeys[i].contains(".")) {
579 arrOfKeys[i] = arrOfKeys[i].substring(arrOfKeys[i].indexOf(".") + 1);
580 if (arrOfKeys[i].equals(key)) {
581 return jsonStringValues.get(k);
586 if (k.endsWith(key)) {
587 return jsonStringValues.get(k);
597 * @param array JsonArray
600 public JsonArray removeNull(JsonArray array) {
601 JsonArrayBuilder builder = Json.createArrayBuilder();
603 for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++index) {
604 JsonValue value = it.next();
605 switch (value.getValueType()) {
607 JsonArray tempArray = removeNull(array.getJsonArray(index));
608 if (!tempArray.isEmpty()) {
609 builder.add(tempArray);
613 JsonObject object = removeNull(array.getJsonObject(index));
614 if (!object.isEmpty()) {
619 String str = array.getString(index);
620 if (str != null && !str.isEmpty()) {
625 builder.add(array.getJsonNumber(index));
629 builder.add(array.getBoolean(index));
636 return builder.build();
642 * @param obj JsonObject
645 public JsonObject removeNull(JsonObject obj) {
646 JsonObjectBuilder builder = Json.createObjectBuilder();
647 for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
648 Entry<String, JsonValue> entry = it.next();
649 String key = entry.getKey();
650 JsonValue value = entry.getValue();
651 switch (value.getValueType()) {
653 JsonArray array = removeNull(obj.getJsonArray(key));
654 if (!array.isEmpty()) {
655 builder.add(key, array);
659 JsonObject object = removeNull(obj.getJsonObject(key));
660 if (!object.isEmpty()) {
661 builder.add(key, object);
665 String str = obj.getString(key);
666 if (str != null && !str.isEmpty()) {
667 builder.add(key, str);
671 builder.add(key, obj.getJsonNumber(key));
675 builder.add(key, obj.getBoolean(key));
682 return builder.build();
691 public String cleanUpJson(String json) {
692 String cleanJson = StringUtils.replaceEach(json, new String[] {"\\\\", "\\\\\\", "\\\\\\\\"},
693 new String[] {"\\", "\\", "\\"});
694 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\\"}, new String[] {"\\"});
696 StringUtils.replaceEach(cleanJson, new String[] {"\\\\", "[[", "]]"}, new String[] {"\\", "[", "]"});
698 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\"", "\\\"", "\"[{", "}]\""},
699 new String[] {"\"", "\"", "[{", "}]"});
700 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[{", "}]\""}, new String[] {"[{", "}]"});
701 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[", "]\""}, new String[] {"[", "]"});
702 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"{", "}\""}, new String[] {"{", "}"});
703 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\"\"", "\"\""}, new String[] {"\"", "\""});
704 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\""}, new String[] {""});
705 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\""}, new String[] {"\""});
706 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"\\\\\\"}, new String[] {"\""});
707 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\\\""}, new String[] {"\""});
708 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[", "]\""}, new String[] {"[", "]"});
715 * @param jsonNode JsonNode
718 public JSONObject decodeContent(JsonNode jsonNode) {
719 Iterator<JsonNode> jsonElements = jsonNode.elements();
720 Iterator<String> jsonKeys = jsonNode.fieldNames();
721 Map<String, String> element = new TreeMap<>();
722 while (jsonElements.hasNext() && jsonKeys.hasNext()) {
723 element.put(jsonKeys.next(), jsonElements.next().toString());
725 JSONObject jsonResult = new JSONObject();
726 JSONArray jsonArray = null;
727 String oldValue = null;
728 String nodeKey = null;
729 String arryKey = null;
730 Boolean isArray = false;
731 JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
732 ObjectNode node = nodeFactory.objectNode();
733 String prevKey = null;
735 for (Entry<String, String> entry : element.entrySet()) {
736 String key = entry.getKey();
737 String value = entry.getValue();
738 if (key.contains(".")) {
739 presKey = key.substring(0, key.indexOf('.'));
740 } else if (key.contains("@")) {
741 presKey = key.substring(0, key.indexOf('@'));
745 // first check if we are different from old.
746 LOGGER.info(key + "\n");
747 if (jsonArray != null && jsonArray.length() > 0 && key.contains("@") && !key.contains(".")
748 && oldValue != null) {
749 if (!oldValue.equals(key.substring(0, key.indexOf('@')))) {
750 jsonResult.put(oldValue, jsonArray);
751 jsonArray = new JSONArray();
753 } else if (jsonArray != null && jsonArray.length() > 0 && !presKey.equals(prevKey) && oldValue != null) {
754 jsonResult.put(oldValue, jsonArray);
756 jsonArray = new JSONArray();
761 if (key.contains(".")) {
762 if (nodeKey == null) {
763 nodeKey = key.substring(0, key.indexOf('.'));
765 if (nodeKey.equals(key.substring(0, key.indexOf('.')))) {
766 node.put(key.substring(key.indexOf('.') + 1), value);
768 if (node.size() != 0) {
769 if (nodeKey.contains("@")) {
770 if (arryKey == null) {
771 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
773 if (nodeKey.endsWith("@0")) {
775 jsonArray = new JSONArray();
777 if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
778 jsonArray.put(decodeContent(node));
780 if ((key.contains("@") && !arryKey.equals(key.substring(0, nodeKey.indexOf('@'))))
781 || !key.contains("@")) {
782 jsonResult.put(arryKey, jsonArray);
783 jsonArray = new JSONArray();
785 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
788 jsonResult.put(nodeKey, decodeContent(node));
790 node = nodeFactory.objectNode();
792 nodeKey = key.substring(0, key.indexOf('.'));
793 if (nodeKey.contains("@")) {
794 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
796 node.put(key.substring(key.indexOf('.') + 1), value);
799 if (node.size() != 0) {
800 if (nodeKey.contains("@")) {
801 if (arryKey == null) {
802 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
804 if (nodeKey.endsWith("@0")) {
806 jsonArray = new JSONArray();
808 if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
809 jsonArray.put(decodeContent(node));
811 jsonResult.put(arryKey, jsonArray);
812 jsonArray = new JSONArray();
813 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
816 jsonResult.put(nodeKey, decodeContent(node));
818 node = nodeFactory.objectNode();
820 if (key.contains("@")) {
822 if (key.endsWith("@0") || jsonArray == null) {
823 jsonArray = new JSONArray();
825 } else if (!key.contains("@")) {
829 if (oldValue == null) {
830 oldValue = key.substring(0, key.indexOf('@'));
832 if (oldValue != prevKey) {
833 oldValue = key.substring(0, key.indexOf('@'));
835 if (oldValue.equals(key.substring(0, key.indexOf('@')))) {
836 jsonArray.put(value);
838 jsonResult.put(oldValue, jsonArray);
839 jsonArray = new JSONArray();
841 oldValue = key.substring(0, key.indexOf('@'));
843 jsonResult.put(key, value);
847 if (node.size() > 0) {
848 if (nodeKey.contains("@")) {
849 if (jsonArray == null) {
850 jsonArray = new JSONArray();
852 if (arryKey == null) {
853 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
855 jsonArray.put(decodeContent(node));
856 jsonResult.put(arryKey, jsonArray);
859 jsonResult.put(nodeKey, decodeContent(node));
862 if (isArray && jsonArray.length() > 0) {
863 jsonResult.put(oldValue, jsonArray);
869 value = {"/policyController/getDCAEMSTemplateData.htm"},
870 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
871 public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response)
873 // TreeSet is used to ensure that individual items appear before their containing collection.
874 allManyTrueKeys = new TreeSet<>();
875 ObjectMapper mapper = new ObjectMapper();
876 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
877 JsonNode root = mapper.readTree(request.getReader());
879 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
880 String servicename = value.split("-v")[0];
881 String version = null;
882 if (value.contains("-v")) {
883 version = value.split("-v")[1];
885 MicroServiceModels returnModel = getAttributeObject(servicename, version);
887 MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value);
888 JSONObject jsonHdDefaultObj = null;
889 if (returnHeaderDefauls != null) {
890 jsonHdDefaultObj = new JSONObject();
891 jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName());
892 jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard());
893 jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel());
894 jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType());
895 jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority());
897 String headDefautlsData = "";
898 if (jsonHdDefaultObj != null) {
899 headDefautlsData = jsonHdDefaultObj.toString();
901 headDefautlsData = "null";
904 // Get all keys with "MANY-true" defined in their value from subAttribute
905 Set<String> allkeys = null;
906 if (returnModel.getSubAttributes() != null && !returnModel.getSubAttributes().isEmpty()) {
907 JSONObject json = new JSONObject(returnModel.getSubAttributes());
909 allkeys = allManyTrueKeys;
910 allManyTrueKeys = new TreeSet<>();
911 LOGGER.info("allkeys : " + allkeys);
914 // Get element order info
915 String dataOrderInfo = returnModel.getDataOrderInfo();
916 if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
917 dataOrderInfo = "\"" + dataOrderInfo + "\"";
919 LOGGER.info("dataOrderInfo : " + dataOrderInfo);
921 String allMnyTrueKeys = "";
922 if (allkeys != null) {
923 allMnyTrueKeys = allkeys.toString();
926 String jsonModel = createMicroSeriveJson(returnModel);
928 JSONObject jsonObject = new JSONObject(jsonModel);
930 JSONObject finalJsonObject = null;
931 if (allkeys != null) {
932 Iterator<String> iter = allkeys.iterator();
933 while (iter.hasNext()) {
934 // Convert to array values for MANY-true keys
935 finalJsonObject = convertToArrayElement(jsonObject, iter.next());
936 jsonObject = finalJsonObject;
940 if (finalJsonObject != null) {
941 LOGGER.info(finalJsonObject.toString());
942 jsonModel = finalJsonObject.toString();
945 // get all properties with "MANY-true" defined in RefAttributes
946 Set<String> manyTrueProperties = getManyTrueProperties(returnModel.getRefAttributes());
947 if (manyTrueProperties != null) {
948 JSONObject jsonObj = new JSONObject(jsonModel);
949 for (String s : manyTrueProperties) {
951 // convert to array element for MANY-true properties
952 finalJsonObject = convertToArrayElement(jsonObj, s.trim());
953 jsonObj = finalJsonObject;
956 if (finalJsonObject != null) {
957 LOGGER.info(finalJsonObject.toString());
958 jsonModel = finalJsonObject.toString();
962 response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
963 response.setContentType(PolicyUtils.APPLICATION_JSON);
964 request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
965 List<Object> list = new ArrayList<>();
966 String responseString = mapper.writeValueAsString(returnModel);
968 JSONObject json = null;
970 if ("".equals(allMnyTrueKeys)) {
971 json = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",dataOrderInfo:"
972 + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}");
974 json = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel
975 + ",allManyTrueKeys: " + allMnyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:"
976 + headDefautlsData + "}");
979 response.getWriter().write(list.toString());
983 @SuppressWarnings({"unchecked", "rawtypes"})
984 private String createMicroSeriveJson(MicroServiceModels returnModel) {
985 Map<String, String> attributeMap = new HashMap<>();
986 Map<String, String> refAttributeMap = new HashMap<>();
987 String attribute = returnModel.getAttributes();
988 if (attribute != null) {
989 attribute = attribute.trim();
991 String refAttribute = returnModel.getRefAttributes();
992 if (refAttribute != null) {
993 refAttribute = refAttribute.trim();
995 String enumAttribute = returnModel.getEnumValues();
996 if (enumAttribute != null) {
997 enumAttribute = enumAttribute.trim();
999 if (!StringUtils.isEmpty(attribute)) {
1000 attributeMap = convert(attribute, ",");
1002 if (!StringUtils.isEmpty(refAttribute)) {
1003 refAttributeMap = convert(refAttribute, ",");
1006 Gson gson = new Gson();
1008 String subAttributes = returnModel.getSubAttributes();
1009 if (subAttributes != null) {
1010 subAttributes = subAttributes.trim();
1015 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
1017 JSONObject object = new JSONObject();
1019 for (Entry<String, String> keySet : attributeMap.entrySet()) {
1020 JSONArray array = new JSONArray();
1021 String value = keySet.getValue();
1022 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
1024 object.put(keySet.getKey().trim(), array);
1026 object.put(keySet.getKey().trim(), value.trim());
1030 for (Entry<String, String> keySet : refAttributeMap.entrySet()) {
1031 JSONArray array = new JSONArray();
1032 String value = keySet.getValue().split(":")[0];
1033 if (gsonObject.containsKey(value)) {
1034 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
1035 array.put(recursiveReference(value, gsonObject, enumAttribute));
1036 object.put(keySet.getKey().trim(), array);
1038 object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
1041 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
1042 array.put(value.trim());
1043 object.put(keySet.getKey().trim(), array);
1045 object.put(keySet.getKey().trim(), value.trim());
1050 return object.toString();
1053 @SuppressWarnings("unchecked")
1054 private JSONObject recursiveReference(String name, Map<String, String> subAttributeMap, String enumAttribute) {
1055 JSONObject object = new JSONObject();
1056 Map<String, String> map;
1057 Object returnClass = subAttributeMap.get(name);
1058 map = (Map<String, String>) returnClass;
1061 for (Entry<String, String> m : map.entrySet()) {
1062 String[] splitValue = m.getValue().split(":");
1063 array = new JSONArray();
1064 if (subAttributeMap.containsKey(splitValue[0])) {
1065 if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])) {
1066 array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
1067 object.put(m.getKey().trim(), array);
1069 object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
1072 if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])) {
1073 array.put(splitValue[0].trim());
1074 object.put(m.getKey().trim(), array);
1076 object.put(m.getKey().trim(), splitValue[0].trim());
1084 public JSONObject convertToArrayElement(JSONObject json, String keyValue) {
1085 return convertToArrayElement(json, new HashSet<>(), keyValue);
1088 private JSONObject convertToArrayElement(JSONObject json, Set<String> keys, String keyValue) {
1089 for (String key : json.keySet()) {
1090 Object obj = json.get(key);
1091 if (key.equals(keyValue.trim())) {
1092 if (!(obj instanceof JSONArray)) {
1093 JSONArray newJsonArray = new JSONArray();
1094 newJsonArray.put(obj);
1095 json.put(key, newJsonArray);
1097 LOGGER.info("key : " + key);
1098 LOGGER.info("obj : " + obj);
1099 LOGGER.info("json.get(key) : " + json.get(key));
1100 LOGGER.info("keyValue : " + keyValue);
1101 keys.addAll(json.keySet());
1106 if (obj instanceof JSONObject) {
1107 convertToArrayElement(json.getJSONObject(key), keyValue);
1110 if (obj instanceof JSONArray) {
1112 if (json.getJSONArray(key).length() > 0) {
1113 convertToArrayElement(json.getJSONArray(key).getJSONObject(0), keyValue);
1115 } catch (Exception ex) {
1116 LOGGER.info("XMI Model load issue : " + ex);
1125 * getManyTrueProperties.
1127 * @param referAttributes String
1128 * @return a Set of String
1130 public Set<String> getManyTrueProperties(String referAttributes) {
1131 LOGGER.info("referAttributes : " + referAttributes);
1132 Set<String> manyTrueProperties = new HashSet<>();
1134 if (referAttributes != null) {
1135 String[] referAarray = referAttributes.split(",");
1136 String[] element = null;
1137 for (int i = 0; i < referAarray.length; i++) {
1138 element = referAarray[i].split("=");
1139 if (element.length > 1 && element[1].contains("MANY-true")) {
1140 manyTrueProperties.add(element[0]);
1145 return manyTrueProperties;
1148 // call this method to start the recursive
1149 private Set<String> getAllKeys(JSONObject json) {
1150 return getAllKeys(json, new HashSet<>());
1153 private Set<String> getAllKeys(JSONArray arr) {
1154 return getAllKeys(arr, new HashSet<>());
1157 private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
1158 for (int i = 0; i < arr.length(); i++) {
1159 Object obj = arr.get(i);
1160 if (obj instanceof JSONObject) {
1161 keys.addAll(getAllKeys(arr.getJSONObject(i)));
1163 if (obj instanceof JSONArray) {
1164 keys.addAll(getAllKeys(arr.getJSONArray(i)));
1171 // this method returns a set of keys with "MANY-true" defined in their value.
1172 private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
1173 for (String key : json.keySet()) {
1174 Object obj = json.get(key);
1175 if (obj instanceof String && ((String) obj).contains("MANY-true")) {
1176 LOGGER.info("key : " + key);
1177 LOGGER.info("obj : " + obj);
1178 allManyTrueKeys.add(key);
1180 if (obj instanceof JSONObject) {
1181 keys.addAll(getAllKeys(json.getJSONObject(key)));
1183 if (obj instanceof JSONArray) {
1184 keys.addAll(getAllKeys(json.getJSONArray(key)));
1192 * getModelServiceVersionData.
1194 * @param request HttpServletRequest
1195 * @param response HttpServletResponse
1196 * @return ModelAndView
1197 * @throws IOException IOException
1200 value = {"/policyController/getModelServiceVersioneData.htm"},
1201 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
1202 public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response)
1203 throws IOException {
1204 ObjectMapper mapper = new ObjectMapper();
1205 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
1207 response.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
1208 response.setContentType(PolicyUtils.APPLICATION_JSON);
1209 request.setCharacterEncoding(PolicyUtils.CHARACTER_ENCODING);
1210 List<Object> list = new ArrayList<>();
1211 String value = mapper.readTree(request.getReader()).get("policyData").toString().replaceAll("^\"|\"$", "");
1212 String servicename = value.split("-v")[0];
1213 Set<String> returnList = getVersionList(servicename);
1214 list.add(new JSONObject("{dcaeModelVersionData: " + mapper.writeValueAsString(returnList) + "}"));
1215 response.getWriter().write(list.toString());
1219 private Set<String> getVersionList(String name) {
1220 MicroServiceModels workingModel;
1221 Set<String> list = new HashSet<>();
1222 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1223 for (int i = 0; i < microServiceModelsData.size(); i++) {
1224 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1225 if (workingModel.getVersion() != null) {
1226 list.add(workingModel.getVersion());
1228 list.add("Default");
1234 private MicroServiceModels getAttributeObject(String name, String version) {
1235 MicroServiceModels workingModel = new MicroServiceModels();
1236 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1237 for (int i = 0; i < microServiceModelsData.size(); i++) {
1238 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1239 if (version != null) {
1240 if (workingModel.getVersion() != null) {
1241 if (workingModel.getVersion().equals(version)) {
1242 return workingModel;
1245 return workingModel;
1248 return workingModel;
1252 return workingModel;
1255 private MicroserviceHeaderdeFaults getHeaderDefaultsObject(String modelName) {
1256 return (MicroserviceHeaderdeFaults) commonClassDao.getEntityItem(MicroserviceHeaderdeFaults.class, "modelName",
1261 value = {"/get_DCAEPriorityValues"},
1262 method = {org.springframework.web.bind.annotation.RequestMethod.GET},
1263 produces = MediaType.APPLICATION_JSON_VALUE)
1264 public void getDCAEPriorityValuesData(HttpServletRequest request, HttpServletResponse response) {
1266 Map<String, Object> model = new HashMap<>();
1267 ObjectMapper mapper = new ObjectMapper();
1268 List<String> priorityList = new ArrayList<>();
1270 for (int i = 1; i < priorityCount; i++) {
1271 priorityList.add(String.valueOf(i));
1273 model.put("priorityDatas", mapper.writeValueAsString(priorityList));
1274 response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
1275 } catch (Exception e) {
1281 * prePopulateDCAEMSPolicyData.
1283 * @param policyAdapter PolicyRestAdapter
1284 * @param entity PolicyEntity
1286 public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1287 if (! (policyAdapter.getPolicyData() instanceof PolicyType)) {
1290 Object policyData = policyAdapter.getPolicyData();
1291 PolicyType policy = (PolicyType) policyData;
1292 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
1293 String policyNameValue =
1294 policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("MS_") + 3);
1295 policyAdapter.setPolicyName(policyNameValue);
1296 String description = "";
1298 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
1299 } catch (Exception e) {
1300 LOGGER.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue, e);
1301 description = policy.getDescription();
1303 policyAdapter.setPolicyDescription(description);
1304 // Get the target data under policy.
1305 TargetType target = policy.getTarget();
1306 if (target == null) {
1309 for (AnyOfType anyOf : target.getAnyOf()) {
1310 for (AllOfType allOf : anyOf.getAllOf()) {
1311 // Under AllOFType we have Match
1312 List<MatchType> matchList = allOf.getMatch();
1313 if (matchList == null) {
1316 Iterator<MatchType> iterMatch = matchList.iterator();
1318 // Can someone please explain why the matchList MUST have
1319 // more than 1 matches???
1321 while (matchList.size() > 1 && iterMatch.hasNext()) {
1322 MatchType match = iterMatch.next();
1324 // Under the match we have attribute value and
1325 // attributeDesignator. So,finally down to the actual attribute.
1327 // First match in the target is OnapName, so set that value.
1328 policyAdapter.setupUsingAttribute(match.getAttributeDesignator().getAttributeId(),
1329 (String) match.getAttributeValue().getContent().get(0));
1331 readFile(policyAdapter, entity);
1339 * @param str the str
1340 * @param split the split
1343 public Map<String, String> convert(String str, String split) {
1344 Map<String, String> map = new HashMap<>();
1345 for (final String entry : str.split(split)) {
1346 String[] parts = entry.split("=");
1347 map.put(parts[0], parts[1]);
1355 * @param policyAdapter the policy adapter
1356 * @param entity the entity
1358 @SuppressWarnings("unchecked")
1359 public void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1361 DCAEMicroServiceObject msBody =
1362 new ObjectMapper().readValue(entity.getConfigurationData().getConfigBody(),
1363 DCAEMicroServiceObject.class);
1364 String policyScopeName = getPolicyScope(msBody.getPolicyScope());
1365 policyAdapter.setPolicyScope(policyScopeName);
1367 policyAdapter.setPriority(msBody.getPriority());
1369 if (msBody.getVersion() != null) {
1370 policyAdapter.setServiceType(msBody.getService());
1371 policyAdapter.setVersion(msBody.getVersion());
1373 policyAdapter.setServiceType(msBody.getService());
1376 LinkedHashMap<String, ?> content = (LinkedHashMap<String, ?>) msBody.getUiContent();
1377 if (content == null) {
1378 content = (LinkedHashMap<String, ?>) msBody.getContent();
1380 if (content != null) {
1381 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
1382 LinkedHashMap<String, ?> map = content;
1383 readRecursivlyJSONContent(map, data);
1384 policyAdapter.setRuleData(data);
1387 } catch (Exception e) {
1394 * readRecursivlyJSONContent.
1396 * @param map Map of String to something
1397 * @param data Map of String to Object
1399 @SuppressWarnings({"rawtypes", "unchecked"})
1400 public void readRecursivlyJSONContent(Map<String, ?> map, Map<String, Object> data) {
1401 for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
1402 Object key = iterator.next();
1403 Object value = map.get(key);
1404 if (value instanceof LinkedHashMap<?, ?>) {
1405 LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<>();
1406 readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
1407 for (Entry<String, Object> entry : secondObjec.entrySet()) {
1408 data.put(key + "." + entry.getKey(), entry.getValue());
1410 } else if (value instanceof ArrayList) {
1411 ArrayList<?> jsonArrayVal = (ArrayList<?>) value;
1412 for (int i = 0; i < jsonArrayVal.size(); i++) {
1413 Object arrayvalue = jsonArrayVal.get(i);
1414 if (arrayvalue instanceof LinkedHashMap<?, ?>) {
1415 LinkedHashMap<String, Object> newData = new LinkedHashMap<>();
1416 readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
1417 for (Entry<String, Object> entry : newData.entrySet()) {
1418 data.put(key + "@" + i + "." + entry.getKey(), entry.getValue());
1420 } else if (arrayvalue instanceof ArrayList) {
1421 ArrayList<?> jsonArrayVal1 = (ArrayList<?>) value;
1422 for (int j = 0; j < jsonArrayVal1.size(); j++) {
1423 Object arrayvalue1 = jsonArrayVal1.get(i);
1424 data.put(key + "@" + j, arrayvalue1.toString());
1427 data.put(key + "@" + i, arrayvalue.toString());
1431 data.put(key.toString(), value.toString());
1439 * @param value String
1442 public String getPolicyScope(String value) {
1443 List<Object> groupList = commonClassDao.getDataById(GroupPolicyScopeList.class, "groupList", value);
1444 if (groupList != null && !groupList.isEmpty()) {
1445 return ((GroupPolicyScopeList) groupList.get(0)).getGroupName();
1451 * Convert the map values and set into JSON body.
1453 * @param attributesMap Map of attributes
1454 * @param attributesRefMap Map of attribute references
1457 public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
1458 Map<String, String> attribute = new HashMap<>();
1462 for (Entry<String, String> entry : attributesMap.entrySet()) {
1463 key = entry.getKey();
1464 value = entry.getValue();
1465 attribute.put(key, value);
1467 for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
1468 key = entryRef.getKey();
1469 value = entryRef.getValue();
1470 attribute.put(key, value);
1472 for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
1473 key = entryList.getKey();
1474 value = entryList.getValue();
1475 attribute.put(key, value);
1477 for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()) {
1478 key = arrayList.getKey();
1480 for (Object textList : arrayList.getValue()) {
1482 temp = new StringBuilder();
1483 temp.append("[" + textList);
1485 temp.append("," + textList);
1488 attribute.put(key, temp + "]");
1495 value = {"/ms_dictionary/set_MSModelData"},
1496 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
1497 public void SetMSModelData(HttpServletRequest request, HttpServletResponse response)
1498 throws IOException, FileUploadException {
1499 modelList = new ArrayList<>();
1500 dirDependencyList = new ArrayList<>();
1501 classMap = new LinkedHashMap<>();
1502 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
1503 boolean zip = false;
1504 boolean yml = false;
1505 String errorMsg = "";
1506 for (FileItem item : items) {
1507 if (item.getName().endsWith(".zip") || item.getName().endsWith(".xmi") || item.getName().endsWith(".yml")) {
1508 this.newModel = new MicroServiceModels();
1510 File file = new File(item.getName());
1511 OutputStream outputStream = new FileOutputStream(file);
1512 IOUtils.copy(item.getInputStream(), outputStream);
1513 outputStream.close();
1514 this.newFile = file.toString();
1515 this.newModel.setModelName(this.newFile.split("-v")[0]);
1517 if (this.newFile.contains("-v")) {
1518 if (item.getName().endsWith(".zip")) {
1519 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
1521 } else if (item.getName().endsWith(".yml")) {
1522 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
1525 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
1528 } catch (Exception e) {
1529 LOGGER.error("Upload error : ", e);
1530 errorMsg = "Upload error:" + e.getMessage();
1536 if (!errorMsg.isEmpty()) {
1537 response.setCharacterEncoding("UTF-8");
1538 response.setContentType("application / json");
1539 request.setCharacterEncoding("UTF-8");
1541 JSONObject json = new JSONObject();
1542 json.put("errorMsg", errorMsg);
1543 response.getWriter().write(json.toString());
1547 List<File> fileList = new ArrayList<>();
1548 MsModelUtils msModelUtils = new MsModelUtils(commonClassDao);
1549 this.directory = "model";
1551 extractFolder(this.newFile);
1552 fileList = listModelFiles(this.directory);
1554 errorMsg = msModelUtils.parseTosca(this.newFile);
1555 if (errorMsg != null) {
1556 response.setCharacterEncoding("UTF-8");
1557 response.setContentType("application / json");
1558 request.setCharacterEncoding("UTF-8");
1559 JSONObject json = new JSONObject();
1560 json.put("errorMsg", errorMsg);
1561 response.getWriter().write(json.toString());
1566 File file = new File(this.newFile);
1569 String modelType = "";
1572 // Process Main Model file first
1573 classMap = new LinkedHashMap<>();
1574 for (File file : fileList) {
1575 if (!file.isDirectory() && file.getName().endsWith(".xmi")) {
1576 retrieveDependency(file.toString());
1580 modelList = createList();
1582 cleanUp(this.newFile);
1586 modelList.add(this.newModel.getModelName());
1587 String className = this.newModel.getModelName();
1588 MsAttributeObject msAttributes = new MsAttributeObject();
1589 msAttributes.setClassName(className);
1591 LinkedHashMap<String, String> returnAttributeList = new LinkedHashMap<>();
1592 returnAttributeList.put(className, msModelUtils.getAttributeString());
1593 msAttributes.setAttribute(returnAttributeList);
1595 msAttributes.setSubClass(msModelUtils.getRetmap());
1597 msAttributes.setMatchingSet(msModelUtils.getMatchableValues());
1599 LinkedHashMap<String, String> returnReferenceList = new LinkedHashMap<>();
1601 returnReferenceList.put(className, msModelUtils.getReferenceAttributes());
1602 msAttributes.setRefAttribute(returnReferenceList);
1604 if (msModelUtils.getListConstraints() != "") {
1605 LinkedHashMap<String, String> enumList = new LinkedHashMap<>();
1606 String[] listArray = msModelUtils.getListConstraints().split("#");
1607 for (String str : listArray) {
1608 String[] strArr = str.split("=");
1609 if (strArr.length > 1) {
1610 enumList.put(strArr[0], strArr[1]);
1613 msAttributes.setEnumType(enumList);
1616 classMap = new LinkedHashMap<>();
1617 classMap.put(className, msAttributes);
1620 response.setCharacterEncoding("UTF-8");
1621 response.setContentType("application / json");
1622 request.setCharacterEncoding("UTF-8");
1624 ObjectMapper mapper = new ObjectMapper();
1625 JSONObject json = new JSONObject();
1626 json.put("classListDatas", modelList);
1627 json.put("modelDatas", mapper.writeValueAsString(classMap));
1628 json.put("modelType", modelType);
1629 json.put("dataOrderInfo", msModelUtils.getDataOrderInfo());
1630 json.put("ruleFormation", msModelUtils.getJsonRuleFormation());
1632 response.getWriter().write(json.toString());
1636 * Unzip file and store in the model directory for processing
1638 @SuppressWarnings("rawtypes")
1639 private void extractFolder(String zipFile) {
1640 final int buffer = 2048;
1641 File file = new File(zipFile);
1643 try (ZipFile zip = new ZipFile(file)) {
1644 String newPath = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1645 this.directory = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1646 checkZipDirectory(this.directory);
1647 new File(newPath).mkdir();
1648 Enumeration zipFileEntries = zip.entries();
1650 // Process each entry
1651 while (zipFileEntries.hasMoreElements()) {
1652 // grab a zip file entry
1653 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
1654 String currentEntry = entry.getName();
1655 File destFile = new File("model" + File.separator + currentEntry);
1656 File destinationParent = destFile.getParentFile();
1658 destinationParent.mkdirs();
1660 if (!entry.isDirectory()) {
1661 BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
1663 byte[] data = new byte[buffer];
1664 try (FileOutputStream fos = new FileOutputStream(destFile);
1665 BufferedOutputStream dest = new BufferedOutputStream(fos, buffer)) {
1666 while ((currentByte = is.read(data, 0, buffer)) != -1) {
1667 dest.write(data, 0, currentByte);
1670 } catch (IOException e) {
1671 LOGGER.error("Failed to write zip contents to {}" + destFile + e);
1673 // PLD should I throw e?
1679 if (currentEntry.endsWith(".zip")) {
1680 extractFolder(destFile.getAbsolutePath());
1683 } catch (IOException e) {
1684 LOGGER.error("Failed to unzip model file " + zipFile, e);
1688 private void retrieveDependency(String workingFile) {
1690 MsModelUtils utils = new MsModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
1691 Map<String, MsAttributeObject> tempMap;
1693 tempMap = utils.processEpackage(workingFile, ModelType.XMI);
1695 classMap.putAll(tempMap);
1696 LOGGER.info(tempMap);
1699 private List<File> listModelFiles(String directoryName) {
1700 File fileDirectory = new File(directoryName);
1701 List<File> resultList = new ArrayList<>();
1702 for (File file : fileDirectory.listFiles()) {
1703 if (file.isFile()) {
1704 resultList.add(file);
1705 } else if (file.isDirectory()) {
1706 dirDependencyList.add(file.getName());
1707 resultList.addAll(listModelFiles(file.getAbsolutePath()));
1716 * @param path String
1718 public void cleanUp(String path) {
1721 FileUtils.forceDelete(new File(path));
1722 } catch (IOException e) {
1723 LOGGER.error("Failed to delete folder " + path, e);
1729 * checkZipDirectory.
1731 * @param zipDirectory String
1733 public void checkZipDirectory(String zipDirectory) {
1734 Path path = Paths.get(zipDirectory);
1736 if (Files.exists(path)) {
1737 cleanUp(zipDirectory);
1741 private List<String> createList() {
1742 List<String> list = new ArrayList<>();
1743 for (Entry<String, MsAttributeObject> entrySet : classMap.entrySet()) {
1744 if (entrySet.getValue().isPolicyTempalate()) {
1745 list.add(entrySet.getKey());
1750 if (list.isEmpty()) {
1751 if (classMap.containsKey(this.newModel.getModelName())) {
1752 list.add(this.newModel.getModelName());
1760 public Map<String, String> getAttributesListRefMap() {
1761 return attributesListRefMap;
1764 public Map<String, LinkedList<String>> getArrayTextList() {
1765 return arrayTextList;
1768 public Map<String, String> getSigRules() {
1772 public void setSigRules(Map<String, String> sigRules) {
1773 this.sigRules = sigRules;
1780 class DCAEMicroServiceObject {
1782 private String service;
1783 private String location;
1784 private String uuid;
1785 private String policyName;
1786 private String description;
1787 private String configName;
1788 private String templateVersion;
1789 private String version;
1790 private String priority;
1791 private String policyScope;
1792 private String riskType;
1793 private String riskLevel;
1794 private String guard = null;
1795 private Object uiContent;
1796 private Object content;