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