c5e29e4222640dc2626ffec92c0c7d27fa48c6ce
[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-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
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
34 import java.io.BufferedInputStream;
35 import java.io.BufferedOutputStream;
36 import java.io.File;
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;
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
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;
72 import lombok.Getter;
73 import lombok.Setter;
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;
81
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;
110
111 @Controller
112 @RequestMapping("/")
113 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
114     private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
115
116     private static CommonClassDao commonClassDao;
117
118     public static CommonClassDao getCommonClassDao() {
119         return commonClassDao;
120     }
121
122     public static void setCommonClassDao(CommonClassDao commonClassDao) {
123         CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
124     }
125
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;
136
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";
149
150     @Autowired
151     private CreateDcaeMicroServiceController(CommonClassDao commonClassDao) {
152         CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
153     }
154
155     public CreateDcaeMicroServiceController() {
156         // Empty Constructor
157     }
158
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<>();
164
165     /**
166      * setDataToPolicyRestAdapter.
167      *
168      * @param policyData PolicyRestAdapter
169      * @param root JsonNode
170      * @return PolicyRestAdapter
171      */
172     public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
173
174         String jsonContent = null;
175         try {
176             LOGGER.info("policyJSON :" + (root.get(POLICYJSON)).toString());
177
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();
182             }
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);
186             tempJson =
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);
194         }
195
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());
204         }
205
206         return policyData;
207     }
208
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");
217             }
218
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");
224             }
225
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);
230                 }
231             }
232         }
233
234         return jsonObj;
235     }
236
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);
244             }
245
246             // for nested objects iteration if required
247             if (keyvalue instanceof JSONObject) {
248                 setOriginalJsonObject((JSONObject) keyvalue, jsonStringValues);
249                 jsonObj.put(keyStr, originalValue);
250             }
251
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);
256                 }
257             }
258         }
259
260         return jsonObj;
261     }
262
263     private GroupPolicyScopeList getPolicyObject(String policyScope) {
264         return (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
265     }
266
267     private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue)
268             throws IOException {
269         ObjectWriter om = new ObjectMapper().writer();
270         String json = "";
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());
278         }
279         if (returnModel.getAnnotation() == null || returnModel.getAnnotation().isEmpty()) {
280             if (policyAdapter.getUuid() != null) {
281                 microServiceObject.setUuid(policyAdapter.getUuid());
282             }
283             if (policyAdapter.getLocation() != null) {
284                 microServiceObject.setLocation(policyAdapter.getLocation());
285             }
286             if (policyAdapter.getConfigName() != null) {
287                 microServiceObject.setConfigName(policyAdapter.getConfigName());
288             }
289             GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
290             if (policyScopeValue != null) {
291                 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());
292             }
293         }
294
295         if (policyAdapter.getPolicyName() != null) {
296             microServiceObject.setPolicyName(policyAdapter.getPolicyName());
297         }
298         if (policyAdapter.getPolicyDescription() != null) {
299             microServiceObject.setDescription(policyAdapter.getPolicyDescription());
300         }
301         if (policyAdapter.getPriority() != null) {
302             microServiceObject.setPriority(policyAdapter.getPriority());
303         } else {
304             microServiceObject.setPriority("9999");
305         }
306
307         if (policyAdapter.getRiskLevel() != null) {
308             microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
309         }
310         if (policyAdapter.getRiskType() != null) {
311             microServiceObject.setRiskType(policyAdapter.getRiskType());
312         }
313         if (policyAdapter.getGuard() != null) {
314             microServiceObject.setGuard(policyAdapter.getGuard());
315         }
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);
327                 ruleCheck = true;
328             }
329         }
330         try {
331             json = om.writeValueAsString(microServiceObject);
332         } catch (JsonProcessingException e) {
333             LOGGER.error("Error writing out the object", e);
334         }
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
339         String tempJson =
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[] {""});
345         }
346         policyAdapter.setJsonBody(cleanJson);
347         // for Triggers
348         ObjectMapper mapper = new ObjectMapper();
349         JsonNode tempJsonNode = mapper.readTree(cleanJson);
350         if (ruleCheck) {
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);
365                         sb.append("(");
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);
371                         }
372                         sb.append(")").toString();
373                         putRuletoJson(tempJsonNode, i, sb, parseKey, primarykeyAlarm);
374                         sigRules = new HashMap<>();
375                     }
376                 } else {
377                     sb = new StringBuilder();
378                     parseData(triggerSig, parseKey);
379                 }
380             }
381             policyAdapter.setJsonBody(finalJson.toString());
382         }
383         return policyAdapter;
384     }
385
386     private JsonNode putRuletoJson(JsonNode tmpJsonNode, int item, StringBuilder sb, String parseKey,
387             String primaryKey) {
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);
400                 }
401                 String key = value.substring(0, value.indexOf('['));
402                 objectNode.remove(key);
403                 objectNode.put(key, sb.toString());
404                 return tmp;
405             } else {
406                 jsonNode = jsonNode.get(value);
407                 if (jsonNode instanceof ArrayNode) {
408                     tempRuleJsonNode = jsonNode;
409                     jsonNode = jsonNode.get(item);
410                 }
411             }
412         }
413         return tmp;
414     }
415
416     public boolean checkPattern(String patternString) {
417         return PATTERN.matcher(patternString).find();
418     }
419
420     /**
421      * Construct rule.
422      *
423      * @param sb the sb
424      * @param jsonNode the json node
425      * @param sigRules2 the sig rules 2
426      */
427     public void constructRule(StringBuilder sb, String jsonNode, Map<String, String> sigRules2) {
428         int count = 0;
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")) {
435             sb.append("(");
436             flag = true;
437         }
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);
443             } else {
444                 if ((count == 0) && (!("AND").equals(rowValue)) && (!("OR").equals(rowValue))) {
445                     sb.append("(");
446                 }
447                 count++;
448                 LOGGER.info(" " + rowValue + " ");
449                 sb.append(" " + rowValue + " ");
450                 if (count % 3 == 0) {
451                     sb.append(")");
452                     count = 0;
453                 }
454             }
455         }
456         if (flag) {
457             sb.append(")");
458         }
459     }
460
461     /**
462      * Parses the data.
463      *
464      * @param jsonNode the json node
465      * @param string the string
466      */
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);
475                 }
476             } else {
477                 parseData(signtures, subIndex);
478             }
479         } else {
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()));
488                 }
489                 sb.append(")");
490                 sigRules.put(key, sb.toString());
491             }
492         }
493     }
494
495     /**
496      * removeNullAttributes.
497      *
498      * @param cleanJson String
499      * @returnString
500      */
501     public String removeNullAttributes(String cleanJson) {
502         ObjectMapper mapper = new ObjectMapper();
503
504         try {
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;
522                         }
523                     }
524
525                     if (value == null || value.isNull()) {
526                         ((ObjectNode) returnNode).remove(key);
527                         remove = true;
528                     }
529                 }
530                 if (remove) {
531                     cleanJson = returnNode.toString();
532                 }
533                 if (value == null || value.isNull()) {
534                     ((ObjectNode) returnNode).remove(key);
535                     remove = true;
536                 }
537             }
538             if (remove) {
539                 cleanJson = returnNode.toString();
540             }
541
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());
547             }
548
549         } catch (IOException e) {
550             LOGGER.error("Error writing out the JsonNode", e);
551         }
552         return cleanJson;
553     }
554
555     /**
556      * To verify if it is a JSON string. If it is, then return its original value.
557      *
558      * @param key holds the values
559      * @return
560      */
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[] {"\\\""});
571                         }
572                     }
573                 }
574             }
575             if (k.endsWith(key)) {
576                 return StringUtils.replaceEach(jsonStringValues.get(k), new String[] {"\""}, new String[] {"\\\""});
577             }
578         }
579
580         return null;
581     }
582
583     /**
584      *  removeNull.
585      *
586      * @param array JsonArray
587      * @return JsonArray
588      */
589     public JsonArray removeNull(JsonArray array) {
590         JsonArrayBuilder builder = Json.createArrayBuilder();
591         int index = 0;
592         for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++index) {
593             JsonValue value = it.next();
594             switch (value.getValueType()) {
595                 case ARRAY:
596                     JsonArray tempArray = removeNull(array.getJsonArray(index));
597                     if (!tempArray.isEmpty()) {
598                         builder.add(tempArray);
599                     }
600                     break;
601                 case OBJECT:
602                     JsonObject object = removeNull(array.getJsonObject(index));
603                     if (!object.isEmpty()) {
604                         builder.add(object);
605                     }
606                     break;
607                 case STRING:
608                     String str = array.getString(index);
609                     if (str != null && !str.isEmpty()) {
610                         builder.add(str);
611                     }
612                     break;
613                 case NUMBER:
614                     builder.add(array.getJsonNumber(index));
615                     break;
616                 case TRUE:
617                 case FALSE:
618                     builder.add(array.getBoolean(index));
619                     break;
620                 case NULL:
621                 default:
622                     break;
623             }
624         }
625         return builder.build();
626     }
627
628     /**
629      * removeNull.
630      *
631      * @param obj JsonObject
632      * @return JsonObject
633      */
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()) {
641                 case ARRAY:
642                     JsonArray array = removeNull(obj.getJsonArray(key));
643                     if (!array.isEmpty()) {
644                         builder.add(key, array);
645                     }
646                     break;
647                 case OBJECT:
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());
654                                 break;
655                             }
656                         }
657                         builder.add(key, object);
658                     }
659                     break;
660                 case STRING:
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);
667                             }
668                         }
669                         builder.add(key, str);
670                     }
671                     break;
672                 case NUMBER:
673                     builder.add(key, obj.getJsonNumber(key));
674                     break;
675                 case TRUE:
676                 case FALSE:
677                     builder.add(key, obj.getBoolean(key));
678                     break;
679                 case NULL:
680                 default:
681                     break;
682             }
683         }
684         return builder.build();
685     }
686
687     /**
688      * cleanUpJson.
689      *
690      * @param json String
691      * @return String
692      */
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[] {"\\"});
697         cleanJson =
698                 StringUtils.replaceEach(cleanJson, new String[] {"\\\\", "[[", "]]"}, new String[] {"\\", "[", "]"});
699
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[] {"[", "]"});
711         return cleanJson;
712     }
713
714     /**
715      * decodeContent.
716      *
717      * @param jsonNode JsonNode
718      * @return JsonNode
719      */
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());
726         }
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;
736         String presKey;
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('@'));
744             } else {
745                 presKey = key;
746             }
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();
754                 }
755             } else if (jsonArray != null && jsonArray.length() > 0 && !presKey.equals(prevKey) && oldValue != null) {
756                 jsonResult.put(oldValue, jsonArray);
757                 isArray = false;
758                 jsonArray = new JSONArray();
759             }
760
761             prevKey = presKey;
762             //
763             if (key.contains(".")) {
764                 if (nodeKey == null) {
765                     nodeKey = key.substring(0, key.indexOf('.'));
766                 }
767                 if (nodeKey.equals(key.substring(0, key.indexOf('.')))) {
768                     node.put(key.substring(key.indexOf('.') + 1), value);
769                 } else {
770                     if (node.size() != 0) {
771                         if (nodeKey.contains("@")) {
772                             if (arryKey == null) {
773                                 arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
774                             }
775                             if (nodeKey.endsWith("@0")) {
776                                 isArray = true;
777                                 jsonArray = new JSONArray();
778                             }
779                             if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
780                                 jsonArray.put(decodeContent(node));
781                             }
782                             if ((key.contains("@") && !arryKey.equals(key.substring(0, nodeKey.indexOf('@'))))
783                                     || !key.contains("@")) {
784                                 jsonResult.put(arryKey, jsonArray);
785                                 jsonArray = new JSONArray();
786                             }
787                             arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
788                         } else {
789                             isArray = false;
790                             jsonResult.put(nodeKey, decodeContent(node));
791                         }
792                         node = nodeFactory.objectNode();
793                     }
794                     nodeKey = key.substring(0, key.indexOf('.'));
795                     if (nodeKey.contains("@")) {
796                         arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
797                     }
798                     node.put(key.substring(key.indexOf('.') + 1), value);
799                 }
800             } else {
801                 if (node.size() != 0) {
802                     if (nodeKey.contains("@")) {
803                         if (arryKey == null) {
804                             arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
805                         }
806                         if (nodeKey.endsWith("@0")) {
807                             isArray = true;
808                             jsonArray = new JSONArray();
809                         }
810                         if (jsonArray != null && arryKey.equals(nodeKey.substring(0, nodeKey.indexOf('@')))) {
811                             jsonArray.put(decodeContent(node));
812                         }
813                         jsonResult.put(arryKey, jsonArray);
814                         jsonArray = new JSONArray();
815                         arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
816                     } else {
817                         isArray = false;
818                         jsonResult.put(nodeKey, decodeContent(node));
819                     }
820                     node = nodeFactory.objectNode();
821                 }
822                 if (key.contains("@")) {
823                     isArray = true;
824                     if (key.endsWith("@0") || jsonArray == null) {
825                         jsonArray = new JSONArray();
826                     }
827                 } else if (!key.contains("@")) {
828                     isArray = false;
829                 }
830                 if (isArray) {
831                     if (oldValue == null) {
832                         oldValue = key.substring(0, key.indexOf('@'));
833                     }
834                     if (oldValue != prevKey) {
835                         oldValue = key.substring(0, key.indexOf('@'));
836                     }
837                     if (oldValue.equals(key.substring(0, key.indexOf('@')))) {
838                         jsonArray.put(value);
839                     } else {
840                         jsonResult.put(oldValue, jsonArray);
841                         jsonArray = new JSONArray();
842                     }
843                     oldValue = key.substring(0, key.indexOf('@'));
844                 } else {
845                     jsonResult.put(key, value);
846                 }
847             }
848         }
849         if (node.size() > 0) {
850             if (nodeKey.contains("@")) {
851                 if (jsonArray == null) {
852                     jsonArray = new JSONArray();
853                 }
854                 if (arryKey == null) {
855                     arryKey = nodeKey.substring(0, nodeKey.indexOf('@'));
856                 }
857                 jsonArray.put(decodeContent(node));
858                 jsonResult.put(arryKey, jsonArray);
859                 isArray = false;
860             } else {
861                 jsonResult.put(nodeKey, decodeContent(node));
862             }
863         }
864         if (isArray && jsonArray.length() > 0) {
865             jsonResult.put(oldValue, jsonArray);
866         }
867         return jsonResult;
868     }
869
870     @RequestMapping(
871             value = {"/policyController/getDCAEMSTemplateData.htm"},
872             method = {org.springframework.web.bind.annotation.RequestMethod.POST})
873     public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response)
874             throws IOException {
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());
880
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];
886         }
887         MicroServiceModels returnModel = getAttributeObject(servicename, version);
888
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());
898         }
899         String headDefautlsData = "";
900         if (jsonHdDefaultObj != null) {
901             headDefautlsData = jsonHdDefaultObj.toString();
902         } else {
903             headDefautlsData = "null";
904         }
905
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());
910             getAllKeys(json);
911             allkeys = allManyTrueKeys;
912             allManyTrueKeys = new TreeSet<>();
913             LOGGER.info("allkeys : " + allkeys);
914         }
915
916         // Get element order info
917         String dataOrderInfo = returnModel.getDataOrderInfo();
918         if (dataOrderInfo != null && !dataOrderInfo.startsWith("\"")) {
919             dataOrderInfo = "\"" + dataOrderInfo + "\"";
920         }
921         LOGGER.info("dataOrderInfo : " + dataOrderInfo);
922
923         String allMnyTrueKeys = "";
924         if (allkeys != null) {
925             allMnyTrueKeys = allkeys.toString();
926         }
927
928         String jsonModel = createMicroSeriveJson(returnModel, allkeys);
929
930         JSONObject jsonObject = new JSONObject(jsonModel);
931
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;
939             }
940         }
941
942         if (finalJsonObject != null) {
943             LOGGER.info(finalJsonObject.toString());
944             jsonModel = finalJsonObject.toString();
945         }
946
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) {
952                 LOGGER.info(s);
953                 // convert to array element for MANY-true properties
954                 finalJsonObject = convertToArrayElement(jsonObj, s.trim());
955                 jsonObj = finalJsonObject;
956             }
957
958             if (finalJsonObject != null) {
959                 LOGGER.info(finalJsonObject.toString());
960                 jsonModel = finalJsonObject.toString();
961             }
962         }
963
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);
969
970         JSONObject json = null;
971
972         if ("".equals(allMnyTrueKeys)) {
973             json = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",dataOrderInfo:"
974                     + dataOrderInfo + ",headDefautlsData:" + headDefautlsData + "}");
975         } else {
976             json = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel
977                     + ",allManyTrueKeys: " + allMnyTrueKeys + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:"
978                     + headDefautlsData + "}");
979         }
980         list.add(json);
981         response.getWriter().write(list.toString());
982         return null;
983     }
984
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();
992         }
993         String refAttribute = returnModel.getRef_attributes();
994         if (refAttribute != null) {
995             refAttribute = refAttribute.trim();
996         }
997         String enumAttribute = returnModel.getEnumValues();
998         if (enumAttribute != null) {
999             enumAttribute = enumAttribute.trim();
1000         }
1001         if (!StringUtils.isEmpty(attribute)) {
1002             attributeMap = convert(attribute, ",");
1003         }
1004         if (!StringUtils.isEmpty(refAttribute)) {
1005             refAttributeMap = convert(refAttribute, ",");
1006         }
1007
1008         Gson gson = new Gson();
1009
1010         String subAttributes = returnModel.getSub_attributes();
1011         if (subAttributes != null) {
1012             subAttributes = subAttributes.trim();
1013         } else {
1014             subAttributes = "";
1015         }
1016
1017         Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
1018
1019         JSONObject object = new JSONObject();
1020
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])) {
1025                 array.put(value);
1026                 object.put(keySet.getKey().trim(), array);
1027             } else {
1028                 object.put(keySet.getKey().trim(), value.trim());
1029             }
1030         }
1031
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);
1039                 } else {
1040                     object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
1041                 }
1042             } else {
1043                 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])) {
1044                     array.put(value.trim());
1045                     object.put(keySet.getKey().trim(), array);
1046                 } else {
1047                     object.put(keySet.getKey().trim(), value.trim());
1048                 }
1049             }
1050         }
1051
1052         return object.toString();
1053     }
1054
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;
1061         JSONArray array;
1062
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);
1070                 } else {
1071                     object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
1072                 }
1073             } else {
1074                 if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])) {
1075                     array.put(splitValue[0].trim());
1076                     object.put(m.getKey().trim(), array);
1077                 } else {
1078                     object.put(m.getKey().trim(), splitValue[0].trim());
1079                 }
1080             }
1081         }
1082
1083         return object;
1084     }
1085
1086     public JSONObject convertToArrayElement(JSONObject json, String keyValue) {
1087         return convertToArrayElement(json, new HashSet<>(), keyValue);
1088     }
1089
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);
1098                 }
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());
1104
1105                 return json;
1106             }
1107
1108             if (obj instanceof JSONObject) {
1109                 convertToArrayElement(json.getJSONObject(key), keyValue);
1110             }
1111
1112             if (obj instanceof JSONArray) {
1113                 convertToArrayElement(json.getJSONArray(key).getJSONObject(0), keyValue);
1114             }
1115         }
1116
1117         return json;
1118     }
1119
1120     /**
1121      * getManyTrueProperties.
1122      *
1123      * @param referAttributes String
1124      * @return a Set of String
1125      */
1126     public Set<String> getManyTrueProperties(String referAttributes) {
1127         LOGGER.info("referAttributes : " + referAttributes);
1128         Set<String> manyTrueProperties = new HashSet<>();
1129
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]);
1137                 }
1138             }
1139         }
1140
1141         return manyTrueProperties;
1142     }
1143
1144     // call this method to start the recursive
1145     private Set<String> getAllKeys(JSONObject json) {
1146         return getAllKeys(json, new HashSet<>());
1147     }
1148
1149     private Set<String> getAllKeys(JSONArray arr) {
1150         return getAllKeys(arr, new HashSet<>());
1151     }
1152
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)));
1158             }
1159             if (obj instanceof JSONArray) {
1160                 keys.addAll(getAllKeys(arr.getJSONArray(i)));
1161             }
1162         }
1163
1164         return keys;
1165     }
1166
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);
1175             }
1176             if (obj instanceof JSONObject) {
1177                 keys.addAll(getAllKeys(json.getJSONObject(key)));
1178             }
1179             if (obj instanceof JSONArray) {
1180                 keys.addAll(getAllKeys(json.getJSONArray(key)));
1181             }
1182         }
1183
1184         return keys;
1185     }
1186
1187     @RequestMapping(
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);
1194
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());
1204         return null;
1205     }
1206
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());
1215             } else {
1216                 list.add("Default");
1217             }
1218         }
1219         return list;
1220     }
1221
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;
1231                     }
1232                 } else {
1233                     return workingModel;
1234                 }
1235             } else {
1236                 return workingModel;
1237             }
1238
1239         }
1240         return workingModel;
1241     }
1242
1243     private MicroserviceHeaderdeFaults getHeaderDefaultsObject(String modelName) {
1244         return (MicroserviceHeaderdeFaults) commonClassDao.getEntityItem(MicroserviceHeaderdeFaults.class, "modelName",
1245                 modelName);
1246     }
1247
1248     @RequestMapping(
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) {
1253         try {
1254             Map<String, Object> model = new HashMap<>();
1255             ObjectMapper mapper = new ObjectMapper();
1256             List<String> priorityList = new ArrayList<>();
1257             priorityCount = 10;
1258             for (int i = 1; i < priorityCount; i++) {
1259                 priorityList.add(String.valueOf(i));
1260             }
1261             model.put("priorityDatas", mapper.writeValueAsString(priorityList));
1262             response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
1263         } catch (Exception e) {
1264             LOGGER.error(e);
1265         }
1266     }
1267
1268     /**
1269      * prePopulateDCAEMSPolicyData.
1270      *
1271      * @param policyAdapter PolicyRestAdapter
1272      * @param entity PolicyEntity
1273      */
1274     public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1275         if (! (policyAdapter.getPolicyData() instanceof PolicyType)) {
1276             return;
1277         }
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 = "";
1285         try {
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();
1290         }
1291         policyAdapter.setPolicyDescription(description);
1292         // Get the target data under policy.
1293         TargetType target = policy.getTarget();
1294         if (target == null) {
1295             return;
1296         }
1297         // Under target we have AnyOFType
1298         List<AnyOfType> anyOfList = target.getAnyOf();
1299         if (anyOfList == null) {
1300             return;
1301         }
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) {
1308                 continue;
1309             }
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) {
1316                     continue;
1317                 }
1318                 Iterator<MatchType> iterMatch = matchList.iterator();
1319                 while (matchList.size() > 1 && iterMatch.hasNext()) {
1320                     MatchType match = iterMatch.next();
1321                     //
1322                     // Under the match we have attribute value and
1323                     // attributeDesignator. So,finally down to the actual attribute.
1324                     //
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);
1332                     }
1333                     if ("ConfigName".equals(attributeId)) {
1334                         policyAdapter.setConfigName(value);
1335                     }
1336                     if ("uuid".equals(attributeId)) {
1337                         policyAdapter.setUuid(value);
1338                     }
1339                     if ("location".equals(attributeId)) {
1340                         policyAdapter.setLocation(value);
1341                     }
1342                     if ("RiskType".equals(attributeId)) {
1343                         policyAdapter.setRiskType(value);
1344                     }
1345                     if ("RiskLevel".equals(attributeId)) {
1346                         policyAdapter.setRiskLevel(value);
1347                     }
1348                     if ("guard".equals(attributeId)) {
1349                         policyAdapter.setGuard(value);
1350                     }
1351                     if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
1352                         PolicyController controller = new PolicyController();
1353                         String newDate = controller.convertDate(value);
1354                         policyAdapter.setTtlDate(newDate);
1355                     }
1356                 }
1357                 readFile(policyAdapter, entity);
1358             }
1359         }
1360     }
1361
1362     /**
1363      * Convert.
1364      *
1365      * @param str the str
1366      * @param split the split
1367      * @return the map
1368      */
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]);
1374         }
1375         return map;
1376     }
1377
1378     /**
1379      * Read file.
1380      *
1381      * @param policyAdapter the policy adapter
1382      * @param entity the entity
1383      */
1384     @SuppressWarnings("unchecked")
1385     public void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1386         try {
1387             DCAEMicroServiceObject msBody =
1388                     new ObjectMapper().readValue(entity.getConfigurationData().getConfigBody(),
1389                             DCAEMicroServiceObject.class);
1390             String policyScopeName = getPolicyScope(msBody.getPolicyScope());
1391             policyAdapter.setPolicyScope(policyScopeName);
1392
1393             policyAdapter.setPriority(msBody.getPriority());
1394
1395             if (msBody.getVersion() != null) {
1396                 policyAdapter.setServiceType(msBody.getService());
1397                 policyAdapter.setVersion(msBody.getVersion());
1398             } else {
1399                 policyAdapter.setServiceType(msBody.getService());
1400             }
1401             //
1402             LinkedHashMap<String, ?> content = (LinkedHashMap<String, ?>) msBody.getUiContent();
1403             if (content == null) {
1404                 content = (LinkedHashMap<String, ?>) msBody.getContent();
1405             }
1406             if (content != null) {
1407                 LinkedHashMap<String, Object> data = new LinkedHashMap<>();
1408                 LinkedHashMap<String, ?> map = content;
1409                 readRecursivlyJSONContent(map, data);
1410                 policyAdapter.setRuleData(data);
1411             }
1412
1413         } catch (Exception e) {
1414             LOGGER.error(e);
1415         }
1416
1417     }
1418
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());
1429                 }
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());
1439                         }
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());
1445                         }
1446                     } else {
1447                         data.put(key + "@" + i, arrayvalue.toString());
1448                     }
1449                 }
1450             } else {
1451                 data.put(key.toString(), value.toString());
1452             }
1453         }
1454     }
1455
1456     /**
1457      * getPolicyScope.
1458      *
1459      * @param value String
1460      * @return String
1461      */
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();
1466         }
1467         return null;
1468     }
1469
1470     /**
1471      * Convert the map values and set into JSON body.
1472      *
1473      * @param attributesMap Map of attributes
1474      * @param attributesRefMap Map of attribute references
1475      * @return Map
1476      */
1477     public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
1478         Map<String, String> attribute = new HashMap<>();
1479         StringBuilder temp;
1480         String key;
1481         String value;
1482         for (Entry<String, String> entry : attributesMap.entrySet()) {
1483             key = entry.getKey();
1484             value = entry.getValue();
1485             attribute.put(key, value);
1486         }
1487         for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
1488             key = entryRef.getKey();
1489             value = entryRef.getValue();
1490             attribute.put(key, value);
1491         }
1492         for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
1493             key = entryList.getKey();
1494             value = entryList.getValue();
1495             attribute.put(key, value);
1496         }
1497         for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()) {
1498             key = arrayList.getKey();
1499             temp = null;
1500             for (Object textList : arrayList.getValue()) {
1501                 if (temp == null) {
1502                     temp = new StringBuilder();
1503                     temp.append("[" + textList);
1504                 } else {
1505                     temp.append("," + textList);
1506                 }
1507             }
1508             attribute.put(key, temp + "]");
1509         }
1510
1511         return attribute;
1512     }
1513
1514     @RequestMapping(
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();
1529                 try {
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]);
1536
1537                     if (this.newFile.contains("-v")) {
1538                         if (item.getName().endsWith(".zip")) {
1539                             this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
1540                             zip = true;
1541                         } else if (item.getName().endsWith(".yml")) {
1542                             this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
1543                             yml = true;
1544                         } else {
1545                             this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
1546                         }
1547                     }
1548                 } catch (Exception e) {
1549                     LOGGER.error("Upload error : ", e);
1550                     errorMsg = "Upload error:" + e.getMessage();
1551                 }
1552             }
1553
1554         }
1555
1556         if (!errorMsg.isEmpty()) {
1557             response.setCharacterEncoding("UTF-8");
1558             response.setContentType("application / json");
1559             request.setCharacterEncoding("UTF-8");
1560
1561             JSONObject j = new JSONObject();
1562             j.put("errorMsg", errorMsg);
1563             response.getWriter().write(j.toString());
1564             return;
1565         }
1566
1567         List<File> fileList = new ArrayList<>();
1568         MSModelUtils msModelUtils = new MSModelUtils(commonClassDao);
1569         this.directory = "model";
1570         if (zip) {
1571             extractFolder(this.newFile);
1572             fileList = listModelFiles(this.directory);
1573         } else if (yml) {
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());
1582                 return;
1583             }
1584
1585         } else {
1586             File file = new File(this.newFile);
1587             fileList.add(file);
1588         }
1589         String modelType = "";
1590         if (!yml) {
1591             modelType = "xmi";
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);
1597                 }
1598             }
1599
1600             modelList = createList();
1601
1602             cleanUp(this.newFile);
1603             cleanUp(directory);
1604         } else {
1605             modelType = "yml";
1606             modelList.add(this.newModel.getModelName());
1607             String className = this.newModel.getModelName();
1608             MSAttributeObject msAttributes = new MSAttributeObject();
1609             msAttributes.setClassName(className);
1610
1611             LinkedHashMap<String, String> returnAttributeList = new LinkedHashMap<>();
1612             returnAttributeList.put(className, msModelUtils.getAttributeString());
1613             msAttributes.setAttribute(returnAttributeList);
1614
1615             msAttributes.setSubClass(msModelUtils.getRetmap());
1616
1617             msAttributes.setMatchingSet(msModelUtils.getMatchableValues());
1618
1619             LinkedHashMap<String, String> returnReferenceList = new LinkedHashMap<>();
1620
1621             returnReferenceList.put(className, msModelUtils.getReferenceAttributes());
1622             msAttributes.setRefAttribute(returnReferenceList);
1623
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]);
1631                     }
1632                 }
1633                 msAttributes.setEnumType(enumList);
1634             }
1635
1636             classMap = new LinkedHashMap<>();
1637             classMap.put(className, msAttributes);
1638
1639         }
1640         response.setCharacterEncoding("UTF-8");
1641         response.setContentType("application / json");
1642         request.setCharacterEncoding("UTF-8");
1643
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());
1651
1652         response.getWriter().write(json.toString());
1653     }
1654
1655     /*
1656      * Unzip file and store in the model directory for processing
1657      */
1658     @SuppressWarnings("rawtypes")
1659     private void extractFolder(String zipFile) {
1660         int buffer = 2048;
1661         File file = new File(zipFile);
1662
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();
1669
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();
1677
1678                 destinationParent.mkdirs();
1679
1680                 if (!entry.isDirectory()) {
1681                     BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
1682                     int currentByte;
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);
1688                         }
1689                         dest.flush();
1690                     } catch (IOException e) {
1691                         LOGGER.error("Failed to write zip contents to {}" + destFile + e);
1692                         //
1693                         // PLD should I throw e?
1694                         //
1695                         throw e;
1696                     }
1697                 }
1698
1699                 if (currentEntry.endsWith(".zip")) {
1700                     extractFolder(destFile.getAbsolutePath());
1701                 }
1702             }
1703         } catch (IOException e) {
1704             LOGGER.error("Failed to unzip model file " + zipFile, e);
1705         }
1706     }
1707
1708     private void retreiveDependency(String workingFile, Boolean modelClass) {
1709
1710         MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
1711         Map<String, MSAttributeObject> tempMap;
1712
1713         tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
1714
1715         classMap.putAll(tempMap);
1716         LOGGER.info(tempMap);
1717
1718         return;
1719
1720     }
1721
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()));
1731             }
1732         }
1733         return resultList;
1734     }
1735
1736     /**
1737      * cleanUp.
1738      *
1739      * @param path String
1740      */
1741     public void cleanUp(String path) {
1742         if (path != null) {
1743             try {
1744                 FileUtils.forceDelete(new File(path));
1745             } catch (IOException e) {
1746                 LOGGER.error("Failed to delete folder " + path, e);
1747             }
1748         }
1749     }
1750
1751     /**
1752      * checkZipDirectory.
1753      *
1754      * @param zipDirectory String
1755      */
1756     public void checkZipDirectory(String zipDirectory) {
1757         Path path = Paths.get(zipDirectory);
1758
1759         if (Files.exists(path)) {
1760             cleanUp(zipDirectory);
1761         }
1762     }
1763
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());
1769             }
1770
1771         }
1772
1773         if (list.isEmpty()) {
1774             if (classMap.containsKey(this.newModel.getModelName())) {
1775                 list.add(this.newModel.getModelName());
1776             } else {
1777                 list.add("EMPTY");
1778             }
1779         }
1780         return list;
1781     }
1782
1783     public Map<String, String> getAttributesListRefMap() {
1784         return attributesListRefMap;
1785     }
1786
1787     public Map<String, LinkedList<String>> getArrayTextList() {
1788         return arrayTextList;
1789     }
1790
1791     public Map<String, String> getSigRules() {
1792         return sigRules;
1793     }
1794
1795     public void setSigRules(Map<String, String> sigRules) {
1796         this.sigRules = sigRules;
1797     }
1798
1799 }
1800
1801
1802 @Getter
1803 @Setter
1804 class DCAEMicroServiceObject {
1805
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;
1821 }