Merge "JUnit additions for PAP-REST,REST,Utils"
[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 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.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.io.PrintWriter;
34 import java.io.StringReader;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Enumeration;
41 import java.util.HashMap;
42 import java.util.HashSet;
43 import java.util.Iterator;
44 import java.util.LinkedHashMap;
45 import java.util.LinkedList;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Map.Entry;
49 import java.util.Set;
50 import java.util.TreeMap;
51 import java.util.UUID;
52 import java.util.zip.ZipEntry;
53 import java.util.zip.ZipFile;
54
55 import javax.json.Json;
56 import javax.json.JsonArray;
57 import javax.json.JsonArrayBuilder;
58 import javax.json.JsonObject;
59 import javax.json.JsonObjectBuilder;
60 import javax.json.JsonReader;
61 import javax.json.JsonValue;
62 import javax.servlet.http.HttpServletRequest;
63 import javax.servlet.http.HttpServletResponse;
64
65 import org.apache.commons.compress.utils.IOUtils;
66 import org.apache.commons.fileupload.FileItem;
67 import org.apache.commons.fileupload.FileUploadException;
68 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
69 import org.apache.commons.fileupload.servlet.ServletFileUpload;
70 import org.apache.commons.io.FileUtils;
71 import org.apache.commons.lang.StringUtils;
72 import org.json.JSONArray;
73 import org.json.JSONObject;
74 import org.onap.policy.common.logging.flexlogger.FlexLogger;
75 import org.onap.policy.common.logging.flexlogger.Logger;
76 import org.onap.policy.rest.XACMLRestProperties;
77 import org.onap.policy.rest.adapter.PolicyRestAdapter;
78 import org.onap.policy.rest.dao.CommonClassDao;
79 import org.onap.policy.rest.jpa.GroupPolicyScopeList;
80 import org.onap.policy.rest.jpa.MicroServiceModels;
81 import org.onap.policy.rest.jpa.PolicyEntity;
82 import org.onap.policy.rest.util.MSAttributeObject;
83 import org.onap.policy.rest.util.MSModelUtils;
84 import org.onap.policy.rest.util.MSModelUtils.MODEL_TYPE;
85 import org.onap.portalsdk.core.controller.RestrictedBaseController;
86 import org.onap.portalsdk.core.web.support.JsonMessage;
87 import org.springframework.beans.factory.annotation.Autowired;
88 import org.springframework.http.MediaType;
89 import org.springframework.stereotype.Controller;
90 import org.springframework.web.bind.annotation.RequestMapping;
91 import org.springframework.web.servlet.ModelAndView;
92 import org.yaml.snakeyaml.Yaml;
93
94 import com.att.research.xacml.util.XACMLProperties;
95 import com.fasterxml.jackson.core.JsonProcessingException;
96 import com.fasterxml.jackson.databind.DeserializationFeature;
97 import com.fasterxml.jackson.databind.JsonNode;
98 import com.fasterxml.jackson.databind.ObjectMapper;
99 import com.fasterxml.jackson.databind.ObjectWriter;
100 import com.fasterxml.jackson.databind.node.JsonNodeFactory;
101 import com.fasterxml.jackson.databind.node.ObjectNode;
102 import com.google.gson.Gson;
103
104 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
105 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
106 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
107 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
108 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
109 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
110 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
111
112 @Controller
113 @RequestMapping("/")
114 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
115         private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
116         private Map<String, String>  matchableValues;
117         private static CommonClassDao commonClassDao;
118         
119         public static CommonClassDao getCommonClassDao() {
120                 return commonClassDao;
121         }
122
123         public static void setCommonClassDao(CommonClassDao commonClassDao) {
124                 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
125         }
126
127         private MicroServiceModels newModel;
128         private String newFile;
129         private String directory;
130         private List<String> modelList = new ArrayList<>();
131         private List<String> dirDependencyList = new ArrayList<>();
132         private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
133         //Tosca Model related Datastructure. 
134         String referenceAttributes;
135         String attributeString;
136         String listConstraints;
137         String subAttributeString;
138         HashMap<String, Object> retmap = new HashMap<>();
139         Set<String> uniqueKeys= new HashSet<>();
140         Set<String> uniqueDataKeys= new HashSet<>();
141         StringBuilder dataListBuffer=new StringBuilder();
142         List<String> dataConstraints= new ArrayList <>();
143         Set<String> allManyTrueKeys= new HashSet <>();
144         
145         public static final String DATATYPE  = "data_types.policy.data.";
146         public static final String PROPERTIES=".properties.";
147         public static final String TYPE=".type";
148         public static final String STRING="string";
149         public static final String INTEGER="integer";
150         public static final String LIST="list";
151         public static final String DEFAULT=".default";
152         public static final String REQUIRED=".required";
153         public static final String MANYFALSE=":MANY-false";
154         public static final String MATCHABLE=".matchable";
155         
156         @Autowired
157         private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){
158                 CreateDcaeMicroServiceController.commonClassDao = commonClassDao;
159         }
160
161         public CreateDcaeMicroServiceController(){
162                 // Empty Constructor
163         }
164
165         protected PolicyRestAdapter policyAdapter = null;
166         private int priorityCount; 
167         private Map<String, String> attributesListRefMap =  new HashMap<>();
168         private Map<String, LinkedList<String>> arrayTextList =  new HashMap<>();
169
170         public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
171                 
172                 String jsonContent = null;
173                 try{
174                         LOGGER.info("policyJSON :" + (root.get("policyJSON")).toString());
175                         
176                         String tempJson = root.get("policyJSON").toString();
177                         
178                         //---replace empty value with the value below before calling decodeContent method.
179                         String dummyValue = "*empty-value*" + UUID.randomUUID().toString();
180                         LOGGER.info("dummyValue:" + dummyValue);
181                         tempJson = StringUtils.replaceEach(tempJson, new String[]{"\"\""}, new String[]{"\""+dummyValue+"\""});
182                         ObjectMapper mapper = new ObjectMapper();
183                         JsonNode tempJsonNode = mapper.readTree(tempJson);
184                         jsonContent = decodeContent(tempJsonNode).toString();
185                         constructJson(policyData, jsonContent, dummyValue);
186                 }catch(Exception e){
187                         LOGGER.error("Error while decoding microservice content", e);
188                 }
189                 
190                 return policyData;
191         }
192         
193         private GroupPolicyScopeList getPolicyObject(String policyScope) {
194                 return (GroupPolicyScopeList) commonClassDao.getEntityItem(GroupPolicyScopeList.class, "name", policyScope);
195         }
196         
197         private PolicyRestAdapter constructJson(PolicyRestAdapter policyAdapter, String jsonContent, String dummyValue) {
198                 ObjectWriter om = new ObjectMapper().writer();
199                 String json="";
200                 DCAEMicroServiceObject microServiceObject = new DCAEMicroServiceObject();
201                 MicroServiceModels returnModel = new MicroServiceModels();
202                 microServiceObject.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_MS));
203                 if(policyAdapter.getServiceType() !=null){
204                         microServiceObject.setService(policyAdapter.getServiceType());
205                         microServiceObject.setVersion(policyAdapter.getVersion());
206                         returnModel = getAttributeObject(microServiceObject.getService(), microServiceObject.getVersion());
207                 }
208                 if (returnModel.getAnnotation()==null || returnModel.getAnnotation().isEmpty()){
209                         if(policyAdapter.getUuid()!=null){
210                                 microServiceObject.setUuid(policyAdapter.getUuid());
211                         }
212                         if(policyAdapter.getLocation()!=null){
213                                 microServiceObject.setLocation(policyAdapter.getLocation());
214                         } 
215                         if(policyAdapter.getConfigName()!=null){
216                                 microServiceObject.setConfigName(policyAdapter.getConfigName());
217                         }
218                         GroupPolicyScopeList policyScopeValue = getPolicyObject(policyAdapter.getPolicyScope());
219                         if(policyScopeValue!=null){
220                                 microServiceObject.setPolicyScope(policyScopeValue.getGroupList());     
221                         }
222                 }
223                 
224                 if(policyAdapter.getPolicyName()!=null){
225                         microServiceObject.setPolicyName(policyAdapter.getPolicyName());
226                 }
227                 if(policyAdapter.getPolicyDescription()!=null){
228                         microServiceObject.setDescription(policyAdapter.getPolicyDescription());
229                 }
230                 if (policyAdapter.getPriority()!=null){
231                         microServiceObject.setPriority(policyAdapter.getPriority());
232                 }else {
233                         microServiceObject.setPriority("9999");
234                 }
235                 
236                 if (policyAdapter.getRiskLevel()!=null){
237                         microServiceObject.setRiskLevel(policyAdapter.getRiskLevel());
238                 }
239                 if (policyAdapter.getRiskType()!=null){
240                         microServiceObject.setRiskType(policyAdapter.getRiskType());
241                 }
242                 if (policyAdapter.getGuard()!=null){
243                         microServiceObject.setGuard(policyAdapter.getGuard());
244                 }
245                 microServiceObject.setContent(jsonContent);
246                 
247                 try {
248                         json = om.writeValueAsString(microServiceObject);
249                 } catch (JsonProcessingException e) {
250                         LOGGER.error("Error writing out the object", e);
251                 }
252                 LOGGER.info("input json: " + json);
253                 LOGGER.info("input jsonContent: " + jsonContent);
254                 String cleanJson = cleanUPJson(json);
255         //--- reset empty value back after called cleanUPJson method and before calling removeNullAttributes
256                 String tempJson = StringUtils.replaceEach(cleanJson, new String[]{"\""+dummyValue+"\""},  new String[]{"\"\""});
257                 LOGGER.info("tempJson: " + tempJson);
258                 cleanJson = removeNullAttributes(tempJson);
259                 policyAdapter.setJsonBody(cleanJson);
260                 return policyAdapter;
261         }
262         
263         private String removeNullAttributes(String cleanJson) {
264                 ObjectMapper mapper = new ObjectMapper();
265
266                 try {
267                         JsonNode rootNode = mapper.readTree(cleanJson);
268                         JsonNode returnNode = mapper.readTree(cleanJson);
269                         Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields();
270                         boolean remove = false;
271                         JsonObject removed = null;
272                         boolean contentChanged = false;
273                         while (fieldsIterator.hasNext()) {
274                                 Map.Entry<String, JsonNode> field = fieldsIterator.next();
275                                 final String key = field.getKey();
276                                 final JsonNode value = field.getValue();
277                                 if("content".equalsIgnoreCase(key)){
278                                         String contentStr = value.toString();
279                                         try(JsonReader reader = Json.createReader(new StringReader(contentStr))){
280                                 JsonObject jsonContent = reader.readObject();                 
281                                                         removed = removeNull(jsonContent);
282                                                         if(!jsonContent.toString().equals(removed.toString())){
283                                                         contentChanged = true;  
284                                                 }
285                     }
286
287                                         if  (value==null || value.isNull()){
288                                                 ((ObjectNode) returnNode).remove(key);
289                                                 remove = true;
290                                         }
291                                 }
292                                 if (remove){
293                                         cleanJson = returnNode.toString();
294                                 }
295                                 if  (value==null || value.isNull()){
296                                         ((ObjectNode) returnNode).remove(key);
297                                         remove = true;
298                                 }
299                         }
300                         if (remove){
301                                 cleanJson = returnNode.toString();
302                         }
303                         
304                         if(contentChanged){
305                                 //set modified content to cleanJson
306                                 JSONObject  jObject  =  new JSONObject(cleanJson);      
307                                 jObject.put("content",removed.toString());
308                                 cleanJson = cleanUPJson(jObject.toString());
309                         }
310                         
311                 } catch (IOException e) {
312                         LOGGER.error("Error writing out the JsonNode",e);
313                 }
314                 return cleanJson;
315         }
316         
317         public static JsonArray removeNull(JsonArray array) {
318             JsonArrayBuilder builder = Json.createArrayBuilder();
319             int i = 0;
320             for (Iterator<JsonValue> it = array.iterator(); it.hasNext(); ++i) {
321                 JsonValue value = it.next();
322                 switch (value.getValueType()) {
323                 case ARRAY:
324                     JsonArray a = removeNull(array.getJsonArray(i));
325                     if (!a.isEmpty())
326                         builder.add(a);
327                     break;
328                 case OBJECT:
329                     JsonObject object = removeNull(array.getJsonObject(i));
330                     if (!object.isEmpty())
331                         builder.add(object);
332                     break;
333                 case STRING:
334                     String s = array.getString(i);
335                     if (s != null && !s.isEmpty())
336                         builder.add(s);
337                     break;
338                 case NUMBER:
339                     builder.add(array.getJsonNumber(i));
340                     break;
341                 case TRUE:
342                 case FALSE:
343                     builder.add(array.getBoolean(i));
344                     break;
345                 case NULL:
346                     break;
347                 }
348             }
349             return builder.build();
350         }
351
352         public static JsonObject removeNull(JsonObject obj) {
353             JsonObjectBuilder builder = Json.createObjectBuilder();
354             for (Iterator<Entry<String, JsonValue>> it = obj.entrySet().iterator(); it.hasNext();) {
355                 Entry<String, JsonValue> e = it.next();
356                 String key = e.getKey();
357                 JsonValue value = e.getValue();
358                 switch (value.getValueType()) {
359                 case ARRAY:
360                     JsonArray array = removeNull(obj.getJsonArray(key));
361                     if (!array.isEmpty())
362                         builder.add(key, array);
363                     break;
364                 case OBJECT:
365                     JsonObject object = removeNull(obj.getJsonObject(key));
366                     if (!object.isEmpty())
367                         builder.add(key, object);
368                     break;
369                 case STRING:
370                     String s = obj.getString(key);
371                     if (s != null && !s.isEmpty())
372                         builder.add(key, s);
373                     break;
374                 case NUMBER:
375                     builder.add(key, obj.getJsonNumber(key));
376                     break;
377                 case TRUE:
378                 case FALSE:
379                     builder.add(key, obj.getBoolean(key));
380                     break;
381                 case NULL:
382                     break;
383                 }
384             }
385             return builder.build();
386         }
387         
388         // Second index of dot should be returned. 
389         public int stringBetweenDots(String str){
390                 String stringToSearch=str;
391                 String[]ss=stringToSearch.split("\\.");
392                 if(ss!=null){
393                         int len= ss.length;
394                         if(len>2){
395                                 uniqueKeys.add(ss[2]);
396                         }
397                 }
398                 
399                 return uniqueKeys.size();
400         }
401         
402         public void stringBetweenDotsForDataFields(String str){
403                 String stringToSearch=str;
404                 String[]ss=stringToSearch.split("\\.");
405                 if(ss!=null){
406                         int len= ss.length;
407
408                         if(len>2){
409                                 uniqueDataKeys.add(ss[0]+"%"+ss[2]);
410                         }
411                 }
412         }
413         
414  
415         @SuppressWarnings("unchecked")
416         public Map<String, String> load(String fileName) throws IOException { 
417                 File newConfiguration = new File(fileName);
418                 Yaml yaml = new Yaml();
419                 Map<Object, Object> yamlMap = null;
420                 try(InputStream is = new FileInputStream(newConfiguration)){
421                         yamlMap = (Map<Object, Object>) yaml.load(is); 
422                 } catch (FileNotFoundException e) {
423                         LOGGER.error(e);
424                 }
425
426                 StringBuilder sb = new StringBuilder(); 
427                 Map<String, String> settings = new HashMap<>(); 
428                 if (yamlMap == null) { 
429                         return settings; 
430                 } 
431                 List<String> path = new ArrayList <>(); 
432                 serializeMap(settings, sb, path, yamlMap); 
433                 return settings; 
434         } 
435
436         public Map<String, String> load(byte[] source) { 
437                 Yaml yaml = new Yaml(); 
438                 @SuppressWarnings("unchecked")
439                 Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(Arrays.toString(source)); 
440                 StringBuilder sb = new StringBuilder(); 
441                 Map<String, String> settings = new HashMap <>(); 
442                 if (yamlMap == null) { 
443                         return settings; 
444                 } 
445                 List<String> path = new ArrayList <>(); 
446                 serializeMap(settings, sb, path, yamlMap); 
447                 return settings; 
448         } 
449
450         @SuppressWarnings({ "unchecked", "rawtypes" })
451         private void serializeMap(Map<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) { 
452                 for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) { 
453                         if (entry.getValue() instanceof Map) { 
454                                 path.add((String) entry.getKey()); 
455                                 serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue()); 
456                                 path.remove(path.size() - 1); 
457                         } else if (entry.getValue() instanceof List) { 
458                                 path.add((String) entry.getKey()); 
459                                 serializeList(settings, sb, path, (List) entry.getValue()); 
460                                 path.remove(path.size() - 1); 
461                         } else { 
462                                 serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue()); 
463                         } 
464                 } 
465         } 
466
467         @SuppressWarnings("unchecked")
468         private void serializeList(Map<String, String> settings, StringBuilder sb, List<String> path, List<String> yamlList) { 
469                 int counter = 0; 
470                 for (Object listEle : yamlList) { 
471                         if (listEle instanceof Map) { 
472                                 path.add(Integer.toString(counter)); 
473                                 serializeMap(settings, sb, path, (Map<Object, Object>) listEle); 
474                                 path.remove(path.size() - 1); 
475                         } else if (listEle instanceof List) { 
476                                 path.add(Integer.toString(counter)); 
477                                 serializeList(settings, sb, path, (List<String>) listEle); 
478                                 path.remove(path.size() - 1); 
479                         } else { 
480                                 serializeValue(settings, sb, path, Integer.toString(counter), listEle); 
481                         } 
482                         counter++; 
483                 } 
484         } 
485
486         private void serializeValue(Map<String, String> settings, StringBuilder sb, List<String> path, String name, Object value) { 
487                 if (value == null) { 
488                         return; 
489                 } 
490                 sb.setLength(0); 
491                 for (String pathEle : path) { 
492                         sb.append(pathEle).append('.'); 
493                 } 
494                 sb.append(name); 
495                 settings.put(sb.toString(), value.toString()); 
496         } 
497     
498         void parseDataAndPolicyNodes(Map<String,String> map){
499                 for(String key:map.keySet()){
500                         if(key.contains("policy.nodes.Root"))
501                         {
502                                 continue;
503                         }
504                         else if(key.contains("policy.nodes")){
505                                 String wordToFind = "policy.nodes.";
506                                 int indexForPolicyNode=key.indexOf(wordToFind);
507                                 String subNodeString= key.substring(indexForPolicyNode+13, key.length());
508
509                                 stringBetweenDots(subNodeString);
510                         }
511                         else if(key.contains("policy.data")){
512                                 String wordToFind="policy.data.";
513                                 int indexForPolicyNode=key.indexOf(wordToFind);
514                                 String subNodeString= key.substring(indexForPolicyNode+12, key.length());
515
516                                 stringBetweenDotsForDataFields(subNodeString);
517                         }
518                 }
519         }
520         
521         HashMap<String,String> parseDataNodes(Map<String,String> map){
522                 HashMap<String,String> dataMapForJson=new HashMap <>(); 
523                 matchableValues = new HashMap <>(); 
524                 for(String uniqueDataKey: uniqueDataKeys){
525                         if(uniqueDataKey.contains("%")){
526                                 String[] uniqueDataKeySplit= uniqueDataKey.split("%");
527                                 String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE;
528                                 String typeValue=map.get(findType);
529                                 LOGGER.info(typeValue);
530                                 if(typeValue != null && typeValue.equalsIgnoreCase(STRING)||
531                                                 typeValue.equalsIgnoreCase(INTEGER)
532                                   )
533                                 {
534                                         String findDefault=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+DEFAULT;
535                                         String defaultValue= map.get(findDefault);
536                                         LOGGER.info("defaultValue is:"+ defaultValue);
537                                         
538                                         String findRequired=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+REQUIRED;
539                                         String requiredValue= map.get(findRequired);
540                                         LOGGER.info("requiredValue is:"+ requiredValue);
541                                         
542                                         String matchable =DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+MATCHABLE;
543
544                                         String matchableValue= map.get(matchable);
545
546                                         if("true".equalsIgnoreCase(matchableValue)){                                            
547                                                 String key=uniqueDataKeySplit[uniqueDataKeySplit.length -1];
548                                                 matchableValues.put(key, "matching-true");                                              
549                                         }
550                                         
551                                         StringBuilder attributeIndividualStringBuilder= new StringBuilder();
552                                         attributeIndividualStringBuilder.append(typeValue+":defaultValue-");
553                                         attributeIndividualStringBuilder.append(defaultValue+":required-");
554                                         attributeIndividualStringBuilder.append(requiredValue+MANYFALSE);
555                                         dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString());         
556                                 }
557                                 else if(typeValue != null && typeValue.equalsIgnoreCase(LIST)){
558                                         
559
560                                         String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type";
561                                         String listValue=map.get(findList);
562                                         if(listValue!=null){
563                                                 LOGGER.info("Type of list is:"+ listValue);
564                                                 //Its userdefined
565                                                 if(listValue.contains(".")){
566                                                         String trimValue=listValue.substring(listValue.lastIndexOf('.')+1);
567                                                         StringBuilder referenceIndividualStringBuilder= new StringBuilder();
568                                                         referenceIndividualStringBuilder.append(trimValue+":MANY-true");
569                                                         dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
570                                                 }//Its string
571                                                 else{
572                                                         StringBuilder stringListItems= new StringBuilder();
573                                                         stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":MANY-false");
574                                                         dataMapForJson.put(uniqueDataKey, stringListItems.toString());
575                                                         dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=[");
576                                                         for(int i=0;i<10;i++){
577                                                                 String findConstraints= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.constraints.0.valid_values."+i;
578                                                                 String constraintsValue=map.get(findConstraints);
579                                                                 LOGGER.info(constraintsValue);
580                                                                 if(constraintsValue==null){
581                                                                         break;
582                                                                 }
583                                                                 else{
584                                                                         if(constraintsValue.contains("=")){
585                                                                                 constraintsValue = constraintsValue.replace("=", "equal-sign");
586                                                                         }
587                                                                         dataConstraints.add(constraintsValue);
588                                                                         dataListBuffer.append(constraintsValue+",");
589                                                                 }
590                                                         }
591                                                         dataListBuffer.append("]#");
592                                                         
593                                                         LOGGER.info(dataListBuffer);
594                                                 }
595                                         }
596                                 }
597                                 else{
598                                         String findUserDefined="data_types.policy.data."+uniqueDataKeySplit[0]+"."+"properties"+"."+uniqueDataKeySplit[1]+".type";
599                                         String userDefinedValue=map.get(findUserDefined);
600                                         String trimValue=userDefinedValue.substring(userDefinedValue.lastIndexOf('.')+1);
601                                         StringBuilder referenceIndividualStringBuilder= new StringBuilder();
602                                         referenceIndividualStringBuilder.append(trimValue+":MANY-false");
603                                         dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString());
604                                         
605                                 }
606                         }else{
607                                 matchableValues.put(uniqueDataKey, "matching-true");
608                         }
609                 }
610                 return dataMapForJson;
611         }
612         
613         void constructJsonForDataFields(HashMap<String,String> dataMapForJson){
614                 HashMap<String,HashMap<String,String>> dataMapKey= new HashMap <>();
615                 HashMap<String,String> hmSub;
616                 for(Map.Entry<String, String> entry: dataMapForJson.entrySet()){
617                         String uniqueDataKey= entry.getKey();
618                         String[] uniqueDataKeySplit=uniqueDataKey.split("%");
619                         String value= dataMapForJson.get(uniqueDataKey);
620                         if(dataMapKey.containsKey(uniqueDataKeySplit[0])){
621                                 hmSub = dataMapKey.get(uniqueDataKeySplit[0]);
622                                 hmSub.put(uniqueDataKeySplit[1], value);
623                         }
624                         else{
625                                 hmSub=new HashMap <>();
626                                 hmSub.put(uniqueDataKeySplit[1], value);
627                         }
628                                 
629                         dataMapKey.put(uniqueDataKeySplit[0], hmSub);
630                 }
631                                 
632                 JSONObject mainObject= new JSONObject();
633                 JSONObject json;
634                 for(Map.Entry<String,HashMap<String,String>> entry: dataMapKey.entrySet()){
635                         String s=entry.getKey();
636                         json= new JSONObject();
637                         HashMap<String,String> jsonHm=dataMapKey.get(s);
638                         for(Map.Entry<String,String> entryMap:jsonHm.entrySet()){
639                                 String key=entryMap.getKey();
640                                 json.put(key, jsonHm.get(key));
641                         }
642                         mainObject.put(s,json);
643                 }       
644                 Iterator<String> keysItr = mainObject.keys();
645                 while(keysItr.hasNext()) {
646                         String key = keysItr.next();
647                         String value = mainObject.get(key).toString();
648                         retmap.put(key, value);
649                 }
650                 
651                 LOGGER.info("#############################################################################");
652                 LOGGER.info(mainObject);
653                 LOGGER.info("###############################################################################"); 
654         }
655         
656         
657         HashMap<String,HashMap<String,String>> parsePolicyNodes(Map<String,String> map){
658                 HashMap<String,HashMap<String,String>> mapKey= new HashMap <>();
659                 for(String uniqueKey: uniqueKeys){
660                         HashMap<String,String> hm;
661
662                         for(Map.Entry<String,String> entry:map.entrySet()){
663                                 String key=entry.getKey();
664                                 if(key.contains(uniqueKey) && key.contains("policy.nodes")){
665                                         if(mapKey.containsKey(uniqueKey)){
666                                                 hm = mapKey.get(uniqueKey);
667                                                 String keyStr= key.substring(key.lastIndexOf('.')+1);
668                                                 String valueStr= map.get(key);
669                                                 if(("type").equals(keyStr)){
670                                                         if(!key.contains("entry_schema"))
671                                                         {
672                                                                 hm.put(keyStr,valueStr);
673                                                         }
674                                                 }else{
675                                                         hm.put(keyStr,valueStr);
676                                                 }
677
678                                         } else {
679                                                 hm = new HashMap <>();
680                                                 String keyStr= key.substring(key.lastIndexOf('.')+1);
681                                                 String valueStr= map.get(key);
682                                                 if(("type").equals(keyStr)){
683                                                         if(!key.contains("entry_schema"))
684                                                         {
685                                                                 hm.put(keyStr,valueStr);
686                                                         }
687                                                 }else{
688                                                         hm.put(keyStr,valueStr);
689                                                 }
690                                                 mapKey.put(uniqueKey, hm);
691                                         }
692                                 }
693                         }
694                 }
695                 return mapKey;
696         }
697
698         void createAttributes(HashMap<String,HashMap<String,String>> mapKey){
699                 StringBuilder attributeStringBuilder= new StringBuilder();
700                 StringBuilder referenceStringBuilder= new StringBuilder();
701                 StringBuilder listBuffer= new StringBuilder();
702                 List<String> constraints= new ArrayList<>();
703                 for(Map.Entry<String,HashMap<String,String>> entry: mapKey.entrySet()){
704                         String keySetString= entry.getKey();
705                         HashMap<String,String> keyValues=mapKey.get(keySetString);
706                         if(keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(STRING)||
707                                         keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(INTEGER)
708                                         ){
709                                 StringBuilder attributeIndividualStringBuilder= new StringBuilder();
710                                 attributeIndividualStringBuilder.append(keySetString+"=");
711                                 attributeIndividualStringBuilder.append(keyValues.get("type")+":defaultValue-");
712                                 attributeIndividualStringBuilder.append(keyValues.get("default")+":required-");
713                                 attributeIndividualStringBuilder.append(keyValues.get("required")+":MANY-false");
714                                 attributeStringBuilder.append(attributeIndividualStringBuilder+",");    
715                 if("true".equalsIgnoreCase(keyValues.get("matchable"))){
716                                     matchableValues.put(keySetString, "matching-true");
717                 }
718                         }
719                         else if(keyValues.get("type") != null && keyValues.get("type").equalsIgnoreCase(LIST)){
720                                 
721                 if("true".equalsIgnoreCase(keyValues.get("matchable"))){
722                                     matchableValues.put(keySetString, "matching-true");
723                 }
724                                 //List Datatype
725                                 Set<String> keys= keyValues.keySet();
726                                 Iterator<String> itr=keys.iterator();
727                                 boolean isDefinedType = false;
728                                 while(itr.hasNext()){
729                                         String key= itr.next();
730                                         if(!("type").equals(key) ||("required").equals(key))
731                                         {
732                                                 String value= keyValues.get(key);
733                                                 //The "." in the value determines if its a string or a user defined type.  
734                                                 if (!value.contains(".")){
735                                                         //This is string
736                                                         if(StringUtils.isNumeric(key) ){  //only integer key for the value of Constrains 
737                                                             constraints.add(keyValues.get(key));
738                                                         }
739                                                 }else{
740                                                         //This is user defined type
741                                                         String trimValue=value.substring(value.lastIndexOf('.')+1);
742                                                         StringBuilder referenceIndividualStringBuilder= new StringBuilder();
743                                                         referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-true");
744                                                         referenceStringBuilder.append(referenceIndividualStringBuilder+",");
745                                                         isDefinedType = true;
746                                                 }
747                                         }                               
748
749                                 }
750
751                                 if(!isDefinedType && keyValues.get("type").equalsIgnoreCase(LIST) &&
752                                         (constraints == null || constraints.isEmpty()) ) {   //type is list but no constraints defined.
753                                                 referenceStringBuilder.append(keySetString+"=MANY-true"+",");
754                                 }
755                         }else{
756                                 //User defined Datatype. 
757                 if("true".equalsIgnoreCase(keyValues.get("matchable"))){
758                                     matchableValues.put(keySetString, "matching-true");
759                 }
760                                 String value=keyValues.get("type");
761                                 if(value != null && !value.isEmpty()){
762                                         String trimValue=value.substring(value.lastIndexOf('.')+1);
763                                         StringBuilder referenceIndividualStringBuilder= new StringBuilder();
764                                         referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-false");
765                                         referenceStringBuilder.append(referenceIndividualStringBuilder+",");
766                                 }else{
767                                         LOGGER.info("keyValues.get(type) is null/empty");
768                                 }
769
770                         }
771                         if(constraints!=null && ! constraints.isEmpty()){
772                                 //List handling. 
773                                 listBuffer.append(keySetString.toUpperCase()+"=[");
774                                 for(String str:constraints){
775                                         listBuffer.append(str+",");
776                                 }
777                                 listBuffer.append("]#");
778                                 LOGGER.info(listBuffer);
779
780
781                                 StringBuilder referenceIndividualStringBuilder= new StringBuilder();
782                                 referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+":MANY-false");
783                                 referenceStringBuilder.append(referenceIndividualStringBuilder+",");
784                                 constraints.clear();
785                         }
786                 }
787                 
788                 dataListBuffer.append(listBuffer);
789                 
790
791                 LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
792                 LOGGER.info("Whole attribute String is:"+attributeStringBuilder);       
793                 LOGGER.info("Whole reference String is:"+referenceStringBuilder);
794                 LOGGER.info("List String is:"+listBuffer);
795                 LOGGER.info("Data list buffer is:"+dataListBuffer);
796                 LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
797                 
798                 this.listConstraints=dataListBuffer.toString();
799                 this.referenceAttributes=referenceStringBuilder.toString();
800                 this.attributeString=attributeStringBuilder.toString();
801         }
802         
803         
804     
805         public void parseTosca (String fileName){
806                 Map<String,String> map= new HashMap<>();
807     
808         try {
809                         map=load(fileName);
810                         
811                         parseDataAndPolicyNodes(map);
812                         
813                         HashMap<String,String> dataMapForJson=parseDataNodes(map);
814                         
815                         constructJsonForDataFields(dataMapForJson);     
816                         
817                         HashMap<String,HashMap<String,String>> mapKey= parsePolicyNodes(map);
818                         
819                         createAttributes(mapKey);
820                 
821         } catch (IOException e) {
822                 LOGGER.error(e);
823         }
824         
825         } 
826
827         private String cleanUPJson(String json) {
828                 String cleanJson = StringUtils.replaceEach(json, new String[]{"\\\\", "\\\\\\", "\\\\\\\\"}, new String[]{"\\", "\\", "\\"});
829                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\"}, new String[]{"\\"});
830                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\", "[[", "]]"}, new String[]{"\\", "[", "]"});
831                 
832                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\"", "\\\"", "\"[{", "}]\""}, new String[]{"\"", "\"", "[{", "}]"});
833                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[{", "}]\""}, new String[]{"[{", "}]"});
834                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
835                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"{", "}\""}, new String[]{"{", "}"});
836                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\"\"", "\"\""}, new String[]{"\"", "\""});
837                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\""}, new String[]{""});
838                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\""}, new String[]{"\""});
839                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\\\\\\"}, new String[]{"\""});
840                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\\""}, new String[]{"\""});
841                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
842                 return cleanJson;
843         }
844         
845         private JSONObject decodeContent(JsonNode jsonNode){
846                 Iterator<JsonNode> jsonElements = jsonNode.elements();
847                 Iterator<String> jsonKeys = jsonNode.fieldNames();
848                 Map<String,String> element = new TreeMap<>();
849                 while(jsonElements.hasNext() && jsonKeys.hasNext()){
850                         element.put(jsonKeys.next(), jsonElements.next().toString());
851                 }
852                 JSONObject jsonResult = new JSONObject();
853                 JSONArray jsonArray = null;
854                 String oldValue = null;
855                 String nodeKey = null;
856                 String arryKey = null;
857                 Boolean isArray = false;
858                 JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
859                 ObjectNode node = nodeFactory.objectNode();
860                 String prevKey = null;
861                 String presKey;
862                 for(Entry<String, String> entry: element.entrySet()){
863                         String key = entry.getKey();
864                         String value = entry.getValue();
865                         if(key.contains(".")){
866                                 presKey = key.substring(0,key.indexOf('.'));
867                         }else if(key.contains("@")){
868                                 presKey = key.substring(0,key.indexOf('@'));
869                         }else{
870                                 presKey = key;
871                         }
872                         // first check if we are different from old.
873                         LOGGER.info(key+"\n");
874                         if(jsonArray!=null && jsonArray.length()>0 && key.contains("@") && !key.contains(".") && oldValue!=null){
875                                 if(!oldValue.equals(key.substring(0,key.indexOf('@')))){
876                                         jsonResult.put(oldValue, jsonArray);
877                                         jsonArray = new JSONArray();
878                                 }
879                         }else if(jsonArray!=null && jsonArray.length()>0 && !presKey.equals(prevKey) && oldValue!=null){ 
880                                 jsonResult.put(oldValue, jsonArray);
881                                 isArray = false;
882                                 jsonArray = new JSONArray();
883                         }
884         
885                         prevKey = presKey;
886                         // 
887                         if(key.contains(".")){
888                                 if(nodeKey==null){
889                                         nodeKey = key.substring(0,key.indexOf('.'));
890                                 }
891                                 if(nodeKey.equals(key.substring(0,key.indexOf('.')))){
892                                         node.put(key.substring(key.indexOf('.')+1), value);
893                                 }else{
894                                         if(node.size()!=0){
895                                                 if(nodeKey.contains("@")){
896                                                         if(arryKey==null){
897                                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
898                                                         }
899                                                         if(nodeKey.endsWith("@0")){
900                                                                 isArray = true;
901                                                                 jsonArray = new JSONArray();
902                                                         }
903                                                         if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf('@')))){
904                                                                 jsonArray.put(decodeContent(node));
905                                                         } 
906                                                         if((key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf('@')))) || !key.contains("@")){
907                                                                 jsonResult.put(arryKey, jsonArray);
908                                                                 jsonArray = new JSONArray();
909                                                         }
910                                                         arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
911                                                 }else{
912                                                         isArray = false;
913                                                         jsonResult.put(nodeKey, decodeContent(node));
914                                                 }
915                                                 node = nodeFactory.objectNode();
916                                         }
917                                         nodeKey = key.substring(0,key.indexOf('.'));
918                                         if(nodeKey.contains("@")){
919                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
920                                         }
921                                         node.put(key.substring(key.indexOf('.')+1), value);
922                                 }
923                         }else {
924                                 if(node.size()!=0){
925                                         if(nodeKey.contains("@")){
926                                                 if(arryKey==null){
927                                                         arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
928                                                 }
929                                                 if(nodeKey.endsWith("@0")){
930                                                         isArray = true;
931                                                         jsonArray = new JSONArray();
932                                                 }
933                                                 if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf('@')))){
934                                                         jsonArray.put(decodeContent(node));
935                                                 }
936                                                 jsonResult.put(arryKey, jsonArray);
937                                                 jsonArray = new JSONArray();
938                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
939                                         }else{
940                                                 isArray = false;
941                                                 jsonResult.put(nodeKey, decodeContent(node));
942                                         }
943                                         node = nodeFactory.objectNode();
944                                 }
945                                 if(key.contains("@")){
946                                         isArray = true;
947                                         if(key.endsWith("@0")|| jsonArray==null){
948                                                 jsonArray = new JSONArray();
949                                         }
950                                 }else if(!key.contains("@")){
951                                         isArray = false;
952                                 }
953                                 if(isArray){
954                                         if(oldValue==null){
955                                                 oldValue = key.substring(0,key.indexOf('@'));
956                                         }
957                                         if(oldValue!=prevKey){
958                                                 oldValue = key.substring(0,key.indexOf('@'));
959                                         }
960                                         if(oldValue.equals(key.substring(0,key.indexOf('@')))){
961                                                 jsonArray.put(value);
962                                         }else{
963                                                 jsonResult.put(oldValue, jsonArray);
964                                                 jsonArray = new JSONArray();
965                                         }
966                                         oldValue = key.substring(0,key.indexOf('@'));
967                                 }else{
968                                         jsonResult.put(key, value);
969                                 }
970                         }
971                 }
972                 if(node.size()>0){
973                         if(nodeKey.contains("@")){
974                                 if(jsonArray==null){
975                                         jsonArray = new JSONArray();
976                                 }
977                                 if(arryKey==null){
978                                         arryKey = nodeKey.substring(0,nodeKey.indexOf('@'));
979                                 }
980                                 jsonArray.put(decodeContent(node));
981                                 jsonResult.put(arryKey, jsonArray);
982                                 isArray = false;
983                         }else{
984                                 jsonResult.put(nodeKey, decodeContent(node));
985                         }
986                 }
987                 if(isArray && jsonArray.length() > 0){
988                         jsonResult.put(oldValue, jsonArray);
989                 }
990                 return jsonResult;
991         }
992         
993         @RequestMapping(value={"/policyController/getDCAEMSTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
994         public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response) throws IOException{
995                 ObjectMapper mapper = new ObjectMapper();
996                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
997                 JsonNode root = mapper.readTree(request.getReader());
998
999                 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
1000                 String  servicename = value.split("-v")[0];
1001                 String version = null;
1002                 if (value.contains("-v")){
1003                         version = value.split("-v")[1];
1004                 }
1005                 MicroServiceModels returnModel = getAttributeObject(servicename, version);
1006                 
1007                 
1008                 //Get all keys with "MANY-true" defined in their value from subAttribute 
1009                 Set<String> allkeys = null;
1010                 if(returnModel.getSub_attributes() != null && !returnModel.getSub_attributes().isEmpty()){
1011                         JSONObject json = new JSONObject(returnModel.getSub_attributes());      
1012                         getAllKeys(json); 
1013                         allkeys = allManyTrueKeys;
1014                         allManyTrueKeys = new  HashSet <>();
1015                         LOGGER.info("allkeys : " + allkeys);
1016                 }
1017                 
1018                 String nameOfTrueKey = "";
1019                 if(allkeys != null){
1020                         nameOfTrueKey = allkeys.toString();
1021                 }
1022                 
1023                 String jsonModel = createMicroSeriveJson(returnModel, allkeys);
1024                 
1025                 JSONObject jsonObject = new JSONObject(jsonModel);
1026                 
1027                 JSONObject finalJsonObject = null;
1028                 if(allkeys != null){
1029                         Iterator<String> iter = allkeys.iterator();
1030                         while(iter.hasNext()){
1031                                 //Convert to array values for MANY-true keys
1032                                 finalJsonObject = convertToArrayElement(jsonObject, iter.next());
1033                         }
1034                 }
1035
1036                 if(finalJsonObject != null){
1037                     LOGGER.info(finalJsonObject.toString());
1038                     jsonModel  = finalJsonObject.toString();
1039                 }
1040                 
1041                 //get all properties with "MANY-true" defined in Ref_attributes
1042                 Set<String> manyTrueProperties = getManyTrueProperties(returnModel.getRef_attributes());
1043                 if(manyTrueProperties != null){
1044                         JSONObject jsonObj = new JSONObject(jsonModel);
1045                         for (String s : manyTrueProperties) {
1046                                 LOGGER.info(s);
1047                                 //convert to array element for MANY-true properties
1048                                 finalJsonObject = convertToArrayElement(jsonObj, s.trim());
1049                         }
1050                         
1051                         if(finalJsonObject != null){
1052                             LOGGER.info(finalJsonObject.toString());
1053                             jsonModel = finalJsonObject.toString();
1054                         }
1055                 }
1056                 
1057                 response.setCharacterEncoding("UTF-8");
1058                 response.setContentType("application / json");
1059                 request.setCharacterEncoding("UTF-8");
1060                 List<Object>  list = new ArrayList<>();
1061                 PrintWriter out = response.getWriter();
1062                 String responseString = mapper.writeValueAsString(returnModel);
1063                 JSONObject j;
1064                 if("".equals(nameOfTrueKey)){
1065                         j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}");     
1066                 }else{
1067                         j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + ",allManyTrueKeys: " + nameOfTrueKey+ "}");       
1068                 }
1069                 list.add(j);
1070                 out.write(list.toString());
1071                 return null;
1072         }
1073         
1074         @SuppressWarnings({ "unchecked", "rawtypes" })
1075         private String createMicroSeriveJson(MicroServiceModels returnModel, Set<String> allkeys) {
1076                 Map<String, String> attributeMap = new HashMap<>();
1077                 Map<String, String> refAttributeMap = new HashMap<>();
1078                 String attribute = returnModel.getAttributes();
1079                 if(attribute != null){
1080                         attribute = attribute.trim();
1081                 }
1082                 String refAttribute = returnModel.getRef_attributes();
1083                 if(refAttribute != null){
1084                         refAttribute = refAttribute.trim();
1085                 }
1086                 String enumAttribute = returnModel.getEnumValues();
1087                 if(enumAttribute != null){
1088                         enumAttribute = enumAttribute.trim();
1089                 }
1090                 if (!StringUtils.isEmpty(attribute)){
1091                         attributeMap = convert(attribute, ",");
1092                 }
1093                 if (!StringUtils.isEmpty(refAttribute)){
1094                         refAttributeMap = convert(refAttribute, ",");
1095                 }
1096
1097                 Gson gson = new Gson();
1098                 
1099                 String subAttributes = returnModel.getSub_attributes();
1100                 if(subAttributes != null){
1101                         subAttributes = subAttributes.trim();
1102                 }else{
1103                         subAttributes = "";
1104                 }
1105                 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
1106                 
1107                 JSONObject object = new JSONObject();
1108                 JSONArray array;
1109                 
1110                 for (Entry<String, String> keySet : attributeMap.entrySet()){
1111                         array = new JSONArray();
1112                         String value = keySet.getValue();
1113                         if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){
1114                                 array.put(value);
1115                                 object.put(keySet.getKey().trim(), array);
1116                         }else {
1117                                 object.put(keySet.getKey().trim(), value.trim());
1118                         }
1119                 }
1120                 
1121                 for (Entry<String, String> keySet : refAttributeMap.entrySet()){
1122                         array = new JSONArray();
1123                         String value = keySet.getValue().split(":")[0];
1124                         if (gsonObject.containsKey(value)){
1125                                 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){
1126                                         array.put(recursiveReference(value, gsonObject, enumAttribute));
1127                                         object.put(keySet.getKey().trim(), array);
1128                                 }else {
1129                                         object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
1130                                 }
1131                         }else {
1132                                 if ("true".equalsIgnoreCase(keySet.getValue().split("MANY-")[1])){
1133                                         array.put(value.trim());
1134                                         object.put(keySet.getKey().trim(), array);
1135                                 }else {
1136                                         object.put(keySet.getKey().trim(), value.trim()); 
1137                                 }
1138                         }
1139                 }
1140                 
1141                 
1142
1143                 return object.toString();
1144         }
1145
1146         @SuppressWarnings("unchecked")
1147         private JSONObject recursiveReference(String name, Map<String,String> subAttributeMap, String enumAttribute) {
1148                 JSONObject object = new JSONObject();
1149                 Map<String, String> map;
1150                 Object returnClass = subAttributeMap.get(name);
1151                 map = (Map<String, String>) returnClass; 
1152                 JSONArray array;
1153                 
1154                 for( Entry<String, String> m:map.entrySet()){  
1155                         String[] splitValue = m.getValue().split(":");
1156                         array = new JSONArray();
1157                         if (subAttributeMap.containsKey(splitValue[0])){
1158                                 if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])){
1159                                         array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
1160                                         object.put(m.getKey().trim(), array);
1161                                 }else {
1162                                         object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
1163                                 }
1164                         } else{
1165                                 if ("true".equalsIgnoreCase(m.getValue().split("MANY-")[1])){
1166                                         array.put(splitValue[0].trim());
1167                                         object.put(m.getKey().trim(), array);
1168                                 }else {
1169                                         object.put(m.getKey().trim(), splitValue[0].trim());
1170                                 }
1171                         }
1172                   }  
1173                 
1174                 return object;
1175         }
1176         
1177         
1178         public static JSONObject convertToArrayElement(JSONObject json, String keyValue) {
1179             return convertToArrayElement(json, new HashSet<>(), keyValue);
1180         }
1181         
1182         private static JSONObject convertToArrayElement(JSONObject json, Set<String> keys, String keyValue) {
1183             for (String key : json.keySet()) {
1184                 Object obj = json.get(key);
1185                 if(key.equals(keyValue.trim())){
1186                         if(!(obj instanceof JSONArray)){
1187                                 JSONArray newJsonArray = new JSONArray();
1188                                 newJsonArray.put(obj);
1189                                 json.put(key, newJsonArray);
1190                         }
1191                         LOGGER.info("key : " + key);
1192                         LOGGER.info("obj : " + obj);
1193                         LOGGER.info("json.get(key) : " + json.get(key));
1194                         LOGGER.info("keyValue : " + keyValue);
1195                     keys.addAll(json.keySet());
1196                     
1197                     return json;
1198                 }
1199
1200                 if (obj instanceof JSONObject) convertToArrayElement(json.getJSONObject(key), keyValue);            
1201             }
1202
1203             return json;
1204         }
1205         
1206         // call this method to get all MANY-true properties 
1207         public static Set<String> getManyTrueProperties(String referAttributes){
1208                 LOGGER.info("referAttributes : " + referAttributes);
1209                 Set<String> manyTrueProperties = new HashSet<>();
1210                 
1211                 if(referAttributes != null){
1212                         String[] referAarray = referAttributes.split(",");
1213                         String []element;
1214                         for(int i=0; i<referAarray.length; i++){
1215                                 element = referAarray[i].split("=");      
1216                                 if(element.length > 1 && element[1].contains("MANY-true")){
1217                                         manyTrueProperties.add(element[0]);
1218                                 }
1219                         }               
1220                 }
1221                 
1222                 return manyTrueProperties;
1223         }
1224         
1225         //call this method to start the recursive
1226         private Set<String> getAllKeys(JSONObject json) {
1227             return getAllKeys(json, new HashSet<>());
1228         }
1229
1230         private Set<String> getAllKeys(JSONArray arr) {
1231             return getAllKeys(arr, new HashSet<>());
1232         }
1233
1234         private Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
1235             for (int i = 0; i < arr.length(); i++) {
1236                 Object obj = arr.get(i);
1237                 if (obj instanceof JSONObject) keys.addAll(getAllKeys(arr.getJSONObject(i)));
1238                 if (obj instanceof JSONArray) keys.addAll(getAllKeys(arr.getJSONArray(i)));
1239             }
1240
1241             return keys;
1242         }
1243     // this method returns a set of keys with "MANY-true" defined in their value.
1244         private Set<String> getAllKeys(JSONObject json, Set<String> keys) {
1245             for (String key : json.keySet()) {
1246                 Object obj = json.get(key);
1247                 if(obj instanceof String && ((String) obj).contains("MANY-true")){
1248                         LOGGER.info("key : " + key);
1249                         LOGGER.info("obj : " + obj);
1250                         allManyTrueKeys.add(key);
1251                 }
1252                 if (obj instanceof JSONObject) keys.addAll(getAllKeys(json.getJSONObject(key)));
1253                 if (obj instanceof JSONArray) keys.addAll(getAllKeys(json.getJSONArray(key)));
1254             }
1255
1256             return keys;
1257         }
1258
1259         
1260         @RequestMapping(value={"/policyController/getModelServiceVersioneData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
1261         public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response) throws IOException{
1262                 ObjectMapper mapper = new ObjectMapper();
1263                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
1264                 JsonNode root = mapper.readTree(request.getReader());
1265
1266                 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
1267                 String  servicename = value.split("-v")[0];
1268                 Set<String> returnList = getVersionList(servicename);
1269                 
1270                 response.setCharacterEncoding("UTF-8");
1271                 response.setContentType("application / json");
1272                 request.setCharacterEncoding("UTF-8");
1273         List<Object>  list = new ArrayList<>();
1274                 PrintWriter out = response.getWriter();
1275                 String responseString = mapper.writeValueAsString(returnList);
1276                 JSONObject j = new JSONObject("{dcaeModelVersionData: " + responseString +"}");
1277                 list.add(j);
1278                 out.write(list.toString());
1279                 return null;
1280         }
1281
1282         private Set<String> getVersionList(String name) {       
1283                 MicroServiceModels workingModel;
1284                 Set<String> list = new HashSet<>();
1285                 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1286                 for (int i = 0; i < microServiceModelsData.size(); i++) {
1287                         workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1288                         if (workingModel.getVersion()!=null){
1289                                 list.add(workingModel.getVersion());
1290                         }else{
1291                                 list.add("Default");
1292                         }
1293                 }
1294                 return list;
1295         }
1296         
1297         private MicroServiceModels getAttributeObject(String name, String version) {    
1298                 MicroServiceModels workingModel = new MicroServiceModels();
1299                 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
1300                 for (int i = 0; i < microServiceModelsData.size(); i++) {
1301                         workingModel = (MicroServiceModels) microServiceModelsData.get(i);
1302                         if(version != null){
1303                                 if (workingModel.getVersion()!=null){
1304                                         if (workingModel.getVersion().equals(version)){
1305                                                 return workingModel;
1306                                         }
1307                                 }else{
1308                                         return workingModel;
1309                                 }
1310                         }else{
1311                                 return workingModel;
1312                         }
1313                         
1314                 }
1315                 return workingModel;
1316         }
1317
1318         @RequestMapping(value={"/get_DCAEPriorityValues"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
1319         public void getDCAEPriorityValuesData(HttpServletRequest request, HttpServletResponse response){
1320                 try{
1321                         Map<String, Object> model = new HashMap<>();
1322                         ObjectMapper mapper = new ObjectMapper();
1323                         List<String> priorityList = new ArrayList<>();
1324                         priorityCount = 10;
1325                         for (int i = 1; i < priorityCount; i++) {
1326                                 priorityList.add(String.valueOf(i));
1327                         }
1328                         model.put("priorityDatas", mapper.writeValueAsString(priorityList));
1329                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
1330                         JSONObject j = new JSONObject(msg);
1331                         response.getWriter().write(j.toString());
1332                 }
1333                 catch (Exception e){
1334                         LOGGER.error(e);
1335                 }
1336         }
1337
1338         public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1339                 if (policyAdapter.getPolicyData() instanceof PolicyType) {
1340                         Object policyData = policyAdapter.getPolicyData();
1341                         PolicyType policy = (PolicyType) policyData;
1342                         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
1343                         String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("MS_") +3);
1344                         policyAdapter.setPolicyName(policyNameValue);
1345                         String description = "";
1346                         try{
1347                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
1348                         }catch(Exception e){
1349                             LOGGER.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue ,e);
1350                                 description = policy.getDescription();
1351                         }
1352                         policyAdapter.setPolicyDescription(description);
1353                         // Get the target data under policy.
1354                         TargetType target = policy.getTarget();
1355                         if (target != null) {
1356                                 // Under target we have AnyOFType
1357                                 List<AnyOfType> anyOfList = target.getAnyOf();
1358                                 if (anyOfList != null) {
1359                                         Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
1360                                         while (iterAnyOf.hasNext()) {
1361                                                 AnyOfType anyOf = iterAnyOf.next();
1362                                                 // Under AnyOFType we have AllOFType
1363                                                 List<AllOfType> allOfList = anyOf.getAllOf();
1364                                                 if (allOfList != null) {
1365                                                         Iterator<AllOfType> iterAllOf = allOfList.iterator();
1366                                                         while (iterAllOf.hasNext()) {
1367                                                                 AllOfType allOf = iterAllOf.next();
1368                                                                 // Under AllOFType we have Match
1369                                                                 List<MatchType> matchList = allOf.getMatch();
1370                                                                 if (matchList != null) {
1371                                                                         Iterator<MatchType> iterMatch = matchList.iterator();
1372                                                                         while (matchList.size()>1 && iterMatch.hasNext()) {
1373                                                                                 MatchType match = iterMatch.next();
1374                                                                                 //
1375                                                                                 // Under the match we have attribute value and
1376                                                                                 // attributeDesignator. So,finally down to the actual attribute.
1377                                                                                 //
1378                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
1379                                                                                 String value = (String) attributeValue.getContent().get(0);
1380                                                                                 AttributeDesignatorType designator = match.getAttributeDesignator();
1381                                                                                 String attributeId = designator.getAttributeId();
1382                                                                                 // First match in the target is OnapName, so set that value.
1383                                                                                 if ("ONAPName".equals(attributeId)) {
1384                                                                                         policyAdapter.setOnapName(value);
1385                                                                                 }
1386                                                                                 if ("ConfigName".equals(attributeId)){
1387                                                                                         policyAdapter.setConfigName(value);
1388                                                                                 }
1389                                                                                 if ("uuid".equals(attributeId)){
1390                                                                                         policyAdapter.setUuid(value);
1391                                                                                 }
1392                                                                                 if ("location".equals(attributeId)){
1393                                                                                         policyAdapter.setLocation(value);
1394                                                                                 }
1395                                                                                 if ("RiskType".equals(attributeId)){
1396                                                                                         policyAdapter.setRiskType(value);
1397                                                                                 }
1398                                                                                 if ("RiskLevel".equals(attributeId)){
1399                                                                                         policyAdapter.setRiskLevel(value);
1400                                                                                 }
1401                                                                                 if ("guard".equals(attributeId)){
1402                                                                                         policyAdapter.setGuard(value);
1403                                                                                 }
1404                                                                                 if ("TTLDate".equals(attributeId) && !value.contains("NA")){
1405                                                                                         PolicyController controller = new PolicyController();
1406                                                                                         String newDate = controller.convertDate(value);
1407                                                                                         policyAdapter.setTtlDate(newDate);
1408                                                                                 }
1409                                                                         }
1410                                                                         readFile(policyAdapter, entity);
1411                                                                 }
1412                                                         }
1413                                                 }
1414                                         }
1415                                 }
1416                         }
1417                 }
1418         }
1419         
1420         public static Map<String, String> convert(String str, String split) {
1421                 Map<String, String> map = new HashMap<>();
1422                 for(final String entry : str.split(split)) {
1423                     String[] parts = entry.split("=");
1424                     map.put(parts[0], parts[1]);
1425                 }
1426                 return map;
1427         }
1428
1429
1430         @SuppressWarnings("unchecked")
1431         private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1432                 String policyScopeName = null;
1433                 ObjectMapper mapper = new ObjectMapper();
1434                 try {
1435                         DCAEMicroServiceObject msBody = mapper.readValue(entity.getConfigurationData().getConfigBody(), DCAEMicroServiceObject.class);
1436                         policyScopeName = getPolicyScope(msBody.getPolicyScope());
1437                         policyAdapter.setPolicyScope(policyScopeName);
1438
1439                         policyAdapter.setPriority(msBody.getPriority());
1440
1441                         if (msBody.getVersion()!= null){
1442                                 policyAdapter.setServiceType(msBody.getService());
1443                                 policyAdapter.setVersion(msBody.getVersion());
1444                         }else{
1445                                 policyAdapter.setServiceType(msBody.getService());
1446                         }
1447                         if(msBody.getContent() != null){
1448                                 LinkedHashMap<String, Object>  data = new LinkedHashMap<>();
1449                                 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) msBody.getContent();
1450                                 readRecursivlyJSONContent(map, data);
1451                                 policyAdapter.setRuleData(data);
1452                         }
1453
1454                 } catch (Exception e) {
1455                         LOGGER.error(e);
1456                 }
1457
1458         }
1459
1460         @SuppressWarnings({ "rawtypes", "unchecked" })
1461         private void readRecursivlyJSONContent(LinkedHashMap<String, ?> map, LinkedHashMap<String, Object> data){
1462                 for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
1463                         Object key =  iterator.next();
1464                         Object value = map.get(key);
1465                         if(value instanceof LinkedHashMap<?, ?>){
1466                                 LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<>(); 
1467                                 readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
1468                                 for( Entry<String, Object> entry : secondObjec.entrySet()){
1469                                         data.put(key+"." + entry.getKey(), entry.getValue());
1470                                 }
1471                         }else if(value instanceof ArrayList){
1472                                 ArrayList<?> jsonArrayVal = (ArrayList<?>)value;
1473                                 for(int i = 0; i < jsonArrayVal.size(); i++){
1474                                         Object arrayvalue = jsonArrayVal.get(i);
1475                                         if(arrayvalue instanceof LinkedHashMap<?, ?>){
1476                                                 LinkedHashMap<String, Object> newData = new LinkedHashMap<>();   
1477                                                 readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
1478                                                 for(Entry<String, Object> entry: newData.entrySet()){
1479                                                         data.put(key+"@"+i+"." +entry.getKey(), entry.getValue());
1480                                                 }
1481                                         }else if(arrayvalue instanceof ArrayList){
1482                                                 ArrayList<?> jsonArrayVal1 = (ArrayList<?>)value;
1483                                                 for(int j = 0; j < jsonArrayVal1.size(); j++){
1484                                                         Object arrayvalue1 = jsonArrayVal1.get(i);
1485                                                         data.put(key+"@"+j, arrayvalue1.toString());
1486                                                 }       
1487                                         }else{
1488                                                 data.put(key+"@"+i, arrayvalue.toString());
1489                                         }       
1490                                 }
1491                         }else{
1492                                 data.put(key.toString(), value.toString());
1493                         }       
1494                 }
1495         }
1496
1497         private String getPolicyScope(String value) {
1498                 List<Object> groupList= commonClassDao.getDataById(GroupPolicyScopeList.class, "groupList", value);
1499                 if(groupList != null && !groupList.isEmpty()){
1500                         GroupPolicyScopeList pScope = (GroupPolicyScopeList) groupList.get(0);
1501                         return pScope.getGroupName();           
1502                 }
1503                 return null;
1504         }
1505
1506         //Convert the map values and set into JSON body
1507         public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
1508                 Map<String, String> attribute = new HashMap<>();
1509                 StringBuilder temp;
1510                 String key;
1511                 String value;
1512                 for (Entry<String, String> entry : attributesMap.entrySet()) {
1513                         key = entry.getKey();
1514                         value = entry.getValue();
1515                         attribute.put(key, value);
1516                 }
1517                 for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
1518                         key = entryRef.getKey();
1519                         value = entryRef.getValue();
1520                         attribute.put(key, value);
1521                 }
1522                 for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
1523                         key = entryList.getKey();
1524                         value = entryList.getValue();
1525                         attribute.put(key, value);
1526                 }
1527                 for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()){
1528                         key = arrayList.getKey();
1529                         temp = null;
1530                         for (Object textList : arrayList.getValue()){
1531                                 if (temp == null){
1532                                         temp = new StringBuilder();
1533                                         temp.append("[" + textList);
1534                                 }else{
1535                                         temp.append("," + textList);
1536                                 }
1537                         }
1538                         attribute.put(key, temp+ "]");                  
1539                 }
1540
1541                 return  attribute;
1542         }
1543         
1544         @RequestMapping(value={"/ms_dictionary/set_MSModelData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
1545         public void SetMSModelData(HttpServletRequest request, HttpServletResponse response) throws IOException, FileUploadException{
1546                 modelList = new ArrayList<>();
1547                 dirDependencyList = new ArrayList<>();
1548                 classMap = new HashMap<>();
1549                 retmap = new HashMap<>();
1550                 uniqueKeys= new HashSet<>();
1551                 uniqueDataKeys= new HashSet<>();
1552                 dataListBuffer=new StringBuilder();
1553                 dataConstraints= new ArrayList <>();
1554                 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
1555                 boolean zip = false;
1556                 boolean yml= false;
1557                 String errorMsg = "";
1558                 for (FileItem item : items) {
1559                         if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")||item.getName().endsWith(".yml")){
1560                                 this.newModel = new MicroServiceModels();
1561                                 try{
1562                                         File file = new File(item.getName());
1563                                         OutputStream outputStream = new FileOutputStream(file);
1564                                         IOUtils.copy(item.getInputStream(), outputStream);
1565                                         outputStream.close();
1566                                         this.newFile = file.toString();
1567                                         this.newModel.setModelName(this.newFile.split("-v")[0]);
1568                                 
1569                                         if (this.newFile.contains("-v")){
1570                                                 if (item.getName().endsWith(".zip")){
1571                                                         this.newModel.setVersion(this.newFile.split("-v")[1].replace(".zip", ""));
1572                                                         zip = true;
1573                                                 }else if(item.getName().endsWith(".yml")){
1574                                                         this.newModel.setVersion(this.newFile.split("-v")[1].replace(".yml", ""));
1575                                                         yml = true;
1576                                                 }
1577                                                 else {
1578                                                         this.newModel.setVersion(this.newFile.split("-v")[1].replace(".xmi", ""));
1579                                                 }
1580                                         }                               
1581                                 }catch(Exception e){
1582                                         LOGGER.error("Upload error : ", e);
1583                                         errorMsg = "Upload error:" + e.getMessage();
1584                                 }
1585                         }
1586                         
1587                 }
1588                 
1589                 if(!errorMsg.isEmpty()){
1590                         
1591                         PrintWriter out = response.getWriter();
1592                         
1593                         response.setCharacterEncoding("UTF-8");
1594                         response.setContentType("application / json");
1595                         request.setCharacterEncoding("UTF-8");
1596                         
1597                         JSONObject j = new JSONObject();
1598                         j.put("errorMsg", errorMsg);
1599                         out.write(j.toString());
1600                         return;
1601                 }
1602                 
1603                 List<File> fileList = new ArrayList<>();
1604                 this.directory = "model";
1605                 if (zip){
1606                         extractFolder(this.newFile);
1607                         fileList = listModelFiles(this.directory);
1608                 }else if (yml){
1609                         parseTosca(this.newFile);
1610                 }else {
1611                         File file = new File(this.newFile);
1612                         fileList.add(file);
1613                 }
1614                 String modelType;
1615                 if(! yml){
1616                         modelType="xmi";
1617                         //Process Main Model file first
1618                         classMap = new HashMap<>();
1619                         for (File file : fileList) {
1620                                 if(!file.isDirectory() && file.getName().endsWith(".xmi")){
1621                         retreiveDependency(file.toString(), true);
1622                     }   
1623                         }
1624                         
1625                         modelList = createList();
1626                         
1627                         cleanUp(this.newFile);
1628                         cleanUp(directory);
1629                 }else{
1630                         modelType="yml";
1631                         modelList.add(this.newModel.getModelName());
1632                         String className=this.newModel.getModelName();
1633                         MSAttributeObject msAttributes= new MSAttributeObject();
1634                         msAttributes.setClassName(className);
1635                         
1636                         HashMap<String, String> returnAttributeList =new HashMap<>();
1637                         returnAttributeList.put(className, this.attributeString);
1638                         msAttributes.setAttribute(returnAttributeList);
1639                         
1640                         msAttributes.setSubClass(this.retmap);
1641                         msAttributes.setMatchingSet(matchableValues);
1642                         HashMap<String, String> returnReferenceList =new HashMap<>();
1643                         returnReferenceList.put(className, this.referenceAttributes);
1644                         msAttributes.setRefAttribute(returnReferenceList);
1645                         
1646                         if(this.listConstraints!=""){
1647                                 HashMap<String, String> enumList =new HashMap<>();
1648                                 String[] listArray=this.listConstraints.split("#");
1649                 for(String str:listArray){
1650                     String[] strArr= str.split("=");
1651                     if(strArr.length>1){
1652                         enumList.put(strArr[0], strArr[1]);
1653                     }
1654                 }
1655                                 msAttributes.setEnumType(enumList);
1656                         }
1657                         
1658                         classMap=new HashMap<>();
1659                         classMap.put(className, msAttributes);
1660                         
1661                 }
1662                 
1663                 PrintWriter out = response.getWriter();
1664                 
1665                 response.setCharacterEncoding("UTF-8");
1666                 response.setContentType("application / json");
1667                 request.setCharacterEncoding("UTF-8");
1668                 
1669                 ObjectMapper mapper = new ObjectMapper();
1670                 JSONObject j = new JSONObject();
1671                 j.put("classListDatas", modelList);
1672                 j.put("modelDatas", mapper.writeValueAsString(classMap));
1673                 j.put("modelType", modelType);
1674                 out.write(j.toString());
1675         }
1676         
1677         /*
1678          * Unzip file and store in the model directory for processing
1679          */
1680         @SuppressWarnings("rawtypes")
1681         private void extractFolder(String zipFile )  {
1682             int BUFFER = 2048;
1683             File file = new File(zipFile);
1684
1685                 try (ZipFile zip = new ZipFile(file)) {
1686                     String newPath =  "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1687                     this.directory = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1688                     checkZipDirectory(this.directory);
1689                     new File(newPath).mkdir();
1690                     Enumeration zipFileEntries = zip.entries();
1691         
1692                     // Process each entry
1693                     while (zipFileEntries.hasMoreElements()){
1694                         // grab a zip file entry
1695                         ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
1696                         String currentEntry = entry.getName();
1697                         File destFile = new File("model" + File.separator + currentEntry);
1698                         File destinationParent = destFile.getParentFile();
1699                         
1700                         destinationParent.mkdirs();
1701         
1702                         if (!entry.isDirectory()){
1703                             BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
1704                             int currentByte;
1705                             byte[] data = new byte[BUFFER];
1706                             try (FileOutputStream fos = new FileOutputStream(destFile);
1707                                         BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER)) {
1708                                     while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
1709                                         dest.write(data, 0, currentByte);
1710                                     }
1711                                     dest.flush();
1712                             } catch (IOException e) {
1713                                 LOGGER.error("Failed to write zip contents to {}" + destFile + e);
1714                                 //
1715                                 // PLD should I throw e?
1716                                 //
1717                                 throw e;
1718                             }
1719                         }
1720         
1721                         if (currentEntry.endsWith(".zip")){
1722                             extractFolder(destFile.getAbsolutePath());
1723                         }
1724                     }
1725             } catch (IOException e) {
1726                 LOGGER.error("Failed to unzip model file " + zipFile, e);
1727                 }
1728         }
1729         
1730         private void retreiveDependency(String workingFile, Boolean modelClass) {
1731                 
1732                 MSModelUtils utils = new MSModelUtils(PolicyController.getMsOnapName(), PolicyController.getMsPolicyName());
1733             Map<String, MSAttributeObject> tempMap;
1734             
1735             tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
1736             
1737             classMap.putAll(tempMap);
1738             LOGGER.info(tempMap);
1739             
1740             return;     
1741             
1742         }
1743                 
1744         private List<File> listModelFiles(String directoryName) {
1745                 File fileDirectory = new File(directoryName);
1746                 List<File> resultList = new ArrayList<>();
1747                 File[] fList = fileDirectory.listFiles();
1748                 for (File file : fList) {
1749                         if (file.isFile()) {
1750                                 resultList.add(file);
1751                         } else if (file.isDirectory()) {
1752                                 dirDependencyList.add(file.getName());
1753                                 resultList.addAll(listModelFiles(file.getAbsolutePath()));
1754                         }
1755                 }
1756                 return resultList;
1757         }
1758         
1759     private void cleanUp(String path) {
1760         if (path!=null){
1761             try {
1762                 FileUtils.forceDelete(new File(path));
1763             } catch (IOException e) {
1764                 LOGGER.error("Failed to delete folder " + path, e);
1765             }  
1766         }
1767     }
1768  
1769     private void checkZipDirectory(String zipDirectory) {
1770         Path path = Paths.get(zipDirectory);
1771  
1772         if (Files.exists(path)) {
1773             cleanUp(zipDirectory);
1774         }
1775     }
1776         
1777     private List<String> createList() {
1778         List<String> list = new  ArrayList<>();
1779         for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()){
1780             if (cMap.getValue().isPolicyTempalate()){
1781                 list.add(cMap.getKey());
1782             }
1783             
1784         }
1785         
1786         if (list.isEmpty()){
1787             if (classMap.containsKey(this.newModel.getModelName())){
1788                 list.add(this.newModel.getModelName());
1789             }else {
1790                 list.add("EMPTY");
1791             }
1792         }
1793         return list;
1794     }
1795
1796         public Map<String, String> getAttributesListRefMap() {
1797                 return attributesListRefMap;
1798         }
1799
1800         public Map<String, LinkedList<String>> getArrayTextList() {
1801                 return arrayTextList;
1802         }
1803
1804 }
1805
1806 class DCAEMicroServiceObject {
1807
1808         private String service;
1809         private String location;
1810         private String uuid;
1811         private String policyName;
1812         private String description;
1813         private String configName;
1814         private String templateVersion;
1815         private String version;
1816         private String priority;
1817         private String policyScope;
1818         private String riskType;
1819         private String riskLevel; 
1820         private String guard = null;
1821
1822         public String getGuard() {
1823                 return guard;
1824         }
1825         public void setGuard(String guard) {
1826                 this.guard = guard;
1827         }
1828         public String getRiskType() {
1829                 return riskType;
1830         }
1831         public void setRiskType(String riskType) {
1832                 this.riskType = riskType;
1833         }
1834         public String getRiskLevel() {
1835                 return riskLevel;
1836         }
1837         public void setRiskLevel(String riskLevel) {
1838                 this.riskLevel = riskLevel;
1839         }
1840         public String getPolicyScope() {
1841                 return policyScope;
1842         }
1843         public void setPolicyScope(String policyScope) {
1844                 this.policyScope = policyScope;
1845         }
1846
1847         public String getPriority() {
1848                 return priority;
1849         }
1850         public void setPriority(String priority) {
1851                 this.priority = priority;
1852         }
1853         public String getVersion() {
1854                 return version;
1855         }
1856         public void setVersion(String version) {
1857                 this.version = version;
1858         }
1859         private Object content;
1860
1861
1862         public String getPolicyName() {
1863                 return policyName;
1864         }
1865         public void setPolicyName(String policyName) {
1866                 this.policyName = policyName;
1867         }
1868         public String getDescription() {
1869                 return description;
1870         }
1871         public void setDescription(String description) {
1872                 this.description = description;
1873         }
1874         public String getConfigName() {
1875                 return configName;
1876         }
1877         public void setConfigName(String configName) {
1878                 this.configName = configName;
1879         }
1880         public Object getContent() {
1881                 return content;
1882         }
1883         public void setContent(Object content) {
1884                 this.content = content;
1885         }
1886
1887         public String getService() {
1888                 return service;
1889         }
1890         public void setService(String service) {
1891                 this.service = service;
1892         }
1893         public String getLocation() {
1894                 return location;
1895         }
1896         public void setLocation(String location) {
1897                 this.location = location;
1898         }
1899
1900         public String getUuid() {
1901                 return uuid;
1902         }
1903         public void setUuid(String uuid) {
1904                 this.uuid = uuid;
1905         }
1906         public String getTemplateVersion() {
1907                 return templateVersion;
1908         }
1909         public void setTemplateVersion(String templateVersion) {
1910                 this.templateVersion = templateVersion;
1911         }
1912
1913 }