From ad6bfb8448f4d56765d37049c436b36e09c3071d Mon Sep 17 00:00:00 2001 From: "Perichiappan, Lakshmanan (lp576d)" Date: Mon, 17 Jul 2017 12:42:14 -0400 Subject: [Policy-68] Tosca parsing for nested objects Change-Id: I380fa82259c51fa532de5d39ac256e5a5ebf1e07 Signed-off-by: Perichiappan, Lakshmanan (lp576d) --- .../CreateDcaeMicroServiceController.java | 460 +++++++++++++-------- 1 file changed, 281 insertions(+), 179 deletions(-) (limited to 'POLICY-SDK-APP/src/main/java') diff --git a/POLICY-SDK-APP/src/main/java/org/openecomp/policy/controller/CreateDcaeMicroServiceController.java b/POLICY-SDK-APP/src/main/java/org/openecomp/policy/controller/CreateDcaeMicroServiceController.java index f3e313144..e156002bf 100644 --- a/POLICY-SDK-APP/src/main/java/org/openecomp/policy/controller/CreateDcaeMicroServiceController.java +++ b/POLICY-SDK-APP/src/main/java/org/openecomp/policy/controller/CreateDcaeMicroServiceController.java @@ -127,6 +127,19 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { HashMap retmap = new HashMap<>(); Set uniqueKeys= new HashSet<>(); Set uniqueDataKeys= new HashSet<>(); + StringBuilder dataListBuffer=new StringBuilder(); + List dataConstraints= new ArrayList <>(); + + public static final String DATATYPE = "data_types.policy.data."; + public static final String PROPERTIES=".properties."; + public static final String TYPE=".type"; + public static final String STRING="string"; + public static final String INTEGER="integer"; + public static final String LIST="list"; + public static final String DEFAULT=".default"; + public static final String REQUIRED=".required"; + public static final String MANYFALSE=":MANY-false"; + @Autowired private CreateDcaeMicroServiceController(CommonClassDao commonClassDao){ @@ -247,7 +260,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { } // Second index of dot should be returned. - public int stringBetweenDots(String str,String value){ + public int stringBetweenDots(String str){ String stringToSearch=str; String[]ss=stringToSearch.split("\\."); if(ss!=null){ @@ -260,7 +273,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { return uniqueKeys.size(); } - public void stringBetweenDotsForDataFields(String str,String value){ + public void stringBetweenDotsForDataFields(String str){ String stringToSearch=str; String[]ss=stringToSearch.split("\\."); if(ss!=null){ @@ -290,7 +303,7 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { if (yamlMap == null) { return settings; } - List path = new ArrayList(); + List path = new ArrayList <>(); serializeMap(settings, sb, path, yamlMap); return settings; } @@ -300,11 +313,11 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { @SuppressWarnings("unchecked") Map yamlMap = (Map) yaml.load(source.toString()); StringBuilder sb = new StringBuilder(); - Map settings = new HashMap(); + Map settings = new HashMap <>(); if (yamlMap == null) { return settings; } - List path = new ArrayList(); + List path = new ArrayList <>(); serializeMap(settings, sb, path, yamlMap); return settings; } @@ -357,211 +370,300 @@ public class CreateDcaeMicroServiceController extends RestrictedBaseController { settings.put(sb.toString(), value.toString()); } - - public void parseTosca (String fileName){ - Map 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()); + void parseDataAndPolicyNodes(Map map){ + 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()); - stringBetweenDotsForDataFields(subNodeString,map.get(key)); - } + stringBetweenDots(subNodeString); } + else if(key.contains("policy.data")){ + String wordToFind="policy.data."; + int indexForPolicyNode=key.indexOf(wordToFind); + String subNodeString= key.substring(indexForPolicyNode+12, key.length()); - 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)); + stringBetweenDotsForDataFields(subNodeString); + } + } + } + + HashMap parseDataNodes(Map map){ + HashMap dataMapForJson=new HashMap <>(); + for(String uniqueDataKey: uniqueDataKeys){ + if(uniqueDataKey.contains("%")){ + String[] uniqueDataKeySplit= uniqueDataKey.split("%",2); + if (uniqueDataKeySplit.length < 2) { + continue; + } + String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE; + String typeValue=map.get(findType); + LOGGER.info(typeValue); + if(typeValue.equalsIgnoreCase(STRING)|| + typeValue.equalsIgnoreCase(INTEGER) + ) + { + String findDefault=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+DEFAULT; + String defaultValue= map.get(findDefault); + LOGGER.info("defaultValue is:"+ defaultValue); + + String findRequired=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+REQUIRED; + String requiredValue= map.get(findRequired); + LOGGER.info("requiredValue is:"+ requiredValue); + + StringBuilder attributeIndividualStringBuilder= new StringBuilder(); + attributeIndividualStringBuilder.append(typeValue+":defaultValue-"); + attributeIndividualStringBuilder.append(defaultValue+":required-"); + attributeIndividualStringBuilder.append(requiredValue+MANYFALSE); + dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString()); + } + else if(typeValue.equalsIgnoreCase(LIST)){ + String findList= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.type"; + String listValue=map.get(findList); + if(listValue!=null){ + LOGGER.info("Type of list is:"+ listValue); + //Its userdefined + if(listValue.contains(".")){ + String trimValue=listValue.substring(listValue.lastIndexOf('.')+1); + StringBuilder referenceIndividualStringBuilder= new StringBuilder(); + referenceIndividualStringBuilder.append(trimValue+":MANY-true"); + dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString()); + }//Its string + else{ + StringBuilder stringListItems= new StringBuilder(); + stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+":MANY-false"); + dataMapForJson.put(uniqueDataKey, stringListItems.toString()); + dataListBuffer.append(uniqueDataKeySplit[1].toUpperCase()+"=["); + for(int i=0;i<10;i++){ + String findConstraints= DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+".entry_schema.constraints.0.valid_values."+i; + String constraintsValue=map.get(findConstraints); + LOGGER.info(constraintsValue); + if(constraintsValue==null){ + break; + } + else{ + dataConstraints.add(constraintsValue); + dataListBuffer.append(constraintsValue+","); + } } - else if(key.contains(containsKey)){ - String typeValue= map.get(key); - userDefinedIndividualString=userDefinedIndividualString.replace("#A", typeValue); - } + dataListBuffer.append("]#"); + + LOGGER.info(dataListBuffer); } } } - if(userDefinedString!=""){ - userDefinedString=userDefinedString+","+userDefinedIndividualString; - }else{ - userDefinedString=userDefinedString+userDefinedIndividualString; + else{ + String findUserDefined="data_types.policy.data."+uniqueDataKeySplit[0]+"."+"properties"+"."+uniqueDataKeySplit[1]+".type"; + String userDefinedValue=map.get(findUserDefined); + String trimValue=userDefinedValue.substring(userDefinedValue.lastIndexOf('.')+1); + StringBuilder referenceIndividualStringBuilder= new StringBuilder(); + referenceIndividualStringBuilder.append(trimValue+":MANY-false"); + dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString()); + } - userDefinedIndividualString=""; } - LOGGER.info("userDefinedString :"+userDefinedString); - - HashMap> 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 list; - if(mapKeyUserdefined.containsKey(firstPartString)){ - list = mapKeyUserdefined.get(firstPartString); - list.add(secondPartString+"<"+splitWithEquals[1]); - } else { - list = new ArrayList(); - list.add(secondPartString+"<"+splitWithEquals[1]); - mapKeyUserdefined.put(firstPartString, list); - } + } + return dataMapForJson; + } + + void constructJsonForDataFields(HashMap dataMapForJson){ + HashMap> dataMapKey= new HashMap <>(); + HashMap hmSub; + for(Map.Entry entry: dataMapForJson.entrySet()){ + String uniqueDataKey= entry.getKey(); + String[] uniqueDataKeySplit=uniqueDataKey.split("%",2); + if (uniqueDataKeySplit.length < 2) { + continue; } - - JSONObject mainObject= new JSONObject();; - JSONObject json; - for(String s: mapKeyUserdefined.keySet()){ - json= new JSONObject(); - List value=mapKeyUserdefined.get(s); - for(String listValue:value){ - String[] splitValue=listValue.split("<"); - json.put(splitValue[0], splitValue[1]); - } - mainObject.put(s,json); + String value= dataMapForJson.get(uniqueDataKey); + if(dataMapKey.containsKey(uniqueDataKeySplit[0])){ + hmSub = dataMapKey.get(uniqueDataKeySplit[0]); + hmSub.put(uniqueDataKeySplit[1], value); } - Iterator keysItr = mainObject.keys(); - while(keysItr.hasNext()) { - String key = keysItr.next(); - String value = mainObject.get(key).toString(); - retmap.put(key, value); + else{ + hmSub=new HashMap <>(); + hmSub.put(uniqueDataKeySplit[1], value); } - - LOGGER.info("#############################################################################"); - LOGGER.info(mainObject); - LOGGER.info("###############################################################################"); - - HashMap> mapKey= new HashMap<>(); - for(String uniqueKey: uniqueKeys){ - HashMap 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{ + + dataMapKey.put(uniqueDataKeySplit[0], hmSub); + } + + JSONObject mainObject= new JSONObject(); + JSONObject json; + for(Map.Entry> entry: dataMapKey.entrySet()){ + String s=entry.getKey(); + json= new JSONObject(); + HashMap jsonHm=dataMapKey.get(s); + for(Map.Entry entryMap:jsonHm.entrySet()){ + String key=entryMap.getKey(); + json.put(key, jsonHm.get(key)); + } + mainObject.put(s,json); + } + Iterator 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> parsePolicyNodes(Map map){ + HashMap> mapKey= new HashMap <>(); + for(String uniqueKey: uniqueKeys){ + HashMap hm; + + for(Map.Entry entry:map.entrySet()){ + String key=entry.getKey(); + if(key.contains(uniqueKey) && key.contains("policy.nodes")){ + if(mapKey.containsKey(uniqueKey)){ + hm = mapKey.get(uniqueKey); + String keyStr= key.substring(key.lastIndexOf('.')+1); + String valueStr= map.get(key); + if(("type").equals(keyStr)){ + if(!key.contains("entry_schema")) + { 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{ + }else{ + hm.put(keyStr,valueStr); + } + + } else { + hm = new HashMap <>(); + String keyStr= key.substring(key.lastIndexOf('.')+1); + String valueStr= map.get(key); + if(("type").equals(keyStr)){ + if(!key.contains("entry_schema")) + { hm.put(keyStr,valueStr); } - mapKey.put(uniqueKey, hm); + }else{ + hm.put(keyStr,valueStr); } + mapKey.put(uniqueKey, hm); } } } + } + return mapKey; + } - StringBuilder attributeStringBuilder= new StringBuilder(); - StringBuilder referenceStringBuilder= new StringBuilder(); - StringBuilder listBuffer= new StringBuilder(); + void createAttributes(HashMap> mapKey){ + StringBuilder attributeStringBuilder= new StringBuilder(); + StringBuilder referenceStringBuilder= new StringBuilder(); + StringBuilder listBuffer= new StringBuilder(); + List constraints= new ArrayList<>(); + for(Map.Entry> entry: mapKey.entrySet()){ + String keySetString= entry.getKey(); + HashMap 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+","); - List constraints= new ArrayList<>(); - for(String keySetString: mapKey.keySet()){ - HashMap 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 keys= keyValues.keySet(); - Iterator 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 if(keyValues.get("type").equalsIgnoreCase(LIST)){ + //List Datatype + Set keys= keyValues.keySet(); + Iterator itr=keys.iterator(); + while(itr.hasNext()){ + String key= itr.next(); + if((!("type").equals(key) ||("required").equals(key))) + { + 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 userdefined 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); + }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+","); - StringBuilder referenceIndividualStringBuilder= new StringBuilder(); - referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+":MANY-false"); - referenceStringBuilder.append(referenceIndividualStringBuilder+","); - constraints.clear(); - } } + if(constraints!=null &&constraints.isEmpty()==false){ + //List handling. + listBuffer.append(keySetString.toUpperCase()+"=["); + for(String str:constraints){ + listBuffer.append(str+","); + } + listBuffer.append("]#"); + LOGGER.info(listBuffer); - 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); + StringBuilder referenceIndividualStringBuilder= new StringBuilder(); + referenceIndividualStringBuilder.append(keySetString+"="+keySetString.toUpperCase()+":MANY-false"); + referenceStringBuilder.append(referenceIndividualStringBuilder+","); + constraints.clear(); + } } + + dataListBuffer.append(listBuffer); + + + LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); + LOGGER.info("Whole attribute String is:"+attributeStringBuilder); + LOGGER.info("Whole reference String is:"+referenceStringBuilder); + LOGGER.info("List String is:"+listBuffer); + LOGGER.info("Data list buffer is:"+dataListBuffer); + LOGGER.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"); + + this.listConstraints=dataListBuffer.toString(); + this.referenceAttributes=referenceStringBuilder.toString(); + this.attributeString=attributeStringBuilder.toString(); + } + + + + public void parseTosca (String fileName){ + Map map= new HashMap<>(); + + try { + map=load(fileName); + + parseDataAndPolicyNodes(map); + + HashMap dataMapForJson=parseDataNodes(map); + + constructJsonForDataFields(dataMapForJson); + + HashMap> mapKey= parsePolicyNodes(map); + + createAttributes(mapKey); + + } catch (IOException e) { + LOGGER.error(e); + } + } private String cleanUPJson(String json) { -- cgit 1.2.3-korg