Policy TestSuite Enabled
[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<String>();
111         private List<String> dirDependencyList = new ArrayList<String>();
112         private HashMap<String,MSAttributeObject > classMap = new HashMap<String,MSAttributeObject>();
113         //Tosca Model related Datastructure. 
114         String referenceAttributes;
115         String attributeString;
116         String listConstraints;
117         String subAttributeString;
118         HashMap<String, Object> retmap = new HashMap<String, Object>();
119         Set<String> uniqueKeys= new HashSet<String>();
120         Set<String> uniqueDataKeys= new HashSet<String>();
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<String, String>();
132         private Map<String, LinkedList<String>> arrayTextList =  new HashMap<String, LinkedList<String>>();
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<String, String>(); 
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<String, String>();
352                 try {
353                         map=load(fileName);
354                         for(String key:map.keySet()){
355                                 if(key.contains("policy.nodes.Root"))
356                                 {
357                                         continue;
358                                 }
359                                 else if(key.contains("policy.nodes")){
360                                         String wordToFind = "policy.nodes.";
361                                         int indexForPolicyNode=key.indexOf(wordToFind);
362                                         String subNodeString= key.substring(indexForPolicyNode+13, key.length());
363
364                                         stringBetweenDots(subNodeString,map.get(key));
365                                 }
366                                 else if(key.contains("policy.data")){
367                                         String wordToFind="policy.data.";
368                                         int indexForPolicyNode=key.indexOf(wordToFind);
369                                         String subNodeString= key.substring(indexForPolicyNode+12, key.length());
370
371                                         stringBetweenDotsForDataFields(subNodeString,map.get(key));
372                                         Iterator<String> itr= uniqueDataKeys.iterator();
373                                         while(itr.hasNext()){
374                                                 logger.info(itr.next());
375                                         }
376                                 }
377                         }
378
379                         String attributeIndividualString="";
380                         String userDefinedIndividualString="";
381                         String referenceIndividualAttributes="";
382
383                         String attributeString="";
384                         String userDefinedString="";
385                         String referenceAttributes="";
386                         String listConstraints="";
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                                 }
410                                 if(userDefinedString!=""){
411                                         userDefinedString=userDefinedString+","+userDefinedIndividualString;
412                                 }else{
413                                         userDefinedString=userDefinedString+userDefinedIndividualString;
414                                 }
415                                 userDefinedIndividualString="";
416                         }
417                         logger.info("userDefinedString   :"+userDefinedString);
418
419                         HashMap<String,ArrayList<String>> mapKey= new HashMap<String,ArrayList<String>>();
420                         String secondPartString="";
421                         String firstPartString="";
422                         for(String value: userDefinedString.split(",")){
423                                 String[] splitWithEquals= value.split("=");
424                                 secondPartString=splitWithEquals[0].substring(splitWithEquals[0].indexOf("%")+1);
425                                 firstPartString=splitWithEquals[0].substring(0, splitWithEquals[0].indexOf("%"));
426                                 ArrayList<String> list;
427                                 if(mapKey.containsKey(firstPartString)){
428                                         list = mapKey.get(firstPartString);
429                                         list.add(secondPartString+"<"+splitWithEquals[1]);
430                                 } else {
431                                         list = new ArrayList<String>();
432                                         list.add(secondPartString+"<"+splitWithEquals[1]);
433                                         mapKey.put(firstPartString, list);
434                                 }
435                         }
436
437                         JSONObject mainObject= new JSONObject();;
438                         JSONObject json;
439                         for(String s: mapKey.keySet()){
440                                 json= new JSONObject();
441                                 List<String> value=mapKey.get(s);
442                                 for(String listValue:value){
443                                         String[] splitValue=listValue.split("<");
444                                         json.put(splitValue[0], splitValue[1]);
445                                 }
446                                 mainObject.put(s,json);
447                         }
448
449                         logger.info(mainObject);
450
451                         Iterator<String> keysItr = mainObject.keys();
452                         while(keysItr.hasNext()) {
453                                 String key = keysItr.next();
454                                 String value = mainObject.get(key).toString();
455                                 retmap.put(key, value);
456                         }
457
458                         for(String str:retmap.keySet()){
459                                 logger.info(str+":"+retmap.get(str));
460                         }
461
462                         String typeValueFromKey="";
463                         boolean userDefinedDataType=false;
464                         boolean isList=false;
465                         for(String uniqueKey: uniqueKeys){
466                                 List<String> constraints= new ArrayList<String>();
467                                 logger.info("====================");
468                                 attributeIndividualString=attributeIndividualString+uniqueKey+"=";
469                                 attributeIndividualString=attributeIndividualString+"#A:defaultValue-#B:required-#C:MANY-false";
470
471                                 logger.info("UniqueStrings: "+uniqueKey);
472                                 for(String key:map.keySet()){
473                                         if(key.contains("policy.nodes.Root")||
474                                                         key.contains("policy.data"))
475                                         {
476                                                 continue;
477                                         }
478                                         else if(key.contains("policy.nodes")){
479                                                 if(key.contains(uniqueKey)){
480                                                         int p=key.lastIndexOf(".");
481                                                         String firstLastOccurance=key.substring(0,p);
482                                                         int p1=firstLastOccurance.lastIndexOf(".");
483                                                         String secondLastOccurance= firstLastOccurance.substring(p1+1,firstLastOccurance.length());
484                                                         if(secondLastOccurance.equals(uniqueKey)){
485                                                                 String checkTypeString= firstLastOccurance+".type";
486                                                                 typeValueFromKey= map.get(checkTypeString);
487                                                         }//Its a list. 
488                                                         else if (key.contains("entry_schema")){
489                                                                 if(key.contains("constraints")){
490                                                                         constraints.add(map.get(key));
491                                                                 }
492                                                                 if(key.contains("type")){
493                                                                         isList=true;
494                                                                         String value= map.get(key);
495                                                                         if(! (value.contains("string")) ||
496                                                                                         (value.contains("integer")) || 
497                                                                                         (value.contains("boolean")) )
498                                                                         {
499                                                                                 if(!key.contains("valid_values")){
500                                                                                         String trimValue=value.substring(value.lastIndexOf(".")+1);
501                                                                                         referenceIndividualAttributes=referenceIndividualAttributes+uniqueKey+"="+trimValue+":MANY-true";
502                                                                                         attributeIndividualString="";
503                                                                                 }
504
505                                                                         }
506                                                                 }
507                                                         }
508
509                                                         if(!(typeValueFromKey.equals("string")||
510                                                                         typeValueFromKey.equals("integer") ||
511                                                                         typeValueFromKey.equals("boolean")))
512                                                         {
513                                                                 if(typeValueFromKey.equals("list")){
514                                                                         isList=true;
515                                                                         userDefinedDataType=false;
516                                                                 }
517                                                                 else{
518                                                                         userDefinedDataType=true;
519                                                                 }
520                                                         }
521                                                         if(userDefinedDataType==false && isList==false){
522                                                                 if(key.contains("default")){
523                                                                         attributeIndividualString=attributeIndividualString.replace("#B", map.get(key));
524                                                                 }
525                                                                 else if(key.contains("required")){
526                                                                         attributeIndividualString=attributeIndividualString.replace("#C", map.get(key));
527                                                                 }
528                                                                 else if(key.contains("type")){
529                                                                         String typeValue= map.get(key);
530                                                                         attributeIndividualString=attributeIndividualString.replace("#A", typeValue);
531                                                                 } 
532                                                         }
533                                                         else if(userDefinedDataType==true){
534                                                                 String checkTypeAndUpdate=key.substring(p+1);
535                                                                 if(checkTypeAndUpdate.equals("type")){
536                                                                         String value=map.get(key);
537                                                                         String trimValue=value.substring(value.lastIndexOf(".")+1);
538                                                                         referenceIndividualAttributes=referenceIndividualAttributes+uniqueKey+"="+trimValue+":MANY-false";
539                                                                 }
540                                                                 attributeIndividualString="";
541                                                         }
542                                                 }
543                                         }
544                                 }
545
546                                 if(constraints!=null &&constraints.isEmpty()==false){
547                                         //List handling. 
548                                         listConstraints=uniqueKey.toUpperCase()+"=[";
549                                         isList=true;
550                                         for(String str:constraints){
551                                                 listConstraints=listConstraints+str+",";
552                                         }
553                                         listConstraints+="],";
554                                         logger.info(listConstraints);
555                                         attributeIndividualString="";
556                                         referenceIndividualAttributes=referenceIndividualAttributes+uniqueKey+"="+uniqueKey.toUpperCase()+":MANY-false";
557                                         constraints=null;
558
559                                 }
560                                 if(userDefinedDataType==false && isList==false){
561                                         if(attributeString!=""){
562                                                 attributeString=attributeString+","+attributeIndividualString;
563                                         }else{
564                                                 attributeString=attributeString+attributeIndividualString;
565                                         }
566                                 }
567                                 if(isList==true || userDefinedDataType==true){
568                                         if(referenceAttributes!=""){
569                                                 referenceAttributes=referenceAttributes+","+referenceIndividualAttributes;
570                                         }else{
571                                                 referenceAttributes=referenceAttributes+referenceIndividualAttributes;
572                                         }
573                                         logger.info("ReferenceAttributes: "+referenceAttributes);
574                                 }
575
576                                 logger.info("AttributeString: "+ attributeString);
577                                 logger.info("ListConstraints is: "+listConstraints);
578
579                                 attributeIndividualString="";
580                                 referenceIndividualAttributes="";
581                                 userDefinedDataType=false;
582                                 isList=false;
583
584                         }
585                         this.listConstraints=listConstraints;
586                         this.referenceAttributes=referenceAttributes;
587                         this.attributeString=attributeString;
588                 } catch (IOException e) {
589                         logger.error(e);
590                 }
591         } 
592
593         private String cleanUPJson(String json) {
594                 String cleanJson = StringUtils.replaceEach(json, new String[]{"\\\\", "\\\\\\", "\\\\\\\\"}, new String[]{"\\", "\\", "\\"});
595                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\"}, new String[]{"\\"});
596                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\", "[[", "]]"}, new String[]{"\\", "[", "]"});
597                 
598                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\"", "\\\"", "\"[{", "}]\""}, new String[]{"\"", "\"", "[{", "}]"});
599                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[{", "}]\""}, new String[]{"[{", "}]"});
600                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
601                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"{", "}\""}, new String[]{"{", "}"});
602                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\"\"", "\"\""}, new String[]{"\"", "\""});
603                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\""}, new String[]{""});
604                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\""}, new String[]{"\""});
605                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"\\\\\\"}, new String[]{"\""});
606                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\\""}, new String[]{"\""});
607                 cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\"[", "]\""}, new String[]{"[", "]"});
608                 return cleanJson;
609         }
610         
611         private JSONObject decodeContent(JsonNode jsonNode){
612                 Iterator<JsonNode> jsonElements = jsonNode.elements();
613                 Iterator<String> jsonKeys = jsonNode.fieldNames();
614                 Map<String,String> element = new TreeMap<String,String>();
615                 while(jsonElements.hasNext() && jsonKeys.hasNext()){
616                         element.put(jsonKeys.next(), jsonElements.next().toString());
617                 }
618                 JSONObject jsonResult = new JSONObject();
619                 JSONArray jsonArray = null;
620                 String oldValue = null;
621                 String nodeKey = null;
622                 String arryKey = null;
623                 Boolean isArray = false;
624                 JsonNodeFactory nodeFactory = JsonNodeFactory.instance;
625                 ObjectNode node = nodeFactory.objectNode();
626                 String prevKey = null;
627                 String presKey = null;
628                 for(String key: element.keySet()){
629                         if(key.contains(".")){
630                                 presKey = key.substring(0,key.indexOf("."));
631                         }else if(key.contains("@")){
632                                 presKey = key.substring(0,key.indexOf("@"));
633                         }else{
634                                 presKey = key;
635                         }
636                         // first check if we are different from old.
637                         logger.info(key+"\n");
638                         if(jsonArray!=null && jsonArray.length()>0 && key.contains("@") && !key.contains(".") && oldValue!=null){
639                                 if(!oldValue.equals(key.substring(0,key.indexOf("@")))){
640                                         jsonResult.put(oldValue, jsonArray);
641                                         jsonArray = new JSONArray();
642                                 }
643                         }else if(jsonArray!=null && jsonArray.length()>0 && !presKey.equals(prevKey) && oldValue!=null){ 
644                                 jsonResult.put(oldValue, jsonArray);
645                                 isArray = false;
646                                 jsonArray = new JSONArray();
647                         }
648         
649                         prevKey = presKey;
650                         // 
651                         if(key.contains(".")){
652                                 if(nodeKey==null){
653                                         nodeKey = key.substring(0,key.indexOf("."));
654                                 }
655                                 if(nodeKey.equals(key.substring(0,key.indexOf(".")))){
656                                         node.put(key.substring(key.indexOf(".")+1), element.get(key));
657                                 }else{
658                                         if(node.size()!=0){
659                                                 if(nodeKey.contains("@")){
660                                                         if(arryKey==null){
661                                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
662                                                         }
663                                                         if(nodeKey.endsWith("@0")){
664                                                                 isArray = true;
665                                                                 jsonArray = new JSONArray();
666                                                         }
667                                                         if(arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
668                                                                 jsonArray.put(decodeContent(node));
669                                                         } 
670                                                         if(key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf("@")))){
671                                                                 jsonResult.put(arryKey, jsonArray);
672                                                                 jsonArray = new JSONArray();
673                                                         }else if(!key.contains("@")){
674                                                                 jsonResult.put(arryKey, jsonArray);
675                                                                 jsonArray = new JSONArray();
676                                                         }
677                                                         arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
678                                                 }else{
679                                                         isArray = false;
680                                                         jsonResult.put(nodeKey, decodeContent(node));
681                                                 }
682                                                 node = nodeFactory.objectNode();
683                                         }
684                                         nodeKey = key.substring(0,key.indexOf("."));
685                                         if(nodeKey.contains("@")){
686                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
687                                         }
688                                         node.put(key.substring(key.indexOf(".")+1), element.get(key));
689                                 }
690                         }else if(node.size()!=0){
691                                 if(nodeKey.contains("@")){
692                                         if(arryKey==null){
693                                                 arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
694                                         }
695                                         if(nodeKey.endsWith("@0")){
696                                                 isArray = true;
697                                                 jsonArray = new JSONArray();
698                                         }
699                                         if(arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
700                                                 jsonArray.put(decodeContent(node));
701                                         }
702                                         jsonResult.put(arryKey, jsonArray);
703                                         jsonArray = new JSONArray();
704                                         arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
705                                 }else{
706                                         isArray = false;
707                                         jsonResult.put(nodeKey, decodeContent(node));
708                                 }
709                                 node = nodeFactory.objectNode();
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                         }else{
736                                 if(key.contains("@")){
737                                         isArray = true;
738                                         if(key.endsWith("@0")|| jsonArray==null){
739                                                 jsonArray = new JSONArray();
740                                         }
741                                 }else if(!key.contains("@")){
742                                         isArray = false;
743                                 }
744                                 if(isArray){
745                                         if(oldValue==null){
746                                                 oldValue = key.substring(0,key.indexOf("@"));
747                                         }
748                                         if(oldValue!=prevKey){
749                                                 oldValue = key.substring(0,key.indexOf("@"));
750                                         }
751                                         if(oldValue.equals(key.substring(0,key.indexOf("@")))){
752                                                 jsonArray.put(element.get(key));
753                                         }else{
754                                                 jsonResult.put(oldValue, jsonArray);
755                                                 jsonArray = new JSONArray();
756                                         }
757                                         oldValue = key.substring(0,key.indexOf("@"));
758                                 }else{
759                                         jsonResult.put(key, element.get(key));
760                                 }
761                         }
762                 }
763                 if(node.size()>0){
764                         if(nodeKey.contains("@")){
765                                 if(jsonArray==null){
766                                         jsonArray = new JSONArray();
767                                 }
768                                 if(arryKey==null){
769                                         arryKey = nodeKey.substring(0,nodeKey.indexOf("@"));
770                                 }
771                                 jsonArray.put(decodeContent(node));
772                                 jsonResult.put(arryKey, jsonArray);
773                                 isArray = false;;
774                         }else{
775                                 jsonResult.put(nodeKey, decodeContent(node));
776                         }
777                 }
778                 if(isArray && jsonArray.length() > 0){
779                         jsonResult.put(oldValue, jsonArray);
780                 }
781                 return jsonResult;
782         }
783         
784         @RequestMapping(value={"/policyController/getDCAEMSTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
785         public ModelAndView getDCAEMSTemplateData(HttpServletRequest request, HttpServletResponse response) throws Exception{
786                 ObjectMapper mapper = new ObjectMapper();
787                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
788                 JsonNode root = mapper.readTree(request.getReader());
789
790                 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
791                 String  servicename = value.toString().split("-v")[0];
792                 String version = null;
793                 if (value.toString().contains("-v")){
794                         version = value.toString().split("-v")[1];
795                 }
796                 MicroServiceModels returnModel = getAttributeObject(servicename, version);
797                 
798                 String jsonModel = createMicroSeriveJson(returnModel);
799                 
800                 response.setCharacterEncoding("UTF-8");
801                 response.setContentType("application / json");
802                 request.setCharacterEncoding("UTF-8");
803                 List<Object>  list = new ArrayList<>();
804                 PrintWriter out = response.getWriter();
805                 String responseString = mapper.writeValueAsString(returnModel);
806                 JSONObject j = new JSONObject("{dcaeModelData: " + responseString + ",jsonValue: " + jsonModel + "}");
807                 list.add(j);
808                 out.write(list.toString());
809                 return null;
810         }
811         
812         @SuppressWarnings({ "unchecked", "rawtypes" })
813         private String createMicroSeriveJson(MicroServiceModels returnModel) {
814                 Map<String, String> attributeMap = new HashMap<String, String>();
815                 Map<String, String> refAttributeMap = new HashMap<String, String>();
816                 String attribute = returnModel.getAttributes();
817                 if(attribute != null){
818                         attribute = attribute.trim();
819                 }
820                 String refAttribute = returnModel.getRef_attributes();
821                 if(refAttribute != null){
822                         refAttribute = refAttribute.trim();
823                 }
824                 String enumAttribute = returnModel.getEnumValues();
825                 if(enumAttribute != null){
826                         enumAttribute = enumAttribute.trim();
827                 }
828                 if (!StringUtils.isEmpty(attribute)){
829                         attributeMap = convert(attribute, ",");
830                 }
831                 if (!StringUtils.isEmpty(refAttribute)){
832                         refAttributeMap = convert(refAttribute, ",");
833                 }
834
835                 Gson gson = new Gson();
836                 
837                 String subAttributes = returnModel.getSub_attributes();
838                 if(subAttributes != null){
839                         subAttributes = subAttributes.trim();
840                 }else{
841                         subAttributes = "";
842                 }
843                 Map gsonObject = (Map) gson.fromJson(subAttributes, Object.class);
844                 
845                 JSONObject object = new JSONObject();
846                 JSONArray array = new JSONArray();
847                 
848                 for (Entry<String, String> keySet : attributeMap.entrySet()){
849                         array = new JSONArray();
850                         String value = keySet.getValue();
851                         if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
852                                 array.put(value);
853                                 object.put(keySet.getKey().trim(), array);
854                         }else {
855                                 object.put(keySet.getKey().trim(), value.trim());
856                         }
857                 }
858                 
859                 for (Entry<String, String> keySet : refAttributeMap.entrySet()){
860                         array = new JSONArray();
861                         String value = keySet.getValue().split(":")[0];
862                         if (gsonObject.containsKey(value)){
863                                 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
864                                         array.put(recursiveReference(value, gsonObject, enumAttribute));
865                                         object.put(keySet.getKey().trim(), array);
866                                 }else {
867                                         object.put(keySet.getKey().trim(), recursiveReference(value, gsonObject, enumAttribute));
868                                 }
869                         }else {
870                                 if (keySet.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
871                                         array.put(value.trim());
872                                         object.put(keySet.getKey().trim(), array);
873                                 }else {
874                                         object.put(keySet.getKey().trim(), value.trim()); 
875                                 }
876                         }
877                 }
878
879                 return object.toString();
880         }
881
882         @SuppressWarnings("unchecked")
883         private JSONObject recursiveReference(String name, Map<String,String> subAttributeMap, String enumAttribute) {
884                 JSONObject object = new JSONObject();
885                 Map<String, String> map = new HashMap<String, String>();
886                 Object returnClass = subAttributeMap.get(name);
887                 map = (Map<String, String>) returnClass; 
888                 JSONArray array = new JSONArray();
889                 
890                 for( Entry<String, String> m:map.entrySet()){  
891                         String[] splitValue = m.getValue().split(":");
892                         array = new JSONArray();
893                         if (subAttributeMap.containsKey(splitValue[0])){
894                                 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
895                                         array.put(recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
896                                         object.put(m.getKey().trim(), array);
897                                 }else {
898                                         object.put(m.getKey().trim(), recursiveReference(splitValue[0], subAttributeMap, enumAttribute));
899                                 }
900                         } else{
901                                 if (m.getValue().split("MANY-")[1].equalsIgnoreCase("true")){
902                                         array.put(splitValue[0].trim());
903                                         object.put(m.getKey().trim(), array);
904                                 }else {
905                                         object.put(m.getKey().trim(), splitValue[0].trim());
906                                 }
907                         }
908                   }  
909                 
910                 return object;
911         }
912
913         
914         @RequestMapping(value={"/policyController/getModelServiceVersioneData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
915         public ModelAndView getModelServiceVersionData(HttpServletRequest request, HttpServletResponse response) throws Exception{
916                 ObjectMapper mapper = new ObjectMapper();
917                 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
918                 JsonNode root = mapper.readTree(request.getReader());
919
920                 String value = root.get("policyData").toString().replaceAll("^\"|\"$", "");
921                 String  servicename = value.toString().split("-v")[0];
922                 Set<String> returnList = getVersionList(servicename);
923                 
924                 response.setCharacterEncoding("UTF-8");
925                 response.setContentType("application / json");
926                 request.setCharacterEncoding("UTF-8");
927         List<Object>  list = new ArrayList<>();
928                 PrintWriter out = response.getWriter();
929                 String responseString = mapper.writeValueAsString(returnList);
930                 JSONObject j = new JSONObject("{dcaeModelVersionData: " + responseString +"}");
931                 list.add(j);
932                 out.write(list.toString());
933                 return null;
934         }
935
936         private Set<String> getVersionList(String name) {       
937                 MicroServiceModels workingModel = new MicroServiceModels();
938                 Set<String> list = new HashSet<String>();
939                 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
940                 for (int i = 0; i < microServiceModelsData.size(); i++) {
941                         workingModel = (MicroServiceModels) microServiceModelsData.get(i);
942                         if (workingModel.getVersion()!=null){
943                                 list.add(workingModel.getVersion());
944                         }else{
945                                 list.add("Default");
946                         }
947                 }
948                 return list;
949         }
950         
951         private MicroServiceModels getAttributeObject(String name, String version) {    
952                 MicroServiceModels workingModel = new MicroServiceModels();
953                 List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
954                 for (int i = 0; i < microServiceModelsData.size(); i++) {
955                         workingModel = (MicroServiceModels) microServiceModelsData.get(i);
956                         if(version != null){
957                                 if (workingModel.getVersion()!=null){
958                                         if (workingModel.getVersion().equals(version)){
959                                                 return workingModel;
960                                         }
961                                 }else{
962                                         return workingModel;
963                                 }
964                         }else{
965                                 return workingModel;
966                         }
967                         
968                 }
969                 return workingModel;
970         }
971
972         @RequestMapping(value={"/get_DCAEPriorityValues"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
973         public void getDCAEPriorityValuesData(HttpServletRequest request, HttpServletResponse response){
974                 try{
975                         Map<String, Object> model = new HashMap<String, Object>();
976                         ObjectMapper mapper = new ObjectMapper();
977                         List<String> priorityList = new ArrayList<String>();
978                         priorityCount = 10;
979                         for (int i = 1; i < priorityCount; i++) {
980                                 priorityList.add(String.valueOf(i));
981                         }
982                         model.put("priorityDatas", mapper.writeValueAsString(priorityList));
983                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
984                         JSONObject j = new JSONObject(msg);
985                         response.getWriter().write(j.toString());
986                 }
987                 catch (Exception e){
988                         logger.error(e);
989                 }
990         }
991
992         public void prePopulateDCAEMSPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
993                 if (policyAdapter.getPolicyData() instanceof PolicyType) {
994                         Object policyData = policyAdapter.getPolicyData();
995                         PolicyType policy = (PolicyType) policyData;
996                         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
997                         String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("MS_") +3);
998                         policyAdapter.setPolicyName(policyNameValue);
999                         String description = "";
1000                         try{
1001                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
1002                         }catch(Exception e){
1003                                 description = policy.getDescription();
1004                         }
1005                         policyAdapter.setPolicyDescription(description);
1006                         // Get the target data under policy.
1007                         TargetType target = policy.getTarget();
1008                         if (target != null) {
1009                                 // Under target we have AnyOFType
1010                                 List<AnyOfType> anyOfList = target.getAnyOf();
1011                                 if (anyOfList != null) {
1012                                         Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
1013                                         while (iterAnyOf.hasNext()) {
1014                                                 AnyOfType anyOf = iterAnyOf.next();
1015                                                 // Under AnyOFType we have AllOFType
1016                                                 List<AllOfType> allOfList = anyOf.getAllOf();
1017                                                 if (allOfList != null) {
1018                                                         Iterator<AllOfType> iterAllOf = allOfList.iterator();
1019                                                         while (iterAllOf.hasNext()) {
1020                                                                 AllOfType allOf = iterAllOf.next();
1021                                                                 // Under AllOFType we have Match
1022                                                                 List<MatchType> matchList = allOf.getMatch();
1023                                                                 if (matchList != null) {
1024                                                                         Iterator<MatchType> iterMatch = matchList.iterator();
1025                                                                         while (matchList.size()>1 && iterMatch.hasNext()) {
1026                                                                                 MatchType match = iterMatch.next();
1027                                                                                 //
1028                                                                                 // Under the match we have attribute value and
1029                                                                                 // attributeDesignator. So,finally down to the actual attribute.
1030                                                                                 //
1031                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
1032                                                                                 String value = (String) attributeValue.getContent().get(0);
1033                                                                                 AttributeDesignatorType designator = match.getAttributeDesignator();
1034                                                                                 String attributeId = designator.getAttributeId();
1035                                                                                 // First match in the target is EcompName, so set that value.
1036                                                                                 if (attributeId.equals("ECOMPName")) {
1037                                                                                         policyAdapter.setEcompName(value);
1038                                                                                 }
1039                                                                                 if (attributeId.equals("ConfigName")){
1040                                                                                         policyAdapter.setConfigName(value);
1041                                                                                 }
1042                                                                                 if (attributeId.equals("uuid")){
1043                                                                                         policyAdapter.setUuid(value);
1044                                                                                 }
1045                                                                                 if (attributeId.equals("location")){
1046                                                                                         policyAdapter.setLocation(value);
1047                                                                                 }
1048                                                                                 if (attributeId.equals("RiskType")){
1049                                                                                         policyAdapter.setRiskType(value);
1050                                                                                 }
1051                                                                                 if (attributeId.equals("RiskLevel")){
1052                                                                                         policyAdapter.setRiskLevel(value);
1053                                                                                 }
1054                                                                                 if (attributeId.equals("guard")){
1055                                                                                         policyAdapter.setGuard(value);
1056                                                                                 }
1057                                                                                 if (attributeId.equals("TTLDate") && !value.contains("NA")){
1058                                                                                         String newDate = convertDate(value, true);
1059                                                                                         policyAdapter.setTtlDate(newDate);
1060                                                                                 }
1061                                                                         }
1062                                                                         readFile(policyAdapter, entity);
1063                                                                 }
1064                                                         }
1065                                                 }
1066                                         }
1067                                 }
1068                         }
1069                 }
1070         }
1071
1072         private String convertDate(String dateTTL, boolean portalType) {
1073                 String formateDate = null;
1074                 String[] date  = dateTTL.split("T");
1075                 String[] parts = date[0].split("-");
1076                         
1077                 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
1078                 return formateDate;
1079         }
1080         
1081         public static Map<String, String> convert(String str, String split) {
1082                 Map<String, String> map = new HashMap<String, String>();
1083                 for(final String entry : str.split(split)) {
1084                     String[] parts = entry.split("=");
1085                     map.put(parts[0], parts[1]);
1086                 }
1087                 return map;
1088         }
1089
1090
1091         @SuppressWarnings("unchecked")
1092         private void readFile(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
1093                 String policyScopeName = null;
1094                 ObjectMapper mapper = new ObjectMapper();
1095                 try {
1096                         DCAEMicroServiceObject msBody = (DCAEMicroServiceObject) mapper.readValue(entity.getConfigurationData().getConfigBody(), DCAEMicroServiceObject.class);
1097                         policyScopeName = getPolicyScope(msBody.getPolicyScope());
1098                         policyAdapter.setPolicyScope(policyScopeName);
1099
1100                         policyAdapter.setPriority(msBody.getPriority());
1101
1102                         if (msBody.getVersion()!= null){
1103                                 policyAdapter.setServiceType(msBody.getService());
1104                                 policyAdapter.setVersion(msBody.getVersion());
1105                         }else{
1106                                 policyAdapter.setServiceType(msBody.getService());
1107                         }
1108                         if(msBody.getContent() != null){
1109                                 LinkedHashMap<String, Object>  data = new LinkedHashMap<String, Object>();
1110                                 LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) msBody.getContent();
1111                                 readRecursivlyJSONContent(map, data);
1112                                 policyAdapter.setRuleData(data);
1113                         }
1114
1115                 } catch (Exception e) {
1116                         logger.error(e);
1117                 }
1118
1119         }
1120
1121         @SuppressWarnings({ "rawtypes", "unchecked" })
1122         private void readRecursivlyJSONContent(LinkedHashMap<String, ?> map, LinkedHashMap<String, Object> data){
1123                 for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
1124                         Object key =  iterator.next();
1125                         Object value = map.get(key);
1126                         if(value instanceof LinkedHashMap<?, ?>){
1127                                 LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<String, Object>(); 
1128                                 readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
1129                                 for(String objKey: secondObjec.keySet()){
1130                                         data.put(key+"." +objKey, secondObjec.get(objKey));
1131                                 }
1132                         }else if(value instanceof ArrayList){
1133                                 ArrayList<?> jsonArrayVal = (ArrayList<?>)value;
1134                                 for(int i = 0; i < jsonArrayVal.size(); i++){
1135                                         Object arrayvalue = jsonArrayVal.get(i);
1136                                         if(arrayvalue instanceof LinkedHashMap<?, ?>){
1137                                                 LinkedHashMap<String, Object> newData = new LinkedHashMap<String, Object>();   
1138                                                 readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
1139                                                 for(String objKey: newData.keySet()){
1140                                                         data.put(key+"@"+i+"." +objKey, newData.get(objKey));
1141                                                 }
1142                                         }else if(arrayvalue instanceof ArrayList){
1143                                                 ArrayList<?> jsonArrayVal1 = (ArrayList<?>)value;
1144                                                 for(int j = 0; j < jsonArrayVal1.size(); j++){
1145                                                         Object arrayvalue1 = jsonArrayVal1.get(i);
1146                                                         data.put(key+"@"+j, arrayvalue1.toString());
1147                                                 }       
1148                                         }else{
1149                                                 data.put(key+"@"+i, arrayvalue.toString());
1150                                         }       
1151                                 }
1152                         }else{
1153                                 data.put(key.toString(), value.toString());
1154                         }       
1155                 }
1156         }
1157
1158         private String getPolicyScope(String value) {   
1159                 GroupPolicyScopeList pScope = new GroupPolicyScopeList();
1160                 List<Object> groupList= commonClassDao.getData(GroupPolicyScopeList.class);
1161                 if(groupList.size() > 0){
1162                         for(int i = 0 ; i < groupList.size() ; i ++){
1163                                 pScope = (GroupPolicyScopeList) groupList.get(i);
1164                                 if (pScope.getGroupList().equals(value)){
1165                                         break;
1166                                 }               
1167                         }
1168                 }
1169                 return pScope.getGroupName();
1170         }
1171
1172         //Convert the map values and set into JSON body
1173         public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
1174                 Map<String, String> attribute = new HashMap<String, String>();
1175                 String temp = null;
1176                 String key;
1177                 String value;
1178                 for (Entry<String, String> entry : attributesMap.entrySet()) {
1179                         key = entry.getKey();
1180                         value = entry.getValue();
1181                         attribute.put(key, value);
1182                 }
1183                 for (Entry<String, String> entryRef : attributesRefMap.entrySet()) {
1184                         key = entryRef.getKey();
1185                         value = entryRef.getValue().toString();
1186                         attribute.put(key, value);
1187                 }
1188                 for (Entry<String, String> entryList : attributesListRefMap.entrySet()) {
1189                         key = entryList.getKey();
1190                         value = entryList.getValue().toString();
1191                         attribute.put(key, value);
1192                 }
1193                 for (Entry<String, LinkedList<String>> arrayList : arrayTextList.entrySet()){
1194                         key = arrayList.getKey();
1195                         temp = null;
1196                         for (Object textList : arrayList.getValue()){
1197                                 if (temp == null){
1198                                         temp = "[" + textList;
1199                                 }else{
1200                                         temp = temp + "," + textList;
1201                                 }
1202                         }
1203                         attribute.put(key, temp+ "]");                  
1204                 }
1205
1206                 return  attribute;
1207         }
1208         
1209         @RequestMapping(value={"/ms_dictionary/set_MSModelData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
1210         public void SetMSModelData(HttpServletRequest request, HttpServletResponse response) throws Exception{
1211                 List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
1212                 boolean zip = false;
1213                 boolean yml= false;
1214                 for (FileItem item : items) {
1215                         if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")||item.getName().endsWith(".yml")){
1216                                 this.newModel = new MicroServiceModels();
1217                                 try{
1218                                         File file = new File(item.getName());
1219                                         OutputStream outputStream = new FileOutputStream(file);
1220                                         IOUtils.copy(item.getInputStream(), outputStream);
1221                                         outputStream.close();
1222                                         this.newFile = file.toString();
1223                                         this.newModel.setModelName(this.newFile.toString().split("-v")[0]);
1224                                 
1225                                         if (this.newFile.toString().contains("-v")){
1226                                                 if (item.getName().endsWith(".zip")){
1227                                                         this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".zip", ""));
1228                                                         zip = true;
1229                                                 }else if(item.getName().endsWith(".yml")){
1230                                                         this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".yml", ""));
1231                                                         yml = true;
1232                                                 }
1233                                                 else {
1234                                                         this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".xmi", ""));
1235                                                 }
1236                                         }
1237                                 
1238                                 }catch(Exception e){
1239                                         logger.error("Upload error : " + e);
1240                                 }
1241                         }
1242                         
1243                 }
1244                 List<File> fileList = new ArrayList<File>();;
1245                 this.directory = "model";
1246                 if (zip){
1247                         extractFolder(this.newFile);
1248                         fileList = listModelFiles(this.directory);
1249                 }else if (yml==true){
1250                         parseTosca(this.newFile);
1251                 }else {
1252                         File file = new File(this.newFile);
1253                         fileList.add(file);
1254                 }
1255                 String modelType= "";
1256                 if(yml==false){
1257                         modelType="xmi";
1258                         //Process Main Model file first
1259                         classMap = new HashMap<String,MSAttributeObject>();
1260                         for (File file : fileList) {
1261                                 if(!file.isDirectory() && file.getName().endsWith(".xmi")){
1262                         retreiveDependency(file.toString(), true);
1263                     }   
1264                         }
1265                         
1266                         modelList = createList();
1267                         
1268                         cleanUp(this.newFile);
1269                         cleanUp(directory);
1270                 }else{
1271                         modelType="yml";
1272                         modelList.add(this.newModel.getModelName());
1273                         String className=this.newModel.getModelName();
1274                         MSAttributeObject msAttributes= new MSAttributeObject();
1275                         msAttributes.setClassName(className);
1276                         
1277                         HashMap<String, String> returnAttributeList =new HashMap<String, String>();
1278                         returnAttributeList.put(className, this.attributeString);
1279                         msAttributes.setAttribute(returnAttributeList);
1280                         
1281                         msAttributes.setSubClass(this.retmap);
1282                         
1283                         HashMap<String, String> returnReferenceList =new HashMap<String, String>();
1284                         //String[] referenceArray=this.referenceAttributes.split("=");
1285                         returnReferenceList.put(className, this.referenceAttributes);
1286                         msAttributes.setRefAttribute(returnReferenceList);
1287                         
1288                         if(this.listConstraints!=""){
1289                                 HashMap<String, String> enumList =new HashMap<String, String>();
1290                                 String[] listArray=this.listConstraints.split("=");
1291                                 enumList.put(listArray[0], listArray[1]);
1292                                 msAttributes.setEnumType(enumList);
1293                         }
1294                         
1295                         classMap=new HashMap<String,MSAttributeObject>();
1296                         classMap.put(className, msAttributes);
1297                         
1298                 }
1299                 
1300                 PrintWriter out = response.getWriter();
1301                 
1302                 response.setCharacterEncoding("UTF-8");
1303                 response.setContentType("application / json");
1304                 request.setCharacterEncoding("UTF-8");
1305                 
1306                 ObjectMapper mapper = new ObjectMapper();
1307                 JSONObject j = new JSONObject();
1308                 j.put("classListDatas", modelList);
1309                 j.put("modelDatas", mapper.writeValueAsString(classMap));
1310                 j.put("modelType", modelType);
1311                 out.write(j.toString());
1312         }
1313         
1314         /*
1315          * Unzip file and store in the model directory for processing
1316          */
1317         @SuppressWarnings("rawtypes")
1318         private void extractFolder(String zipFile )  {
1319             int BUFFER = 2048;
1320             File file = new File(zipFile);
1321
1322             ZipFile zip;
1323                 try {
1324                         zip = new ZipFile(file);
1325                     String newPath =  "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1326                     this.directory = "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
1327                     checkZipDirectory(this.directory);
1328                     new File(newPath).mkdir();
1329                     Enumeration zipFileEntries = zip.entries();
1330         
1331                     // Process each entry
1332                     while (zipFileEntries.hasMoreElements()){
1333                         // grab a zip file entry
1334                         ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
1335                         String currentEntry = entry.getName();
1336                         File destFile = new File("model" + File.separator + currentEntry);
1337                         File destinationParent = destFile.getParentFile();
1338                         
1339                         destinationParent.mkdirs();
1340         
1341                         if (!entry.isDirectory()){
1342                             BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
1343                             int currentByte;
1344                             byte data[] = new byte[BUFFER];
1345                             FileOutputStream fos = new FileOutputStream(destFile);
1346                             BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
1347                             while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
1348                                 dest.write(data, 0, currentByte);
1349                             }
1350                             dest.flush();
1351                             dest.close();
1352                             is.close();
1353                         }
1354         
1355                         if (currentEntry.endsWith(".zip")){
1356                             extractFolder(destFile.getAbsolutePath());
1357                         }
1358                     }
1359             } catch (IOException e) {
1360                 logger.error("Failed to unzip model file " + zipFile);
1361                 }
1362         }
1363         
1364         private void retreiveDependency(String workingFile, Boolean modelClass) {
1365                 
1366                 MSModelUtils utils = new MSModelUtils(PolicyController.msEcompName, PolicyController.msPolicyName);
1367             HashMap<String, MSAttributeObject> tempMap = new HashMap<String, MSAttributeObject>();
1368             
1369             tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
1370             
1371             classMap.putAll(tempMap);
1372             logger.info(tempMap);
1373             
1374             return;     
1375             
1376         }
1377                 
1378         private List<File> listModelFiles(String directoryName) {
1379                 File directory = new File(directoryName);
1380                 List<File> resultList = new ArrayList<File>();
1381                 File[] fList = directory.listFiles();
1382                 for (File file : fList) {
1383                         if (file.isFile()) {
1384                                 resultList.add(file);
1385                         } else if (file.isDirectory()) {
1386                                 dirDependencyList.add(file.getName());
1387                                 resultList.addAll(listModelFiles(file.getAbsolutePath()));
1388                         }
1389                 }
1390                 return resultList;
1391         }
1392         
1393     private void cleanUp(String path) {
1394         if (path!=null){
1395             try {
1396                 FileUtils.forceDelete(new File(path));
1397             } catch (IOException e) {
1398                 logger.error("Failed to delete folder " + path);
1399             }  
1400         }
1401     }
1402  
1403     private void checkZipDirectory(String zipDirectory) {
1404         Path path = Paths.get(zipDirectory);
1405  
1406         if (Files.exists(path)) {
1407             cleanUp(zipDirectory);
1408         }
1409     }
1410         
1411     private List<String> createList() {
1412         List<String> list = new  ArrayList<String>();
1413         for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()){
1414             if (cMap.getValue().isPolicyTempalate()){
1415                 list.add(cMap.getKey());
1416             }
1417             
1418         }
1419         
1420         if (list.isEmpty()){
1421             if (classMap.containsKey(this.newModel.getModelName())){
1422                 list.add(this.newModel.getModelName());
1423             }else {
1424                 list.add("EMPTY");
1425             }
1426         }
1427         return list;
1428     }
1429
1430 }
1431
1432 class DCAEMicroServiceObject {
1433
1434         public String service;
1435         public String location;
1436         public String uuid;
1437         public String policyName;
1438         public String description;
1439         public String configName;
1440         public String templateVersion;
1441         public String version;
1442         public String priority;
1443         public String policyScope;
1444         public String riskType;
1445         public String riskLevel; 
1446         public String guard = null;
1447
1448         public String getGuard() {
1449                 return guard;
1450         }
1451         public void setGuard(String guard) {
1452                 this.guard = guard;
1453         }
1454         public String getRiskType() {
1455                 return riskType;
1456         }
1457         public void setRiskType(String riskType) {
1458                 this.riskType = riskType;
1459         }
1460         public String getRiskLevel() {
1461                 return riskLevel;
1462         }
1463         public void setRiskLevel(String riskLevel) {
1464                 this.riskLevel = riskLevel;
1465         }
1466         public String getPolicyScope() {
1467                 return policyScope;
1468         }
1469         public void setPolicyScope(String policyScope) {
1470                 this.policyScope = policyScope;
1471         }
1472
1473         public String getPriority() {
1474                 return priority;
1475         }
1476         public void setPriority(String priority) {
1477                 this.priority = priority;
1478         }
1479         public String getVersion() {
1480                 return version;
1481         }
1482         public void setVersion(String version) {
1483                 this.version = version;
1484         }
1485         private Object content;
1486
1487
1488         public String getPolicyName() {
1489                 return policyName;
1490         }
1491         public void setPolicyName(String policyName) {
1492                 this.policyName = policyName;
1493         }
1494         public String getDescription() {
1495                 return description;
1496         }
1497         public void setDescription(String description) {
1498                 this.description = description;
1499         }
1500         public String getConfigName() {
1501                 return configName;
1502         }
1503         public void setConfigName(String configName) {
1504                 this.configName = configName;
1505         }
1506         public Object getContent() {
1507                 return content;
1508         }
1509         public void setContent(Object content) {
1510                 this.content = content;
1511         }
1512
1513         public String getService() {
1514                 return service;
1515         }
1516         public void setService(String service) {
1517                 this.service = service;
1518         }
1519         public String getLocation() {
1520                 return location;
1521         }
1522         public void setLocation(String location) {
1523                 this.location = location;
1524         }
1525
1526         public String getUuid() {
1527                 return uuid;
1528         }
1529         public void setUuid(String uuid) {
1530                 this.uuid = uuid;
1531         }
1532         public String getTemplateVersion() {
1533                 return templateVersion;
1534         }
1535         public void setTemplateVersion(String templateVersion) {
1536                 this.templateVersion = templateVersion;
1537         }
1538
1539 }