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