Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / CreateDcaeMicroServiceController.java
index 570ceac..c3daf6d 100644 (file)
@@ -24,8 +24,11 @@ package org.openecomp.policy.controller;
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintWriter;
 import java.nio.file.Files;
@@ -75,6 +78,7 @@ import org.springframework.http.MediaType;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.servlet.ModelAndView;
+import org.yaml.snakeyaml.Yaml;
 
 import com.att.research.xacml.util.XACMLProperties;
 import com.fasterxml.jackson.core.JsonProcessingException;
@@ -88,6 +92,7 @@ import com.google.gson.Gson;
 
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
@@ -96,15 +101,23 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
 @Controller
 @RequestMapping("/")
 public class CreateDcaeMicroServiceController extends RestrictedBaseController {
-       private static final Logger logger = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
+       private static final Logger LOGGER = FlexLogger.getLogger(CreateDcaeMicroServiceController.class);
 
        private static CommonClassDao commonClassDao;
        private MicroServiceModels newModel;
        private String newFile;
        private String directory;
-       private List<String> modelList = new ArrayList<String>();
-       private List<String> dirDependencyList = new ArrayList<String>();
-       private HashMap<String,MSAttributeObject > classMap = new HashMap<String,MSAttributeObject>();
+       private List<String> modelList = new ArrayList<>();
+       private List<String> dirDependencyList = new ArrayList<>();
+       private HashMap<String,MSAttributeObject > classMap = new HashMap<>();
+       //Tosca Model related Datastructure. 
+       String referenceAttributes;
+       String attributeString;
+       String listConstraints;
+       String subAttributeString;
+       HashMap<String, Object> retmap = new HashMap<>();
+       Set<String> uniqueKeys= new HashSet<>();
+       Set<String> uniqueDataKeys= new HashSet<>();
        
        @Autowired
        private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){
@@ -115,8 +128,8 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
 
        protected PolicyRestAdapter policyAdapter = null;
        private int priorityCount; 
-       private Map<String, String> attributesListRefMap =  new HashMap<String, String>();
-       private Map<String, LinkedList<String>> arrayTextList =  new HashMap<String, LinkedList<String>>();
+       private Map<String, String> attributesListRefMap =  new HashMap<>();
+       private Map<String, LinkedList<String>> arrayTextList =  new HashMap<>();
 
        public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData, JsonNode root) {
                
@@ -125,7 +138,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        jsonContent = decodeContent(root.get("policyJSON")).toString();
                        constructJson(policyData, jsonContent);
                }catch(Exception e){
-                       logger.error("Error while decoding microservice content");
+                       LOGGER.error("Error while decoding microservice content");
                }
                
                return policyData;
@@ -189,9 +202,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                try {
                        json = om.writeValueAsString(microServiceObject);
                } catch (JsonProcessingException e) {
-                       logger.error("Error writing out the object");
+                       LOGGER.error("Error writing out the object");
                }
-               System.out.println(json);
+               LOGGER.info(json);
                String cleanJson = cleanUPJson(json);
                cleanJson = removeNullAttributes(cleanJson);
                policyAdapter.setJsonBody(cleanJson);
@@ -219,12 +232,327 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                cleanJson = returnNode.toString();
                        }
                } catch (IOException e) {
-                       logger.error("Error writing out the JsonNode");
+                       LOGGER.error("Error writing out the JsonNode");
                }
                return cleanJson;
        }
+       
+       // Second index of dot should be returned. 
+       public void stringBetweenDots(String str,String value){
+               String stringToSearch=str;
+               String[]ss=stringToSearch.split("\\.");
+               if(ss!=null){
+                       int len= ss.length;
+                       if(len>2){
+                               uniqueKeys.add(ss[2]);
+                       }
+               }
+       }
+       
+       public void stringBetweenDotsForDataFields(String str,String value){
+               String stringToSearch=str;
+               String[]ss=stringToSearch.split("\\.");
+               if(ss!=null){
+                       int len= ss.length;
 
+                       if(len>2){
+                               uniqueDataKeys.add(ss[0]+"%"+ss[2]);
+                       }
+               }
+       }
        
