2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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;
34 import java.io.BufferedInputStream;
35 import java.io.BufferedOutputStream;
37 import java.io.FileOutputStream;
38 import java.io.IOException;
39 import java.io.OutputStream;
40 import java.io.PrintWriter;
41 import java.io.StringReader;
42 import java.nio.file.Files;
43 import java.nio.file.Path;
44 import java.nio.file.Paths;
45 import java.util.ArrayList;
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;
63 import javax.json.Json;
64 import javax.json.JsonArray;
65 import javax.json.JsonArrayBuilder;
66 import javax.json.JsonObject;
67 import javax.json.JsonObjectBuilder;
68 import javax.json.JsonReader;
69 import javax.json.JsonValue;
70 import javax.servlet.http.HttpServletRequest;
71 import javax.servlet.http.HttpServletResponse;
74 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
75 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
76 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
77 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
78 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
79 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
80 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
82 import org.apache.commons.compress.utils.IOUtils;
83 import org.apache.commons.fileupload.FileItem;
84 import org.apache.commons.fileupload.FileUploadException;
85 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
86 import org.apache.commons.fileupload.servlet.ServletFileUpload;
87 import org.apache.commons.io.FileUtils;
88 import org.apache.commons.lang.StringUtils;
89 import org.json.JSONArray;
90 import org.json.JSONObject;
91 import org.onap.policy.common.logging.flexlogger.FlexLogger;
92 import org.onap.policy.common.logging.flexlogger.Logger;
93 import org.onap.policy.rest.XACMLRestProperties;
94 import org.onap.policy.rest.adapter.PolicyRestAdapter;
95 import org.onap.policy.rest.dao.CommonClassDao;
96 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
97 import org.onap.policy.rest.jpa.MicroServiceModels;
98 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
99 import org.onap.policy.rest.jpa.PolicyEntity;
100 import org.onap.policy.rest.util.MSAttributeObject;
101 import org.onap.policy.rest.util.MSModelUtils;
102 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
103 import org.onap.portalsdk.core.controller.RestrictedBaseController;
104 import org.onap.portalsdk.core.web.support.JsonMessage;
105 import org.springframework.beans.factory.annotation.Autowired;
106 import org.springframework.http.MediaType;
107 import org.springframework.stereotype.Controller;
108 import org.springframework.web.bind.annotation.RequestMapping;
109 import org.springframework.web.servlet.ModelAndView;
113 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
114 private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
116 private static CommonClassDao commonClassDao;
118 public static CommonClassDao getCommonClassDao() {
119 return commonClassDao;
122 public static void setCommonClassDao(CommonClassDao commonClassDao) {
123 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
126 private MicroServiceModels newModel;
127 private String newFile;
128 private String directory;
129 private List<String> modelList = new ArrayList<>();
130 private List<String> dirDependencyList = new ArrayList<>();
131 private LinkedHashMap<String, MSAttributeObject> classMap = new LinkedHashMap<>();
132 String referenceAttributes;
133 String attributeString;
134 Set<String> allManyTrueKeys = null;
135 private Map<String, String> sigRules = null;
137 public static final String DATATYPE = "data_types.policy.data.";
138 public static final String PROPERTIES = ".properties.";
139 public static final String TYPE = ".type";
140 public static final String STRING = "string";
141 public static final String INTEGER = "integer";
142 public static final String LIST = "list";
143 public static final String DEFAULT = ".default";
144 public static final String REQUIRED = ".required";
145 public static final String MATCHABLE = ".matchable";
146 public static final String MANYFALSE = ":MANY-false";
147 private static final Pattern PATTERN = Pattern.compile("[A][0-9]");
148 private static final String POLICYJSON = "policyJSON";
151 private CreateDcaeMicroServiceController(CommonClassDao commonClassDao) {
152 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
155 public CreateDcaeMicroServiceController() {
159 protected PolicyRestAdapter policyAdapter = null;
160 private int priorityCount;
161 private Map<String, String> attributesListRefMap = new HashMap<>();
162 private Map<String, LinkedList<String>> arrayTextList = new HashMap<>();
163 private Map<String, String> jsonStringValues = new HashMap<>();
166 * setDataToPolicyRestAdapter.
168 * @param policyData PolicyRestAdapter
169 * @param root JsonNode
170 * @return PolicyRestAdapter
172 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
174 String jsonContent = null;
176 LOGGER.info("policyJSON :" + (root.get(POLICYJSON)).toString());
178 String tempJson = root.get(POLICYJSON).toString();
179 JSONObject policyJson = new JSONObject(root.get(POLICYJSON).toString());
180 if (policyJson != null) {
181 tempJson = saveOriginalJsonObject(policyJson, jsonStringValues).toString();
183 // ---replace empty value with the value below before calling decodeContent method.
184 String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
185 LOGGER.info("dummyValue:" + dummyValue);
187 StringUtils.replaceEach(tempJson, new String[] {"\"\""}, new String[] {"\"" + dummyValue + "\""});
188 ObjectMapper mapper = new ObjectMapper();
189 JsonNode tempJsonNode = mapper.readTree(tempJson);
190 jsonContent = decodeContent(tempJsonNode).toString();
191 constructJson(policyData, jsonContent, dummyValue);
192 } catch (Exception e) {
193 LOGGER.error("Error while decoding microservice content", e);
196 // ----Here is the final step to reset the original value back.
197 if (policyData.getJsonBody() != null && jsonStringValues.size() > 0) {
198 String contentBody = policyData.getJsonBody();
199 JSONObject contentJson = new JSONObject(contentBody);
200 JSONObject content = contentJson.getJSONObject("content");
201 content = setOriginalJsonObject(content, jsonStringValues);
202 contentJson.put("content", content);
203 policyData.setJsonBody(contentJson.toString());
209 private JSONObject saveOriginalJsonObject(JSONObject jsonObj, Map<String, String> jsonStringValues) {
210 for (Object key : jsonObj.keySet()) {
211 String keyStr = (String) key;
212 Object keyvalue = jsonObj.get(keyStr);
213 if (keyvalue.toString().contains("{\\\"") || keyvalue.toString().contains("\\\"")) {
214 jsonStringValues.put(keyStr, keyvalue.toString());
215 // --- set default value
216 jsonObj.put(keyStr, "JSON_STRING");
219 // for nested objects iteration if required
220 if (keyvalue instanceof JSONObject) {
221 saveOriginalJsonObject((JSONObject) keyvalue, jsonStringValues);
222 // --- set default value
223 jsonObj.put(keyStr, "JSON_STRING");
226 if (keyvalue instanceof JSONArray) {
227 for (int i = 0; i < ((JSONArray) keyvalue).length(); i++) {
228 JSONObject temp = ((JSONArray) keyvalue).getJSONObject(i);
229 saveOriginalJsonObject(temp, jsonStringValues);
237 private JSONObject setOriginalJsonObject(JSONObject jsonObj, Map<String, String> jsonStringValues) {
238 for (Object key : jsonObj.keySet()) {
239 String keyStr = (String) key;
240 Object keyvalue = jsonObj.get(keyStr);
241 String originalValue = getOriginalValue(keyStr);
242 if (originalValue != null) {
243 jsonObj.put(keyStr, originalValue);
246 // for nested objects iteration if required
247 if (keyvalue instanceof JSONObject) {
248 setOriginalJsonObject((JSONObject) keyvalue, jsonStringValues);
249 jsonObj.put(keyStr, originalValue);
252 if (keyvalue instanceof JSONArray) {
253 for (int i = 0; i < ((JSONArray) keyvalue).length(); i++) {
254 JSONObject temp = ((JSONArray) keyvalue).getJSONObject(i);
255 setOriginalJsonObject(temp, jsonStringValues);
263 private GroupPolicyScopeList getPolicyObject(String policyScope) {
264 return (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
267 private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue)
269 ObjectWriter om = new ObjectMapper().writer();
271 DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
272 MicroServiceModels returnModel = new MicroServiceModels();
273 microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_MS));
274 if (policyAdapter.getServiceType() != null) {
275 microServiceObject.setService(policyAdapter.getServiceType());
276 microServiceObject.setVersion(policyAdapter.getVersion());
277 returnModel = getAttributeObject(microServiceObject.getService(), microServiceObject.getVersion());
279 if (returnModel.getAnnotation() == null || returnModel.getAnnotation().isEmpty()) {
280 if (policyAdapter.getUuid() != null) {
281 microServiceObject.setUuid(policyAdapter.getUuid());
283 if (policyAdapter.getLocation() != null) {
284 microServiceObject.setLocation(policyAdapter.getLocation());
286 if (policyAdapter.getConfigName() != null) {
287 microServiceObject.setConfigName(policyAdapter.getConfigName());
289 GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
290 if (policyScopeValue != null) {
291 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());
295 if (policyAdapter.getPolicyName() != null) {
296 microServiceObject.setPolicyName(policyAdapter.getPolicyName());
298 if (policyAdapter.getPolicyDescription() != null) {
299 microServiceObject.setDescription(policyAdapter.getPolicyDescription());
301 if (policyAdapter.getPriority() != null) {
302 microServiceObject.setPriority(policyAdapter.getPriority());
304 microServiceObject.setPriority("9999");
307 if (policyAdapter.getRiskLevel() != null) {
308 microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
310 if (policyAdapter.getRiskType() != null) {
311 microServiceObject.setRiskType(policyAdapter.getRiskType());
313 if (policyAdapter.getGuard() != null) {
314 microServiceObject.setGuard(policyAdapter.getGuard());
316 microServiceObject.setContent(jsonContent);
317 String modelName = policyAdapter.getServiceType();
318 String versionName = policyAdapter.getVersion();
319 List<Object> triggerData = commonClassDao.getDataById(MicroServiceModels.class, "modelName:version",
320 modelName + ":" + versionName);
321 MicroServiceModels model = null;
322 boolean ruleCheck = false;
323 if (!triggerData.isEmpty()) {
324 model = (MicroServiceModels) triggerData.get(0);
325 if (model.getRuleFormation() != null) {
326 microServiceObject.setUiContent(jsonContent);
331 json = om.writeValueAsString(microServiceObject);
332 } catch (JsonProcessingException e) {
333 LOGGER.error("Error writing out the object", e);
335 LOGGER.info("input json: " + json);
336 LOGGER.info("input jsonContent: " + jsonContent);
337 String cleanJson = cleanUpJson(json);
338 // --- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
340 StringUtils.replaceEach(cleanJson, new String[] {"\"" + dummyValue + "\""}, new String[] {"\"\""});
341 LOGGER.info("tempJson: " + tempJson);
342 cleanJson = removeNullAttributes(tempJson);
343 if (cleanJson.contains("\\")) {
344 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\"}, new String[] {""});
346 policyAdapter.setJsonBody(cleanJson);
348 ObjectMapper mapper = new ObjectMapper();
349 JsonNode tempJsonNode = mapper.readTree(cleanJson);
351 ObjectNode finalJson = (ObjectNode) tempJsonNode;
352 JsonNode object = tempJsonNode.get("content");
353 String primaryKey1 = model.getRuleFormation();
354 String[] primaryKeyForSignatures = primaryKey1.split("@");
355 for (String primaryKeyForSignature : primaryKeyForSignatures) {
356 String primarykeyAlarm = primaryKeyForSignature.substring(0, primaryKeyForSignature.indexOf('.'));
357 JsonNode triggerSig = object.get(primarykeyAlarm);
358 sigRules = new HashMap<>();
359 String parseKey = primaryKeyForSignature.substring(primaryKeyForSignature.indexOf('.') + 1);
360 StringBuilder sb = null;
361 if (triggerSig instanceof ArrayNode) {
362 for (int i = 0; i < triggerSig.size(); i++) {
363 sb = new StringBuilder();
364 parseData(triggerSig.get(i), parseKey);
366 List<?> keyList = new ArrayList<>(sigRules.keySet());
367 for (int j = keyList.size() - 1; j >= 0; j--) {
368 String key = (String) keyList.get(j);
369 String jsonNode = sigRules.get(key);
370 constructRule(sb, jsonNode, sigRules);
372 sb.append(")").toString();
373 putRuletoJson(tempJsonNode, i, sb, parseKey, primarykeyAlarm);
374 sigRules = new HashMap<>();
377 sb = new StringBuilder();
378 parseData(triggerSig, parseKey);
381 policyAdapter.setJsonBody(finalJson.toString());
383 return policyAdapter;
386 private JsonNode putRuletoJson(JsonNode tmpJsonNode, int item, StringBuilder sb, String parseKey,
388 JsonNode tmp = tmpJsonNode;
389 ObjectNode objectNode = (ObjectNode) tmp;
390 JsonNode jsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
391 JsonNode tempRuleJsonNode = tmpJsonNode.get("content").get(primaryKey).get(item);
392 String[] tempSt = parseKey.split("\\.");
393 for (String value : tempSt) {
394 if (value.contains("[")) {
395 if (tempRuleJsonNode instanceof ArrayNode) {
396 JsonNode tempRuleNode = tempRuleJsonNode.get(item);
397 ((ArrayNode) tempRuleJsonNode).removeAll();
398 ((ArrayNode) tempRuleJsonNode).add(tempRuleNode);
399 objectNode = (ObjectNode) tempRuleJsonNode.get(item);
401 String key = value.substring(0, value.indexOf('['));
402 objectNode.remove(key);
403 objectNode.put(key, sb.toString());
406 jsonNode = jsonNode.get(value);
407 if (jsonNode instanceof ArrayNode) {
408 tempRuleJsonNode = jsonNode;
409 jsonNode = jsonNode.get(item);
416 public boolean checkPattern(String patternString) {
417 return PATTERN.matcher(patternString).find();
424 * @param jsonNode the json node
425 * @param sigRules2 the sig rules 2
427 public void constructRule(StringBuilder sb, String jsonNode, Map<String, String> sigRules2) {
429 String cleanJsonNode = jsonNode.replace("\"\"", " ");
430 cleanJsonNode = cleanJsonNode.replaceAll("\"", "");
431 cleanJsonNode = cleanJsonNode.replaceAll("\\(", "");
432 cleanJsonNode = cleanJsonNode.replaceAll("\\)", "");
433 boolean flag = false;
434 if (cleanJsonNode.contains("OR")) {
438 for (String rowValue : cleanJsonNode.split(" ")) {
439 if (checkPattern(rowValue)) {
440 String value = sigRules2.get(rowValue);
441 LOGGER.info(" Value is:" + value);
442 constructRule(sb, value, sigRules2);
444 if ((count == 0) && (!("AND").equals(rowValue)) && (!("OR").equals(rowValue))) {
448 LOGGER.info(" " + rowValue + " ");
449 sb.append(" " + rowValue + " ");
450 if (count % 3 == 0) {
464 * @param jsonNode the json node
465 * @param string the string
467 public void parseData(JsonNode jsonNode, String string) {
468 if (string.contains(".")) {
469 String firstIndex = string.substring(0, string.indexOf('.'));
470 JsonNode signtures = jsonNode.get(firstIndex);
471 String subIndex = string.substring(firstIndex.length() + 1);
472 if (signtures instanceof ArrayNode) {
473 for (int i = 0; i < signtures.size(); i++) {
474 parseData(signtures.get(i), subIndex);
477 parseData(signtures, subIndex);
480 if (string.contains("[")) {
481 String ruleIndex = string.substring(0, string.indexOf('['));
482 String[] keys = string.substring(string.indexOf('[') + 1, string.lastIndexOf(']')).split(",");
483 String key = "A" + Integer.valueOf(sigRules.size() + 1);
484 JsonNode node = jsonNode.get(ruleIndex);
485 StringBuilder sb = new StringBuilder("(");
486 for (int i = 0; i < keys.length; i++) {
487 sb.append(node.get(keys[i].trim()));
490 sigRules.put(key, sb.toString());
496 * removeNullAttributes.
498 * @param cleanJson String
501 public String removeNullAttributes(String cleanJson) {
502 ObjectMapper mapper = new ObjectMapper();
505 JsonNode rootNode = mapper.readTree(cleanJson);
506 JsonNode returnNode = mapper.readTree(cleanJson);
507 Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
508 boolean remove = false;
509 JsonObject removed = null;
510 boolean contentChanged = false;
511 while (fieldsIterator.hasNext()) {
512 Map.Entry<String, JsonNode> field = fieldsIterator.next();
513 final String key = field.getKey();
514 final JsonNode value = field.getValue();
515 if ("content".equalsIgnoreCase(key)) {
516 String contentStr = value.toString();
517 try (JsonReader reader = Json.createReader(new StringReader(contentStr))) {
518 JsonObject jsonContent = reader.readObject();
519 removed = removeNull(jsonContent);
520 if (!jsonContent.toString().equals(removed.toString())) {
521 contentChanged = true;
525 if (value == null || value.isNull()) {
526 ((ObjectNode) returnNode).remove(key);
531 cleanJson = returnNode.toString();
533 if (value == null || value.isNull()) {
534 ((ObjectNode) returnNode).remove(key);
539 cleanJson = returnNode.toString();
542 if (contentChanged) {
543 // set modified content to cleanJson
544 JSONObject jsonObject = new JSONObject(cleanJson);
545 jsonObject.put("content", removed.toString());
546 cleanJson = cleanUpJson(jsonObject.toString());
549 } catch (IOException e) {
550 LOGGER.error("Error writing out the JsonNode", e);
556 * To verify if it is a JSON string. If it is, then return its original value.
558 * @param key holds the values
561 private String getOriginalValue(String key) {
562 for (String k : jsonStringValues.keySet()) {
563 if (k.contains("@")) {
564 String[] arrOfKeys = k.split("@");
565 for (int i = 0; i < arrOfKeys.length; i++) {
566 if (arrOfKeys[i].contains(".")) {
567 arrOfKeys[i] = arrOfKeys[i].substring(arrOfKeys[i].indexOf(".") + 1);
568 if (arrOfKeys[i].equals(key)) {
569 return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""},
570 new String[] {"\\\""});
575 if (k.endsWith(key)) {
576 return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""}, new String[] {"\\\""});
586 * @param array JsonArray
589 public JsonArray removeNull(JsonArray array) {
590 JsonArrayBuilder builder = Json.createArrayBuilder();
592 for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++index) {
593 JsonValue value = it.next();
594 switch (value.getValueType()) {
596 JsonArray tempArray = removeNull(array.getJsonArray(index));
597 if (!tempArray.isEmpty()) {
598 builder.add(tempArray);
602 JsonObject object = removeNull(array.getJsonObject(index));
603 if (!object.isEmpty()) {
608 String str = array.getString(index);
609 if (str != null && !str.isEmpty()) {
614 builder.add(array.getJsonNumber(index));
618 builder.add(array.getBoolean(index));
625 return builder.build();
631 * @param obj JsonObject
634 public JsonObject removeNull(JsonObject obj) {
635 JsonObjectBuilder builder = Json.createObjectBuilder();
636 for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
637 Entry<String, JsonValue> entry = it.next();
638 String key = entry.getKey();
639 JsonValue value = entry.getValue();
640 switch (value.getValueType()) {
642 JsonArray array = removeNull(obj.getJsonArray(key));
643 if (!array.isEmpty()) {
644 builder.add(key, array);
648 JsonObject object = removeNull(obj.getJsonObject(key));
649 if (!object.isEmpty()) {
650 if (!jsonStringValues.isEmpty()) {
651 String originalValue = getOriginalValue(key);
652 if (originalValue != null) {
653 builder.add(key, object.toString());
657 builder.add(key, object);
661 String str = obj.getString(key);
662 if (str != null && !str.isEmpty()) {
663 if (!jsonStringValues.isEmpty()) {
664 String originalValue = getOriginalValue(key);
665 if (originalValue != null) {
666 str = getOriginalValue(key);
669 builder.add(key, str);
673 builder.add(key, obj.getJsonNumber(key));
677 builder.add(key, obj.getBoolean(key));
684 return builder.build();
693 public String cleanUpJson(String json) {
694 String cleanJson = StringUtils.replaceEach(json, new String[] {"\\\\", "\\\\\\", "\\\\\\\\"},
695 new String[] {"\\", "\\", "\\"});
696 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\\"}, new String[] {"\\"});
698 StringUtils.replaceEach(cleanJson, new String[] {"\\\\", "[[", "]]"}, new String[] {"\\", "[", "]"});
700 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\"", "\\\"", "\"[{", "}]\""},
701 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[] {"\""});
709 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\\\\\\\""}, new String[] {"\""});
710 cleanJson = StringUtils.replaceEach(cleanJson, new String[] {"\"[", "]\""}, new String[] {"[", "]"});
717 * @param jsonNode JsonNode
720 public JSONObject decodeContent(JsonNode jsonNode) {
721 Iterator<JsonNode> jsonElements = jsonNode.elements();
722 Iterator<String> jsonKeys = jsonNode.fieldNames();
723 Map<String, String> element = new TreeMap<>();
724 while (jsonElements.hasNext() && jsonKeys.hasNext()) {
725 element.put(jsonKeys.next(), jsonElements.next().toString());
727 JSONObject jsonResult = new JSONObject();
728 JSONArray jsonArray = null;
729 String oldValue = null;
730 String nodeKey = null;
731 String arryKey = null;
732 Boolean isArray = false;
733 JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
734 ObjectNode node = nodeFactory.objectNode();
735 String prevKey = null;
737 for (Entry<String, String> entry : element.entrySet()) {
738 String key = entry.getKey();
739 String value = entry.getValue();
740 if (key.contains(".")) {
741 presKey = key.substring(0, key.indexOf('.'));
742 } else if (key.contains("@")) {
743 presKey = key.substring(0, key.indexOf('@'));
747 // first check if we are different from old.
748 LOGGER.info(key + "\n");
749 if (jsonArray != null && jsonArray.length() > 0 && key.contains("@") && !key.contains(".")
750 && oldValue != null) {
751 if (!oldValue.equals(key.substring(0, key.indexOf('@')))) {
752 jsonResult.put(oldValue, jsonArray);
753 jsonArray = new JSONArray();
755 } else if (jsonArray != null && jsonArray.length() > 0 && !presKey.equals(prevKey) && oldValue != null) {
756 jsonResult.put(oldValue, jsonArray);
758 jsonArray = new JSONArray();
763 if (key.contains(".")) {
764 if (nodeKey == null) {
765 nodeKey = key.substring(0, key.indexOf('.'));
767 if (nodeKey.equals(key.substring(0, key.indexOf('.')))) {
768 node.put(key.substring(key.indexOf('.') + 1), value);
770 if (node.size() != 0) {
771 if (nodeKey.contains("@")) {
772 if (arryKey == null) {
773 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
775 if (nodeKey.endsWith("@0")) {
777 jsonArray = new JSONArray();
779 if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
780 jsonArray.put(decodeContent(node));
782 if ((key.contains("@") && !arryKey.equals(key.substring(0, nodeKey.indexOf('@'))))
783 || !key.contains("@")) {
784 jsonResult.put(arryKey, jsonArray);
785 jsonArray = new JSONArray();
787 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
790 jsonResult.put(nodeKey, decodeContent(node));
792 node = nodeFactory.objectNode();
794 nodeKey = key.substring(0, key.indexOf('.'));
795 if (nodeKey.contains("@")) {
796 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
798 node.put(key.substring(key.indexOf('.') + 1), value);
801 if (node.size() != 0) {
802 if (nodeKey.contains("@")) {
803 if (arryKey == null) {
804 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
806 if (nodeKey.endsWith("@0")) {
808 jsonArray = new JSONArray();
810 if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
811 jsonArray.put(decodeContent(node));
813 jsonResult.put(arryKey, jsonArray);
814 jsonArray = new JSONArray();
815 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
818 jsonResult.put(nodeKey, decodeContent(node));
820 node = nodeFactory.objectNode();
822 if (key.contains("@")) {
824 if (key.endsWith("@0") || jsonArray == null) {
825 jsonArray = new JSONArray();
827 } else if (!key.contains("@")) {
831 if (oldValue == null) {
832 oldValue = key.substring(0, key.indexOf('@'));
834 if (oldValue != prevKey) {
835 oldValue = key.substring(0, key.indexOf('@'));
837 if (oldValue.equals(key.substring(0, key.indexOf('@')))) {
838 jsonArray.put(value);
840 jsonResult.put(oldValue, jsonArray);
841 jsonArray = new JSONArray();
843 oldValue = key.substring(0, key.indexOf('@'));
845 jsonResult.put(key, value);
849 if (node.size() > 0) {
850 if (nodeKey.contains("@")) {
851 if (jsonArray == null) {
852 jsonArray = new JSONArray();
854 if (arryKey == null) {
855 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
857 jsonArray.put(decodeContent(node));
858 jsonResult.put(arryKey, jsonArray);
861 jsonResult.put(nodeKey, decodeContent(node));
864 if (isArray && jsonArray.length() > 0) {
865 jsonResult.put(oldValue, jsonArray);
871 value = {"/policyController/getDCAEMSTemplateData.htm"},
872 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
873 public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response)
875 // TreeSet is used to ensure that individual items appear before their containing collection.
876 allManyTrueKeys = new TreeSet<>();
877 ObjectMapper mapper = new ObjectMapper();
878 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
879 JsonNode root = mapper.readTree(request.getReader());
881 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
882 String servicename = value.split("-v")[0];
883 String version = null;
884 if (value.contains("-v")) {
885 version = value.split("-v")[1];
887 MicroServiceModels returnModel = getAttributeObject(servicename, version);
889 MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value);
890 JSONObject jsonHdDefaultObj = null;
891 if (returnHeaderDefauls != null) {
892 jsonHdDefaultObj = new JSONObject();
893 jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName());
894 jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard());
895 jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel());
896 jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType());
897 jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority());
899 String headDefautlsData = "";
900 if (jsonHdDefaultObj != null) {
901 headDefautlsData = jsonHdDefaultObj.toString();
903 headDefautlsData = "null";
906 // Get all keys with "MANY-true" defined in their value from subAttribute
907 Set<String> allkeys = null;
908 if (returnModel.getSub_attributes() != null && !returnModel.getSub_attributes().isEmpty()) {
909 JSONObject json = new JSONObject(returnModel.getSub_attributes());
911 allkeys = allManyTrueKeys;
912 allManyTrueKeys = new TreeSet<>();
913 LOGGER.info("allkeys : " + allkeys);
916 // Get element order info
917 String dataOrderInfo = returnModel.getDataOrderInfo();
918 if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
919 dataOrderInfo = "\"" + dataOrderInfo + "\"";
921 LOGGER.info("dataOrderInfo : " + dataOrderInfo);
923 String allMnyTrueKeys = "";
924 if (allkeys != null) {
925 allMnyTrueKeys = allkeys.toString();
928 String jsonModel = createMicroSeriveJson(returnModel, allkeys);
930 JSONObject jsonObject = new JSONObject(jsonModel);
932 JSONObject finalJsonObject = null;
933 if (allkeys != null) {
934 Iterator<String> iter = allkeys.iterator();
935 while (iter.hasNext()) {
936 // Convert to array values for MANY-true keys
937 finalJsonObject = convertToArrayElement(jsonObject, iter.next());
938 jsonObject = finalJsonObject;
942 if (finalJsonObject != null) {
943 LOGGER.info(finalJsonObject.toString());
944 jsonModel = finalJsonObject.toString();
947 // get all properties with "MANY-true" defined in Ref_attributes
948 Set<String> manyTrueProperties = getManyTrueProperties(returnModel.getRef_attributes());
949 if (manyTrueProperties != null) {
950 JSONObject jsonObj = new JSONObject(jsonModel);
951 for (String s : manyTrueProperties) {
953 // convert to array element for MANY-true properties
954 finalJsonObject = convertToArrayElement(jsonObj, s.trim());
955 jsonObj = finalJsonObject;
958 if (finalJsonObject != null) {
959 LOGGER.info(finalJsonObject.toString());
960 jsonModel = finalJsonObject.toString();
964 response.setCharacterEncoding("UTF-8");
965 response.setContentType("application / json");
966 request.setCharacterEncoding("UTF-8");
967 List<Object> list = new ArrayList<>();
968 String responseString = mapper.writeValueAsString(returnModel);
970 JSONObject json = null;
972 if ("".equals(allMnyTrueKeys)) {
973 json = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",dataOrderInfo:"
974 + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}");
976 json = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel
977 + ",allManyTrueKeys: " + allMnyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:"
978 + headDefautlsData + "}");
981 response.getWriter().write(list.toString());
985 @SuppressWarnings({"unchecked", "rawtypes"})
986 private String createMicroSeriveJson(MicroServiceModels returnModel, Set<String> allkeys) {
987 Map<String, String> attributeMap = new HashMap<>();
988 Map<String, String> refAttributeMap = new HashMap<>();
989 String attribute = returnModel.getAttributes();
990 if (attribute != null) {
991 attribute = attribute.trim();
993 String refAttribute = returnModel.getRef_attributes();
994 if (refAttribute != null) {
995 refAttribute = refAttribute.trim();
997 String enumAttribute = returnModel.getEnumValues();
998 if (enumAttribute != null) {
999 enumAttribute = enumAttribute.trim();
1001 if (!StringUtils.isEmpty(attribute)) {
1002 attributeMap = convert(attribute, ",");
1004 if (!StringUtils.isEmpty(refAttribute)) {
1005 refAttributeMap = convert(refAttribute, ",");
1008 Gson gson = new Gson();
1010 String subAttributes = returnModel.getSub_attributes();
1011 if (subAttributes != null) {
1012 subAttributes = subAttributes.trim();
1017 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
1019 JSONObject object = new JSONObject();
1021 for (Entry<String, String> keySet : attributeMap.entrySet()) {
1022 JSONArray array = new JSONArray();
1023 String value = keySet.getValue();
1024 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
1026 object.put(keySet.getKey().trim(), array);
1028 object.put(keySet.getKey().trim(), value.trim());
1032 for (Entry<String, String> keySet : refAttributeMap.entrySet()) {
1033 JSONArray array = new JSONArray();
1034 String value = keySet.getValue().split(":")[0];
1035 if (gsonObject.containsKey(value)) {
1036 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
1037 array.put(recursiveReference(value, gsonObject, enumAttribute));
1038 object.put(keySet.getKey().trim(), array);
1040 object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
1043 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
1044 array.put(value.trim());
1045 object.put(keySet.getKey().trim(), array);
1047 object.put(keySet.getKey().trim(), value.trim());
1052 return object.toString();
1055 @SuppressWarnings("unchecked")
1056 private JSONObject recursiveReference(String name, Map<String, String> subAttributeMap, String enumAttribute) {
1057 JSONObject object = new JSONObject();
1058 Map<String, String> map;
1059 Object returnClass = subAttributeMap.get(name);
1060 map = (Map<String, String>) returnClass;
1063 for (Entry<String, String> m : map.entrySet()) {
1064 String[] splitValue = m.getValue().split(":");
1065 array = new JSONArray();
1066 if (subAttributeMap.containsKey(splitValue[0])) {
1067 if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])) {
1068 array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
1069 object.put(m.getKey().trim(), array);
1071 object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
1074 if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])) {
1075 array.put(splitValue[0].trim());
1076 object.put(m.getKey().trim(), array);
1078 object.put(m.getKey().trim(), splitValue[0].trim());
1086 public JSONObject convertToArrayElement(JSONObject json, String keyValue) {
1087 return convertToArrayElement(json, new HashSet<>(), keyValue);
1090 private JSONObject convertToArrayElement(JSONObject json, Set<String> keys, String keyValue) {
1091 for (String key : json.keySet()) {
1092 Object obj = json.get(key);
1093 if (key.equals(keyValue.trim())) {
1094 if (!(obj instanceof JSONArray)) {
1095 JSONArray newJsonArray = new JSONArray();
1096 newJsonArray.put(obj);
1097 json.put(key, newJsonArray);
1099 LOGGER.info("key : " + key);
1100 LOGGER.info("obj : " + obj);
1101 LOGGER.info("json.get(key) : " + json.get(key));
1102 LOGGER.info("keyValue : " + keyValue);
1103 keys.addAll(json.keySet());
1108 if (obj instanceof JSONObject) {
1109 convertToArrayElement(json.getJSONObject(key), keyValue);
1112 if (obj instanceof JSONArray) {
1113 convertToArrayElement(json.getJSONArray(key).getJSONObject(0), keyValue);
1121 * getManyTrueProperties.
1123 * @param referAttributes String
1124 * @return a Set of String
1126 public Set<String> getManyTrueProperties(String referAttributes) {
1127 LOGGER.info("referAttributes : " + referAttributes);
1128 Set<String> manyTrueProperties = new HashSet<>();
1130 if (referAttributes != null) {
1131 String[] referAarray = referAttributes.split(",");
1132 String[] element = null;
1133 for (int i = 0; i < referAarray.length; i++) {
1134 element = referAarray[i].split("=");
1135 if (element.length > 1 && element[1].contains("MANY-true")) {
1136 manyTrueProperties.add(element[0]);
1141 return manyTrueProperties;
1144 // call this method to start the recursive
1145 private Set<String> getAllKeys(JSONObject json) {
1146 return getAllKeys(json, new HashSet<>());
1149 private Set<String> getAllKeys(JSONArray arr) {
1150 return getAllKeys(arr, new HashSet<>());
1153 private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
1154 for (int i = 0; i < arr.length(); i++) {
1155 Object obj = arr.get(i);
1156 if (obj instanceof JSONObject) {
1157 keys.addAll(getAllKeys(arr.getJSONObject(i)));
1159 if (obj instanceof JSONArray) {
1160 keys.addAll(getAllKeys(arr.getJSONArray(i)));
1167 // this method returns a set of keys with "MANY-true" defined in their value.
1168 private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
1169 for (String key : json.keySet()) {
1170 Object obj = json.get(key);
1171 if (obj instanceof String && ((String) obj).contains("MANY-true")) {
1172 LOGGER.info("key : " + key);
1173 LOGGER.info("obj : " + obj);
1174 allManyTrueKeys.add(key);
1176 if (obj instanceof JSONObject) {
1177 keys.addAll(getAllKeys(json.getJSONObject(key)));
1179 if (obj instanceof JSONArray) {
1180 keys.addAll(getAllKeys(json.getJSONArray(key)));
1188 value = {"/policyController/getModelServiceVersioneData.htm"},
1189 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
1190 public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response)
1191 throws IOException {
1192 ObjectMapper mapper = new ObjectMapper();
1193 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
1195 response.setCharacterEncoding("UTF-8");
1196 response.setContentType("application / json");
1197 request.setCharacterEncoding("UTF-8");
1198 List<Object> list = new ArrayList<>();
1199 String value = mapper.readTree(request.getReader()).get("policyData").toString().replaceAll("^\"|\"$", "");
1200 String servicename = value.split("-v")[0];
1201 Set<String> returnList = getVersionList(servicename);
1202 list.add(new JSONObject("{dcaeModelVersionData: " + mapper.writeValueAsString(returnList) + "}"));
1203 response.getWriter().write(list.toString());
1207 private Set<String> getVersionList(String name) {
1208 MicroServiceModels workingModel;
1209 Set<String> list = new HashSet<>();
1210 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1211 for (int i = 0; i < microServiceModelsData.size(); i++) {
1212 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1213 if (workingModel.getVersion() != null) {
1214 list.add(workingModel.getVersion());
1216 list.add("Default");
1222 private MicroServiceModels getAttributeObject(String name, String version) {
1223 MicroServiceModels workingModel = new MicroServiceModels();
1224 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1225 for (int i = 0; i < microServiceModelsData.size(); i++) {
1226 workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1227 if (version != null) {
1228 if (workingModel.getVersion() != null) {
1229 if (workingModel.getVersion().equals(version)) {
1230 return workingModel;
1233 return workingModel;
1236 return workingModel;
1240 return workingModel;
1243 private MicroserviceHeaderdeFaults getHeaderDefaultsObject(String modelName) {
1244 return (MicroserviceHeaderdeFaults) commonClassDao.getEntityItem(MicroserviceHeaderdeFaults.class, "modelName",
1249 value = {"/get_DCAEPriorityValues"},
1250 method = {org.springframework.web.bind.annotation.RequestMethod.GET},
1251 produces = MediaType.APPLICATION_JSON_VALUE)
1252 public void getDCAEPriorityValuesData(HttpServletRequest request, HttpServletResponse response) {
1254 Map<String, Object> model = new HashMap<>();
1255 ObjectMapper mapper = new ObjectMapper();
1256 List<String> priorityList = new ArrayList<>();
1258 for (int i = 1; i < priorityCount; i++) {
1259 priorityList.add(String.valueOf(i));
1261 model.put("priorityDatas", mapper.writeValueAsString(priorityList));
1262 response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
1263 } catch (Exception e) {
1269 * prePopulateDCAEMSPolicyData.
1271 * @param policyAdapter PolicyRestAdapter
1272 * @param entity PolicyEntity
1274 public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1275 if (! (policyAdapter.getPolicyData() instanceof PolicyType)) {
1278 Object policyData = policyAdapter.getPolicyData();
1279 PolicyType policy = (PolicyType) policyData;
1280 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
1281 String policyNameValue =
1282 policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("MS_") + 3);
1283 policyAdapter.setPolicyName(policyNameValue);
1284 String description = "";
1286 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
1287 } catch (Exception e) {
1288 LOGGER.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue, e);
1289 description = policy.getDescription();
1291 policyAdapter.setPolicyDescription(description);
1292 // Get the target data under policy.
1293 TargetType target = policy.getTarget();
1294 if (target == null) {
1297 // Under target we have AnyOFType
1298 List<AnyOfType> anyOfList = target.getAnyOf();
1299 if (anyOfList == null) {
1302 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
1303 while (iterAnyOf.hasNext()) {
1304 AnyOfType anyOf = iterAnyOf.next();
1305 // Under AnyOFType we have AllOFType
1306 List<AllOfType> allOfList = anyOf.getAllOf();
1307 if (allOfList == null) {
1310 Iterator<AllOfType> iterAllOf = allOfList.iterator();
1311 while (iterAllOf.hasNext()) {
1312 AllOfType allOf = iterAllOf.next();
1313 // Under AllOFType we have Match
1314 List<MatchType> matchList = allOf.getMatch();
1315 if (matchList == null) {
1318 Iterator<MatchType> iterMatch = matchList.iterator();
1319 while (matchList.size() > 1 && iterMatch.hasNext()) {
1320 MatchType match = iterMatch.next();
1322 // Under the match we have attribute value and
1323 // attributeDesignator. So,finally down to the actual attribute.
1325 AttributeValueType attributeValue = match.getAttributeValue();
1326 String value = (String) attributeValue.getContent().get(0);
1327 AttributeDesignatorType designator = match.getAttributeDesignator();
1328 String attributeId = designator.getAttributeId();
1329 // First match in the target is OnapName, so set that value.
1330 if ("ONAPName".equals(attributeId)) {
1331 policyAdapter.setOnapName(value);
1333 if ("ConfigName".equals(attributeId)) {
1334 policyAdapter.setConfigName(value);
1336 if ("uuid".equals(attributeId)) {
1337 policyAdapter.setUuid(value);
1339 if ("location".equals(attributeId)) {
1340 policyAdapter.setLocation(value);
1342 if ("RiskType".equals(attributeId)) {
1343 policyAdapter.setRiskType(value);
1345 if ("RiskLevel".equals(attributeId)) {
1346 policyAdapter.setRiskLevel(value);
1348 if ("guard".equals(attributeId)) {
1349 policyAdapter.setGuard(value);
1351 if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
1352 PolicyController controller = new PolicyController();
1353 String newDate = controller.convertDate(value);
1354 policyAdapter.setTtlDate(newDate);
1357 readFile(policyAdapter, entity);
1365 * @param str the str
1366 * @param split the split
1369 public Map<String, String> convert(String str, String split) {
1370 Map<String, String> map = new HashMap<>();
1371 for (final String entry : str.split(split)) {
1372 String[] parts = entry.split("=");
1373 map.put(parts[0], parts[1]);
1381 * @param policyAdapter the policy adapter
1382 * @param entity the entity
1384 @SuppressWarnings("unchecked")
1385 public void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1387 DCAEMicroServiceObject msBody =
1388 new ObjectMapper().readValue(entity.getConfigurationData().getConfigBody(),
1389 DCAEMicroServiceObject.class);
1390 String policyScopeName = getPolicyScope(msBody.getPolicyScope());
1391 policyAdapter.setPolicyScope(policyScopeName);
1393 policyAdapter.setPriority(msBody.getPriority());
1395 if (msBody.getVersion() != null) {
1396 policyAdapter.setServiceType(msBody.getService());
1397 policyAdapter.setVersion(msBody.getVersion());
1399 policyAdapter.setServiceType(msBody.getService());
1402 LinkedHashMap<String, ?> content = (LinkedHashMap<String, ?>) msBody.getUiContent();
1403 if (content == null) {
1404 content = (LinkedHashMap<String, ?>) msBody.getContent();
1406 if (content != null) {
1407 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
1408 LinkedHashMap<String, ?> map = content;
1409 readRecursivlyJSONContent(map, data);
1410 policyAdapter.setRuleData(data);
1413 } catch (Exception e) {
1419 @SuppressWarnings({"rawtypes", "unchecked"})
1420 public void readRecursivlyJSONContent(Map<String, ?> map, Map<String, Object> data) {
1421 for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
1422 Object key = iterator.next();
1423 Object value = map.get(key);
1424 if (value instanceof LinkedHashMap<?, ?>) {
1425 LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<>();
1426 readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
1427 for (Entry<String, Object> entry : secondObjec.entrySet()) {
1428 data.put(key + "." + entry.getKey(), entry.getValue());
1430 } else if (value instanceof ArrayList) {
1431 ArrayList<?> jsonArrayVal = (ArrayList<?>) value;
1432 for (int i = 0; i < jsonArrayVal.size(); i++) {
1433 Object arrayvalue = jsonArrayVal.get(i);
1434 if (arrayvalue instanceof LinkedHashMap<?, ?>) {
1435 LinkedHashMap<String, Object> newData = new LinkedHashMap<>();
1436 readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
1437 for (Entry<String, Object> entry : newData.entrySet()) {
1438 data.put(key + "@" + i + "." + entry.getKey(), entry.getValue());
1440 } else if (arrayvalue instanceof ArrayList) {
1441 ArrayList<?> jsonArrayVal1 = (ArrayList<?>) value;
1442 for (int j = 0; j < jsonArrayVal1.size(); j++) {
1443 Object arrayvalue1 = jsonArrayVal1.get(i);
1444 data.put(key + "@" + j, arrayvalue1.toString());
1447 data.put(key + "@" + i, arrayvalue.toString());
1451 data.put(key.toString(), value.toString());
1459 * @param value String
1462 public String getPolicyScope(String value) {
1463 List<Object> groupList = commonClassDao.getDataById(GroupPolicyScopeList.class, "groupList", value);
1464 if (groupList != null && !groupList.isEmpty()) {
1465 return ((GroupPolicyScopeList) groupList.get(0)).getGroupName();
1471 * Convert the map values and set into JSON body.
1473 * @param attributesMap Map of attributes
1474 * @param attributesRefMap Map of attribute references
1477 public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
1478 Map<String, String> attribute = new HashMap<>();
1482 for (Entry<String, String> entry : attributesMap.entrySet()) {
1483 key = entry.getKey();
1484 value = entry.getValue();
1485 attribute.put(key, value);
1487 for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
1488 key = entryRef.getKey();
1489 value = entryRef.getValue();
1490 attribute.put(key, value);
1492 for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
1493 key = entryList.getKey();
1494 value = entryList.getValue();
1495 attribute.put(key, value);
1497 for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()) {
1498 key = arrayList.getKey();
1500 for (Object textList : arrayList.getValue()) {
1502 temp = new StringBuilder();
1503 temp.append("[" + textList);
1505 temp.append("," + textList);
1508 attribute.put(key, temp + "]");
1515 value = {"/ms_dictionary/set_MSModelData"},
1516 method = {org.springframework.web.bind.annotation.RequestMethod.POST})
1517 public void SetMSModelData(HttpServletRequest request, HttpServletResponse response)
1518 throws IOException, FileUploadException {
1519 modelList = new ArrayList<>();
1520 dirDependencyList = new ArrayList<>();
1521 classMap = new LinkedHashMap<>();
1522 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
1523 boolean zip = false;
1524 boolean yml = false;
1525 String errorMsg = "";
1526 for (FileItem item : items) {
1527 if (item.getName().endsWith(".zip") || item.getName().endsWith(".xmi") || item.getName().endsWith(".yml")) {
1528 this.newModel = new MicroServiceModels();
1530 File file = new File(item.getName());
1531 OutputStream outputStream = new FileOutputStream(file);
1532 IOUtils.copy(item.getInputStream(), outputStream);
1533 outputStream.close();
1534 this.newFile = file.toString();
1535 this.newModel.setModelName(this.newFile.split("-v")[0]);
1537 if (this.newFile.contains("-v")) {
1538 if (item.getName().endsWith(".zip")) {
1539 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
1541 } else if (item.getName().endsWith(".yml")) {
1542 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
1545 this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
1548 } catch (Exception e) {
1549 LOGGER.error("Upload error : ", e);
1550 errorMsg = "Upload error:" + e.getMessage();
1556 if (!errorMsg.isEmpty()) {
1557 response.setCharacterEncoding("UTF-8");
1558 response.setContentType("application / json");
1559 request.setCharacterEncoding("UTF-8");
1561 JSONObject j = new JSONObject();
1562 j.put("errorMsg", errorMsg);
1563 response.getWriter().write(j.toString());
1567 List<File> fileList = new ArrayList<>();
1568 MSModelUtils msModelUtils = new MSModelUtils(commonClassDao);
1569 this.directory = "model";
1571 extractFolder(this.newFile);
1572 fileList = listModelFiles(this.directory);
1574 errorMsg = msModelUtils.parseTosca(this.newFile);
1575 if (errorMsg != null) {
1576 response.setCharacterEncoding("UTF-8");
1577 response.setContentType("application / json");
1578 request.setCharacterEncoding("UTF-8");
1579 JSONObject json = new JSONObject();
1580 json.put("errorMsg", errorMsg);
1581 response.getWriter().write(json.toString());
1586 File file = new File(this.newFile);
1589 String modelType = "";
1592 // Process Main Model file first
1593 classMap = new LinkedHashMap<>();
1594 for (File file : fileList) {
1595 if (!file.isDirectory() && file.getName().endsWith(".xmi")) {
1596 retreiveDependency(file.toString(), true);
1600 modelList = createList();
1602 cleanUp(this.newFile);
1606 modelList.add(this.newModel.getModelName());
1607 String className = this.newModel.getModelName();
1608 MSAttributeObject msAttributes = new MSAttributeObject();
1609 msAttributes.setClassName(className);
1611 LinkedHashMap<String, String> returnAttributeList = new LinkedHashMap<>();
1612 returnAttributeList.put(className, msModelUtils.getAttributeString());
1613 msAttributes.setAttribute(returnAttributeList);
1615 msAttributes.setSubClass(msModelUtils.getRetmap());
1617 msAttributes.setMatchingSet(msModelUtils.getMatchableValues());
1619 LinkedHashMap<String, String> returnReferenceList = new LinkedHashMap<>();
1621 returnReferenceList.put(className, msModelUtils.getReferenceAttributes());
1622 msAttributes.setRefAttribute(returnReferenceList);
1624 if (msModelUtils.getListConstraints() != "") {
1625 LinkedHashMap<String, String> enumList = new LinkedHashMap<>();
1626 String[] listArray = msModelUtils.getListConstraints().split("#");
1627 for (String str : listArray) {
1628 String[] strArr = str.split("=");
1629 if (strArr.length > 1) {
1630 enumList.put(strArr[0], strArr[1]);
1633 msAttributes.setEnumType(enumList);
1636 classMap = new LinkedHashMap<>();
1637 classMap.put(className, msAttributes);
1640 response.setCharacterEncoding("UTF-8");
1641 response.setContentType("application / json");
1642 request.setCharacterEncoding("UTF-8");
1644 ObjectMapper mapper = new ObjectMapper();
1645 JSONObject json = new JSONObject();
1646 json.put("classListDatas", modelList);
1647 json.put("modelDatas", mapper.writeValueAsString(classMap));
1648 json.put("modelType", modelType);
1649 json.put("dataOrderInfo", msModelUtils.getDataOrderInfo());
1650 json.put("ruleFormation", msModelUtils.getJsonRuleFormation());
1652 response.getWriter().write(json.toString());
1656 * Unzip file and store in the model directory for processing
1658 @SuppressWarnings("rawtypes")
1659 private void extractFolder(String zipFile) {
1661 File file = new File(zipFile);
1663 try (ZipFile zip = new ZipFile(file)) {
1664 String newPath = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1665 this.directory = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1666 checkZipDirectory(this.directory);
1667 new File(newPath).mkdir();
1668 Enumeration zipFileEntries = zip.entries();
1670 // Process each entry
1671 while (zipFileEntries.hasMoreElements()) {
1672 // grab a zip file entry
1673 ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
1674 String currentEntry = entry.getName();
1675 File destFile = new File("model" + File.separator + currentEntry);
1676 File destinationParent = destFile.getParentFile();
1678 destinationParent.mkdirs();
1680 if (!entry.isDirectory()) {
1681 BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
1683 byte[] data = new byte[buffer];
1684 try (FileOutputStream fos = new FileOutputStream(destFile);
1685 BufferedOutputStream dest = new BufferedOutputStream(fos, buffer)) {
1686 while ((currentByte = is.read(data, 0, buffer)) != -1) {
1687 dest.write(data, 0, currentByte);
1690 } catch (IOException e) {
1691 LOGGER.error("Failed to write zip contents to {}" + destFile + e);
1693 // PLD should I throw e?
1699 if (currentEntry.endsWith(".zip")) {
1700 extractFolder(destFile.getAbsolutePath());
1703 } catch (IOException e) {
1704 LOGGER.error("Failed to unzip model file " + zipFile, e);
1708 private void retreiveDependency(String workingFile, Boolean modelClass) {
1710 MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
1711 Map<String, MSAttributeObject> tempMap;
1713 tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
1715 classMap.putAll(tempMap);
1716 LOGGER.info(tempMap);
1722 private List<File> listModelFiles(String directoryName) {
1723 File fileDirectory = new File(directoryName);
1724 List<File> resultList = new ArrayList<>();
1725 for (File file : fileDirectory.listFiles()) {
1726 if (file.isFile()) {
1727 resultList.add(file);
1728 } else if (file.isDirectory()) {
1729 dirDependencyList.add(file.getName());
1730 resultList.addAll(listModelFiles(file.getAbsolutePath()));
1739 * @param path String
1741 public void cleanUp(String path) {
1744 FileUtils.forceDelete(new File(path));
1745 } catch (IOException e) {
1746 LOGGER.error("Failed to delete folder " + path, e);
1752 * checkZipDirectory.
1754 * @param zipDirectory String
1756 public void checkZipDirectory(String zipDirectory) {
1757 Path path = Paths.get(zipDirectory);
1759 if (Files.exists(path)) {
1760 cleanUp(zipDirectory);
1764 private List<String> createList() {
1765 List<String> list = new ArrayList<>();
1766 for (Entry<String, MSAttributeObject> entrySet : classMap.entrySet()) {
1767 if (entrySet.getValue().isPolicyTempalate()) {
1768 list.add(entrySet.getKey());
1773 if (list.isEmpty()) {
1774 if (classMap.containsKey(this.newModel.getModelName())) {
1775 list.add(this.newModel.getModelName());
1783 public Map<String, String> getAttributesListRefMap() {
1784 return attributesListRefMap;
1787 public Map<String, LinkedList<String>> getArrayTextList() {
1788 return arrayTextList;
1791 public Map<String, String> getSigRules() {
1795 public void setSigRules(Map<String, String> sigRules) {
1796 this.sigRules = sigRules;
1804 class DCAEMicroServiceObject {
1806 private String service;
1807 private String location;
1808 private String uuid;
1809 private String policyName;
1810 private String description;
1811 private String configName;
1812 private String templateVersion;
1813 private String version;
1814 private String priority;
1815 private String policyScope;
1816 private String riskType;
1817 private String riskLevel;
1818 private String guard = null;
1819 private Object uiContent;
1820 private Object content;