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