+       public Map<String, String> load(String fileName) throws IOException { 
+               File newConfiguration = new File(fileName);
+               InputStream is = null;
+               try {
+                       is = new FileInputStream(newConfiguration);
+               } catch (FileNotFoundException e) {
+                       LOGGER.error(e);
+               }
+
+               Yaml yaml = new Yaml();
+               @SuppressWarnings("unchecked")
+               Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(is); 
+               StringBuilder sb = new StringBuilder(); 
+               Map<String, String> settings = new HashMap<>(); 
+               if (yamlMap == null) { 
+                       return settings; 
+               } 
+               List<String> path = new ArrayList<String>(); 
+               serializeMap(settings, sb, path, yamlMap); 
+               return settings; 
+       } 
+
+       public Map<String, String> load(byte[] source) throws IOException { 
+               Yaml yaml = new Yaml(); 
+               @SuppressWarnings("unchecked")
+               Map<Object, Object> yamlMap = (Map<Object, Object>) yaml.load(source.toString()); 
+               StringBuilder sb = new StringBuilder(); 
+               Map<String, String> settings = new HashMap<String, String>(); 
+               if (yamlMap == null) { 
+                       return settings; 
+               } 
+               List<String> path = new ArrayList<String>(); 
+               serializeMap(settings, sb, path, yamlMap); 
+               return settings; 
+       } 
+
+       @SuppressWarnings({ "unchecked", "rawtypes" })
+       private void serializeMap(Map<String, String> settings, StringBuilder sb, List<String> path, Map<Object, Object> yamlMap) { 
+               for (Map.Entry<Object, Object> entry : yamlMap.entrySet()) { 
+                       if (entry.getValue() instanceof Map) { 
+                               path.add((String) entry.getKey()); 
+                               serializeMap(settings, sb, path, (Map<Object, Object>) entry.getValue()); 
+                               path.remove(path.size() - 1); 
+                       } else if (entry.getValue() instanceof List) { 
+                               path.add((String) entry.getKey()); 
+                               serializeList(settings, sb, path, (List) entry.getValue()); 
+                               path.remove(path.size() - 1); 
+                       } else { 
+                               serializeValue(settings, sb, path, (String) entry.getKey(), entry.getValue()); 
+                       } 
+               } 
+       } 
+
+       @SuppressWarnings("unchecked")
+       private void serializeList(Map<String, String> settings, StringBuilder sb, List<String> path, List<String> yamlList) { 
+               int counter = 0; 
+               for (Object listEle : yamlList) { 
+                       if (listEle instanceof Map) { 
+                               path.add(Integer.toString(counter)); 
+                               serializeMap(settings, sb, path, (Map<Object, Object>) listEle); 
+                               path.remove(path.size() - 1); 
+                       } else if (listEle instanceof List) { 
+                               path.add(Integer.toString(counter)); 
+                               serializeList(settings, sb, path, (List<String>) listEle); 
+                               path.remove(path.size() - 1); 
+                       } else { 
+                               serializeValue(settings, sb, path, Integer.toString(counter), listEle); 
+                       } 
+                       counter++; 
+               } 
+       } 
+
+       private void serializeValue(Map<String, String> settings, StringBuilder sb, List<String> path, String name, Object value) { 
+               if (value == null) { 
+                       return; 
+               } 
+               sb.setLength(0); 
+               for (String pathEle : path) { 
+                       sb.append(pathEle).append('.'); 
+               } 
+               sb.append(name); 
+               settings.put(sb.toString(), value.toString()); 
+       } 
+    
+    
+       public void parseTosca (String fileName){
+               Map<String,String> map= new HashMap<>();
+               try {
+                       map=load(fileName);
+                       for(String key:map.keySet()){
+                               if(key.contains("policy.nodes.Root")){
+                                       continue;
+                               }
+                               else if(key.contains("policy.nodes")){
+                                       String wordToFind = "policy.nodes.";
+                                       int indexForPolicyNode=key.indexOf(wordToFind);
+                                       String subNodeString= key.substring(indexForPolicyNode+13, key.length());
+
+                                       stringBetweenDots(subNodeString,map.get(key));
+                               }
+                               else if(key.contains("policy.data")){
+                                       String wordToFind="policy.data.";
+                                       int indexForPolicyNode=key.indexOf(wordToFind);
+                                       String subNodeString= key.substring(indexForPolicyNode+12, key.length());
+
+                                       stringBetweenDotsForDataFields(subNodeString,map.get(key));
+                               }
+                       }
+
+                       String userDefinedIndividualString="";
+                       String userDefinedString="";
+
+                       for(String uniqueDataKey: uniqueDataKeys){
+                               String[] uniqueDataKeySplit= uniqueDataKey.split("%");
+                               userDefinedIndividualString=userDefinedIndividualString+uniqueDataKey+"=";
+                               userDefinedIndividualString=userDefinedIndividualString+"#A:defaultValue-#B:required-#C:MANY-false";
+                               for(String key:map.keySet()){
+                                       if(key.contains("policy.data")){
+                                               String containsKey= uniqueDataKeySplit[1]+".type";
+                                               if(key.contains(uniqueDataKeySplit[0])){
+                                                       if(key.contains("default")){
+                                                               userDefinedIndividualString=userDefinedIndividualString.replace("#B", map.get(key));
+                                                       }
+                                                       else if(key.contains("required")){
+                                                               userDefinedIndividualString=userDefinedIndividualString.replace("#C", map.get(key));
+                                                       }
+                                                       else if(key.contains(containsKey)){
+                                                               String typeValue= map.get(key);
+                                                               userDefinedIndividualString=userDefinedIndividualString.replace("#A", typeValue);
+                                                       } 
+                                               }
+                                       }
+                               }
+                               if(userDefinedString!=""){
+                                       userDefinedString=userDefinedString+","+userDefinedIndividualString;
+                               }else{
+                                       userDefinedString=userDefinedString+userDefinedIndividualString;
+                               }
+                               userDefinedIndividualString="";
+                       }
+                       LOGGER.info("userDefinedString   :"+userDefinedString);
+
+                       HashMap<String,ArrayList<String>> mapKeyUserdefined= new HashMap<>();
+                       String secondPartString="";
+                       String firstPartString="";
+                       for(String value: userDefinedString.split(",")){
+                               String[] splitWithEquals= value.split("=");
+                               secondPartString=splitWithEquals[0].substring(splitWithEquals[0].indexOf("%")+1);
+                               firstPartString=splitWithEquals[0].substring(0, splitWithEquals[0].indexOf("%"));
+                               ArrayList<String> list;
+                               if(mapKeyUserdefined.containsKey(firstPartString)){
+                                       list = mapKeyUserdefined.get(firstPartString);
+                                       list.add(secondPartString+"<"+splitWithEquals[1]);
+                               } else {
+                                       list = new ArrayList<String>();
+                                       list.add(secondPartString+"<"+splitWithEquals[1]);
+                                       mapKeyUserdefined.put(firstPartString, list);
+                               }
+                       }
+
+                       JSONObject mainObject= new JSONObject();;
+                       JSONObject json;
+                       for(String s: mapKeyUserdefined.keySet()){
+                               json= new JSONObject();
+                               List<String> value=mapKeyUserdefined.get(s);
+                               for(String listValue:value){
+                                       String[] splitValue=listValue.split("<");
+                                       json.put(splitValue[0], splitValue[1]);
+                               }
+                               mainObject.put(s,json);
+                       }
+                       Iterator<String> keysItr = mainObject.keys();
+                       while(keysItr.hasNext()) {
+                               String key = keysItr.next();
+                               String value = mainObject.get(key).toString();
+                               retmap.put(key, value);
+                       }
+
+                       LOGGER.info("#############################################################################");
+                       LOGGER.info(mainObject);
+                       LOGGER.info("###############################################################################");
+
+                       HashMap<String,HashMap<String,String>> mapKey= new HashMap<>();
+                       for(String uniqueKey: uniqueKeys){
+                               HashMap<String,String> hm;
+
+                               for(String key:map.keySet()){
+                                       if(key.contains(uniqueKey)){
+                                               if(mapKey.containsKey(uniqueKey)){
+                                                       hm = mapKey.get(uniqueKey);
+                                                       String keyStr= key.substring(key.lastIndexOf(".")+1);
+                                                       String valueStr= map.get(key);
+                                                       if(keyStr.equals("type")){
+                                                               if(!key.contains("entry_schema")){
+                                                                       hm.put(keyStr,valueStr);
+                                                               }
+                                                       }else{
+                                                               hm.put(keyStr,valueStr);
+                                                       }
+                                               } else {
+                                                       hm = new HashMap<>();
+                                                       String keyStr= key.substring(key.lastIndexOf(".")+1);
+                                                       String valueStr= map.get(key);
+                                                       if(keyStr.equals("type")){
+                                                               if(!key.contains("entry_schema")){
+                                                                       hm.put(keyStr,valueStr);
+                                                               }
+                                                       }else{
+                                                               hm.put(keyStr,valueStr);
+                                                       }
+                                                       mapKey.put(uniqueKey, hm);
+                                               }
+                                       }
+                               }
+                       }
+
+                       StringBuilder attributeStringBuilder= new StringBuilder();
+                       StringBuilder referenceStringBuilder= new StringBuilder();
+                       StringBuilder listBuffer= new StringBuilder();
+
+                       List<String> constraints= new ArrayList<>();
+                       for(String keySetString: mapKey.keySet()){
+                               HashMap<String,String> keyValues=mapKey.get(keySetString);
+                               if(keyValues.get("type").equalsIgnoreCase("string")|| keyValues.get("type").equalsIgnoreCase("integer")){
+                                       StringBuilder attributeIndividualStringBuilder= new StringBuilder();
+                                       attributeIndividualStringBuilder.append(keySetString+"=");
+                                       attributeIndividualStringBuilder.append(keyValues.get("type")+":defaultValue-");
+                                       attributeIndividualStringBuilder.append(keyValues.get("default")+":required-");
+                                       attributeIndividualStringBuilder.append(keyValues.get("required")+":MANY-false");
+                                       attributeStringBuilder.append(attributeIndividualStringBuilder+",");    
+                               }
+                               else if(keyValues.get("type").equalsIgnoreCase("list")){
+                                       //List Datatype
+                                       Set<String> keys= keyValues.keySet();
+                                       Iterator<String> itr=keys.iterator();
+                                       while(itr.hasNext()){
+                                               String key= itr.next().toString();
+                                               if((!key.equals("type"))){
+                                                       String value= keyValues.get(key);
+                                                       //The "." in the value determines if its a string or a user defined type.  
+                                                       if (!value.contains(".")){
+                                                               //This is string
+                                                               constraints.add(keyValues.get(key));
+                                                       }else{
+                                                               //This is user defined string
+                                                               String trimValue=value.substring(value.lastIndexOf(".")+1);
+                                                               StringBuilder referenceIndividualStringBuilder= new StringBuilder();
+                                                               referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-true");
+                                                               referenceStringBuilder.append(referenceIndividualStringBuilder+",");
+                                                       }
+                                               }
+                                       }
+                               }else{
+                                       //User defined Datatype. 
+                                       String value=keyValues.get("type");
+                                       String trimValue=value.substring(value.lastIndexOf(".")+1);
+                                       StringBuilder referenceIndividualStringBuilder= new StringBuilder();
+                                       referenceIndividualStringBuilder.append(keySetString+"="+trimValue+":MANY-false");
+                                       referenceStringBuilder.append(referenceIndividualStringBuilder+",");
+                               }
+                               if(constraints!=null &&constraints.isEmpty()==false){
+                                       //List handling. 
+                                       listBuffer.append(keySetString.toUpperCase()+"=[");
+                                       for(String str:constraints){
+                                               listBuffer.append(str+",");
+                                       }
+                                       listBuffer.append("]#");
+                                       LOGGER.info(listBuffer);
+
+
+                                       StringBuilder referenceIndividualStringBuilder= new StringBuilder();
+                                       referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+":MANY-false");
+                                       referenceStringBuilder.append(referenceIndividualStringBuilder+",");
+                                       constraints.clear();
+                               }
+                       }
+
+                       LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
+                       LOGGER.info("Whole attribute String is:"+attributeStringBuilder);       
+                       LOGGER.info("Whole reference String is:"+referenceStringBuilder);
+                       LOGGER.info("List String is:"+listBuffer);
+                       LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
+                       this.listConstraints=listBuffer.toString();
+                       this.referenceAttributes=referenceStringBuilder.toString();
+                       this.attributeString=attributeStringBuilder.toString();
+
+               } catch (IOException e) {
+                       LOGGER.error(e);
+               }
+       } 
+
        private String cleanUPJson(String json) {
                String cleanJson = StringUtils.replaceEach(json, new String[]{"\\\\", "\\\\\\", "\\\\\\\\"}, new String[]{"\\", "\\", "\\"});
                cleanJson = StringUtils.replaceEach(cleanJson, new String[]{"\\\\\\"}, new String[]{"\\"});
@@ -246,7 +574,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
        private JSONObject decodeContent(JsonNode jsonNode){
                Iterator<JsonNode> jsonElements = jsonNode.elements();
                Iterator<String> jsonKeys = jsonNode.fieldNames();
-               Map<String,String> element = new TreeMap<String,String>();
+               Map<String,String> element = new TreeMap<>();
                while(jsonElements.hasNext() && jsonKeys.hasNext()){
                        element.put(jsonKeys.next(), jsonElements.next().toString());
                }
@@ -269,7 +597,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                presKey = key;
                        }
                        // first check if we are different from old.
-                       System.out.println(key+"\n");
+                       LOGGER.info(key+"\n");
                        if(jsonArray!=null && jsonArray.length()>0 && key.contains("@") && !key.contains(".") && oldValue!=null){
                                if(!oldValue.equals(key.substring(0,key.indexOf("@")))){
                                        jsonResult.put(oldValue, jsonArray);
@@ -299,7 +627,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                                                isArray = true;
                                                                jsonArray = new JSONArray();
                                                        }
-                                                       if(arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
+                                                       if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
                                                                jsonArray.put(decodeContent(node));
                                                        } 
                                                        if(key.contains("@") && !arryKey.equals(key.substring(0,nodeKey.indexOf("@")))){
@@ -331,7 +659,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                                isArray = true;
                                                jsonArray = new JSONArray();
                                        }
-                                       if(arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
+                                       if(jsonArray != null && arryKey.equals(nodeKey.substring(0,nodeKey.indexOf("@")))){
                                                jsonArray.put(decodeContent(node));
                                        }
                                        jsonResult.put(arryKey, jsonArray);
@@ -444,10 +772,10 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                return null;
        }
        
-       @SuppressWarnings("unchecked")
+       @SuppressWarnings({ "unchecked", "rawtypes" })
        private String createMicroSeriveJson(MicroServiceModels returnModel) {
-               Map<String, String> attributeMap = new HashMap<String, String>();
-               Map<String, String> refAttributeMap = new HashMap<String, String>();
+               Map<String, String> attributeMap = new HashMap<>();
+               Map<String, String> refAttributeMap = new HashMap<>();
                String attribute = returnModel.getAttributes();
                if(attribute != null){
                        attribute = attribute.trim();
@@ -517,7 +845,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
        @SuppressWarnings("unchecked")
        private JSONObject recursiveReference(String name, Map<String,String> subAttributeMap, String enumAttribute) {
                JSONObject object = new JSONObject();
-               Map<String, String> map = new HashMap<String, String>();
+               Map<String, String> map = new HashMap<>();
                Object returnClass = subAttributeMap.get(name);
                map = (Map<String, String>) returnClass; 
                JSONArray array = new JSONArray();
@@ -570,7 +898,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
 
        private Set<String> getVersionList(String name) {       
                MicroServiceModels workingModel = new MicroServiceModels();
-               Set<String> list = new HashSet<String>();
+               Set<String> list = new HashSet<>();
                List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName", name);
                for (int i = 0; i < microServiceModelsData.size(); i++) {
                        workingModel = (MicroServiceModels) microServiceModelsData.get(i);
@@ -607,9 +935,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
        @RequestMapping(value={"/get_DCAEPriorityValues"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
        public void getDCAEPriorityValuesData(HttpServletRequest request, HttpServletResponse response){
                try{
-                       Map<String, Object> model = new HashMap<String, Object>();
+                       Map<String, Object> model = new HashMap<>();
                        ObjectMapper mapper = new ObjectMapper();
-                       List<String> priorityList = new ArrayList<String>();
+                       List<String> priorityList = new ArrayList<>();
                        priorityCount = 10;
                        for (int i = 1; i < priorityCount; i++) {
                                priorityList.add(String.valueOf(i));
@@ -620,7 +948,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        response.getWriter().write(j.toString());
                }
                catch (Exception e){
-                       e.printStackTrace();
+                       LOGGER.error(e);
                }
        }
 
@@ -656,52 +984,45 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                                                // Under AllOFType we have Match
                                                                List<MatchType> matchList = allOf.getMatch();
                                                                if (matchList != null) {
-                                                                       int index = 0;
                                                                        Iterator<MatchType> iterMatch = matchList.iterator();
                                                                        while (matchList.size()>1 && iterMatch.hasNext()) {
                                                                                MatchType match = iterMatch.next();
                                                                                //
-                                                                               // Under the match we have attributevalue and
+                                                                               // Under the match we have attribute value and
                                                                                // attributeDesignator. So,finally down to the actual attribute.
                                                                                //
                                                                                AttributeValueType attributeValue = match.getAttributeValue();
                                                                                String value = (String) attributeValue.getContent().get(0);
-
+                                                                               AttributeDesignatorType designator = match.getAttributeDesignator();
+                                                                               String attributeId = designator.getAttributeId();
                                                                                // First match in the target is EcompName, so set that value.
-                                                                               if (index == 0) {
+                                                                               if (attributeId.equals("ECOMPName")) {
                                                                                        policyAdapter.setEcompName(value);
                                                                                }
-                                                                               if (index == 1){
+                                                                               if (attributeId.equals("ConfigName")){
                                                                                        policyAdapter.setConfigName(value);
                                                                                }
-                                                                               if (index == 2){
-                                                                                       if(value != null){
-                                                                                               readFile(policyAdapter, entity);
-                                                                                       }
-                                                                               }
-                                                                               if (index == 3){
+                                                                               if (attributeId.equals("uuid")){
                                                                                        policyAdapter.setUuid(value);
                                                                                }
-                                                                               if (index == 4){
+                                                                               if (attributeId.equals("location")){
                                                                                        policyAdapter.setLocation(value);
                                                                                }
-                                                                               if (index ==  5){
+                                                                               if (attributeId.equals("RiskType")){
                                                                                        policyAdapter.setRiskType(value);
                                                                                }
-
-                                                                               if (index ==  6){
+                                                                               if (attributeId.equals("RiskLevel")){
                                                                                        policyAdapter.setRiskLevel(value);
                                                                                }
-
-                                                                               if (index ==  7){
+                                                                               if (attributeId.equals("guard")){
                                                                                        policyAdapter.setGuard(value);
                                                                                }
-                                                                               if (index == 8 && !value.contains("NA")){
+                                                                               if (attributeId.equals("TTLDate") && !value.contains("NA")){
                                                                                        String newDate = convertDate(value, true);
                                                                                        policyAdapter.setTtlDate(newDate);
                                                                                }
-                                                                               index++;
                                                                        }
+                                                                       readFile(policyAdapter, entity);
                                                                }
                                                        }
                                                }
@@ -721,7 +1042,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
        }
        
        public static Map<String, String> convert(String str, String split) {
-               Map<String, String> map = new HashMap<String, String>();
+               Map<String, String> map = new HashMap<>();
                for(final String entry : str.split(split)) {
                    String[] parts = entry.split("=");
                    map.put(parts[0], parts[1]);
@@ -748,14 +1069,14 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                policyAdapter.setServiceType(msBody.getService());
                        }
                        if(msBody.getContent() != null){
-                               LinkedHashMap<String, Object>  data = new LinkedHashMap<String, Object>();
+                               LinkedHashMap<String, Object>  data = new LinkedHashMap<>();
                                LinkedHashMap<String, ?> map = (LinkedHashMap<String, ?>) msBody.getContent();
                                readRecursivlyJSONContent(map, data);
                                policyAdapter.setRuleData(data);
                        }
 
                } catch (Exception e) {
-                       e.printStackTrace();
+                       LOGGER.error(e);
                }
 
        }
@@ -766,7 +1087,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        Object key =  iterator.next();
                        Object value = map.get(key);
                        if(value instanceof LinkedHashMap<?, ?>){
-                               LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<String, Object>(); 
+                               LinkedHashMap<String, Object> secondObjec = new LinkedHashMap<>(); 
                                readRecursivlyJSONContent((LinkedHashMap<String, ?>) value, secondObjec);
                                for(String objKey: secondObjec.keySet()){
                                        data.put(key+"." +objKey, secondObjec.get(objKey));
@@ -776,7 +1097,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                for(int i = 0; i < jsonArrayVal.size(); i++){
                                        Object arrayvalue = jsonArrayVal.get(i);
                                        if(arrayvalue instanceof LinkedHashMap<?, ?>){
-                                               LinkedHashMap<String, Object> newData = new LinkedHashMap<String, Object>();   
+                                               LinkedHashMap<String, Object> newData = new LinkedHashMap<>();   
                                                readRecursivlyJSONContent((LinkedHashMap<String, ?>) arrayvalue, newData);
                                                for(String objKey: newData.keySet()){
                                                        data.put(key+"@"+i+"." +objKey, newData.get(objKey));
@@ -813,7 +1134,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
 
        //Convert the map values and set into JSON body
        public Map<String, String> convertMap(Map<String, String> attributesMap, Map<String, String> attributesRefMap) {
-               Map<String, String> attribute = new HashMap<String, String>();
+               Map<String, String> attribute = new HashMap<>();
                String temp = null;
                String key;
                String value;
@@ -852,8 +1173,9 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
        public void SetMSModelData(HttpServletRequest request, HttpServletResponse response) throws Exception{
                List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                boolean zip = false;
+               boolean yml= false;
                for (FileItem item : items) {
-                       if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")){
+                       if(item.getName().endsWith(".zip") || item.getName().endsWith(".xmi")||item.getName().endsWith(".yml")){
                                this.newModel = new MicroServiceModels();
                                try{
                                        File file = new File(item.getName());
@@ -862,41 +1184,86 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                                        outputStream.close();
                                        this.newFile = file.toString();
                                        this.newModel.setModelName(this.newFile.toString().split("-v")[0]);
+                               
                                        if (this.newFile.toString().contains("-v")){
                                                if (item.getName().endsWith(".zip")){
                                                        this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".zip", ""));
                                                        zip = true;
-                                               }else {
+                                               }else if(item.getName().endsWith(".yml")){
+                                                       this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".yml", ""));
+                                                       yml = true;
+                                               }
+                                               else {
                                                        this.newModel.setVersion(this.newFile.toString().split("-v")[1].replace(".xmi", ""));
                                                }
                                        }
+                               
                                }catch(Exception e){
-                                       logger.error("Upload error : " + e);
+                                       LOGGER.error("Upload error : " + e);
                                }
                        }
+                       
                }
-               List<File> fileList = new ArrayList<File>();;
+               List<File> fileList = new ArrayList<>();;
                this.directory = "model";
                if (zip){
                        extractFolder(this.newFile);
                        fileList = listModelFiles(this.directory);
+               }else if (yml==true){
+                       parseTosca(this.newFile);
                }else {
                        File file = new File(this.newFile);
                        fileList.add(file);
                }
-               
-               //Process Main Model file first
-               classMap = new HashMap<String,MSAttributeObject>();
-               for (File file : fileList) {
-                       if(!file.isDirectory() && file.getName().endsWith(".xmi")){
-               retreiveDependency(file.toString(), true);
-            }  
+               String modelType= "";
+               if(yml==false){
+                       modelType="xmi";
+                       //Process Main Model file first
+                       classMap = new HashMap<>();
+                       for (File file : fileList) {
+                               if(!file.isDirectory() && file.getName().endsWith(".xmi")){
+                       retreiveDependency(file.toString(), true);
+                   }   
+                       }
+                       
+                       modelList = createList();
+                       
+                       cleanUp(this.newFile);
+                       cleanUp(directory);
+               }else{
+                       modelType="yml";
+                       modelList.add(this.newModel.getModelName());
+                       String className=this.newModel.getModelName();
+                       MSAttributeObject msAttributes= new MSAttributeObject();
+                       msAttributes.setClassName(className);
+                       
+                       HashMap<String, String> returnAttributeList =new HashMap<>();
+                       returnAttributeList.put(className, this.attributeString);
+                       msAttributes.setAttribute(returnAttributeList);
+                       
+                       msAttributes.setSubClass(this.retmap);
+                       
+                       HashMap<String, String> returnReferenceList =new HashMap<>();
+                       //String[] referenceArray=this.referenceAttributes.split("=");
+                       returnReferenceList.put(className, this.referenceAttributes);
+                       msAttributes.setRefAttribute(returnReferenceList);
+                       
+                       if(this.listConstraints!=""){
+                               HashMap<String, String> enumList =new HashMap<>();
+                               String[] listArray=this.listConstraints.split("#");
+                for(String str:listArray){
+                    String[] strArr= str.split("=");
+                    if(strArr.length>1){
+                        enumList.put(strArr[0], strArr[1]);
+                    }
+                }
+                               msAttributes.setEnumType(enumList);
+                       }
+                       
+                       classMap=new HashMap<>();
+                       classMap.put(className, msAttributes);
+                       
                }
-       
-               modelList = createList();
-               
-               cleanUp(this.newFile);
-               cleanUp(directory);
                
                PrintWriter out = response.getWriter();
                
@@ -908,6 +1275,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                JSONObject j = new JSONObject();
                j.put("classListDatas", modelList);
                j.put("modelDatas", mapper.writeValueAsString(classMap));
+               j.put("modelType", modelType);
                out.write(j.toString());
        }
        
@@ -919,7 +1287,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
            int BUFFER = 2048;
            File file = new File(zipFile);
 
-           ZipFile zip;
+           ZipFile zip = null;
                try {
                        zip = new ZipFile(file);
                    String newPath =  "model" + File.separator + zipFile.substring(0, zipFile.length() - 4);
@@ -957,19 +1325,26 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                        }
                    }
            } catch (IOException e) {
-               logger.error("Failed to unzip model file " + zipFile);
+               LOGGER.error("Failed to unzip model file " + zipFile);
+               }finally{
+                       try {
+                               if(zip != null)
+                               zip.close();
+                       } catch (IOException e) {
+                               LOGGER.error("Exception Occured While closing zipfile " + e);
+                       }
                }
        }
        
        private void retreiveDependency(String workingFile, Boolean modelClass) {
                
                MSModelUtils utils = new MSModelUtils(PolicyController.msEcompName, PolicyController.msPolicyName);
-           HashMap<String, MSAttributeObject> tempMap = new HashMap<String, MSAttributeObject>();
+           HashMap<String, MSAttributeObject> tempMap = new HashMap<>();
            
            tempMap = utils.processEpackage(workingFile, MODEL_TYPE.XMI);
            
            classMap.putAll(tempMap);
-           System.out.println(tempMap);
+           LOGGER.info(tempMap);
            
            return;     
            
@@ -977,7 +1352,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
                
        private List<File> listModelFiles(String directoryName) {
                File directory = new File(directoryName);
-               List<File> resultList = new ArrayList<File>();
+               List<File> resultList = new ArrayList<>();
                File[] fList = directory.listFiles();
                for (File file : fList) {
                        if (file.isFile()) {
@@ -995,7 +1370,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
             try {
                 FileUtils.forceDelete(new File(path));
             } catch (IOException e) {
-               logger.error("Failed to delete folder " + path);
+               LOGGER.error("Failed to delete folder " + path);
             }  
         }
     }
@@ -1009,7 +1384,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController {
     }
        
     private List<String> createList() {
-        List<String> list = new  ArrayList<String>();
+        List<String> list = new  ArrayList<>();
         for (Entry<String, MSAttributeObject> cMap : classMap.entrySet()){
             if (cMap.getValue().isPolicyTempalate()){
                 list.add(cMap.getKey());