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