diff options
Diffstat (limited to 'ONAP-REST/src/main')
7 files changed, 1229 insertions, 106 deletions
diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/XACMLRestProperties.java b/ONAP-REST/src/main/java/org/onap/policy/rest/XACMLRestProperties.java index e52a4110e..325dda58d 100644 --- a/ONAP-REST/src/main/java/org/onap/policy/rest/XACMLRestProperties.java +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/XACMLRestProperties.java @@ -347,6 +347,10 @@ public class XACMLRestProperties extends XACMLProperties { */ public static final String TemplateVersion_MS= "xacml.rest.microServices"; /* + * Optimization Policy Template Version + */ + public static final String TemplateVersion_OOF= "xacml.rest.optimization"; + /* * Firewall Policy Template Version */ public static final String TemplateVersion_FW= "xacml.rest.firewallPolicy"; diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/MicroServiceModels.java b/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/MicroServiceModels.java index f323251cc..4029ad5fb 100644 --- a/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/MicroServiceModels.java +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/MicroServiceModels.java @@ -68,6 +68,9 @@ public class MicroServiceModels implements Serializable{ @Column (name="sub_attributes", nullable=false, length=2000) private String sub_attributes; + @Column (name="dataOrderInfo", nullable=true, length=2000) + private String dataOrderInfo; + @Column (name="version", nullable=false, length=2000) private String version; @@ -84,6 +87,14 @@ public class MicroServiceModels implements Serializable{ public void setSub_attributes(String sub_attributes) { this.sub_attributes = sub_attributes; } + + public String getDataOrderInfo() { + return dataOrderInfo; + } + + public void setDataOrderInfo(String dataOrderInfo) { + this.dataOrderInfo = dataOrderInfo; + } public String getVersion() { return version; @@ -168,4 +179,4 @@ public class MicroServiceModels implements Serializable{ public void setAnnotation(String annotation) { this.annotation = annotation; } -} +}
\ No newline at end of file diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/MicroserviceHeaderdeFaults.java b/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/MicroserviceHeaderdeFaults.java new file mode 100644 index 000000000..b876484de --- /dev/null +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/MicroserviceHeaderdeFaults.java @@ -0,0 +1,123 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-REST + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.rest.jpa; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQuery; +import javax.persistence.OrderBy; +import javax.persistence.PrePersist; +import javax.persistence.PreUpdate; +import javax.persistence.Table; + +@Entity +@Table(name="MicroserviceHeaderdeFaults") +@NamedQuery(name="MicroserviceHeaderdeFaults.findAll", query="SELECT e FROM MicroserviceHeaderdeFaults e ") +public class MicroserviceHeaderdeFaults implements Serializable { + private static final long serialVersionUID = 1L; + + private static String domain; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name="id") + private int id; + + @Column(name="onapName") + private String onapName; + + @Column(name="guard") + private String guard ; + + @Column(name="priority") + private String priority; + + @Column(name="riskType") + private String riskType ; + + @Column(name="riskLevel") + + private String riskLevel; + + @Column(name="modelName", nullable=false) + @OrderBy("asc") + private String modelName; + + @PrePersist + public void prePersist() { + + } + @PreUpdate + public void preUpdate() { + } + + + public int getId() { + return this.id; + } + + public void setId(int id) { + this.id = id; + } + + public String getModelName() { + return modelName; + } + public void setModelName(String modelName) { + this.modelName = modelName; + } + public String getOnapName() { + return onapName; + } + public void setOnapName(String onapName) { + this.onapName = onapName; + } + public String getGuard() { + return guard; + } + public void setGuard(String guard) { + this.guard = guard; + } + public String getPriority() { + return priority; + } + public void setPriority(String priority) { + this.priority = priority; + } + public String getRiskType() { + return riskType; + } + public void setRiskType(String riskType) { + this.riskType = riskType; + } + public String getRiskLevel() { + return riskLevel; + } + public void setRiskLevel(String riskLevel) { + this.riskLevel = riskLevel; + } + +}
\ No newline at end of file diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/OptimizationModels.java b/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/OptimizationModels.java new file mode 100644 index 000000000..1accc9968 --- /dev/null +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/jpa/OptimizationModels.java @@ -0,0 +1,182 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-REST + * ================================================================================ + * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.policy.rest.jpa; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.NamedQuery; +import javax.persistence.OrderBy; +import javax.persistence.Table; +/* + * JPA for the OOF Models. + * + * @version: 0.1 + */ + + +@Entity +@Table(name="OptimizationModels") +@NamedQuery(name="OptimizationModels.findAll", query="SELECT b FROM OptimizationModels b ") +public class OptimizationModels implements Serializable{ + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name="id") + private int id; + + @Column(name="modelName", nullable=false, unique=true) + @OrderBy("asc") + private String modelName; + + @Column(name="description", nullable=true, length=2048) + private String description; + + @Column(name="dependency", nullable=true, length=2048) + private String dependency; + + @Column(name="attributes", nullable=false, length=255) + private String attributes; + + @Column(name="ref_attributes", nullable=false, length=255) + private String refattributes; + + @Column (name="sub_attributes", nullable=false, length=2000) + private String subattributes; + + @Column (name="dataOrderInfo", nullable=true, length=2000) + private String dataOrderInfo; + + @Column (name="version", nullable=false, length=2000) + private String version; + + @Column (name="enumValues", nullable=false, length=2000) + private String enumValues; + + @Column (name="annotation", nullable=false, length=2000) + private String annotation; + + public String getSubattributes() { + return subattributes; + } + + public void setSubattributes(String subattributes) { + this.subattributes = subattributes; + } + + public String getDataOrderInfo() { + return dataOrderInfo; + } + + public void setDataOrderInfo(String dataOrderInfo) { + this.dataOrderInfo = dataOrderInfo; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + @ManyToOne + @JoinColumn(name="imported_by") + private UserInfo userCreatedBy; + + public UserInfo getUserCreatedBy() { + return userCreatedBy; + } + + public void setUserCreatedBy(UserInfo userCreatedBy) { + this.userCreatedBy = userCreatedBy; + } + + public String getAttributes() { + return attributes; + } + + public void setAttributes(String attributes) { + this.attributes = attributes; + } + + public String getRefattributes() { + return refattributes; + } + + public void setRefattributes(String refattributes) { + this.refattributes = refattributes; + } + + public int getId() { + return this.id; + } + + public void setId(int id) { + this.id = id; + } + + public String getDescription() { + return this.description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getDependency() { + return dependency; + } + + public void setDependency(String dependency) { + this.dependency = dependency; + } + + public String getModelName(){ + return this.modelName; + } + + public void setModelName(String modelName){ + this.modelName = modelName; + } + + public String getEnumValues() { + return enumValues; + } + + public void setEnumValues(String enumValues) { + this.enumValues = enumValues; + } + + public String getAnnotation() { + return annotation; + } + + public void setAnnotation(String annotation) { + this.annotation = annotation; + } +} 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<String> orderedElements = new ArrayList<>(); + private String dataOrderInfo = null; + private Set<String> uniqueDataKeys= new HashSet<>(); + private Set<String> uniqueKeys= new HashSet<>(); + private String listConstraints = null; + private String referenceAttributes; + private LinkedHashMap<String, Object> retmap = new LinkedHashMap<>(); + private Map<String, String> 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<String> 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<String, String> listRef = classMap.get(key).getRefAttribute(); for ( Entry<String, String> eSet : listAttributes.entrySet()){ String key2 = eSet.getKey(); - tempAttribute.put(key2, "matching-true"); + tempAttribute.put(key2, MATCHINGTRUE); } for ( Entry<String, String> eSet : listRef.entrySet()){ String key3 = eSet.getKey(); - tempAttribute.put(key3, "matching-true"); + tempAttribute.put(key3, MATCHINGTRUE); } } @@ -232,7 +273,7 @@ public class MSModelUtils { HashMap<String, String> 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<String, String> 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<String> getDependencyList(EClassifier eClassifier, String className){ + public List<String> getDependencyList(EClassifier eClassifier){ List<String> returnValue = new ArrayList<>();; EList<EClass> somelist = ((EClass) eClassifier).getEAllSuperTypes(); if (somelist.isEmpty()){ @@ -617,10 +652,9 @@ public class MSModelUtils { Map<String, String> returnClass = getRefclass(classMap, className); returnObject.put(className, returnClass); for (Entry<String, String> 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<String, Object> subClassAttributes, Map<String, MSAttributeObject> classMap, String className) { + public String createJson(Map<String, MSAttributeObject> classMap, String className) { boolean enumType; Map<String, Map<String, String>> myObject = new HashMap<>(); for ( Entry<String, String> 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<String, Map<String, String>> 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<String> getFullDependencyList(List<String> dependency, Map<String,MSAttributeObject > classMap) { @@ -702,4 +734,529 @@ public class MSModelUtils { return returnList; } -} + + /* + * For TOSCA Model + */ + public void parseTosca (String fileName){ + LinkedHashMap<String,String> map= new LinkedHashMap<>(); + + try { + map=load(fileName); + + parseDataAndPolicyNodes(map); + + LinkedHashMap<String,String> dataMapForJson=parseDataNodes(map); + + constructJsonForDataFields(dataMapForJson); + + LinkedHashMap<String,LinkedHashMap<String,String>> mapKey= parsePolicyNodes(map); + + createAttributes(mapKey); + + } catch (IOException e) { + logger.error(e); + } + + } + + @SuppressWarnings("unchecked") + public LinkedHashMap<String, String> load(String fileName) throws IOException { + File newConfiguration = new File(fileName); + StringBuilder orderInfo = new StringBuilder("["); + Yaml yaml = new Yaml(); + LinkedHashMap<Object, Object> yamlMap = null; + try(InputStream is = new FileInputStream(newConfiguration)){ + yamlMap = (LinkedHashMap<Object, Object>) yaml.load(is); + } catch (FileNotFoundException e) { + logger.error(e); + } + + StringBuilder sb = new StringBuilder(); + LinkedHashMap<String, String> 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<String> path = new ArrayList <>(); + serializeMap(settings, sb, path, yamlMap); + return settings; + } + + @SuppressWarnings({ "unchecked", "rawtypes" }) + private void serializeMap(LinkedHashMap<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(LinkedHashMap<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(LinkedHashMap<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()); + } + + + void parseDataAndPolicyNodes(LinkedHashMap<String,String> 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<String,String> dataMapForJson){ + LinkedHashMap<String,LinkedHashMap<String,String>> dataMapKey= new LinkedHashMap <>(); + LinkedHashMap<String, String> hmSub; + for(Map.Entry<String, String> 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<String,LinkedHashMap<String,String>> entry: dataMapKey.entrySet()){ + String s=entry.getKey(); + json= new JSONObject(); + HashMap<String,String> jsonHm=dataMapKey.get(s); + for(Map.Entry<String,String> entryMap:jsonHm.entrySet()){ + String key=entryMap.getKey(); + json.put(key, jsonHm.get(key)); + } + 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("###############################################################################"); + } + + LinkedHashMap<String,String> parseDataNodes(LinkedHashMap<String,String> map){ + LinkedHashMap<String,String> 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<String, LinkedHashMap<String, String>> parsePolicyNodes(Map<String,String> map){ + LinkedHashMap<String,LinkedHashMap<String,String>> mapKey= new LinkedHashMap <>(); + for(String uniqueKey: uniqueKeys){ + LinkedHashMap<String,String> hm; + + for(Map.Entry<String,String> 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<String,LinkedHashMap<String,String>> mapKey){ + StringBuilder attributeStringBuilder= new StringBuilder(); + StringBuilder referenceStringBuilder= new StringBuilder(); + StringBuilder listBuffer= new StringBuilder(); + List<String> constraints= new ArrayList<>(); + for(Map.Entry<String,LinkedHashMap<String,String>> entry: mapKey.entrySet()){ + String keySetString= entry.getKey(); + LinkedHashMap<String,String> 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<String> keys= keyValues.keySet(); + Iterator<String> 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<Object, Object> 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<Object, Object>) 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<String, Object> getRetmap() { + return retmap; + } + + public void setRetmap(LinkedHashMap<String, Object> retmap) { + this.retmap = retmap; + } + public Map<String, String> getMatchableValues() { + return matchableValues; + } + + public void setMatchableValues(Map<String, String> 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 diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java index 47291cf7f..2e685d423 100644 --- a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidation.java @@ -44,6 +44,7 @@ import org.onap.policy.rest.adapter.ClosedLoopPMBody; import org.onap.policy.rest.adapter.PolicyRestAdapter; import org.onap.policy.rest.dao.CommonClassDao; import org.onap.policy.rest.jpa.MicroServiceModels; +import org.onap.policy.rest.jpa.OptimizationModels; import org.onap.policy.rest.jpa.SafePolicyWarning; import org.onap.policy.utils.PolicyUtils; import org.onap.policy.xacml.api.XACMLErrorConstants; @@ -65,12 +66,19 @@ public class PolicyValidation { public static final String DECISION_POLICY = "Decision"; public static final String CLOSEDLOOP_POLICY = "ClosedLoop_Fault"; public static final String CLOSEDLOOP_PM = "ClosedLoop_PM"; - public static final String ENFORCER_CONFIG_POLICY= "Enforcer Config"; - public static final String MICROSERVICES="Micro Service"; - public static final String FIREWALL="Firewall Config"; + public static final String ENFORCER_CONFIG_POLICY = "Enforcer Config"; + public static final String MICROSERVICES = "Micro Service"; + public static final String FIREWALL = "Firewall Config"; + public static final String OPTIMIZATION="Optimization"; + public static final String BRMSPARAM = "BRMS_Param"; + public static final String BRMSRAW = "BRMS_Raw"; public static final String HTML_ITALICS_LNBREAK = "</i><br>"; public static final String SUCCESS = "success"; public static final String EMPTY_COMPONENT_ATTR = "Component Attributes: One or more Fields in Component Attributes is Empty."; + public static final String ISREQUIRED = " is required"; + public static final String SPACESINVALIDCHARS = " : value has spaces or invalid characters</i><br>"; + public static final String RULEALGORITHMS = "<b>Rule Algorithms</b>:<i>"; + public static final String VALUE = "value"; private static Map<String, String> mapAttribute = new HashMap<>(); private static Map<String, String> jsonRequestMap = new HashMap<>(); @@ -122,8 +130,8 @@ public class PolicyValidation { String key = null; if(((LinkedHashMap<?, ?>) attribute).get("key") != null){ key = ((LinkedHashMap<?, ?>) attribute).get("key").toString(); - if(!PolicyUtils.policySpecialCharValidator(key).contains("success")){ - responseString.append("<b>Attributes or Component Attributes</b>:<i>" + value + " : value has spaces or invalid characters</i><br>"); + if(!PolicyUtils.policySpecialCharValidator(key).contains(SUCCESS)){ + responseString.append("<b>Attributes or Component Attributes</b>:<i>" + value + SPACESINVALIDCHARS); valid = false; } }else{ @@ -131,7 +139,7 @@ public class PolicyValidation { if("Base".equals(policyData.getConfigPolicyType())){ responseString.append("<b>Attributes</b>:<i> has one missing Attribute key</i><br>"); } - if("BRMS_Param".equals(policyData.getConfigPolicyType()) || "BRMS_Raw".equals(policyData.getConfigPolicyType())){ + if(BRMSPARAM.equals(policyData.getConfigPolicyType()) || BRMSRAW.equals(policyData.getConfigPolicyType())){ responseString.append("<b>Rule Attributes</b>:<i> has one missing Attribute key</i><br>"); } }else{ @@ -139,18 +147,18 @@ public class PolicyValidation { } valid = false; } - if(((LinkedHashMap<?, ?>) attribute).get("value") != null){ - value = ((LinkedHashMap<?, ?>) attribute).get("value").toString(); - if(!PolicyUtils.policySpecialCharValidator(value).contains("success")){ + if(((LinkedHashMap<?, ?>) attribute).get(VALUE) != null){ + value = ((LinkedHashMap<?, ?>) attribute).get(VALUE).toString(); + if(!PolicyUtils.policySpecialCharValidator(value).contains(SUCCESS)){ if(CONFIG_POLICY.equals(policyData.getPolicyType())){ if("Base".equals(policyData.getConfigPolicyType())){ - responseString.append("<b>Attributes</b>:<i>" + value + " : value has spaces or invalid characters</i><br>"); + responseString.append("<b>Attributes</b>:<i>" + value + SPACESINVALIDCHARS); } - if("BRMS_Param".equals(policyData.getConfigPolicyType()) || "BRMS_Raw".equals(policyData.getConfigPolicyType())){ - responseString.append("<b>Rule Attributes</b>:<i>" + value + " : value has spaces or invalid characters</i><br>"); + if(BRMSPARAM.equals(policyData.getConfigPolicyType()) || BRMSRAW.equals(policyData.getConfigPolicyType())){ + responseString.append("<b>Rule Attributes</b>:<i>" + value + SPACESINVALIDCHARS); } }else{ - responseString.append("<b>Component Attributes</b>:<i>" + value + " : value has spaces or invalid characters</i><br>"); + responseString.append("<b>Component Attributes</b>:<i>" + value + SPACESINVALIDCHARS); } valid = false; } @@ -159,7 +167,7 @@ public class PolicyValidation { if("Base".equals(policyData.getConfigPolicyType())){ responseString.append("<b>Attributes</b>:<i> has one missing Attribute value</i><br>"); } - if("BRMS_Param".equals(policyData.getConfigPolicyType()) || "BRMS_Raw".equals(policyData.getConfigPolicyType())){ + if(BRMSPARAM.equals(policyData.getConfigPolicyType()) || BRMSRAW.equals(policyData.getConfigPolicyType())){ responseString.append("<b>Rule Attributes</b>:<i> has one missing Attribute value</i><br>"); } }else{ @@ -180,10 +188,10 @@ public class PolicyValidation { responseString.append("<b>Settings Attributes</b>:<i> has one missing Attribute key</i><br>"); valid = false; } - if(((LinkedHashMap<?, ?>) attribute).get("value") != null){ - value = ((LinkedHashMap<?, ?>) attribute).get("value").toString(); - if(!PolicyUtils.policySpecialCharValidator(value).contains("success")){ - responseString.append("<b>Settings Attributes</b>:<i>" + value + " : value has spaces or invalid characters</i><br>"); + if(((LinkedHashMap<?, ?>) attribute).get(VALUE) != null){ + value = ((LinkedHashMap<?, ?>) attribute).get(VALUE).toString(); + if(!PolicyUtils.policySpecialCharValidator(value).contains(SUCCESS)){ + responseString.append("<b>Settings Attributes</b>:<i>" + value + SPACESINVALIDCHARS); valid = false; } }else{ @@ -199,21 +207,21 @@ public class PolicyValidation { if(attribute instanceof LinkedHashMap<?, ?>){ String label = ((LinkedHashMap<?, ?>) attribute).get("id").toString(); if(((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField1") == null){ - responseString.append("<b>Rule Algorithms</b>:<i>" + label + " : Field 1 value is not selected</i><br>"); + responseString.append(RULEALGORITHMS + label + " : Field 1 value is not selected</i><br>"); valid = false; } if(((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmCombo") == null){ - responseString.append("<b>Rule Algorithms</b>:<i>" + label + " : Field 2 value is not selected</i><br>"); + responseString.append(RULEALGORITHMS + label + " : Field 2 value is not selected</i><br>"); valid = false; } if(((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2") != null){ String value = ((LinkedHashMap<?, ?>) attribute).get("dynamicRuleAlgorithmField2").toString(); - if(!PolicyUtils.policySpecialCharValidator(value).contains("success")){ - responseString.append("<b>Rule Algorithms</b>:<i>" + label + " : Field 3 value has special characters</i><br>"); + if(!PolicyUtils.policySpecialCharValidator(value).contains(SUCCESS)){ + responseString.append(RULEALGORITHMS + label + " : Field 3 value has special characters</i><br>"); valid = false; } }else{ - responseString.append("<b>Rule Algorithms</b>:<i>" + label + " : Field 3 value is empty</i><br>"); + responseString.append(RULEALGORITHMS + label + " : Field 3 value is empty</i><br>"); valid = false; } } @@ -223,7 +231,7 @@ public class PolicyValidation { if(CONFIG_POLICY.equalsIgnoreCase(policyData.getPolicyType())){ if ("Base".equals(policyData.getConfigPolicyType()) || CLOSEDLOOP_POLICY.equals(policyData.getConfigPolicyType()) || CLOSEDLOOP_PM.equals(policyData.getConfigPolicyType()) || ENFORCER_CONFIG_POLICY.equals(policyData.getConfigPolicyType()) - || MICROSERVICES.equals(policyData.getConfigPolicyType())) { + || MICROSERVICES.equals(policyData.getConfigPolicyType()) || OPTIMIZATION.equals(policyData.getConfigPolicyType())) { if(!Strings.isNullOrEmpty(policyData.getOnapName())) { String onapNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getOnapName()); @@ -269,7 +277,8 @@ public class PolicyValidation { responseString.append("<b>Guard</b>: Guard Value Should not be Empty" + HTML_ITALICS_LNBREAK); valid = false; } - + + // Validate Config Base Policy Data if("Base".equalsIgnoreCase(policyData.getConfigPolicyType())){ if(!Strings.isNullOrEmpty(policyData.getConfigName())) { String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName()); @@ -320,7 +329,8 @@ public class PolicyValidation { valid = false; } } - + + // Validate Config Firewall Policy Data if(FIREWALL.equalsIgnoreCase(policyData.getConfigPolicyType())){ if(policyData.getConfigName() != null && !policyData.getConfigName().isEmpty()){ String configNameValidate = PolicyUtils.policySpecialCharValidator(policyData.getConfigName()); @@ -337,11 +347,15 @@ public class PolicyValidation { valid = false; } } - if("BRMS_Param".equalsIgnoreCase(policyData.getConfigPolicyType()) && Strings.isNullOrEmpty(policyData.getRuleName())){ + + // Validate BRMS_Param Policy Data + if(BRMSPARAM.equalsIgnoreCase(policyData.getConfigPolicyType()) && Strings.isNullOrEmpty(policyData.getRuleName())){ responseString.append("<b>BRMS Template</b>:<i>BRMS Template is required" + HTML_ITALICS_LNBREAK); valid = false; } - if("BRMS_Raw".equalsIgnoreCase(policyData.getConfigPolicyType())){ + + // Validate BRMS_Raw Policy Data + if(BRMSRAW.equalsIgnoreCase(policyData.getConfigPolicyType())){ if(policyData.getConfigBodyData() != null && !policyData.getConfigBodyData().isEmpty()){ String message = PolicyUtils.brmsRawValidate(policyData.getConfigBodyData()); @@ -355,6 +369,8 @@ public class PolicyValidation { valid = false; } } + + // Validate ClosedLoop_PM Policy Data if(CLOSEDLOOP_PM.equalsIgnoreCase(policyData.getConfigPolicyType())){ try{ if(Strings.isNullOrEmpty(policyData.getServiceTypePolicyName().get("serviceTypePolicyName").toString())){ @@ -407,6 +423,8 @@ public class PolicyValidation { valid = false; } } + + // Validate ClosedLoop_Fault Policy Data if(CLOSEDLOOP_POLICY.equalsIgnoreCase(policyData.getConfigPolicyType())){ if(policyData.getJsonBody() != null){ @@ -511,8 +529,10 @@ public class PolicyValidation { valid = false; } } - + + // Validate MicroServices Policy Data if (MICROSERVICES.equals(policyData.getConfigPolicyType())){ + if(!Strings.isNullOrEmpty(policyData.getServiceType())){ modelRequiredFieldsList.clear(); @@ -529,7 +549,7 @@ public class PolicyValidation { } if(!Strings.isNullOrEmpty(version)) { - MicroServiceModels returnModel = getAttributeObject(service, version); + MicroServiceModels returnModel = getMSModelData(service, version); if(returnModel != null) { @@ -639,11 +659,11 @@ public class PolicyValidation { if(Strings.isNullOrEmpty(jsonRequestMap.get(requiredField)) || "\"\"".equals(value) || "".equals(jsonRequestMap.get(requiredField))){ - responseString.append("<b>Micro Service Model</b>:<i> " + requiredField + " is required" + HTML_ITALICS_LNBREAK); + responseString.append("<b>Micro Service Model</b>:<i> " + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); valid = false; } } else { - responseString.append("<b>Micro Service Model</b>:<i> " + requiredField + " is required" + HTML_ITALICS_LNBREAK); + responseString.append("<b>Micro Service Model</b>:<i> " + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); valid = false; } } @@ -667,6 +687,144 @@ public class PolicyValidation { responseString.append("<b>Priority</b>:<i> Priority is required" + HTML_ITALICS_LNBREAK); valid = false; } + } + + // Validate Optimization Policy Data + if (OPTIMIZATION.equals(policyData.getConfigPolicyType())){ + + if(!Strings.isNullOrEmpty(policyData.getServiceType())){ + + modelRequiredFieldsList.clear(); + pullJsonKeyPairs((JsonNode) policyData.getPolicyJSON()); + + String service; + String version; + if (policyData.getServiceType().contains("-v")){ + service = policyData.getServiceType().split("-v")[0]; + version = policyData.getServiceType().split("-v")[1]; + }else { + service = policyData.getServiceType(); + version = policyData.getVersion(); + } + + if(!Strings.isNullOrEmpty(version)) { + OptimizationModels returnModel = getOptimizationModelData(service, version); + + if(returnModel != null) { + + String annotation = returnModel.getAnnotation(); + String refAttributes = returnModel.getRefattributes(); + String subAttributes = returnModel.getSubattributes(); + String modelAttributes = returnModel.getAttributes(); + + if (!Strings.isNullOrEmpty(annotation)){ + Map<String, String> rangeMap = Splitter.on(",").withKeyValueSeparator("=").split(annotation); + for (Entry<String, String> rMap : rangeMap.entrySet()){ + if (rMap.getValue().contains("range::")){ + String value = mapAttribute.get(rMap.getKey().trim()); + String[] tempString = rMap.getValue().split("::")[1].split("-"); + int startNum = Integer.parseInt(tempString[0]); + int endNum = Integer.parseInt(tempString[1]); + String returnString = "InvalidreturnModel Range:" + rMap.getKey() + " must be between " + + startNum + " - " + endNum + ","; + + if(value != null) { + if (PolicyUtils.isInteger(value.replace("\"", ""))){ + int result = Integer.parseInt(value.replace("\"", "")); + if (result < startNum || result > endNum){ + responseString.append(returnString); + valid = false; + } + }else { + responseString.append(returnString); + valid = false; + } + } else { + responseString.append("<b>"+rMap.getKey()+"</b>:<i>" + rMap.getKey() + + " is required for the Optimization model " + service + HTML_ITALICS_LNBREAK); + valid = false; + } + + } + } + } + + // If request comes from the API we need to validate required fields in the Micro Service Model + // GUI request are already validated from the SDK-APP + if("API".equals(policyData.getApiflag())){ + // get list of required fields from the sub_Attributes of the Model + if(!Strings.isNullOrEmpty(subAttributes)) { + JsonObject subAttributesJson = stringToJsonObject(subAttributes); + findRequiredFields(subAttributesJson); + } + + // get list of required fields from the attributes of the Model + if (!Strings.isNullOrEmpty(modelAttributes)) { + Map<String, String> modelAttributesMap = null; + if (",".equals(modelAttributes.substring(modelAttributes.length()-1))) { + String attributeString = modelAttributes.substring(0, modelAttributes.length()-1); + modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributeString); + } else { + modelAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes); + } + String json = new ObjectMapper().writeValueAsString(modelAttributesMap); + findRequiredFields(stringToJsonObject(json)); + } + + // get list of required fields from the ref_Attributes of the Model + if (!Strings.isNullOrEmpty(refAttributes)) { + Map<String, String> refAttributesMap = null; + if (",".equals(refAttributes.substring(refAttributes.length()-1))) { + String attributesString = refAttributes.substring(0, refAttributes.length()-1); + refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(attributesString); + } else { + refAttributesMap = Splitter.on(",").withKeyValueSeparator("=").split(modelAttributes); + } + String json = new ObjectMapper().writeValueAsString(refAttributesMap); + findRequiredFields(stringToJsonObject(json)); + } + + if (modelRequiredFieldsList!=null || !modelRequiredFieldsList.isEmpty()) { + // create jsonRequestMap with all json keys and values from request + JsonNode rootNode = (JsonNode) policyData.getPolicyJSON(); + jsonRequestMap.clear(); + pullModelJsonKeyPairs(rootNode); + + // validate if the requiredFields are in the request + for(String requiredField : modelRequiredFieldsList) { + if (jsonRequestMap.containsKey(requiredField)) { + String value = jsonRequestMap.get(requiredField); + if(Strings.isNullOrEmpty(jsonRequestMap.get(requiredField)) || + "\"\"".equals(value) || + "".equals(jsonRequestMap.get(requiredField))){ + responseString.append("<b>Optimization Service Model</b>:<i> " + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); + valid = false; + } + } else { + responseString.append("<b>Optimization Service Model</b>:<i> " + requiredField + ISREQUIRED + HTML_ITALICS_LNBREAK); + valid = false; + } + } + } + } + } else { + responseString.append("<b>Optimization Service Model</b>:<i> Invalid Model. The model name, " + service + + " of version, " + version + " was not found in the dictionary" + HTML_ITALICS_LNBREAK); + valid = false; + } + } else { + responseString.append("<b>Optimization Service Version</b>:<i> Optimization Service Version is required" + HTML_ITALICS_LNBREAK); + valid = false; + } + } else { + responseString.append("<b>Optimization Service</b>:<i> Optimization Service Model is required" + HTML_ITALICS_LNBREAK); + valid = false; + } + + if(Strings.isNullOrEmpty(policyData.getPriority())){ + responseString.append("<b>Priority</b>:<i> Priority is required" + HTML_ITALICS_LNBREAK); + valid = false; + } } } if (DECISION_POLICY.equalsIgnoreCase(policyData.getPolicyType())){ @@ -864,7 +1022,7 @@ public class PolicyValidation { return res; } - private MicroServiceModels getAttributeObject(String name, String version) { + private MicroServiceModels getMSModelData(String name, String version) { MicroServiceModels workingModel = null; try{ List<Object> microServiceModelsData = commonClassDao.getDataById(MicroServiceModels.class, "modelName:version", name+":"+version); @@ -880,6 +1038,23 @@ public class PolicyValidation { return workingModel; } + + private OptimizationModels getOptimizationModelData(String name, String version) { + OptimizationModels workingModel = null; + try{ + List<Object> optimizationModelsData = commonClassDao.getDataById(OptimizationModels.class, "modelName:version", name+":"+version); + if(optimizationModelsData != null){ + workingModel = (OptimizationModels) optimizationModelsData.get(0); + } + }catch(Exception e){ + String message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Template. The template name, " + + name + " was not found in the dictionary: "; + LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE + message + e); + return null; + } + + return workingModel; + } private void pullJsonKeyPairs(JsonNode rootNode) { Iterator<Map.Entry<String, JsonNode>> fieldsIterator = rootNode.fields(); diff --git a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java index 76584e7c7..c80e5bb3b 100644 --- a/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java +++ b/ONAP-REST/src/main/java/org/onap/policy/rest/util/PolicyValidationRequestWrapper.java @@ -54,8 +54,18 @@ public class PolicyValidationRequestWrapper { public static final String CONFIG_NAME="configName"; public static final String INVALIDJSON = " improper JSON format: "; public static final String ONAPNAME = "onapname"; - public static final String SERVICETYPE_POLICY_NAME = "serviceTypePolicyName"; + public static final String CONTENT = "content"; + public static final String GUARD = "guard"; + public static final String LOCATION = "location"; + public static final String POLICYSCOPE = "policyScope"; + public static final String PRIORITY = "priority"; + public static final String RISKLEVEL = "riskLevel"; + public static final String RISKTYPE = "riskType"; + public static final String SERVICE = "service"; + public static final String VERSION = "version"; + public static final String SERVICETYPE_POLICY_NAME = "serviceTypePolicyName"; + public PolicyRestAdapter populateRequestParameters(HttpServletRequest request) { PolicyRestAdapter policyData = null; @@ -281,8 +291,8 @@ public class PolicyValidationRequestWrapper { // get values and attributes from the JsonObject if(json != null){ - if (json.containsKey("content")){ - String content = json.get("content").toString(); + if (json.containsKey(CONTENT)){ + String content = json.get(CONTENT).toString(); ObjectMapper mapper = new ObjectMapper(); JsonNode policyJSON = null; try { @@ -294,44 +304,44 @@ public class PolicyValidationRequestWrapper { } policyData.setPolicyJSON(policyJSON); } - if (json.containsKey("service")){ - String serviceType = json.get("service").toString().replace("\"", ""); + if (json.containsKey(SERVICE)){ + String serviceType = json.get(SERVICE).toString().replace("\"", ""); policyData.setServiceType(serviceType); } if (json.containsKey("uuid")){ String uuid = json.get("uuid").toString().replace("\"", ""); policyData.setUuid(uuid); } - if (json.containsKey("location")){ - String msLocation = json.get("location").toString().replace("\"", ""); + if (json.containsKey(LOCATION)){ + String msLocation = json.get(LOCATION).toString().replace("\"", ""); policyData.setLocation(msLocation); } if (json.containsKey(CONFIG_NAME)){ String configName = json.get(CONFIG_NAME).toString().replace("\"", ""); policyData.setConfigName(configName); } - if(json.containsKey("priority")){ - String priority = json.get("priority").toString().replace("\"", ""); + if(json.containsKey(PRIORITY)){ + String priority = json.get(PRIORITY).toString().replace("\"", ""); policyData.setPriority(priority); } - if(json.containsKey("version")){ - String version = json.get("version").toString().replace("\"", ""); + if(json.containsKey(VERSION)){ + String version = json.get(VERSION).toString().replace("\"", ""); policyData.setVersion(version); } - if(json.containsKey("policyScope")){ - String policyScope = json.get("policyScope").toString().replace("\"", ""); + if(json.containsKey(POLICYSCOPE)){ + String policyScope = json.get(POLICYSCOPE).toString().replace("\"", ""); policyData.setPolicyScope(policyScope); } - if(json.containsKey("riskType")){ - String riskType = json.get("riskType").toString().replace("\"", ""); + if(json.containsKey(RISKTYPE)){ + String riskType = json.get(RISKTYPE).toString().replace("\"", ""); policyData.setRiskType(riskType); } - if(json.containsKey("riskLevel")){ - String riskLevel = json.get("riskLevel").toString().replace("\"", ""); + if(json.containsKey(RISKLEVEL)){ + String riskLevel = json.get(RISKLEVEL).toString().replace("\"", ""); policyData.setRiskLevel(riskLevel); } - if(json.containsKey("guard")){ - String guard = json.get("guard").toString().replace("\"", ""); + if(json.containsKey(GUARD)){ + String guard = json.get(GUARD).toString().replace("\"", ""); policyData.setGuard(guard); } } else { @@ -340,6 +350,67 @@ public class PolicyValidationRequestWrapper { return null; } + }else if("Optimization".equals(parameters.getPolicyConfigType().toString())){ + + policyData.setConfigPolicyType("Optimization"); + + // get values and attributes from the JsonObject + if(json != null){ + if (json.containsKey(CONTENT)){ + String content = json.get(CONTENT).toString(); + ObjectMapper mapper = new ObjectMapper(); + JsonNode policyJSON = null; + try { + policyJSON = mapper.readTree(content); + } catch (IOException e) { + String message = XACMLErrorConstants.ERROR_DATA_ISSUE+ INVALIDJSON + parameters.getConfigBody(); + LOGGER.error(message, e); + return null; + } + policyData.setPolicyJSON(policyJSON); + } + if (json.containsKey(SERVICE)){ + String serviceType = json.get(SERVICE).toString().replace("\"", ""); + policyData.setServiceType(serviceType); + } + if (json.containsKey("uuid")){ + String uuid = json.get("uuid").toString().replace("\"", ""); + policyData.setUuid(uuid); + } + if (json.containsKey(LOCATION)){ + String msLocation = json.get(LOCATION).toString().replace("\"", ""); + policyData.setLocation(msLocation); + } + if (json.containsKey(CONFIG_NAME)){ + String configName = json.get(CONFIG_NAME).toString().replace("\"", ""); + policyData.setConfigName(configName); + } + if(json.containsKey(PRIORITY)){ + String priority = json.get(PRIORITY).toString().replace("\"", ""); + policyData.setPriority(priority); + } + if(json.containsKey(VERSION)){ + String version = json.get(VERSION).toString().replace("\"", ""); + policyData.setVersion(version); + } + if(json.containsKey(POLICYSCOPE)){ + String policyScope = json.get(POLICYSCOPE).toString().replace("\"", ""); + policyData.setPolicyScope(policyScope); + } + if(json.containsKey(RISKTYPE)){ + String riskType = json.get(RISKTYPE).toString().replace("\"", ""); + policyData.setRiskType(riskType); + } + if(json.containsKey(RISKLEVEL)){ + String riskLevel = json.get(RISKLEVEL).toString().replace("\"", ""); + policyData.setRiskLevel(riskLevel); + } + if(json.containsKey(GUARD)){ + String guard = json.get(GUARD).toString().replace("\"", ""); + policyData.setGuard(guard); + } + } + } else if("Fault".equals(parameters.getPolicyConfigType().toString())){ policyData.setConfigPolicyType("ClosedLoop_Fault"); |