Merge "Remove exclusion of dme"
[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-2018 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
24 import java.io.BufferedInputStream;
25 import java.io.BufferedOutputStream;
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.OutputStream;
30 import java.io.PrintWriter;
31 import java.io.StringReader;
32 import java.nio.file.Files;
33 import java.nio.file.Path;
34 import java.nio.file.Paths;
35 import java.util.ArrayList;
36 import java.util.Enumeration;
37 import java.util.HashMap;
38 import java.util.HashSet;
39 import java.util.Iterator;
40 import java.util.LinkedHashMap;
41 import java.util.LinkedList;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Map.Entry;
45 import java.util.Set;
46 import java.util.TreeMap;
47 import java.util.UUID;
48 import java.util.zip.ZipEntry;
49 import java.util.zip.ZipFile;
50
51 import javax.json.Json;
52 import javax.json.JsonArray;
53 import javax.json.JsonArrayBuilder;
54 import javax.json.JsonObject;
55 import javax.json.JsonObjectBuilder;
56 import javax.json.JsonReader;
57 import javax.json.JsonValue;
58 import javax.servlet.http.HttpServletRequest;
59 import javax.servlet.http.HttpServletResponse;
60
61 import org.apache.commons.compress.utils.IOUtils;
62 import org.apache.commons.fileupload.FileItem;
63 import org.apache.commons.fileupload.FileUploadException;
64 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
65 import org.apache.commons.fileupload.servlet.ServletFileUpload;
66 import org.apache.commons.io.FileUtils;
67 import org.apache.commons.lang.StringUtils;
68 import org.json.JSONArray;
69 import org.json.JSONObject;
70 import org.onap.policy.common.logging.flexlogger.FlexLogger;
71 import org.onap.policy.common.logging.flexlogger.Logger;
72 import org.onap.policy.rest.XACMLRestProperties;
73 import org.onap.policy.rest.adapter.PolicyRestAdapter;
74 import org.onap.policy.rest.dao.CommonClassDao;
75 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
76 import org.onap.policy.rest.jpa.MicroServiceModels;
77 import org.onap.policy.rest.jpa.MicroserviceHeaderdeFaults;
78 import org.onap.policy.rest.jpa.PolicyEntity;
79 import org.onap.policy.rest.util.MSAttributeObject;
80 import org.onap.policy.rest.util.MSModelUtils;
81 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
82 import org.onap.portalsdk.core.controller.RestrictedBaseController;
83 import org.onap.portalsdk.core.web.support.JsonMessage;
84 import org.springframework.beans.factory.annotation.Autowired;
85 import org.springframework.http.MediaType;
86 import org.springframework.stereotype.Controller;
87 import org.springframework.web.bind.annotation.RequestMapping;
88 import org.springframework.web.servlet.ModelAndView;
89 import com.att.research.xacml.util.XACMLProperties;
90 import com.fasterxml.jackson.core.JsonProcessingException;
91 import com.fasterxml.jackson.databind.DeserializationFeature;
92 import com.fasterxml.jackson.databind.JsonNode;
93 import com.fasterxml.jackson.databind.ObjectMapper;
94 import com.fasterxml.jackson.databind.ObjectWriter;
95 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
96 import com.fasterxml.jackson.databind.node.ObjectNode;
97 import com.google.gson.Gson;
98
99 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
100 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
101 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
102 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
103 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
104 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
105 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
106
107 @Controller
108 @RequestMapping("/")
109 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
110         private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
111
112         private static CommonClassDao commonClassDao;
113         
114         public static CommonClassDao getCommonClassDao() {
115                 return commonClassDao;
116         }
117
118         public static void setCommonClassDao(CommonClassDao commonClassDao) {
119                 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
120         }
121
122         private MicroServiceModels newModel;
123         private String newFile;
124         private String directory;
125         private List<String> modelList = new ArrayList<>();
126         private List<String> dirDependencyList = new ArrayList<>();
127         private LinkedHashMap<String,MSAttributeObject > classMap = new LinkedHashMap<>();
128         String referenceAttributes;
129         String attributeString;
130         Set<String> allManyTrueKeys= new HashSet <>();
131
132         public static final String DATATYPE  = "data_types.policy.data.";
133         public static final String PROPERTIES=".properties.";
134         public static final String TYPE=".type";
135         public static final String STRING="string";
136         public static final String INTEGER="integer";
137         public static final String LIST="list";
138         public static final String DEFAULT=".default";
139         public static final String REQUIRED=".required";
140         public static final String MATCHABLE=".matchable";
141         public static final String MANYFALSE=":MANY-false";
142         
143         
144         @Autowired
145         private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){
146                 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
147         }
148
149         public CreateDcaeMicroServiceController(){
150                 // Empty Constructor
151         }
152
153         protected PolicyRestAdapter policyAdapter = null;
154         private int priorityCount; 
155         private Map<String, String> attributesListRefMap =  new HashMap<>();
156         private Map<String, LinkedList<String>> arrayTextList =  new HashMap<>();
157
158         public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
159                 
160                 String jsonContent = null;
161                 try{
162                         LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
163                         
164                         String tempJson = root.get("policyJSON").toString();
165                         
166                         //---replace empty value with the value below before calling decodeContent method.
167                         String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
168                         LOGGER.info("dummyValue:" + dummyValue);
169                         tempJson = StringUtils.replaceEach(tempJson, new String[]{"\"\""}, new String[]{"\""+dummyValue+"\""});
170                         ObjectMapper mapper = new ObjectMapper();
171                         JsonNode tempJsonNode = mapper.readTree(tempJson);
172                         jsonContent = decodeContent(tempJsonNode).toString();
173                         constructJson(policyData, jsonContent, dummyValue);
174                 }catch(Exception e){
175                         LOGGER.error("Error while decoding microservice content", e);
176                 }
177                 
178                 return policyData;
179         }
180         
181         private GroupPolicyScopeList getPolicyObject(String policyScope) {
182                 return (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
183         }
184         
185         private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
186                 ObjectWriter om = new ObjectMapper().writer();
187                 String json="";
188                 DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
189                 MicroServiceModels returnModel = new MicroServiceModels();
190                 microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_MS));
191                 if(policyAdapter.getServiceType() !=null){
192                         microServiceObject.setService(policyAdapter.getServiceType());
193                         microServiceObject.setVersion(policyAdapter.getVersion());
194                         returnModel = getAttributeObject(microServiceObject.getService(), microServiceObject.getVersion());
195                 }
196                 if (returnModel.getAnnotation()==null || returnModel.getAnnotation().isEmpty()){
197                         if(policyAdapter.getUuid()!=null){
198                                 microServiceObject.setUuid(policyAdapter.getUuid());
199                         }
200                         if(policyAdapter.getLocation()!=null){
201                                 microServiceObject.setLocation(policyAdapter.getLocation());
202                         } 
203                         if(policyAdapter.getConfigName()!=null){
204                                 microServiceObject.setConfigName(policyAdapter.getConfigName());
205                         }
206                         GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
207                         if(policyScopeValue!=null){
208                                 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());     
209                         }
210                 }
211                 
212                 if(policyAdapter.getPolicyName()!=null){
213                         microServiceObject.setPolicyName(policyAdapter.getPolicyName());
214                 }
215                 if(policyAdapter.getPolicyDescription()!=null){
216                         microServiceObject.setDescription(policyAdapter.getPolicyDescription());
217                 }
218                 if (policyAdapter.getPriority()!=null){
219                         microServiceObject.setPriority(policyAdapter.getPriority());
220                 }else {
221                         microServiceObject.setPriority("9999");
222                 }
223                 
224                 if (policyAdapter.getRiskLevel()!=null){
225                         microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
226                 }
227                 if (policyAdapter.getRiskType()!=null){
228                         microServiceObject.setRiskType(policyAdapter.getRiskType());
229                 }
230                 if (policyAdapter.getGuard()!=null){
231                         microServiceObject.setGuard(policyAdapter.getGuard());
232                 }
233                 microServiceObject.setContent(jsonContent);
234                 
235                 try {
236                         json = om.writeValueAsString(microServiceObject);
237                 } catch (JsonProcessingException e) {
238                         LOGGER.error("Error writing out the object", e);
239                 }
240                 LOGGER.info("input json: " + json);
241                 LOGGER.info("input jsonContent: " + jsonContent);
242                 String cleanJson = cleanUPJson(json);
243         //--- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
244                 String tempJson = StringUtils.replaceEach(cleanJson, new String[]{"\""+dummyValue+"\""},  new String[]{"\"\""});
245                 LOGGER.info("tempJson: " + tempJson);
246                 cleanJson = removeNullAttributes(tempJson);
247                 policyAdapter.setJsonBody(cleanJson);
248                 return policyAdapter;
249         }
250         
251         public String removeNullAttributes(String cleanJson) {
252                 ObjectMapper mapper = new ObjectMapper();
253
254                 try {
255                         JsonNode rootNode = mapper.readTree(cleanJson);
256                         JsonNode returnNode = mapper.readTree(cleanJson);
257                         Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
258                         boolean remove = false;
259                         JsonObject removed = null;
260                         boolean contentChanged = false;
261                         while (fieldsIterator.hasNext()) {
262                                 Map.Entry<String, JsonNode> field = fieldsIterator.next();
263                                 final String key = field.getKey();
264                                 final JsonNode value = field.getValue();
265                                 if("content".equalsIgnoreCase(key)){
266                                         String contentStr = value.toString();
267                                         try(JsonReader reader = Json.createReader(new StringReader(contentStr))){
268                                 JsonObject jsonContent = reader.readObject();                 
269                                                         removed = removeNull(jsonContent);
270                                                         if(!jsonContent.toString().equals(removed.toString())){
271                                                         contentChanged = true;  
272                                                 }
273                     }
274
275                                         if  (value==null || value.isNull()){
276                                                 ((ObjectNode) returnNode).remove(key);
277                                                 remove = true;
278                                         }
279                                 }
280                                 if (remove){
281                                         cleanJson = returnNode.toString();
282                                 }
283                                 if  (value==null || value.isNull()){
284                                         ((ObjectNode) returnNode).remove(key);
285                                         remove = true;
286                                 }
287                         }
288                         if (remove){
289                                 cleanJson = returnNode.toString();
290                         }
291                         
292                         if(contentChanged){
293                                 //set modified content to cleanJson
294                                 JSONObject  jObject  =  new JSONObject(cleanJson);      
295                                 jObject.put("content",removed.toString());
296                                 cleanJson = cleanUPJson(jObject.toString());
297                         }
298                         
299                 } catch (IOException e) {
300                         LOGGER.error("Error writing out the JsonNode",e);
301                 }
302                 return cleanJson;
303         }
304         
305         public static JsonArray removeNull(JsonArray array) {
306             JsonArrayBuilder builder = Json.createArrayBuilder();
307             int i = 0;
308             for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++i) {
309                 JsonValue value = it.next();
310                 switch (value.getValueType()) {
311                 case ARRAY:
312                     JsonArray a = removeNull(array.getJsonArray(i));
313                     if (!a.isEmpty())
314                         builder.add(a);
315                     break;
316                 case OBJECT:
317                     JsonObject object = removeNull(array.getJsonObject(i));
318                     if (!object.isEmpty())
319                         builder.add(object);
320                     break;
321                 case STRING:
322                     String s = array.getString(i);
323                     if (s != null && !s.isEmpty())
324                         builder.add(s);
325                     break;
326                 case NUMBER:
327                     builder.add(array.getJsonNumber(i));
328                     break;
329                 case TRUE:
330                 case FALSE:
331                     builder.add(array.getBoolean(i));
332                     break;
333                 case NULL:
334                     break;
335                 }
336             }
337             return builder.build();
338         }
339
340         public static JsonObject removeNull(JsonObject obj) {
341             JsonObjectBuilder builder = Json.createObjectBuilder();
342             for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
343                 Entry<String, JsonValue> e = it.next();
344                 String key = e.getKey();
345                 JsonValue value = e.getValue();
346                 switch (value.getValueType()) {
347                 case ARRAY:
348                     JsonArray array = removeNull(obj.getJsonArray(key));
349                     if (!array.isEmpty())
350                         builder.add(key, array);
351                     break;
352                 case OBJECT:
353                     JsonObject object = removeNull(obj.getJsonObject(key));
354                     if (!object.isEmpty())
355                         builder.add(key, object);
356                     break;
357                 case STRING:
358                     String s = obj.getString(key);
359                     if (s != null && !s.isEmpty())
360                         builder.add(key, s);
361                     break;
362                 case NUMBER:
363                     builder.add(key, obj.getJsonNumber(key));
364                     break;
365                 case TRUE:
366                 case FALSE:
367                     builder.add(key, obj.getBoolean(key));
368                     break;
369                 case NULL:
370                     break;
371                 }
372             }
373             return builder.build();
374         }
375         
376         public String cleanUPJson(String json) {
377                 String cleanJson = StringUtils.replaceEach(json, new String[]{"\\\\", "\\\\\\", "\\\\\\\\"}, new String[]{"\\", "\\", "\\"});
378                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\"}, new String[]{"\\"});
379                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\", "[[", "]]"}, new String[]{"\\", "[", "]"});
380                 
381                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\"", "\\\"", "\"[{", "}]\""}, new String[]{"\"", "\"", "[{", "}]"});
382                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[{", "}]\""}, new String[]{"[{", "}]"});
383                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
384                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"{", "}\""}, new String[]{"{", "}"});
385                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\"\"", "\"\""}, new String[]{"\"", "\""});
386                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\""}, new String[]{""});
387                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\""}, new String[]{"\""});
388                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\\\\\\"}, new String[]{"\""});
389                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\\""}, new String[]{"\""});
390                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
391                 return cleanJson;
392         }
393         
394         public JSONObject decodeContent(JsonNode jsonNode){
395                 Iterator<JsonNode> jsonElements = jsonNode.elements();
396                 Iterator<String> jsonKeys = jsonNode.fieldNames();
397                 Map<String,String> element = new TreeMap<>();
398                 while(jsonElements.hasNext() && jsonKeys.hasNext()){
399                         element.put(jsonKeys.next(), jsonElements.next().toString());
400                 }
401                 JSONObject jsonResult = new JSONObject();
402                 JSONArray jsonArray = null;
403                 String oldValue = null;
404                 String nodeKey = null;
405                 String arryKey = null;
406                 Boolean isArray = false;
407                 JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
408                 ObjectNode node = nodeFactory.objectNode();
409                 String prevKey = null;
410                 String presKey;
411                 for(Entry<String, String> entry: element.entrySet()){
412                         String key = entry.getKey();
413                         String value = entry.getValue();
414                         if(key.contains(".")){
415                                 presKey = key.substring(0,key.indexOf('.'));
416                         }else if(key.contains("@")){
417                                 presKey = key.substring(0,key.indexOf('@'));
418                         }else{
419                                 presKey = key;
420                         }
421                         // first check if we are different from old.
422                         LOGGER.info(key+"\n");
423                         if(jsonArray!=null && jsonArray.length()>0 && key.contains("@") && !key.contains(".") && oldValue!=null){
424                                 if(!oldValue.equals(key.substring(0,key.indexOf('@')))){
425                                         jsonResult.put(oldValue, jsonArray);
426                                         jsonArray = new JSONArray();
427                                 }
428                         }else if(jsonArray!=null && jsonArray.length()>0 && !presKey.equals(prevKey) && oldValue!=null){ 
429                                 jsonResult.put(oldValue, jsonArray);
430                                 isArray = false;
431                                 jsonArray = new JSONArray();
432                         }
433         
434                         prevKey = presKey;
435                         // 
436                         if(key.contains(".")){
437                                 if(nodeKey==null){
438                                         nodeKey = key.substring(0,key.indexOf('.'));
439                                 }
440                                 if(nodeKey.equals(key.substring(0,key.indexOf('.')))){
441                                         node.put(key.substring(key.indexOf('.')+1), value);
442                                 }else{
443                                         if(node.size()!=0){
444                                                 if(nodeKey.contains("@")){
445                                                         if(arryKey==null){
446                                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
447                                                         }
448                                                         if(nodeKey.endsWith("@0")){
449                                                                 isArray = true;
450                                                                 jsonArray = new JSONArray();
451                                                         }
452                                                         if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf('@')))){
453                                                                 jsonArray.put(decodeContent(node));
454                                                         } 
455                                                         if((key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf('@')))) || !key.contains("@")){
456                                                                 jsonResult.put(arryKey, jsonArray);
457                                                                 jsonArray = new JSONArray();
458                                                         }
459                                                         arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
460                                                 }else{
461                                                         isArray = false;
462                                                         jsonResult.put(nodeKey, decodeContent(node));
463                                                 }
464                                                 node = nodeFactory.objectNode();
465                                         }
466                                         nodeKey = key.substring(0,key.indexOf('.'));
467                                         if(nodeKey.contains("@")){
468                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
469                                         }
470                                         node.put(key.substring(key.indexOf('.')+1), value);
471                                 }
472                         }else {
473                                 if(node.size()!=0){
474                                         if(nodeKey.contains("@")){
475                                                 if(arryKey==null){
476                                                         arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
477                                                 }
478                                                 if(nodeKey.endsWith("@0")){
479                                                         isArray = true;
480                                                         jsonArray = new JSONArray();
481                                                 }
482                                                 if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf('@')))){
483                                                         jsonArray.put(decodeContent(node));
484                                                 }
485                                                 jsonResult.put(arryKey, jsonArray);
486                                                 jsonArray = new JSONArray();
487                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
488                                         }else{
489                                                 isArray = false;
490                                                 jsonResult.put(nodeKey, decodeContent(node));
491                                         }
492                                         node = nodeFactory.objectNode();
493                                 }
494                                 if(key.contains("@")){
495                                         isArray = true;
496                                         if(key.endsWith("@0")|| jsonArray==null){
497                                                 jsonArray = new JSONArray();
498                                         }
499                                 }else if(!key.contains("@")){
500                                         isArray = false;
501                                 }
502                                 if(isArray){
503                                         if(oldValue==null){
504                                                 oldValue = key.substring(0,key.indexOf('@'));
505                                         }
506                                         if(oldValue!=prevKey){
507                                                 oldValue = key.substring(0,key.indexOf('@'));
508                                         }
509                                         if(oldValue.equals(key.substring(0,key.indexOf('@')))){
510                                                 jsonArray.put(value);
511                                         }else{
512                                                 jsonResult.put(oldValue, jsonArray);
513                                                 jsonArray = new JSONArray();
514                                         }
515                                         oldValue = key.substring(0,key.indexOf('@'));
516                                 }else{
517                                         jsonResult.put(key, value);
518                                 }
519                         }
520                 }
521                 if(node.size()>0){
522                         if(nodeKey.contains("@")){
523                                 if(jsonArray==null){
524                                         jsonArray = new JSONArray();
525                                 }
526                                 if(arryKey==null){
527                                         arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
528                                 }
529                                 jsonArray.put(decodeContent(node));
530                                 jsonResult.put(arryKey, jsonArray);
531                                 isArray = false;
532                         }else{
533                                 jsonResult.put(nodeKey, decodeContent(node));
534                         }
535                 }
536                 if(isArray && jsonArray.length() > 0){
537                         jsonResult.put(oldValue, jsonArray);
538                 }
539                 return jsonResult;
540         }
541         
542         @RequestMapping(value={"/policyController/getDCAEMSTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
543         public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response) throws IOException{
544                 ObjectMapper mapper = new ObjectMapper();
545                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
546                 JsonNode root = mapper.readTree(request.getReader());
547
548                 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
549                 String  servicename = value.split("-v")[0];
550                 String version = null;
551                 if (value.contains("-v")){
552                         version = value.split("-v")[1];
553                 }
554                 MicroServiceModels returnModel = getAttributeObject(servicename, version);
555                 
556                 MicroserviceHeaderdeFaults returnHeaderDefauls = getHeaderDefaultsObject(value);
557                 JSONObject jsonHdDefaultObj = null;
558                 if(returnHeaderDefauls != null){
559                         jsonHdDefaultObj = new JSONObject();
560                         jsonHdDefaultObj.put("onapName", returnHeaderDefauls.getOnapName());
561                         jsonHdDefaultObj.put("guard", returnHeaderDefauls.getGuard());
562                         jsonHdDefaultObj.put("riskLevel", returnHeaderDefauls.getRiskLevel());
563                         jsonHdDefaultObj.put("riskType", returnHeaderDefauls.getRiskType());
564                         jsonHdDefaultObj.put("priority", returnHeaderDefauls.getPriority());
565                 }
566                 String headDefautlsData = "";
567                 if(jsonHdDefaultObj != null){                   
568                         headDefautlsData = jsonHdDefaultObj.toString();
569                 }else{
570                         headDefautlsData = "null";
571                 }
572                 
573                 //Get all keys with "MANY-true" defined in their value from subAttribute 
574                 Set<String> allkeys = null;
575                 if(returnModel.getSub_attributes() != null && !returnModel.getSub_attributes().isEmpty()){
576                         JSONObject json = new JSONObject(returnModel.getSub_attributes());      
577                         getAllKeys(json); 
578                         allkeys = allManyTrueKeys;
579                         allManyTrueKeys = new  HashSet <>();
580                         LOGGER.info("allkeys : " + allkeys);
581                 }
582                 
583                 //Get element order info 
584                 String dataOrderInfo = returnModel.getDataOrderInfo();
585         if(dataOrderInfo != null && !dataOrderInfo.startsWith("\"")){
586             dataOrderInfo = "\"" + dataOrderInfo + "\"";
587         }
588         LOGGER.info("dataOrderInfo : " + dataOrderInfo);
589         
590                 String allManyTrueKeys = "";
591                 if(allkeys != null){
592                         allManyTrueKeys = allkeys.toString();
593                 }
594                 
595                 String jsonModel = createMicroSeriveJson(returnModel, allkeys);
596                 
597                 JSONObject jsonObject = new JSONObject(jsonModel);
598                 
599                 JSONObject finalJsonObject = null;
600                 if(allkeys != null){
601                         Iterator<String> iter = allkeys.iterator();
602                         while(iter.hasNext()){
603                                 //Convert to array values for MANY-true keys
604                                 finalJsonObject = convertToArrayElement(jsonObject, iter.next());
605                         }
606                 }
607
608                 if(finalJsonObject != null){
609                     LOGGER.info(finalJsonObject.toString());
610                     jsonModel  = finalJsonObject.toString();
611                 }
612                 
613                 //get all properties with "MANY-true" defined in Ref_attributes
614                 Set<String> manyTrueProperties = getManyTrueProperties(returnModel.getRef_attributes());
615                 if(manyTrueProperties != null){
616                         JSONObject jsonObj = new JSONObject(jsonModel);
617                         for (String s : manyTrueProperties) {
618                                 LOGGER.info(s);
619                                 //convert to array element for MANY-true properties
620                                 finalJsonObject = convertToArrayElement(jsonObj, s.trim());
621                         }
622                         
623                         if(finalJsonObject != null){
624                             LOGGER.info(finalJsonObject.toString());
625                             jsonModel = finalJsonObject.toString();
626                         }
627                 }
628                 
629                 response.setCharacterEncoding("UTF-8");
630                 response.setContentType("application / json");
631                 request.setCharacterEncoding("UTF-8");
632                 List<Object>  list = new ArrayList<>();
633                 PrintWriter out = response.getWriter();
634                 String responseString = mapper.writeValueAsString(returnModel);
635
636                 JSONObject j = null;
637                 
638                 if("".equals(allManyTrueKeys)){
639                         j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData +"}");        
640                 }else{
641                         j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + allManyTrueKeys+",dataOrderInfo:" + dataOrderInfo + ",headDefautlsData:" + headDefautlsData+ "}"); 
642                 }
643                 list.add(j);
644                 out.write(list.toString());
645                 return null;
646         }
647         
648         @SuppressWarnings({ "unchecked", "rawtypes" })
649         private String createMicroSeriveJson(MicroServiceModels returnModel, Set<String> allkeys) {
650                 Map<String, String> attributeMap = new HashMap<>();
651                 Map<String, String> refAttributeMap = new HashMap<>();
652                 String attribute = returnModel.getAttributes();
653                 if(attribute != null){
654                         attribute = attribute.trim();
655                 }
656                 String refAttribute = returnModel.getRef_attributes();
657                 if(refAttribute != null){
658                         refAttribute = refAttribute.trim();
659                 }
660                 String enumAttribute = returnModel.getEnumValues();
661                 if(enumAttribute != null){
662                         enumAttribute = enumAttribute.trim();
663                 }
664                 if (!StringUtils.isEmpty(attribute)){
665                         attributeMap = convert(attribute, ",");
666                 }
667                 if (!StringUtils.isEmpty(refAttribute)){
668                         refAttributeMap = convert(refAttribute, ",");
669                 }
670
671                 Gson gson = new Gson();
672                 
673                 String subAttributes = returnModel.getSub_attributes();
674                 if(subAttributes != null){
675                         subAttributes = subAttributes.trim();
676                 }else{
677                         subAttributes = "";
678                 }
679                 
680                 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
681                 
682                 JSONObject object = new JSONObject();
683                 JSONArray array = new JSONArray();
684                 
685                 for (Entry<String, String> keySet : attributeMap.entrySet()){
686                         array = new JSONArray();
687                         String value = keySet.getValue();
688                         if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){
689                                 array.put(value);
690                                 object.put(keySet.getKey().trim(), array);
691                         }else {
692                                 object.put(keySet.getKey().trim(), value.trim());
693                         }
694                 }
695                 
696                 for (Entry<String, String> keySet : refAttributeMap.entrySet()){
697                         array = new JSONArray();
698                         String value = keySet.getValue().split(":")[0];
699                         if (gsonObject.containsKey(value)){
700                                 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){
701                                         array.put(recursiveReference(value, gsonObject, enumAttribute));
702                                         object.put(keySet.getKey().trim(), array);
703                                 }else {
704                                         object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
705                                 }
706                         }else {
707                                 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){
708                                         array.put(value.trim());
709                                         object.put(keySet.getKey().trim(), array);
710                                 }else {
711                                         object.put(keySet.getKey().trim(), value.trim()); 
712                                 }
713                         }
714                 }
715
716                 return object.toString();
717         }
718
719         @SuppressWarnings("unchecked")
720         private JSONObject recursiveReference(String name, Map<String,String> subAttributeMap, String enumAttribute) {
721                 JSONObject object = new JSONObject();
722                 Map<String, String> map;
723                 Object returnClass = subAttributeMap.get(name);
724                 map = (Map<String, String>) returnClass; 
725                 JSONArray array;
726                 
727                 for( Entry<String, String> m:map.entrySet()){  
728                         String[] splitValue = m.getValue().split(":");
729                         array = new JSONArray();
730                         if (subAttributeMap.containsKey(splitValue[0])){
731                                 if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])){
732                                         array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
733                                         object.put(m.getKey().trim(), array);
734                                 }else {
735                                         object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
736                                 }
737                         } else{
738                                 if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])){
739                                         array.put(splitValue[0].trim());
740                                         object.put(m.getKey().trim(), array);
741                                 }else {
742                                         object.put(m.getKey().trim(), splitValue[0].trim());
743                                 }
744                         }
745                   }  
746                 
747                 return object;
748         }
749         
750         
751         public static JSONObject convertToArrayElement(JSONObject json, String keyValue) {
752             return convertToArrayElement(json, new HashSet<>(), keyValue);
753         }
754         
755         private static JSONObject convertToArrayElement(JSONObject json, Set<String> keys, String keyValue) {
756             for (String key : json.keySet()) {
757                 Object obj = json.get(key);
758                 if(key.equals(keyValue.trim())){
759                         if(!(obj instanceof JSONArray)){
760                                 JSONArray newJsonArray = new JSONArray();
761                                 newJsonArray.put(obj);
762                                 json.put(key, newJsonArray);
763                         }
764                         LOGGER.info("key : " + key);
765                         LOGGER.info("obj : " + obj);
766                         LOGGER.info("json.get(key) : " + json.get(key));
767                         LOGGER.info("keyValue : " + keyValue);
768                     keys.addAll(json.keySet());
769                     
770                     return json;
771                 }
772
773                 if (obj instanceof JSONObject) convertToArrayElement(json.getJSONObject(key), keyValue);            
774             }
775
776             return json;
777         }
778         
779         // call this method to get all MANY-true properties 
780         public static Set<String> getManyTrueProperties(String referAttributes){
781                 LOGGER.info("referAttributes : " + referAttributes);
782                 Set<String> manyTrueProperties = new HashSet<>();
783                 
784                 if(referAttributes != null){
785                         String[] referAarray = referAttributes.split(",");
786                         String []element= null;
787                         for(int i=0; i<referAarray.length; i++){
788                                 element = referAarray[i].split("=");      
789                                 if(element.length > 1 && element[1].contains("MANY-true")){
790                                         manyTrueProperties.add(element[0]);
791                                 }
792                         }               
793                 }
794                 
795                 return manyTrueProperties;
796         }
797         
798         //call this method to start the recursive
799         private Set<String> getAllKeys(JSONObject json) {
800             return getAllKeys(json, new HashSet<>());
801         }
802
803         private Set<String> getAllKeys(JSONArray arr) {
804             return getAllKeys(arr, new HashSet<>());
805         }
806
807         private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
808             for (int i = 0; i < arr.length(); i++) {
809                 Object obj = arr.get(i);
810                 if (obj instanceof JSONObject) keys.addAll(getAllKeys(arr.getJSONObject(i)));
811                 if (obj instanceof JSONArray) keys.addAll(getAllKeys(arr.getJSONArray(i)));
812             }
813
814             return keys;
815         }
816         
817     // this method returns a set of keys with "MANY-true" defined in their value.
818         private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
819             for (String key : json.keySet()) {
820                 Object obj = json.get(key);
821                 if(obj instanceof String && ((String) obj).contains("MANY-true")){
822                         LOGGER.info("key : " + key);
823                         LOGGER.info("obj : " + obj);
824                         allManyTrueKeys.add(key);
825                 }
826                 if (obj instanceof JSONObject) keys.addAll(getAllKeys(json.getJSONObject(key)));
827                 if (obj instanceof JSONArray) keys.addAll(getAllKeys(json.getJSONArray(key)));
828             }
829
830             return keys;
831         }
832
833         
834         @RequestMapping(value={"/policyController/getModelServiceVersioneData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
835         public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response) throws IOException{
836                 ObjectMapper mapper = new ObjectMapper();
837                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
838                 JsonNode root = mapper.readTree(request.getReader());
839
840                 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
841                 String  servicename = value.split("-v")[0];
842                 Set<String> returnList = getVersionList(servicename);
843                 
844                 response.setCharacterEncoding("UTF-8");
845                 response.setContentType("application / json");
846                 request.setCharacterEncoding("UTF-8");
847         List<Object>  list = new ArrayList<>();
848                 PrintWriter out = response.getWriter();
849                 String responseString = mapper.writeValueAsString(returnList);
850                 JSONObject j = new JSONObject("{dcaeModelVersionData: " + responseString +"}");
851                 list.add(j);
852                 out.write(list.toString());
853                 return null;
854         }
855
856         private Set<String> getVersionList(String name) {       
857                 MicroServiceModels workingModel;
858                 Set<String> list = new HashSet<>();
859                 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
860                 for (int i = 0; i < microServiceModelsData.size(); i++) {
861                         workingModel = (MicroServiceModels) microServiceModelsData.get(i);
862                         if (workingModel.getVersion()!=null){
863                                 list.add(workingModel.getVersion());
864                         }else{
865                                 list.add("Default");
866                         }
867                 }
868                 return list;
869         }
870         
871         private MicroServiceModels getAttributeObject(String name, String version) {    
872                 MicroServiceModels workingModel = new MicroServiceModels();
873                 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
874                 for (int i = 0; i < microServiceModelsData.size(); i++) {
875                         workingModel = (MicroServiceModels) microServiceModelsData.get(i);
876                         if(version != null){
877                                 if (workingModel.getVersion()!=null){
878                                         if (workingModel.getVersion().equals(version)){
879                                                 return workingModel;
880                                         }
881                                 }else{
882                                         return workingModel;
883                                 }
884                         }else{
885                                 return workingModel;
886                         }
887                         
888                 }
889                 return workingModel;
890         }
891         
892         private MicroserviceHeaderdeFaults getHeaderDefaultsObject(String modelName) {  
893                 return (MicroserviceHeaderdeFaults) commonClassDao.getEntityItem(MicroserviceHeaderdeFaults.class, "modelName", modelName);
894         }       
895
896         @RequestMapping(value={"/get_DCAEPriorityValues"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
897         public void getDCAEPriorityValuesData(HttpServletRequest request, HttpServletResponse response){
898                 try{
899                         Map<String, Object> model = new HashMap<>();
900                         ObjectMapper mapper = new ObjectMapper();
901                         List<String> priorityList = new ArrayList<>();
902                         priorityCount = 10;
903                         for (int i = 1; i < priorityCount; i++) {
904                                 priorityList.add(String.valueOf(i));
905                         }
906                         model.put("priorityDatas", mapper.writeValueAsString(priorityList));
907                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
908                         JSONObject j = new JSONObject(msg);
909                         response.getWriter().write(j.toString());
910                 }
911                 catch (Exception e){
912                         LOGGER.error(e);
913                 }
914         }
915
916         public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
917                 if (policyAdapter.getPolicyData() instanceof PolicyType) {
918                         Object policyData = policyAdapter.getPolicyData();
919                         PolicyType policy = (PolicyType) policyData;
920                         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
921                         String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("MS_") +3);
922                         policyAdapter.setPolicyName(policyNameValue);
923                         String description = "";
924                         try{
925                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
926                         }catch(Exception e){
927                             LOGGER.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue ,e);
928                                 description = policy.getDescription();
929                         }
930                         policyAdapter.setPolicyDescription(description);
931                         // Get the target data under policy.
932                         TargetType target = policy.getTarget();
933                         if (target != null) {
934                                 // Under target we have AnyOFType
935                                 List<AnyOfType> anyOfList = target.getAnyOf();
936                                 if (anyOfList != null) {
937                                         Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
938                                         while (iterAnyOf.hasNext()) {
939                                                 AnyOfType anyOf = iterAnyOf.next();
940                                                 // Under AnyOFType we have AllOFType
941                                                 List<AllOfType> allOfList = anyOf.getAllOf();
942                                                 if (allOfList != null) {
943                                                         Iterator<AllOfType> iterAllOf = allOfList.iterator();
944                                                         while (iterAllOf.hasNext()) {
945                                                                 AllOfType allOf = iterAllOf.next();
946                                                                 // Under AllOFType we have Match
947                                                                 List<MatchType> matchList = allOf.getMatch();
948                                                                 if (matchList != null) {
949                                                                         Iterator<MatchType> iterMatch = matchList.iterator();
950                                                                         while (matchList.size()>1 && iterMatch.hasNext()) {
951                                                                                 MatchType match = iterMatch.next();
952                                                                                 //
953                                                                                 // Under the match we have attribute value and
954                                                                                 // attributeDesignator. So,finally down to the actual attribute.
955                                                                                 //
956                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
957                                                                                 String value = (String) attributeValue.getContent().get(0);
958                                                                                 AttributeDesignatorType designator = match.getAttributeDesignator();
959                                                                                 String attributeId = designator.getAttributeId();
960                                                                                 // First match in the target is OnapName, so set that value.
961                                                                                 if ("ONAPName".equals(attributeId)) {
962                                                                                         policyAdapter.setOnapName(value);
963                                                                                 }
964                                                                                 if ("ConfigName".equals(attributeId)){
965                                                                                         policyAdapter.setConfigName(value);
966                                                                                 }
967                                                                                 if ("uuid".equals(attributeId)){
968                                                                                         policyAdapter.setUuid(value);
969                                                                                 }
970                                                                                 if ("location".equals(attributeId)){
971                                                                                         policyAdapter.setLocation(value);
972                                                                                 }
973                                                                                 if ("RiskType".equals(attributeId)){
974                                                                                         policyAdapter.setRiskType(value);
975                                                                                 }
976                                                                                 if ("RiskLevel".equals(attributeId)){
977                                                                                         policyAdapter.setRiskLevel(value);
978                                                                                 }
979                                                                                 if ("guard".equals(attributeId)){
980                                                                                         policyAdapter.setGuard(value);
981                                                                                 }
982                                                                                 if ("TTLDate".equals(attributeId) && !value.contains("NA")){
983                                                                                         PolicyController controller = new PolicyController();
984                                                                                         String newDate = controller.convertDate(value);
985                                                                                         policyAdapter.setTtlDate(newDate);
986                                                                                 }
987                                                                         }
988                                                                         readFile(policyAdapter, entity);
989                                                                 }
990                                                         }
991                                                 }
992                                         }
993                                 }
994                         }
995                 }
996         }
997         
998         public static Map<String, String> convert(String str, String split) {
999                 Map<String, String> map = new HashMap<>();
1000                 for(final String entry : str.split(split)) {
1001                     String[] parts = entry.split("=");
1002                     map.put(parts[0], parts[1]);
1003                 }
1004                 return map;
1005         }
1006
1007
1008         @SuppressWarnings("unchecked")
1009         private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1010                 String policyScopeName = null;
1011                 ObjectMapper mapper = new ObjectMapper();
1012                 try {
1013                         DCAEMicroServiceObject msBody = mapper.readValue(entity.getConfigurationData().getConfigBody(), DCAEMicroServiceObject.class);
1014                         policyScopeName = getPolicyScope(msBody.getPolicyScope());
1015                         policyAdapter.setPolicyScope(policyScopeName);
1016
1017                         policyAdapter.setPriority(msBody.getPriority());
1018
1019                         if (msBody.getVersion()!= null){
1020                                 policyAdapter.setServiceType(msBody.getService());
1021                                 policyAdapter.setVersion(msBody.getVersion());
1022                         }else{
1023                                 policyAdapter.setServiceType(msBody.getService());
1024                         }
1025                         if(msBody.getContent() != null){
1026                                 LinkedHashMap<String, Object>  data = new LinkedHashMap<>();
1027                                 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) msBody.getContent();
1028                                 readRecursivlyJSONContent(map, data);
1029                                 policyAdapter.setRuleData(data);
1030                         }
1031
1032                 } catch (Exception e) {
1033                         LOGGER.error(e);
1034                 }
1035
1036         }
1037
1038         @SuppressWarnings({ "rawtypes", "unchecked" }) 
1039         public void readRecursivlyJSONContent(LinkedHashMap<String, ?> map, LinkedHashMap<String, Object> data){
1040                 for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
1041                         Object key =  iterator.next();
1042                         Object value = map.get(key);
1043                         if(value instanceof LinkedHashMap<?, ?>){
1044                                 LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<>(); 
1045                                 readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
1046                                 for( Entry<String, Object> entry : secondObjec.entrySet()){
1047                                         data.put(key+"." + entry.getKey(), entry.getValue());
1048                                 }
1049                         }else if(value instanceof ArrayList){
1050                                 ArrayList<?> jsonArrayVal = (ArrayList<?>)value;
1051                                 for(int i = 0; i < jsonArrayVal.size(); i++){
1052                                         Object arrayvalue = jsonArrayVal.get(i);
1053                                         if(arrayvalue instanceof LinkedHashMap<?, ?>){
1054                                                 LinkedHashMap<String, Object> newData = new LinkedHashMap<>();   
1055                                                 readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
1056                                                 for(Entry<String, Object> entry: newData.entrySet()){
1057                                                         data.put(key+"@"+i+"." +entry.getKey(), entry.getValue());
1058                                                 }
1059                                         }else if(arrayvalue instanceof ArrayList){
1060                                                 ArrayList<?> jsonArrayVal1 = (ArrayList<?>)value;
1061                                                 for(int j = 0; j < jsonArrayVal1.size(); j++){
1062                                                         Object arrayvalue1 = jsonArrayVal1.get(i);
1063                                                         data.put(key+"@"+j, arrayvalue1.toString());
1064                                                 }       
1065                                         }else{
1066                                                 data.put(key+"@"+i, arrayvalue.toString());
1067                                         }       
1068                                 }
1069                         }else{
1070                                 data.put(key.toString(), value.toString());
1071                         }       
1072                 }
1073         }
1074
1075         public String getPolicyScope(String value) {
1076                 List<Object> groupList= commonClassDao.getDataById(GroupPolicyScopeList.class, "groupList", value);
1077                 if(groupList != null && !groupList.isEmpty()){
1078                         GroupPolicyScopeList pScope = (GroupPolicyScopeList) groupList.get(0);
1079                         return pScope.getGroupName();           
1080                 }
1081                 return null;
1082         }
1083
1084         //Convert the map values and set into JSON body
1085         public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
1086                 Map<String, String> attribute = new HashMap<>();
1087                 StringBuilder temp;
1088                 String key;
1089                 String value;
1090                 for (Entry<String, String> entry : attributesMap.entrySet()) {
1091                         key = entry.getKey();
1092                         value = entry.getValue();
1093                         attribute.put(key, value);
1094                 }
1095                 for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
1096                         key = entryRef.getKey();
1097                         value = entryRef.getValue();
1098                         attribute.put(key, value);
1099                 }
1100                 for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
1101                         key = entryList.getKey();
1102                         value = entryList.getValue();
1103                         attribute.put(key, value);
1104                 }
1105                 for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()){
1106                         key = arrayList.getKey();
1107                         temp = null;
1108                         for (Object textList : arrayList.getValue()){
1109                                 if (temp == null){
1110                                         temp = new StringBuilder();
1111                                         temp.append("[" + textList);
1112                                 }else{
1113                                         temp.append("," + textList);
1114                                 }
1115                         }
1116                         attribute.put(key, temp+ "]");                  
1117                 }
1118
1119                 return  attribute;
1120         }
1121         
1122         @RequestMapping(value={"/ms_dictionary/set_MSModelData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
1123         public void SetMSModelData(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException{
1124                 modelList = new ArrayList<>();
1125                 dirDependencyList = new ArrayList<>();
1126                 classMap = new LinkedHashMap<>();
1127                 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
1128                 boolean zip = false;
1129                 boolean yml= false;
1130                 String errorMsg = "";
1131                 for (FileItem item : items) {
1132                         if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")||item.getName().endsWith(".yml")){
1133                                 this.newModel = new MicroServiceModels();
1134                                 try{
1135                                         File file = new File(item.getName());
1136                                         OutputStream outputStream = new FileOutputStream(file);
1137                                         IOUtils.copy(item.getInputStream(), outputStream);
1138                                         outputStream.close();
1139                                         this.newFile = file.toString();
1140                                         this.newModel.setModelName(this.newFile.split("-v")[0]);
1141                                 
1142                                         if (this.newFile.contains("-v")){
1143                                                 if (item.getName().endsWith(".zip")){
1144                                                         this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
1145                                                         zip = true;
1146                                                 }else if(item.getName().endsWith(".yml")){
1147                                                         this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
1148                                                         yml = true;
1149                                                 }
1150                                                 else {
1151                                                         this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
1152                                                 }
1153                                         }                               
1154                                 }catch(Exception e){
1155                                         LOGGER.error("Upload error : ", e);
1156                                         errorMsg = "Upload error:" + e.getMessage();
1157                                 }
1158                         }
1159                         
1160                 }
1161                 
1162                 if(!errorMsg.isEmpty()){
1163                         
1164                         PrintWriter out = response.getWriter();
1165                         
1166                         response.setCharacterEncoding("UTF-8");
1167                         response.setContentType("application / json");
1168                         request.setCharacterEncoding("UTF-8");
1169
1170                         JSONObject j = new JSONObject();
1171                         j.put("errorMsg", errorMsg);
1172                         out.write(j.toString());
1173                         return;
1174                 }
1175                 
1176                 List<File> fileList = new ArrayList<>();
1177                 MSModelUtils msMLUtils =  new MSModelUtils();
1178                 this.directory = "model";
1179                 if (zip){
1180                         extractFolder(this.newFile);
1181                         fileList = listModelFiles(this.directory);
1182                 }else if (yml==true){
1183                         
1184                         errorMsg = msMLUtils.parseTosca(this.newFile);
1185                         if(errorMsg != null){
1186                                 PrintWriter out = response.getWriter();                         
1187                                 response.setCharacterEncoding("UTF-8");
1188                                 response.setContentType("application / json");
1189                                 request.setCharacterEncoding("UTF-8");
1190                                 JSONObject j = new JSONObject();
1191                                 j.put("errorMsg", errorMsg);
1192                             out.write(j.toString());
1193                             return;
1194                         }
1195                         
1196                 }else {
1197                         File file = new File(this.newFile);
1198                         fileList.add(file);
1199                 }
1200                 String modelType= "";
1201                 if(!yml){
1202                         modelType="xmi";
1203                         //Process Main Model file first
1204                         classMap = new LinkedHashMap<>();
1205                         for (File file : fileList) {
1206                                 if(!file.isDirectory() && file.getName().endsWith(".xmi")){
1207                         retreiveDependency(file.toString(), true);
1208                     }   
1209                         }
1210                         
1211                         modelList = createList();
1212                         
1213                         cleanUp(this.newFile);
1214                         cleanUp(directory);
1215                 }else{
1216                         modelType="yml";
1217                         modelList.add(this.newModel.getModelName());
1218                         String className=this.newModel.getModelName();
1219                         MSAttributeObject msAttributes= new MSAttributeObject();
1220                         msAttributes.setClassName(className);
1221                         
1222                         LinkedHashMap<String, String> returnAttributeList =new LinkedHashMap<>();
1223                         returnAttributeList.put(className, msMLUtils.getAttributeString());
1224                         msAttributes.setAttribute(returnAttributeList);
1225                         
1226                         msAttributes.setSubClass(msMLUtils.getRetmap());
1227                         
1228                         msAttributes.setMatchingSet(msMLUtils.getMatchableValues());
1229                         
1230                         LinkedHashMap<String, String> returnReferenceList =new LinkedHashMap<>();
1231
1232                         returnReferenceList.put(className, msMLUtils.getReferenceAttributes());
1233                         msAttributes.setRefAttribute(returnReferenceList);
1234                         
1235                         if(msMLUtils.getListConstraints()!=""){
1236                                 LinkedHashMap<String, String> enumList =new LinkedHashMap<>();
1237                                 String[] listArray=msMLUtils.getListConstraints().split("#");
1238                 for(String str:listArray){
1239                     String[] strArr= str.split("=");
1240                     if(strArr.length>1){
1241                         enumList.put(strArr[0], strArr[1]);
1242                     }
1243                 }
1244                                 msAttributes.setEnumType(enumList);
1245                         }
1246                         
1247                         classMap=new LinkedHashMap<>();
1248                         classMap.put(className, msAttributes);
1249                         
1250                 }
1251                 
1252                 PrintWriter out = response.getWriter();
1253                 
1254                 response.setCharacterEncoding("UTF-8");
1255                 response.setContentType("application / json");
1256                 request.setCharacterEncoding("UTF-8");
1257                 
1258                 ObjectMapper mapper = new ObjectMapper();
1259                 JSONObject j = new JSONObject();
1260                 j.put("classListDatas", modelList);
1261                 j.put("modelDatas", mapper.writeValueAsString(classMap));
1262                 j.put("modelType", modelType);
1263                 j.put("dataOrderInfo", msMLUtils.getDataOrderInfo());
1264                 
1265                 out.write(j.toString());
1266         }
1267         
1268         /*
1269          * Unzip file and store in the model directory for processing
1270          */
1271         @SuppressWarnings("rawtypes")
1272         private void extractFolder(String zipFile )  {
1273             int BUFFER = 2048;
1274             File file = new File(zipFile);
1275
1276                 try (ZipFile zip = new ZipFile(file)) {
1277                     String newPath =  "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1278                     this.directory = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1279                     checkZipDirectory(this.directory);
1280                     new File(newPath).mkdir();
1281                     Enumeration zipFileEntries = zip.entries();
1282         
1283                     // Process each entry
1284                     while (zipFileEntries.hasMoreElements()){
1285                         // grab a zip file entry
1286                         ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
1287                         String currentEntry = entry.getName();
1288                         File destFile = new File("model" + File.separator + currentEntry);
1289                         File destinationParent = destFile.getParentFile();
1290                         
1291                         destinationParent.mkdirs();
1292         
1293                         if (!entry.isDirectory()){
1294                             BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
1295                             int currentByte;
1296                             byte[] data = new byte[BUFFER];
1297                             try (FileOutputStream fos = new FileOutputStream(destFile);
1298                                         BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
1299                                     while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
1300                                         dest.write(data, 0, currentByte);
1301                                     }
1302                                     dest.flush();
1303                             } catch (IOException e) {
1304                                 LOGGER.error("Failed to write zip contents to {}" + destFile + e);
1305                                 //
1306                                 // PLD should I throw e?
1307                                 //
1308                                 throw e;
1309                             }
1310                         }
1311         
1312                         if (currentEntry.endsWith(".zip")){
1313                             extractFolder(destFile.getAbsolutePath());
1314                         }
1315                     }
1316             } catch (IOException e) {
1317                 LOGGER.error("Failed to unzip model file " + zipFile, e);
1318                 }
1319         }
1320         
1321         private void retreiveDependency(String workingFile, Boolean modelClass) {
1322                 
1323                 MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
1324             Map<String, MSAttributeObject> tempMap;
1325             
1326             tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
1327             
1328             classMap.putAll(tempMap);
1329             LOGGER.info(tempMap);
1330             
1331             return;     
1332             
1333         }
1334                 
1335         private List<File> listModelFiles(String directoryName) {
1336                 File fileDirectory = new File(directoryName);
1337                 List<File> resultList = new ArrayList<>();
1338                 File[] fList = fileDirectory.listFiles();
1339                 for (File file : fList) {
1340                         if (file.isFile()) {
1341                                 resultList.add(file);
1342                         } else if (file.isDirectory()) {
1343                                 dirDependencyList.add(file.getName());
1344                                 resultList.addAll(listModelFiles(file.getAbsolutePath()));
1345                         }
1346                 }
1347                 return resultList;
1348         }
1349         
1350     public void cleanUp(String path) {
1351         if (path!=null){
1352             try {
1353                 FileUtils.forceDelete(new File(path));
1354             } catch (IOException e) {
1355                 LOGGER.error("Failed to delete folder " + path, e);
1356             }  
1357         }
1358     }
1359  
1360     public void checkZipDirectory(String zipDirectory) {
1361         Path path = Paths.get(zipDirectory);
1362  
1363         if (Files.exists(path)) {
1364             cleanUp(zipDirectory);
1365         }
1366     }
1367         
1368     private List<String> createList() {
1369         List<String> list = new  ArrayList<>();
1370         for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()){
1371             if (cMap.getValue().isPolicyTempalate()){
1372                 list.add(cMap.getKey());
1373             }
1374             
1375         }
1376         
1377         if (list.isEmpty()){
1378             if (classMap.containsKey(this.newModel.getModelName())){
1379                 list.add(this.newModel.getModelName());
1380             }else {
1381                 list.add("EMPTY");
1382             }
1383         }
1384         return list;
1385     }
1386
1387         public Map<String, String> getAttributesListRefMap() {
1388                 return attributesListRefMap;
1389         }
1390
1391         public Map<String, LinkedList<String>> getArrayTextList() {
1392                 return arrayTextList;
1393         }
1394
1395 }
1396
1397 class DCAEMicroServiceObject {
1398
1399         private String service;
1400         private String location;
1401         private String uuid;
1402         private String policyName;
1403         private String description;
1404         private String configName;
1405         private String templateVersion;
1406         private String version;
1407         private String priority;
1408         private String policyScope;
1409         private String riskType;
1410         private String riskLevel; 
1411         private String guard = null;
1412
1413         public String getGuard() {
1414                 return guard;
1415         }
1416         public void setGuard(String guard) {
1417                 this.guard = guard;
1418         }
1419         public String getRiskType() {
1420                 return riskType;
1421         }
1422         public void setRiskType(String riskType) {
1423                 this.riskType = riskType;
1424         }
1425         public String getRiskLevel() {
1426                 return riskLevel;
1427         }
1428         public void setRiskLevel(String riskLevel) {
1429                 this.riskLevel = riskLevel;
1430         }
1431         public String getPolicyScope() {
1432                 return policyScope;
1433         }
1434         public void setPolicyScope(String policyScope) {
1435                 this.policyScope = policyScope;
1436         }
1437
1438         public String getPriority() {
1439                 return priority;
1440         }
1441         public void setPriority(String priority) {
1442                 this.priority = priority;
1443         }
1444         public String getVersion() {
1445                 return version;
1446         }
1447         public void setVersion(String version) {
1448                 this.version = version;
1449         }
1450         private Object content;
1451
1452
1453         public String getPolicyName() {
1454                 return policyName;
1455         }
1456         public void setPolicyName(String policyName) {
1457                 this.policyName = policyName;
1458         }
1459         public String getDescription() {
1460                 return description;
1461         }
1462         public void setDescription(String description) {
1463                 this.description = description;
1464         }
1465         public String getConfigName() {
1466                 return configName;
1467         }
1468         public void setConfigName(String configName) {
1469                 this.configName = configName;
1470         }
1471         public Object getContent() {
1472                 return content;
1473         }
1474         public void setContent(Object content) {
1475                 this.content = content;
1476         }
1477
1478         public String getService() {
1479                 return service;
1480         }
1481         public void setService(String service) {
1482                 this.service = service;
1483         }
1484         public String getLocation() {
1485                 return location;
1486         }
1487         public void setLocation(String location) {
1488                 this.location = location;
1489         }
1490
1491         public String getUuid() {
1492                 return uuid;
1493         }
1494         public void setUuid(String uuid) {
1495                 this.uuid = uuid;
1496         }
1497         public String getTemplateVersion() {
1498                 return templateVersion;
1499         }
1500         public void setTemplateVersion(String templateVersion) {
1501                 this.templateVersion = templateVersion;
1502         }
1503
1504 }