Update css file name in conf.py
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreateDcaeMicroServiceController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controller;
22
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;
35 import java.io.File;
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;
53 import java.util.Map;
54 import java.util.Map.Entry;
55 import java.util.Set;
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;
71 import lombok.Getter;
72 import lombok.Setter;
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;
108
109 @Controller
110 @RequestMapping("/")
111 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
112     private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
113
114     private static CommonClassDao commonClassDao;
115
116     public static CommonClassDao getCommonClassDao() {
117         return commonClassDao;
118     }
119
120     public static void setCommonClassDao(CommonClassDao commonClassDao) {
121         CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
122     }
123
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;
134
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";
147
148     @Autowired
149     private CreateDcaeMicroServiceController(CommonClassDao commonClassDao) {
150         CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
151     }
152
153     public CreateDcaeMicroServiceController() {
154         // Empty Constructor
155     }
156
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<>();
162
163     /**
164      * setDataToPolicyRestAdapter.
165      *
166      * @param policyData PolicyRestAdapter
167      * @param root JsonNode
168      * @return PolicyRestAdapter
169      */
170     public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
171
172         String jsonContent = null;
173         try {
174             LOGGER.info("policyJSON :" + (root.get(POLICYJSON)).toString());
175
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);
182             tempJson =
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);
190         }
191
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()));
200         }
201
202         return policyData;
203     }
204
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);
214                 }
215             }
216             tempJson = saveOriginalJsonObject(policyJson, jsonStringValues).toString();
217         }
218         return tempJson;
219     }
220
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");
229             }
230
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");
236             }
237
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);
242                 }
243             }
244         }
245
246         return jsonObj;
247     }
248
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);
256             }
257
258             // for nested objects iteration if required
259             if (keyvalue instanceof JSONObject) {
260                 setOriginalJsonObject((JSONObject) keyvalue, jsonStringValues);
261                 jsonObj.put(keyStr, originalValue);
262             }
263
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);
268                 }
269             }
270         }
271
272         return jsonObj;
273     }
274
275     private GroupPolicyScopeList getPolicyObject(String policyScope) {
276         return (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
277     }
278
279     private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue)
280             throws IOException {
281         ObjectWriter om = new ObjectMapper().writer();
282         String json = "";
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());
290         }
291         if (returnModel.getAnnotation() == null || returnModel.getAnnotation().isEmpty()) {
292             if (policyAdapter.getUuid() != null) {
293                 microServiceObject.setUuid(policyAdapter.getUuid());
294             }
295             if (policyAdapter.getLocation() != null) {
296                 microServiceObject.setLocation(policyAdapter.getLocation());
297             }
298             if (policyAdapter.getConfigName() != null) {
299                 microServiceObject.setConfigName(policyAdapter.getConfigName());
300             }
301             GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
302             if (policyScopeValue != null) {
303                 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());
304             }
305         }
306
307         if (policyAdapter.getPolicyName() != null) {
308             microServiceObject.setPolicyName(policyAdapter.getPolicyName());
309         }
310         if (policyAdapter.getPolicyDescription() != null) {
311             microServiceObject.setDescription(policyAdapter.getPolicyDescription());
312         }
313         if (policyAdapter.getPriority() != null) {
314             microServiceObject.setPriority(policyAdapter.getPriority());
315         } else {
316             microServiceObject.setPriority("9999");
317         }
318
319         if (policyAdapter.getRiskLevel() != null) {
320             microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
321         }
322         if (policyAdapter.getRiskType() != null) {
323             microServiceObject.setRiskType(policyAdapter.getRiskType());
324         }
325         if (policyAdapter.getGuard() != null) {
326             microServiceObject.setGuard(policyAdapter.getGuard());
327         }
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);
339                 ruleCheck = true;
340             }
341         }
342         try {
343             json = om.writeValueAsString(microServiceObject);
344         } catch (JsonProcessingException e) {
345             LOGGER.error("Error writing out the object", e);
346         }
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
351         String tempJson =
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[] {""});
357         }
358         policyAdapter.setJsonBody(cleanJson);
359         // for Triggers
360         ObjectMapper mapper = new ObjectMapper();
361         JsonNode tempJsonNode = mapper.readTree(cleanJson);
362         if (! ruleCheck) {
363             return policyAdapter;
364         }
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);
379                     sb.append("(");
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);
385                     }
386                     sb.append(")").toString();
387                     putRuletoJson(tempJsonNode, i, sb, parseKey, primarykeyAlarm);
388                     sigRules = new HashMap<>();
389                 }
390             } else {
391                 parseData(triggerSig, parseKey);
392             }
393         }
394         policyAdapter.setJsonBody(finalJson.toString());
395         return policyAdapter;
396     }
397
398     private JsonNode putRuletoJson(JsonNode tmpJsonNode, int item, StringBuilder sb, String parseKey,
399             String primaryKey) {
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);
412                 }
413                 String key = value.substring(0, value.indexOf('['));
414                 objectNode.remove(key);
415                 objectNode.put(key, sb.toString());
416                 return tmp;
417             } else {
418                 jsonNode = jsonNode.get(value);
419                 if (jsonNode instanceof ArrayNode) {
420                     tempRuleJsonNode = jsonNode;
421                     jsonNode = jsonNode.get(item);
422                 }
423             }
424         }
425         return tmp;
426     }
427
428     public boolean checkPattern(String patternString) {
429         return PATTERN.matcher(patternString).find();
430     }
431
432     /**
433      * Construct rule.
434      *
435      * @param sb the sb
436      * @param jsonNode the json node
437      * @param sigRules2 the sig rules 2
438      */
439     public void constructRule(StringBuilder sb, String jsonNode, Map<String, String> sigRules2) {
440         int count = 0;
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")) {
447             sb.append("(");
448             flag = true;
449         }
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);
455             } else {
456                 if ((count == 0) && (!("AND").equals(rowValue)) && (!("OR").equals(rowValue))) {
457                     sb.append("(");
458                 }
459                 count++;
460                 LOGGER.info(" " + rowValue + " ");
461                 sb.append(" " + rowValue + " ");
462                 if (count % 3 == 0) {
463                     sb.append(")");
464                     count = 0;
465                 }
466             }
467         }
468         if (flag) {
469             sb.append(")");
470         }
471     }
472
473     /**
474      * Parses the data.
475      *
476      * @param jsonNode the json node
477      * @param string the string
478      */
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);
487                 }
488             } else {
489                 parseData(signtures, subIndex);
490             }
491         } else {
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()));
500                 }
501                 sb.append(")");
502                 sigRules.put(key, sb.toString());
503             }
504         }
505     }
506
507     /**
508      * removeNullAttributes.
509      *
510      * @param cleanJson String
511      * @returnString
512      */
513     public String removeNullAttributes(String cleanJson) {
514         ObjectMapper mapper = new ObjectMapper();
515
516         try {
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;
534                         }
535                     }
536
537                     if (value == null || value.isNull()) {
538                         ((ObjectNode) returnNode).remove(key);
539                         remove = true;
540                     }
541                 }
542                 if (remove) {
543                     cleanJson = returnNode.toString();
544                 }
545                 if (value == null || value.isNull()) {
546                     ((ObjectNode) returnNode).remove(key);
547                     remove = true;
548                 }
549             }
550             if (remove) {
551                 cleanJson = returnNode.toString();
552             }
553
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());
559             }
560
561         } catch (IOException e) {
562             LOGGER.error("Error writing out the JsonNode", e);
563         }
564         return cleanJson;
565     }
566
567     /**
568      * To verify if it is a JSON string. If it is, then return its original value.
569      *
570      * @param key holds the values
571      * @return
572      */
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);
582                         }
583                     }
584                 }
585             }
586             if (k.endsWith(key)) {
587                 return jsonStringValues.get(k);
588             }
589         }
590
591         return null;
592     }
593
594     /**
595      *  removeNull.
596      *
597      * @param array JsonArray
598      * @return JsonArray
599      */
600     public JsonArray removeNull(JsonArray array) {
601         JsonArrayBuilder builder = Json.createArrayBuilder();
602         int index = 0;
603         for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++index) {
604             JsonValue value = it.next();
605             switch (value.getValueType()) {
606                 case ARRAY:
607                     JsonArray tempArray = removeNull(array.getJsonArray(index));
608                     if (!tempArray.isEmpty()) {
609                         builder.add(tempArray);
610                     }
611                     break;
612                 case OBJECT:
613                     JsonObject object = removeNull(array.getJsonObject(index));
614                     if (!object.isEmpty()) {
615                         builder.add(object);
616                     }
617                     break;
618                 case STRING:
619                     String str = array.getString(index);
620                     if (str != null && !str.isEmpty()) {
621                         builder.add(str);
622                     }
623                     break;
624                 case NUMBER:
625                     builder.add(array.getJsonNumber(index));
626                     break;
627                 case TRUE:
628                 case FALSE:
629                     builder.add(array.getBoolean(index));
630                     break;
631                 case NULL:
632                 default:
633                     break;
634             }
635         }
636         return builder.build();
637     }
638
639     /**
640      * removeNull.
641      *
642      * @param obj JsonObject
643      * @return JsonObject
644      */
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()) {
652                 case ARRAY:
653                     JsonArray array = removeNull(obj.getJsonArray(key));
654                     if (!array.isEmpty()) {
655                         builder.add(key, array);
656                     }
657                     break;
658                 case OBJECT:
659                     JsonObject object = removeNull(obj.getJsonObject(key));
660                     if (!object.isEmpty()) {
661                         builder.add(key, object);
662                     }
663                     break;
664                 case STRING:
665                     String str = obj.getString(key);
666                     if (str != null && !str.isEmpty()) {
667                         builder.add(key, str);
668                     }
669                     break;
670                 case NUMBER:
671                     builder.add(key, obj.getJsonNumber(key));
672                     break;
673                 case TRUE:
674                 case FALSE:
675                     builder.add(key, obj.getBoolean(key));
676                     break;
677                 case NULL:
678                 default:
679                     break;
680             }
681         }
682         return builder.build();
683     }
684
685     /**
686      * cleanUpJson.
687      *
688      * @param json String
689      * @return String
690      */
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[] {"\\"});
695         cleanJson =
696                 StringUtils.replaceEach(cleanJson, new String[] {"\\\\", "[[", "]]"}, new String[] {"\\", "[", "]"});
697
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[] {"[", "]"});
709         return cleanJson;
710     }
711
712     /**
713      * decodeContent.
714      *
715      * @param jsonNode JsonNode
716      * @return JsonNode
717      */
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());
724         }
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;
734         String presKey;
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('@'));
742             } else {
743                 presKey = key;
744             }
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();
752                 }
753             } else if (jsonArray != null && jsonArray.length() > 0 && !presKey.equals(prevKey) && oldValue != null) {
754                 jsonResult.put(oldValue, jsonArray);
755                 isArray = false;
756                 jsonArray = new JSONArray();
757             }
758
759             prevKey = presKey;
760             //
761             if (key.contains(".")) {
762                 if (nodeKey == null) {
763                     nodeKey = key.substring(0, key.indexOf('.'));
764                 }
765                 if (nodeKey.equals(key.substring(0, key.indexOf('.')))) {
766                     node.put(key.substring(key.indexOf('.') + 1), value);
767                 } else {
768                     if (node.size() != 0) {
769                         if (nodeKey.contains("@")) {
770                             if (arryKey == null) {
771                                 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
772                             }
773                             if (nodeKey.endsWith("@0")) {
774                                 isArray = true;
775                                 jsonArray = new JSONArray();
776                             }
777                             if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
778                                 jsonArray.put(decodeContent(node));
779                             }
780                             if ((key.contains("@") && !arryKey.equals(key.substring(0, nodeKey.indexOf('@'))))
781                                     || !key.contains("@")) {
782                                 jsonResult.put(arryKey, jsonArray);
783                                 jsonArray = new JSONArray();
784                             }
785                             arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
786                         } else {
787                             isArray = false;
788                             jsonResult.put(nodeKey, decodeContent(node));
789                         }
790                         node = nodeFactory.objectNode();
791                     }
792                     nodeKey = key.substring(0, key.indexOf('.'));
793                     if (nodeKey.contains("@")) {
794                         arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
795                     }
796                     node.put(key.substring(key.indexOf('.') + 1), value);
797                 }
798             } else {
799                 if (node.size() != 0) {
800                     if (nodeKey.contains("@")) {
801                         if (arryKey == null) {
802                             arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
803                         }
804                         if (nodeKey.endsWith("@0")) {
805                             isArray = true;
806                             jsonArray = new JSONArray();
807                         }
808                         if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
809                             jsonArray.put(decodeContent(node));
810                         }
811                         jsonResult.put(arryKey, jsonArray);
812                         jsonArray = new JSONArray();
813                         arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
814                     } else {
815                         isArray = false;
816                         jsonResult.put(nodeKey, decodeContent(node));
817                     }
818                     node = nodeFactory.objectNode();
819                 }
820                 if (key.contains("@")) {
821                     isArray = true;
822                     if (key.endsWith("@0") || jsonArray == null) {
823                         jsonArray = new JSONArray();
824                     }
825                 } else if (!key.contains("@")) {
826                     isArray = false;
827                 }
828                 if (isArray) {
829                     if (oldValue == null) {
830                         oldValue = key.substring(0, key.indexOf('@'));
831                     }
832                     if (oldValue != prevKey) {
833                         oldValue = key.substring(0, key.indexOf('@'));
834                     }
835                     if (oldValue.equals(key.substring(0, key.indexOf('@')))) {
836                         jsonArray.put(value);
837                     } else {
838                         jsonResult.put(oldValue, jsonArray);
839                         jsonArray = new JSONArray();
840                     }
841                     oldValue = key.substring(0, key.indexOf('@'));
842                 } else {
843                     jsonResult.put(key, value);
844                 }
845             }
846         }
847         if (node.size() > 0) {
848             if (nodeKey.contains("@")) {
849                 if (jsonArray == null) {
850                     jsonArray = new JSONArray();
851                 }
852                 if (arryKey == null) {
853                     arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
854                 }
855                 jsonArray.put(decodeContent(node));
856                 jsonResult.put(arryKey, jsonArray);
857                 isArray = false;
858             } else {
859                 jsonResult.put(nodeKey, decodeContent(node));
860             }
861         }
862         if (isArray && jsonArray.length() > 0) {
863             jsonResult.put(oldValue, jsonArray);
864         }
865         return jsonResult;
866     }
867
868     @RequestMapping(
869             value = {"/policyController/getDCAEMSTemplateData.htm"},
870             method = {org.springframework.web.bind.annotation.RequestMethod.POST})
871     public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response)
872             throws IOException {
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());
878
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];
884         }
885         MicroServiceModels returnModel = getAttributeObject(servicename, version);
886
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());
896         }
897         String headDefautlsData = "";
898         if (jsonHdDefaultObj != null) {
899             headDefautlsData = jsonHdDefaultObj.toString();
900         } else {
901             headDefautlsData = "null";
902         }
903
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());
908             getAllKeys(json);
909             allkeys = allManyTrueKeys;
910             allManyTrueKeys = new TreeSet<>();
911             LOGGER.info("allkeys : " + allkeys);
912         }
913
914         // Get element order info
915         String dataOrderInfo = returnModel.getDataOrderInfo();
916         if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
917             dataOrderInfo = "\"" + dataOrderInfo + "\"";
918         }
919         LOGGER.info("dataOrderInfo : " + dataOrderInfo);
920
921         String allMnyTrueKeys = "";
922         if (allkeys != null) {
923             allMnyTrueKeys = allkeys.toString();
924         }
925
926         String jsonModel = createMicroSeriveJson(returnModel);
927
928         JSONObject jsonObject = new JSONObject(jsonModel);
929
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;
937             }
938         }
939
940         if (finalJsonObject != null) {
941             LOGGER.info(finalJsonObject.toString());
942             jsonModel = finalJsonObject.toString();
943         }
944
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) {
950                 LOGGER.info(s);
951                 // convert to array element for MANY-true properties
952                 finalJsonObject = convertToArrayElement(jsonObj, s.trim());
953                 jsonObj = finalJsonObject;
954             }
955
956             if (finalJsonObject != null) {
957                 LOGGER.info(finalJsonObject.toString());
958                 jsonModel = finalJsonObject.toString();
959             }
960         }
961
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);
967
968         JSONObject json = null;
969
970         if ("".equals(allMnyTrueKeys)) {
971             json = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",dataOrderInfo:"
972                     + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}");
973         } else {
974             json = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel
975                     + ",allManyTrueKeys: " + allMnyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:"
976                     + headDefautlsData + "}");
977         }
978         list.add(json);
979         response.getWriter().write(list.toString());
980         return null;
981     }
982
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();
990         }
991         String refAttribute = returnModel.getRefAttributes();
992         if (refAttribute != null) {
993             refAttribute = refAttribute.trim();
994         }
995         String enumAttribute = returnModel.getEnumValues();
996         if (enumAttribute != null) {
997             enumAttribute = enumAttribute.trim();
998         }
999         if (!StringUtils.isEmpty(attribute)) {
1000             attributeMap = convert(attribute, ",");
1001         }
1002         if (!StringUtils.isEmpty(refAttribute)) {
1003             refAttributeMap = convert(refAttribute, ",");
1004         }
1005
1006         Gson gson = new Gson();
1007
1008         String subAttributes = returnModel.getSubAttributes();
1009         if (subAttributes != null) {
1010             subAttributes = subAttributes.trim();
1011         } else {
1012             subAttributes = "";
1013         }
1014
1015         Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
1016
1017         JSONObject object = new JSONObject();
1018
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])) {
1023                 array.put(value);
1024                 object.put(keySet.getKey().trim(), array);
1025             } else {
1026                 object.put(keySet.getKey().trim(), value.trim());
1027             }
1028         }
1029
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);
1037                 } else {
1038                     object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
1039                 }
1040             } else {
1041                 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
1042                     array.put(value.trim());
1043                     object.put(keySet.getKey().trim(), array);
1044                 } else {
1045                     object.put(keySet.getKey().trim(), value.trim());
1046                 }
1047             }
1048         }
1049
1050         return object.toString();
1051     }
1052
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;
1059         JSONArray array;
1060
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);
1068                 } else {
1069                     object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
1070                 }
1071             } else {
1072                 if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])) {
1073                     array.put(splitValue[0].trim());
1074                     object.put(m.getKey().trim(), array);
1075                 } else {
1076                     object.put(m.getKey().trim(), splitValue[0].trim());
1077                 }
1078             }
1079         }
1080
1081         return object;
1082     }
1083
1084     public JSONObject convertToArrayElement(JSONObject json, String keyValue) {
1085         return convertToArrayElement(json, new HashSet<>(), keyValue);
1086     }
1087
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);
1096                 }
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());
1102
1103                 return json;
1104             }
1105
1106             if (obj instanceof JSONObject) {
1107                 convertToArrayElement(json.getJSONObject(key), keyValue);
1108             }
1109
1110             if (obj instanceof JSONArray) {
1111                 try {
1112                     if (json.getJSONArray(key).length() > 0) {
1113                             convertToArrayElement(json.getJSONArray(key).getJSONObject(0), keyValue);
1114                     }
1115                 } catch (Exception ex) {
1116                     LOGGER.info("XMI Model load issue : " + ex);
1117                 }
1118             }
1119         }
1120
1121         return json;
1122     }
1123
1124     /**
1125      * getManyTrueProperties.
1126      *
1127      * @param referAttributes String
1128      * @return a Set of String
1129      */
1130     public Set<String> getManyTrueProperties(String referAttributes) {
1131         LOGGER.info("referAttributes : " + referAttributes);
1132         Set<String> manyTrueProperties = new HashSet<>();
1133
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]);
1141                 }
1142             }
1143         }
1144
1145         return manyTrueProperties;
1146     }
1147
1148     // call this method to start the recursive
1149     private Set<String> getAllKeys(JSONObject json) {
1150         return getAllKeys(json, new HashSet<>());
1151     }
1152
1153     private Set<String> getAllKeys(JSONArray arr) {
1154         return getAllKeys(arr, new HashSet<>());
1155     }
1156
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)));
1162             }
1163             if (obj instanceof JSONArray) {
1164                 keys.addAll(getAllKeys(arr.getJSONArray(i)));
1165             }
1166         }
1167
1168         return keys;
1169     }
1170
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);
1179             }
1180             if (obj instanceof JSONObject) {
1181                 keys.addAll(getAllKeys(json.getJSONObject(key)));
1182             }
1183             if (obj instanceof JSONArray) {
1184                 keys.addAll(getAllKeys(json.getJSONArray(key)));
1185             }
1186         }
1187
1188         return keys;
1189     }
1190
1191     /**
1192      * getModelServiceVersionData.
1193      *
1194      * @param request HttpServletRequest
1195      * @param response HttpServletResponse
1196      * @return ModelAndView
1197      * @throws IOException IOException
1198      */
1199     @RequestMapping(
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);
1206
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());
1216         return null;
1217     }
1218
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());
1227             } else {
1228                 list.add("Default");
1229             }
1230         }
1231         return list;
1232     }
1233
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;
1243                     }
1244                 } else {
1245                     return workingModel;
1246                 }
1247             } else {
1248                 return workingModel;
1249             }
1250
1251         }
1252         return workingModel;
1253     }
1254
1255     private MicroserviceHeaderdeFaults getHeaderDefaultsObject(String modelName) {
1256         return (MicroserviceHeaderdeFaults) commonClassDao.getEntityItem(MicroserviceHeaderdeFaults.class, "modelName",
1257                 modelName);
1258     }
1259
1260     @RequestMapping(
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) {
1265         try {
1266             Map<String, Object> model = new HashMap<>();
1267             ObjectMapper mapper = new ObjectMapper();
1268             List<String> priorityList = new ArrayList<>();
1269             priorityCount = 10;
1270             for (int i = 1; i < priorityCount; i++) {
1271                 priorityList.add(String.valueOf(i));
1272             }
1273             model.put("priorityDatas", mapper.writeValueAsString(priorityList));
1274             response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
1275         } catch (Exception e) {
1276             LOGGER.error(e);
1277         }
1278     }
1279
1280     /**
1281      * prePopulateDCAEMSPolicyData.
1282      *
1283      * @param policyAdapter PolicyRestAdapter
1284      * @param entity PolicyEntity
1285      */
1286     public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1287         if (! (policyAdapter.getPolicyData() instanceof PolicyType)) {
1288             return;
1289         }
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 = "";
1297         try {
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();
1302         }
1303         policyAdapter.setPolicyDescription(description);
1304         // Get the target data under policy.
1305         TargetType target = policy.getTarget();
1306         if (target == null) {
1307             return;
1308         }
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) {
1314                     continue;
1315                 }
1316                 Iterator<MatchType> iterMatch = matchList.iterator();
1317                 //
1318                 // Can someone please explain why the matchList MUST have
1319                 // more than 1 matches???
1320                 //
1321                 while (matchList.size() > 1 && iterMatch.hasNext()) {
1322                     MatchType match = iterMatch.next();
1323                     //
1324                     // Under the match we have attribute value and
1325                     // attributeDesignator. So,finally down to the actual attribute.
1326                     //
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));
1330                 }
1331                 readFile(policyAdapter, entity);
1332             }
1333         }
1334     }
1335
1336     /**
1337      * Convert.
1338      *
1339      * @param str the str
1340      * @param split the split
1341      * @return the map
1342      */
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]);
1348         }
1349         return map;
1350     }
1351
1352     /**
1353      * Read file.
1354      *
1355      * @param policyAdapter the policy adapter
1356      * @param entity the entity
1357      */
1358     @SuppressWarnings("unchecked")
1359     public void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1360         try {
1361             DCAEMicroServiceObject msBody =
1362                     new ObjectMapper().readValue(entity.getConfigurationData().getConfigBody(),
1363                             DCAEMicroServiceObject.class);
1364             String policyScopeName = getPolicyScope(msBody.getPolicyScope());
1365             policyAdapter.setPolicyScope(policyScopeName);
1366
1367             policyAdapter.setPriority(msBody.getPriority());
1368
1369             if (msBody.getVersion() != null) {
1370                 policyAdapter.setServiceType(msBody.getService());
1371                 policyAdapter.setVersion(msBody.getVersion());
1372             } else {
1373                 policyAdapter.setServiceType(msBody.getService());
1374             }
1375             //
1376             LinkedHashMap<String, ?> content = (LinkedHashMap<String, ?>) msBody.getUiContent();
1377             if (content == null) {
1378                 content = (LinkedHashMap<String, ?>) msBody.getContent();
1379             }
1380             if (content != null) {
1381                 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
1382                 LinkedHashMap<String, ?> map = content;
1383                 readRecursivlyJSONContent(map, data);
1384                 policyAdapter.setRuleData(data);
1385             }
1386
1387         } catch (Exception e) {
1388             LOGGER.error(e);
1389         }
1390
1391     }
1392
1393     /**
1394      * readRecursivlyJSONContent.
1395      *
1396      * @param map Map of String to something
1397      * @param data Map of String to Object
1398      */
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());
1409                 }
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());
1419                         }
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());
1425                         }
1426                     } else {
1427                         data.put(key + "@" + i, arrayvalue.toString());
1428                     }
1429                 }
1430             } else {
1431                 data.put(key.toString(), value.toString());
1432             }
1433         }
1434     }
1435
1436     /**
1437      * getPolicyScope.
1438      *
1439      * @param value String
1440      * @return String
1441      */
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();
1446         }
1447         return null;
1448     }
1449
1450     /**
1451      * Convert the map values and set into JSON body.
1452      *
1453      * @param attributesMap Map of attributes
1454      * @param attributesRefMap Map of attribute references
1455      * @return Map
1456      */
1457     public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
1458         Map<String, String> attribute = new HashMap<>();
1459         StringBuilder temp;
1460         String key;
1461         String value;
1462         for (Entry<String, String> entry : attributesMap.entrySet()) {
1463             key = entry.getKey();
1464             value = entry.getValue();
1465             attribute.put(key, value);
1466         }
1467         for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
1468             key = entryRef.getKey();
1469             value = entryRef.getValue();
1470             attribute.put(key, value);
1471         }
1472         for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
1473             key = entryList.getKey();
1474             value = entryList.getValue();
1475             attribute.put(key, value);
1476         }
1477         for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()) {
1478             key = arrayList.getKey();
1479             temp = null;
1480             for (Object textList : arrayList.getValue()) {
1481                 if (temp == null) {
1482                     temp = new StringBuilder();
1483                     temp.append("[" + textList);
1484                 } else {
1485                     temp.append("," + textList);
1486                 }
1487             }
1488             attribute.put(key, temp + "]");
1489         }
1490
1491         return attribute;
1492     }
1493
1494     @RequestMapping(
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();
1509                 try {
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]);
1516
1517                     if (this.newFile.contains("-v")) {
1518                         if (item.getName().endsWith(".zip")) {
1519                             this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
1520                             zip = true;
1521                         } else if (item.getName().endsWith(".yml")) {
1522                             this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
1523                             yml = true;
1524                         } else {
1525                             this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
1526                         }
1527                     }
1528                 } catch (Exception e) {
1529                     LOGGER.error("Upload error : ", e);
1530                     errorMsg = "Upload error:" + e.getMessage();
1531                 }
1532             }
1533
1534         }
1535
1536         if (!errorMsg.isEmpty()) {
1537             response.setCharacterEncoding("UTF-8");
1538             response.setContentType("application / json");
1539             request.setCharacterEncoding("UTF-8");
1540
1541             JSONObject json = new JSONObject();
1542             json.put("errorMsg", errorMsg);
1543             response.getWriter().write(json.toString());
1544             return;
1545         }
1546
1547         List<File> fileList = new ArrayList<>();
1548         MsModelUtils msModelUtils = new MsModelUtils(commonClassDao);
1549         this.directory = "model";
1550         if (zip) {
1551             extractFolder(this.newFile);
1552             fileList = listModelFiles(this.directory);
1553         } else if (yml) {
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());
1562                 return;
1563             }
1564
1565         } else {
1566             File file = new File(this.newFile);
1567             fileList.add(file);
1568         }
1569         String modelType = "";
1570         if (!yml) {
1571             modelType = "xmi";
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());
1577                 }
1578             }
1579
1580             modelList = createList();
1581
1582             cleanUp(this.newFile);
1583             cleanUp(directory);
1584         } else {
1585             modelType = "yml";
1586             modelList.add(this.newModel.getModelName());
1587             String className = this.newModel.getModelName();
1588             MsAttributeObject msAttributes = new MsAttributeObject();
1589             msAttributes.setClassName(className);
1590
1591             LinkedHashMap<String, String> returnAttributeList = new LinkedHashMap<>();
1592             returnAttributeList.put(className, msModelUtils.getAttributeString());
1593             msAttributes.setAttribute(returnAttributeList);
1594
1595             msAttributes.setSubClass(msModelUtils.getRetmap());
1596
1597             msAttributes.setMatchingSet(msModelUtils.getMatchableValues());
1598
1599             LinkedHashMap<String, String> returnReferenceList = new LinkedHashMap<>();
1600
1601             returnReferenceList.put(className, msModelUtils.getReferenceAttributes());
1602             msAttributes.setRefAttribute(returnReferenceList);
1603
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]);
1611                     }
1612                 }
1613                 msAttributes.setEnumType(enumList);
1614             }
1615
1616             classMap = new LinkedHashMap<>();
1617             classMap.put(className, msAttributes);
1618
1619         }
1620         response.setCharacterEncoding("UTF-8");
1621         response.setContentType("application / json");
1622         request.setCharacterEncoding("UTF-8");
1623
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());
1631
1632         response.getWriter().write(json.toString());
1633     }
1634
1635     /*
1636      * Unzip file and store in the model directory for processing
1637      */
1638     @SuppressWarnings("rawtypes")
1639     private void extractFolder(String zipFile) {
1640         final int buffer = 2048;
1641         File file = new File(zipFile);
1642
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();
1649
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();
1657
1658                 destinationParent.mkdirs();
1659
1660                 if (!entry.isDirectory()) {
1661                     BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
1662                     int currentByte;
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);
1668                         }
1669                         dest.flush();
1670                     } catch (IOException e) {
1671                         LOGGER.error("Failed to write zip contents to {}" + destFile + e);
1672                         //
1673                         // PLD should I throw e?
1674                         //
1675                         throw e;
1676                     }
1677                 }
1678
1679                 if (currentEntry.endsWith(".zip")) {
1680                     extractFolder(destFile.getAbsolutePath());
1681                 }
1682             }
1683         } catch (IOException e) {
1684             LOGGER.error("Failed to unzip model file " + zipFile, e);
1685         }
1686     }
1687
1688     private void retrieveDependency(String workingFile) {
1689
1690         MsModelUtils utils = new MsModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
1691         Map<String, MsAttributeObject> tempMap;
1692
1693         tempMap = utils.processEpackage(workingFile, ModelType.XMI);
1694
1695         classMap.putAll(tempMap);
1696         LOGGER.info(tempMap);
1697     }
1698
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()));
1708             }
1709         }
1710         return resultList;
1711     }
1712
1713     /**
1714      * cleanUp.
1715      *
1716      * @param path String
1717      */
1718     public void cleanUp(String path) {
1719         if (path != null) {
1720             try {
1721                 FileUtils.forceDelete(new File(path));
1722             } catch (IOException e) {
1723                 LOGGER.error("Failed to delete folder " + path, e);
1724             }
1725         }
1726     }
1727
1728     /**
1729      * checkZipDirectory.
1730      *
1731      * @param zipDirectory String
1732      */
1733     public void checkZipDirectory(String zipDirectory) {
1734         Path path = Paths.get(zipDirectory);
1735
1736         if (Files.exists(path)) {
1737             cleanUp(zipDirectory);
1738         }
1739     }
1740
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());
1746             }
1747
1748         }
1749
1750         if (list.isEmpty()) {
1751             if (classMap.containsKey(this.newModel.getModelName())) {
1752                 list.add(this.newModel.getModelName());
1753             } else {
1754                 list.add("EMPTY");
1755             }
1756         }
1757         return list;
1758     }
1759
1760     public Map<String, String> getAttributesListRefMap() {
1761         return attributesListRefMap;
1762     }
1763
1764     public Map<String, LinkedList<String>> getArrayTextList() {
1765         return arrayTextList;
1766     }
1767
1768     public Map<String, String> getSigRules() {
1769         return sigRules;
1770     }
1771
1772     public void setSigRules(Map<String, String> sigRules) {
1773         this.sigRules = sigRules;
1774     }
1775
1776 }
1777
1778 @Getter
1779 @Setter
1780 class DCAEMicroServiceObject {
1781
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;
1797 }