X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ONAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Frest%2Futil%2FMSModelUtils.java;fp=ONAP-REST%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Frest%2Futil%2FMSModelUtils.java;h=f8b8b5278fe8edef2e2c3fd6ad8420274927804d;hb=b6d9063e06ab8cdf2d97fc75810792659344e4a8;hp=18fd80c63595f6b2a0f1dfd0b9453721f099cf59;hpb=c8bed1e392b935ec50aaeeb5fb82d25a9568b790;p=policy%2Fengine.git diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/MSModelUtils.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/MSModelUtils.java index 18fd80c63..f8b8b5278 100644 --- a/ONAP-REST/src/main/java/org/onap/policy/rest/util/MSModelUtils.java +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/MSModelUtils.java @@ -20,13 +20,21 @@ package org.onap.policy.rest.util; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.Map.Entry; import org.apache.commons.lang.StringUtils; @@ -54,6 +62,7 @@ import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl; import org.json.JSONObject; import org.onap.policy.rest.XACMLRestProperties; +import org.yaml.snakeyaml.Yaml; import com.att.research.xacml.util.XACMLProperties; import com.google.gson.Gson; @@ -71,6 +80,38 @@ public class MSModelUtils { private String onap = ""; private String policy = ""; private String eProxyURI = "eProxyURI:"; + private List orderedElements = new ArrayList<>(); + private String dataOrderInfo = null; + private Set uniqueDataKeys= new HashSet<>(); + private Set uniqueKeys= new HashSet<>(); + private String listConstraints = null; + private String referenceAttributes; + private LinkedHashMap retmap = new LinkedHashMap<>(); + private Map matchableValues; + public static final String PROPERTIES=".properties."; + public static final String DATATYPE = "data_types.policy.data."; + public static final String TYPE=".type"; + public static final String REQUIRED=".required"; + public static final String MATCHABLE=".matchable"; + 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 MANYFALSE=":MANY-false"; + public static final String MANYTRUE=":MANY-true"; + public static final String DEFAULTVALUE=":defaultValue-"; + public static final String REQUIREDVALUE=":required-"; + public static final String MATCHABLEKEY="matchable"; + public static final String REQUIREDFALSE=":required-false"; + public static final String REQUIREDTRUE=":required-true"; + public static final String MATCHINGTRUE="matching-true"; + + private StringBuilder dataListBuffer=new StringBuilder(); + private List dataConstraints= new ArrayList <>(); + private String attributeString = null; + + public MSModelUtils(){ + } public MSModelUtils(String onap, String policy){ this.onap = onap; @@ -110,7 +151,7 @@ public class MSModelUtils { if (obj instanceof EEnum) { enumMap.putAll(getEEnum(obj)); }else if (obj instanceof EClass) { - String temp = getDependencyList(eClassifier, className).toString(); + String temp = getDependencyList(eClassifier).toString(); returnValue = StringUtils.replaceEach(temp, new String[]{"[", "]"}, new String[]{"", ""}); getAttributes(className, returnValue, root); } @@ -135,11 +176,11 @@ public class MSModelUtils { Map listRef = classMap.get(key).getRefAttribute(); for ( Entry eSet : listAttributes.entrySet()){ String key2 = eSet.getKey(); - tempAttribute.put(key2, "matching-true"); + tempAttribute.put(key2, MATCHINGTRUE); } for ( Entry eSet : listRef.entrySet()){ String key3 = eSet.getKey(); - tempAttribute.put(key3, "matching-true"); + tempAttribute.put(key3, MATCHINGTRUE); } } @@ -232,7 +273,7 @@ public class MSModelUtils { HashMap annotationSet = new HashMap<>(); String matching; String range; - String dictionary; + String annotationDict; // Pulling out dependency from file while (treeItr.hasNext()) { @@ -254,14 +295,13 @@ public class MSModelUtils { if (range!=null){ annotationSet.put(eStrucClassifier.getName(), range); } - dictionary = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy); - if (dictionary!=null){ - annotationSet.put(eStrucClassifier.getName(), dictionary); + annotationDict = annotationValue(eStrucClassifier, ANNOTATION_TYPE.DICTIONARY, policy); + if (annotationDict!=null){ + annotationSet.put(eStrucClassifier.getName(), annotationDict); } } } - } else if (requiredMatchAttribute){ - if (obj instanceof EStructuralFeature) { + } else if (requiredMatchAttribute && (obj instanceof EStructuralFeature)) { EStructuralFeature eStrucClassifier = (EStructuralFeature) obj; if (!eStrucClassifier.getEAnnotations().isEmpty()) { matching = annotationValue(eStrucClassifier, ANNOTATION_TYPE.MATCHING, policy); @@ -275,7 +315,6 @@ public class MSModelUtils { } } } - } } } return annotationSet; @@ -305,22 +344,20 @@ public class MSModelUtils { rollingCount = rollingCount+processClass; } - if (requiredAttribute) { - if (obj instanceof EStructuralFeature) { - EStructuralFeature eStrucClassifier = (EStructuralFeature) obj; - if (!eStrucClassifier.getEAnnotations().isEmpty()) { - annotation = annotationTest(eStrucClassifier, configuration, onap); - if (annotation && obj instanceof EReference) { - EClass refType = ((EReference) obj).getEReferenceType(); - if(!refType.toString().contains(eProxyURI)){ - String required = ":required-false"; - if(eStrucClassifier.getLowerBound() == 1){ - required = ":required-true"; - } - subAttribute.put(eStrucClassifier.getName(), refType.getName() + required); + if (requiredAttribute && (obj instanceof EStructuralFeature)) { + EStructuralFeature eStrucClassifier = (EStructuralFeature) obj; + if (!eStrucClassifier.getEAnnotations().isEmpty()) { + annotation = annotationTest(eStrucClassifier, configuration, onap); + if (annotation && obj instanceof EReference) { + EClass refType = ((EReference) obj).getEReferenceType(); + if(!refType.toString().contains(eProxyURI)){ + String required = REQUIREDFALSE; + if(eStrucClassifier.getLowerBound() == 1){ + required = REQUIREDTRUE; } - } - } + subAttribute.put(eStrucClassifier.getName(), refType.getName() + required); + } + } } } } @@ -329,7 +366,7 @@ public class MSModelUtils { public String checkDefultValue(String defultValue) { if (defultValue!=null){ - return ":defaultValue-"+ defultValue; + return DEFAULTVALUE+ defultValue; } return ":defaultValue-NA"; @@ -341,11 +378,11 @@ public class MSModelUtils { if (pattern!=null){ if (upper == Integer.parseInt(pattern.split(",")[1]) && lower==Integer.parseInt(pattern.split(",")[0])){ - return ":required-true"; + return REQUIREDTRUE; } } - return ":required-false"; + return REQUIREDFALSE; } public JSONObject buildJavaObject(Map map){ @@ -376,8 +413,7 @@ public class MSModelUtils { rollingCount = rollingCount+processClass; } - if (requiredAttribute) { - if (obj instanceof EStructuralFeature) { + if (requiredAttribute && (obj instanceof EStructuralFeature)) { EStructuralFeature eStrucClassifier = (EStructuralFeature) obj; if (!eStrucClassifier.getEAnnotations().isEmpty()) { annotation = annotationTest(eStrucClassifier, configuration, onap); @@ -389,9 +425,9 @@ public class MSModelUtils { refAttribute.put(eStrucClassifier.getName(), refValue); } else { String array = arrayCheck(((EStructuralFeature) obj).getUpperBound()); - String required = ":required-false"; + String required = REQUIREDFALSE; if(((EStructuralFeature) obj).getLowerBound() == 1){ - required = ":required-true"; + required = REQUIREDTRUE; } refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required); } @@ -399,15 +435,14 @@ public class MSModelUtils { EClassifier refType = ((EAttributeImpl) obj).getEType(); if (refType instanceof EEnumImpl){ String array = arrayCheck(((EStructuralFeature) obj).getUpperBound()); - String required = ":required-false"; + String required = REQUIREDFALSE; if(((EStructuralFeature) obj).getLowerBound() == 1){ - required = ":required-true"; + required = REQUIREDTRUE; } refAttribute.put(eStrucClassifier.getName(), refType.getName() + array + required); } } } - } } } @@ -427,9 +462,12 @@ public class MSModelUtils { eAnnotation = eStrucClassifier.getEAnnotations().get(i); onapType = eAnnotation.getDetails().get(0).getValue(); onapValue = eAnnotation.getDetails().get(0).getKey(); + if (annotationType.contains(type) && onapType.contains(annotation)){ return true; - } else if (annotationType.contains(type) && onapValue.contains(annotation)){ + } + + if (annotationType.contains(type) && onapValue.contains(annotation)){ return true; } } @@ -464,8 +502,7 @@ public class MSModelUtils { } public boolean isRequiredAttribute(EObject obj, String className){ EClassifier eClassifier = (EClassifier) obj; - String workingClass = eClassifier.getName(); - workingClass.trim(); + String workingClass = eClassifier.getName().trim(); if (workingClass.equalsIgnoreCase(className)){ return true; } @@ -530,8 +567,7 @@ public class MSModelUtils { } - if (requiredAttribute){ - if (obj instanceof EStructuralFeature) { + if (requiredAttribute && (obj instanceof EStructuralFeature)) { EStructuralFeature eStrucClassifier = (EStructuralFeature) obj; if (!eStrucClassifier.getEAnnotations().isEmpty()) { annotation = annotationTest(eStrucClassifier, configuration, onap); @@ -552,7 +588,6 @@ public class MSModelUtils { refAttribute.put(name, attributeValue); } } - } } } return refAttribute; @@ -562,13 +597,13 @@ public class MSModelUtils { public String arrayCheck(int upperBound) { if (upperBound == -1){ - return ":MANY-true"; + return MANYTRUE; } - return ":MANY-false"; + return MANYFALSE; } - public List getDependencyList(EClassifier eClassifier, String className){ + public List getDependencyList(EClassifier eClassifier){ List returnValue = new ArrayList<>();; EList somelist = ((EClass) eClassifier).getEAllSuperTypes(); if (somelist.isEmpty()){ @@ -617,10 +652,9 @@ public class MSModelUtils { Map returnClass = getRefclass(classMap, className); returnObject.put(className, returnClass); for (Entry reAttribute :returnClass.entrySet()){ - if (reAttribute.getValue().split(":")[1].contains("MANY")){ - if (classMap.get(reAttribute.getValue().split(":")[0]) != null){ + if (reAttribute.getValue().split(":")[1].contains("MANY") && + classMap.get(reAttribute.getValue().split(":")[0]) != null){ returnObject.putAll(recursiveReference(classMap, reAttribute.getValue().split(":")[0])); - } } } @@ -629,18 +663,16 @@ public class MSModelUtils { } - public String createJson(Map subClassAttributes, Map classMap, String className) { + public String createJson(Map classMap, String className) { boolean enumType; Map> myObject = new HashMap<>(); for ( Entry map : classMap.get(className).getRefAttribute().entrySet()){ String value = map.getValue().split(":")[0]; if (value!=null){ enumType = classMap.get(className).getEnumType().containsKey(value); - if (!enumType){ - if (map.getValue().split(":")[1].contains("MANY")){ + if (!enumType && map.getValue().split(":")[1].contains("MANY")){ Map> testRecursive = recursiveReference(classMap, map.getValue().split(":")[0] ); myObject.putAll(testRecursive); - } } } } @@ -680,7 +712,7 @@ public class MSModelUtils { } } - return createJson(workingMap, classMap, modelName); + return createJson(classMap, modelName); } public List getFullDependencyList(List dependency, Map classMap) { @@ -702,4 +734,529 @@ public class MSModelUtils { return returnList; } -} + + /* + * For TOSCA Model + */ + public void parseTosca (String fileName){ + LinkedHashMap map= new LinkedHashMap<>(); + + try { + map=load(fileName); + + parseDataAndPolicyNodes(map); + + LinkedHashMap dataMapForJson=parseDataNodes(map); + + constructJsonForDataFields(dataMapForJson); + + LinkedHashMap> mapKey= parsePolicyNodes(map); + + createAttributes(mapKey); + + } catch (IOException e) { + logger.error(e); + } + + } + + @SuppressWarnings("unchecked") + public LinkedHashMap load(String fileName) throws IOException { + File newConfiguration = new File(fileName); + StringBuilder orderInfo = new StringBuilder("["); + Yaml yaml = new Yaml(); + LinkedHashMap yamlMap = null; + try(InputStream is = new FileInputStream(newConfiguration)){ + yamlMap = (LinkedHashMap) yaml.load(is); + } catch (FileNotFoundException e) { + logger.error(e); + } + + StringBuilder sb = new StringBuilder(); + LinkedHashMap settings = new LinkedHashMap<>(); + if (yamlMap == null) { + return settings; + } + + findNode(yamlMap); + + orderedElements.stream().forEach((string) -> { + orderInfo.append(string); + orderInfo.append(","); + logger.info("Content: " + string); + }); + + orderInfo.append("]"); + + dataOrderInfo = orderInfo.toString(); + dataOrderInfo = dataOrderInfo.replace(",]", "]"); + + logger.info("dataOrderInfo :" + dataOrderInfo); + + List path = new ArrayList <>(); + serializeMap(settings, sb, path, yamlMap); + return settings; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private void serializeMap(LinkedHashMap settings, StringBuilder sb, List path, Map yamlMap) { + for (Map.Entry entry : yamlMap.entrySet()) { + + if (entry.getValue() instanceof Map) { + path.add((String) entry.getKey()); + serializeMap(settings, sb, path, (Map) 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(LinkedHashMap settings, StringBuilder sb, List path, List yamlList) { + int counter = 0; + for (Object listEle : yamlList) { + if (listEle instanceof Map) { + path.add(Integer.toString(counter)); + serializeMap(settings, sb, path, (Map) listEle); + path.remove(path.size() - 1); + } else if (listEle instanceof List) { + path.add(Integer.toString(counter)); + serializeList(settings, sb, path, (List) listEle); + path.remove(path.size() - 1); + } else { + serializeValue(settings, sb, path, Integer.toString(counter), listEle); + } + counter++; + } + } + + private void serializeValue(LinkedHashMap settings, StringBuilder sb, List 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()); + } + + + void parseDataAndPolicyNodes(LinkedHashMap 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()); + + 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()); + + stringBetweenDotsForDataFields(subNodeString); + } + } + } + + // Second index of dot should be returned. + public int stringBetweenDots(String str){ + String stringToSearch=str; + String[]ss=stringToSearch.split("\\."); + if(ss!=null){ + int len= ss.length; + if(len>2){ + uniqueKeys.add(ss[2]); + } + } + + return uniqueKeys.size(); + } + + + public void stringBetweenDotsForDataFields(String str){ + String stringToSearch=str; + String[]ss=stringToSearch.split("\\."); + if(ss!=null){ + int len= ss.length; + + if(len>2){ + uniqueDataKeys.add(ss[0]+"%"+ss[2]); + } + } + } + + void constructJsonForDataFields(LinkedHashMap dataMapForJson){ + LinkedHashMap> dataMapKey= new LinkedHashMap <>(); + LinkedHashMap hmSub; + for(Map.Entry entry: dataMapForJson.entrySet()){ + String uniqueDataKey= entry.getKey(); + String[] uniqueDataKeySplit=uniqueDataKey.split("%"); + String value= dataMapForJson.get(uniqueDataKey); + if(dataMapKey.containsKey(uniqueDataKeySplit[0])){ + hmSub = dataMapKey.get(uniqueDataKeySplit[0]); + hmSub.put(uniqueDataKeySplit[1], value); + } + else{ + hmSub=new LinkedHashMap <>(); + hmSub.put(uniqueDataKeySplit[1], value); + } + + 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("###############################################################################"); + } + + LinkedHashMap parseDataNodes(LinkedHashMap map){ + LinkedHashMap dataMapForJson=new LinkedHashMap <>(); + matchableValues = new HashMap <>(); + for(String uniqueDataKey: uniqueDataKeys){ + if(uniqueDataKey.contains("%")){ + String[] uniqueDataKeySplit= uniqueDataKey.split("%"); + String findType=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+TYPE; + String typeValue=map.get(findType); + logger.info(typeValue); + + String findRequired=DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+REQUIRED; + String requiredValue= map.get(findRequired); + + String matchable =DATATYPE+uniqueDataKeySplit[0]+PROPERTIES+uniqueDataKeySplit[1]+MATCHABLE; + + String matchableValue= map.get(matchable); + + if(matchableValue != null && matchableValue.equalsIgnoreCase("true")){ + if(uniqueDataKey.contains("%")){ + String[] keys= uniqueDataKey.split("%"); + String key=keys[keys.length -1]; + matchableValues.put(key, MATCHINGTRUE); + }else{ + matchableValues.put(uniqueDataKey, MATCHINGTRUE); + } + } + + if(requiredValue == null || requiredValue.isEmpty()){ + requiredValue = "false"; + } + if(typeValue != null && (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); + logger.info("requiredValue is:"+ requiredValue); + + StringBuilder attributeIndividualStringBuilder= new StringBuilder(); + attributeIndividualStringBuilder.append(typeValue+DEFAULTVALUE); + attributeIndividualStringBuilder.append(defaultValue+REQUIREDVALUE); + attributeIndividualStringBuilder.append(requiredValue+MANYFALSE); + dataMapForJson.put(uniqueDataKey, attributeIndividualStringBuilder.toString()); + } + else if(typeValue != null && typeValue.equalsIgnoreCase(LIST)){ + logger.info("requiredValue is:"+ requiredValue); + 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+REQUIREDVALUE); + referenceIndividualStringBuilder.append(requiredValue+MANYTRUE); + dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString()); + }//Its string + else{ + StringBuilder stringListItems= new StringBuilder(); + stringListItems.append(uniqueDataKeySplit[1].toUpperCase()+REQUIREDVALUE+requiredValue +MANYFALSE); + 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; + logger.info("findConstraints => " + findConstraints); + String constraintsValue=map.get(findConstraints); + logger.info("constraintsValue => " + constraintsValue); + if(constraintsValue==null){ + break; + } + else{ + logger.info("constraintsValue => " + constraintsValue); + if(constraintsValue.contains("=")){ + constraintsValue = constraintsValue.replace("=", "equal-sign"); + } + dataConstraints.add(constraintsValue); + dataListBuffer.append(constraintsValue+","); + } + } + dataListBuffer.append("]#"); + logger.info(dataListBuffer); + } + } + } + else{ + String findUserDefined=DATATYPE+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+REQUIREDVALUE); + referenceIndividualStringBuilder.append(requiredValue+MANYFALSE); + dataMapForJson.put(uniqueDataKey, referenceIndividualStringBuilder.toString()); + + } + } + } + + return dataMapForJson; + } + + + LinkedHashMap> parsePolicyNodes(Map map){ + LinkedHashMap> mapKey= new LinkedHashMap <>(); + for(String uniqueKey: uniqueKeys){ + LinkedHashMap 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.put(keyStr,valueStr); + } + + } else { + hm = new LinkedHashMap <>(); + 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.put(keyStr,valueStr); + } + mapKey.put(uniqueKey, hm); + } + } + } + } + return mapKey; + } + + void createAttributes(LinkedHashMap> 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(); + LinkedHashMap keyValues=mapKey.get(keySetString); + if(STRING.equalsIgnoreCase(keyValues.get("type"))|| + INTEGER.equalsIgnoreCase(keyValues.get("type"))){ + StringBuilder attributeIndividualStringBuilder= new StringBuilder(); + attributeIndividualStringBuilder.append(keySetString+"="); + attributeIndividualStringBuilder.append(keyValues.get("type")+DEFAULTVALUE); + attributeIndividualStringBuilder.append(keyValues.get("default")+REQUIREDVALUE); + attributeIndividualStringBuilder.append(keyValues.get("required")+MANYFALSE); + attributeStringBuilder.append(attributeIndividualStringBuilder+","); + if("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){ + matchableValues.put(keySetString, MATCHINGTRUE); + } + } + else if(LIST.equalsIgnoreCase(keyValues.get("type"))){ + + if(("true").equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){ + matchableValues.put(keySetString, MATCHINGTRUE); + } + //List Datatype + Set keys= keyValues.keySet(); + Iterator itr=keys.iterator(); + boolean isDefinedType = false; + 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 + if(StringUtils.isNumeric(key) ){ //only integer key for the value of Constrains + constraints.add(keyValues.get(key)); + } + }else{ + //This is user defined type + String trimValue=value.substring(value.lastIndexOf('.')+1); + StringBuilder referenceIndividualStringBuilder= new StringBuilder(); + referenceIndividualStringBuilder.append(keySetString+"="+trimValue+MANYTRUE); + referenceStringBuilder.append(referenceIndividualStringBuilder+","); + isDefinedType = true; + } + } + + } + + if(!isDefinedType && LIST.equalsIgnoreCase(keyValues.get("type"))){ //type is not user defined and is a list but no constraints defined. + if(constraints == null || constraints.isEmpty()){ + referenceStringBuilder.append(keySetString+"=MANY-true"+","); + } + } + }else{ + //User defined Datatype. + if("true".equalsIgnoreCase(keyValues.get(MATCHABLEKEY))){ + matchableValues.put(keySetString, MATCHINGTRUE); + } + String value=keyValues.get("type"); + if(value != null && !value.isEmpty()){ + String trimValue=value.substring(value.lastIndexOf('.')+1); + StringBuilder referenceIndividualStringBuilder= new StringBuilder(); + referenceIndividualStringBuilder.append(keySetString+"="+trimValue+MANYFALSE); + referenceStringBuilder.append(referenceIndividualStringBuilder+","); + }else{ + logger.info("keyValues.get(type) is null/empty"); + } + + } + if(constraints!=null && !constraints.isEmpty()){ + //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()+MANYFALSE); + 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(); + } + + @SuppressWarnings("unchecked") + public void findNode(LinkedHashMap map) { + + map.forEach((key,value) -> { + // if the value is properties and its type is map object, then save all the keys + if(key.equals("properties") && value instanceof Map){ + saveNodes((LinkedHashMap)value); + } + + if(!key.equals("policy.nodes.Root") && value instanceof Map){ + //value is a Map object, then make a recursive call + findNode((LinkedHashMap) value); + } + }); + + } + + public void saveNodes(LinkedHashMap map) { + + map.forEach((key,value) -> { + + orderedElements.add((String)key); + + }); + + } + + public String getAttributeString() { + return attributeString; + } + public void setAttributeString(String attributeString) { + this.attributeString = attributeString; + } + + public LinkedHashMap getRetmap() { + return retmap; + } + + public void setRetmap(LinkedHashMap retmap) { + this.retmap = retmap; + } + public Map getMatchableValues() { + return matchableValues; + } + + public void setMatchableValues(Map matchableValues) { + this.matchableValues = matchableValues; + } + public String getReferenceAttributes() { + return referenceAttributes; + } + + public void setReferenceAttributes(String referenceAttributes) { + this.referenceAttributes = referenceAttributes; + } + public String getListConstraints() { + return listConstraints; + } + + public void setListConstraints(String listConstraints) { + this.listConstraints = listConstraints; + } + public String getDataOrderInfo() { + return dataOrderInfo; + } + + public void setDataOrderInfo(String dataOrderInfo) { + this.dataOrderInfo = dataOrderInfo; + } + +} \ No newline at end of file