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