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