From 5450bdbfb94fb5217617da6c41971fd26f7e81b5 Mon Sep 17 00:00:00 2001 From: "waqas.ikram" Date: Tue, 20 Feb 2018 15:44:50 +0000 Subject: JUnit test for policy/engine PolicyEngineAPI Change-Id: I31991245bb5cc877722f7ce14530222a102d6472 Issue-ID: POLICY-606 Signed-off-by: waqas.ikram --- .../org/onap/policy/api/DeletePolicyCondition.java | 57 ++-- .../java/org/onap/policy/api/DictionaryType.java | 144 ++++----- .../main/java/org/onap/policy/api/PolicyClass.java | 68 ++-- .../org/onap/policy/api/PolicyConfigStatus.java | 97 +++--- .../java/org/onap/policy/api/PolicyDecision.java | 69 +++-- .../org/onap/policy/api/PolicyResponseStatus.java | 108 ++++--- .../main/java/org/onap/policy/api/PolicyType.java | 79 ++--- .../java/org/onap/policy/api/RuleProvider.java | 86 +++--- .../onap/policy/models/APIDictionaryResponse.java | 22 +- .../main/java/org/onap/policy/std/MatchStore.java | 342 ++++++++------------- .../src/main/java/org/onap/policy/std/Matches.java | 79 +++-- .../org/onap/policy/std/NotificationUnMarshal.java | 80 ++--- 12 files changed, 612 insertions(+), 619 deletions(-) (limited to 'PolicyEngineAPI/src/main/java') diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/api/DeletePolicyCondition.java b/PolicyEngineAPI/src/main/java/org/onap/policy/api/DeletePolicyCondition.java index 117ac716e..34b481719 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/api/DeletePolicyCondition.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/api/DeletePolicyCondition.java @@ -29,38 +29,41 @@ import com.fasterxml.jackson.annotation.JsonCreator; * @version 0.1 */ public enum DeletePolicyCondition { - - /** - * Indicates a condition to only delete the current version of the policy. - */ - ONE("Current Version"), - - /** - * Indicates a condition to delete all versions of the policy. - */ - ALL("All Versions"); - private String name; - private DeletePolicyCondition(String name){ - this.name = name; - } + /** + * Indicates a condition to only delete the current version of the policy. + */ + ONE("Current Version"), - /** - * Returns the String format of delete condition for this Policy - * @return the String of the delete condition for this Policy - */ - @Override - public String toString(){ - return this.name; - } + /** + * Indicates a condition to delete all versions of the policy. + */ + ALL("All Versions"); - @JsonCreator - public static DeletePolicyCondition create (String value) { - for(DeletePolicyCondition type: values()){ - if(type.toString().equals(value) || type.equals(DeletePolicyCondition.valueOf(value))){ + private final String name; + + private DeletePolicyCondition(final String name) { + this.name = name; + } + + /** + * Returns the String format of delete condition for this + * Policy + * + * @return the String of the delete condition for this Policy + */ + @Override + public String toString() { + return this.name; + } + + @JsonCreator + public static DeletePolicyCondition create(final String value) { + for (final DeletePolicyCondition type : values()) { + if (type.toString().equalsIgnoreCase(value) || type.name().equalsIgnoreCase(value)) { return type; } } - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Invalid value: " + value); } } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/api/DictionaryType.java b/PolicyEngineAPI/src/main/java/org/onap/policy/api/DictionaryType.java index 499295d65..56b73221f 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/api/DictionaryType.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/api/DictionaryType.java @@ -22,80 +22,82 @@ package org.onap.policy.api; import com.fasterxml.jackson.annotation.JsonCreator; -public enum DictionaryType{ - /** - * Indicates Common Dictionaries. - */ - Common("Common"), - /** - * Indicates Action Policy Dictionaries - */ - Action("Action"), - /** - * Indicates ClosedLoop Policy Dictionaries. - */ - ClosedLoop("ClosedLoop"), - /** - * Indicates Firewall Config Policy Dictionaries. - */ - Firewall("Firewall"), - /** - * Indicates Decision Policy Dictionaries. - */ - Decision("Decision"), - /** - * Indicates BRMS Policy Dictionaries. - */ - BRMS("BRMS"), - /** - * Indicates DCAE Micro Service Policy Dictionaries. - */ - MicroService("MicroService"), - /** - * Indicates Descriptive Scope Dictionaries - */ - DescriptiveScope("DescriptiveScope"), - /** - * Indicates Policy Scope Dictionaries - */ - PolicyScope("PolicyScope"), - /** - * Indicates Enforcer Dictionaries - */ - Enforcer("Enforcer"), - /** - * Indicates SafePolicy Dictionaries - */ - SafePolicy("SafePolicy"), - /** - * Enum support entry to extend dictionary - */ - Extended("Extended"), - ; - - private String name; - - private DictionaryType(String typeName){ - this.name = typeName; - } - - /** - * Returns the String format of Type for this PolicyClass - * @return the String of the Type for this PolicyClass - */ - @Override - public String toString() { - return this.name; - } - - @JsonCreator - public static DictionaryType create (String value) { - for(DictionaryType type: values()){ - if(type.toString().equals(value) || type.equals(DictionaryType.valueOf(value))){ +public enum DictionaryType { + /** + * Indicates Common Dictionaries. + */ + Common("Common"), + /** + * Indicates Action Policy Dictionaries + */ + Action("Action"), + /** + * Indicates ClosedLoop Policy Dictionaries. + */ + ClosedLoop("ClosedLoop"), + /** + * Indicates Firewall Config Policy Dictionaries. + */ + Firewall("Firewall"), + /** + * Indicates Decision Policy Dictionaries. + */ + Decision("Decision"), + /** + * Indicates BRMS Policy Dictionaries. + */ + BRMS("BRMS"), + /** + * Indicates DCAE Micro Service Policy Dictionaries. + */ + MicroService("MicroService"), + /** + * Indicates Descriptive Scope Dictionaries + */ + DescriptiveScope("DescriptiveScope"), + /** + * Indicates Policy Scope Dictionaries + */ + PolicyScope("PolicyScope"), + /** + * Indicates Enforcer Dictionaries + */ + Enforcer("Enforcer"), + /** + * Indicates SafePolicy Dictionaries + */ + SafePolicy("SafePolicy"), + /** + * Enum support entry to extend dictionary + */ + Extended("Extended"); + + private final String name; + + private DictionaryType(final String typeName) { + this.name = typeName; + } + + /** + * Returns the String format of Type for this + * PolicyClass + * + * @return the String of the Type for this + * PolicyClass + */ + @Override + public String toString() { + return this.name; + } + + @JsonCreator + public static DictionaryType create(final String value) { + for (final DictionaryType type : values()) { + if (type.toString().equalsIgnoreCase(value) || type.name().equalsIgnoreCase(value)) { return type; } } - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Invalid value: " + value); } } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyClass.java b/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyClass.java index 6a648e8e6..fb389bb03 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyClass.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyClass.java @@ -29,40 +29,44 @@ import com.fasterxml.jackson.annotation.JsonCreator; * @version 0.1 */ public enum PolicyClass { - /** - * Indicates Config based Policy. - */ - Config("Config"), - /** - * Indicates Action based Policy. - */ - Action("Action"), - /** - * Indicates Decision based Policy. - */ - Decision("Decision") - ; - private String name; - - private PolicyClass(String typeName){ - this.name = typeName; - } - - /** - * Returns the String format of Type for this PolicyClass - * @return the String of the Type for this PolicyClass - */ - @Override - public String toString() { - return this.name; - } - @JsonCreator - public static PolicyClass create (String value) { - for(PolicyClass type: values()){ - if(type.toString().equals(value) || type.equals(PolicyClass.valueOf(value))){ + /** + * Indicates Config based Policy. + */ + Config("Config"), + /** + * Indicates Action based Policy. + */ + Action("Action"), + /** + * Indicates Decision based Policy. + */ + Decision("Decision"); + + private final String name; + + private PolicyClass(final String typeName) { + this.name = typeName; + } + + /** + * Returns the String format of Type for this + * PolicyClass + * + * @return the String of the Type for this + * PolicyClass + */ + @Override + public String toString() { + return this.name; + } + + @JsonCreator + public static PolicyClass create(final String value) { + for (final PolicyClass type : values()) { + if (type.toString().equalsIgnoreCase(value) || type.name().equalsIgnoreCase(value)) { return type; } } - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Invalid value: " + value); } } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyConfigStatus.java b/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyConfigStatus.java index cee156786..f1bbea167 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyConfigStatus.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyConfigStatus.java @@ -23,58 +23,63 @@ package org.onap.policy.api; import com.fasterxml.jackson.annotation.JsonCreator; /** - * Enumeration of PolicyConfigStatus that can be returned as a part of + * Enumeration of PolicyConfigStatus that can be returned as a part of * {@link org.onap.policy.api.PolicyConfig}. * * @version 0.1 */ -public enum PolicyConfigStatus { - /** - * Indicates that the Configuration has been successfully retrieved. - */ - CONFIG_RETRIEVED("retrieved"), - /** - * Indicates that there is no Configuration Retrieved from PolicyConfig. - */ - CONFIG_NOT_FOUND("not_found"), - ; - - private String name; - private PolicyConfigStatus(String name){ - this.name = name; - } - - /** - * Get the PolicyConfigStatus based on String representation of PolicyConfig - * - * @param configStatus the String Configuration Status - * @return the PolicyConfigResponse with the name matching CONFIG_RETRIEVED or CONFIG_NOT_FOUND - * if no match is found - */ - public static PolicyConfigStatus getStatus(String configStatus) { - if("retrieved".equalsIgnoreCase(configStatus)) { - return CONFIG_RETRIEVED; - }else { - return CONFIG_NOT_FOUND; - } - } - - /** - * Returns the String name for this PolicyConfigStatus - * - * @return the String name for this PolicyConfigStatus - */ - @Override - public String toString(){ - return this.name; - } - @JsonCreator - public static PolicyConfigStatus create (String value) { - for(PolicyConfigStatus type: values()){ - if(type.toString().equals(value) || type.equals(PolicyConfigStatus.valueOf(value))){ +public enum PolicyConfigStatus { + /** + * Indicates that the Configuration has been successfully retrieved. + */ + CONFIG_RETRIEVED("retrieved"), + /** + * Indicates that there is no Configuration Retrieved from PolicyConfig. + */ + CONFIG_NOT_FOUND("not_found"); + + private final String name; + + private PolicyConfigStatus(final String name) { + this.name = name; + } + + /** + * Get the PolicyConfigStatus based on String + * representation of PolicyConfig + * + * @param configStatus + * the String Configuration Status + * @return the PolicyConfigResponse with the name matching + * CONFIG_RETRIEVED or CONFIG_NOT_FOUND if + * no match is found + */ + public static PolicyConfigStatus getStatus(final String configStatus) { + if (CONFIG_RETRIEVED.name.equalsIgnoreCase(configStatus)) { + return CONFIG_RETRIEVED; + } + return CONFIG_NOT_FOUND; + } + + /** + * Returns the String name for this + * PolicyConfigStatus + * + * @return the String name for this + * PolicyConfigStatus + */ + @Override + public String toString() { + return this.name; + } + + @JsonCreator + public static PolicyConfigStatus create(final String value) { + for (final PolicyConfigStatus type : values()) { + if (type.toString().equalsIgnoreCase(value) || type.name().equalsIgnoreCase(value)) { return type; } } - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Invalid value: " + value); } } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyDecision.java b/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyDecision.java index 6bae84cc7..782d44459 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyDecision.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyDecision.java @@ -23,47 +23,48 @@ package org.onap.policy.api; import com.fasterxml.jackson.annotation.JsonCreator; /** - * Enumeration of PolicyDecision that can be returned as a part of + * Enumeration of PolicyDecision that can be returned as a part of * {@link org.onap.policy.api.DecisionResponse} getDecision(). * * @version 0.1 */ public enum PolicyDecision { - /** - * Indicates that the Decision is to Permit. - */ - PERMIT("permit"), - /** - * Indicates that the Decision is to Deny. - */ - DENY("deny"), - /** - * Indicates that the Decision process has some issues. - */ - ERROR("error") - ; - - private String name; - private PolicyDecision(String name){ - this.name = name; - } - - /** - * Returns the String name for this PolicyDecision - * - * @return the String name for this PolicyDecision - */ - @Override - public String toString(){ - return this.name; - } - @JsonCreator - public static PolicyDecision create (String value) { - for(PolicyDecision type: values()){ - if(type.toString().equals(value) || type.equals(PolicyDecision.valueOf(value))){ + /** + * Indicates that the Decision is to Permit. + */ + PERMIT("permit"), + /** + * Indicates that the Decision is to Deny. + */ + DENY("deny"), + /** + * Indicates that the Decision process has some issues. + */ + ERROR("error"); + + private final String name; + + private PolicyDecision(final String name) { + this.name = name; + } + + /** + * Returns the String name for this PolicyDecision + * + * @return the String name for this PolicyDecision + */ + @Override + public String toString() { + return this.name; + } + + @JsonCreator + public static PolicyDecision create(final String value) { + for (final PolicyDecision type : values()) { + if (type.toString().equalsIgnoreCase(value) || type.name().equalsIgnoreCase(value)) { return type; } } - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Invalid value: " + value); } } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyResponseStatus.java b/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyResponseStatus.java index 63cb9185c..0db373ced 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyResponseStatus.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyResponseStatus.java @@ -28,59 +28,65 @@ import com.fasterxml.jackson.annotation.JsonCreator; * * @version 0.2 */ -public enum PolicyResponseStatus { - /** - * Indicates there is no action required. - */ - NO_ACTION_REQUIRED("no_action"), - /** - * Indicates that an action has been advised. - */ - ACTION_ADVISED("action_advised"), - /** - * Indicates that an action has been taken. - */ - ACTION_TAKEN("action_taken") - ; - - private String name; - private PolicyResponseStatus(String name){ - this.name = name; - } - - /** - * Get the PolicyResponseStatus based on String representation of PolicyResponse - * - * @param responseStatus the String Response Status - * @return the PolicyResponseStatus with the name matching ACTION_ADVISED or ACTION_TAKEN or NO_ACTION_REQUIRED - */ - public static PolicyResponseStatus getStatus(String responseStatus) { - if("action_advised".equalsIgnoreCase(responseStatus)) { - return ACTION_ADVISED; - }else if("action_taken".equalsIgnoreCase(responseStatus)) { - return ACTION_TAKEN; - }else { - return NO_ACTION_REQUIRED; - } - } - - /** - * Returns the String name for this PolicyResponseStatus - * - * @return the String name for this PolicyResponseStatus - */ - @Override - public String toString(){ - return this.name; - } - - @JsonCreator - public static PolicyResponseStatus create (String value) { - for(PolicyResponseStatus type: values()){ - if(type.toString().equals(value) || type.equals(PolicyResponseStatus.valueOf(value))){ +public enum PolicyResponseStatus { + /** + * Indicates there is no action required. + */ + NO_ACTION_REQUIRED("no_action"), + /** + * Indicates that an action has been advised. + */ + ACTION_ADVISED("action_advised"), + /** + * Indicates that an action has been taken. + */ + ACTION_TAKEN("action_taken"); + + private final String name; + + private PolicyResponseStatus(final String name) { + this.name = name; + } + + /** + * Get the PolicyResponseStatus based on String + * representation of PolicyResponse + * + * @param responseStatus + * the String Response Status + * @return the PolicyResponseStatus with the name matching + * ACTION_ADVISED or ACTION_TAKEN or + * NO_ACTION_REQUIRED + */ + public static PolicyResponseStatus getStatus(final String responseStatus) { + if (ACTION_ADVISED.name.equalsIgnoreCase(responseStatus)) { + return ACTION_ADVISED; + } else if (ACTION_TAKEN.name.equalsIgnoreCase(responseStatus)) { + return ACTION_TAKEN; + } else { + return NO_ACTION_REQUIRED; + } + } + + /** + * Returns the String name for this + * PolicyResponseStatus + * + * @return the String name for this + * PolicyResponseStatus + */ + @Override + public String toString() { + return this.name; + } + + @JsonCreator + public static PolicyResponseStatus create(final String value) { + for (final PolicyResponseStatus type : values()) { + if (type.toString().equalsIgnoreCase(value) || type.name().equalsIgnoreCase(value)) { return type; } } - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Invalid value: " + value); } } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyType.java b/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyType.java index a29a6c440..0ff5480bf 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyType.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/api/PolicyType.java @@ -29,45 +29,48 @@ import com.fasterxml.jackson.annotation.JsonCreator; * @version 0.2 */ public enum PolicyType { - /** - * Indicates the response is Properties type - */ - PROPERTIES("Properties"), - /** - * Indicates the response is JSON type - */ - JSON("json"), - /** - * Indicates the response is XML type - */ - XML("xml"), - /** - * Indicates the response is Other type - */ - OTHER("other") - ; - - private String name; - - private PolicyType(String typeName) { - this.name = typeName; - } - - /** - * Returns the String format of Type for this PolicyType - * @return the String of the Type for this PolicyType - */ - @Override - public String toString() { - return this.name; - } - @JsonCreator - public static PolicyType create (String value) { - for(PolicyType type: values()){ - if(type.toString().equalsIgnoreCase(value)){ + /** + * Indicates the response is Properties type + */ + PROPERTIES("Properties"), + /** + * Indicates the response is JSON type + */ + JSON("json"), + /** + * Indicates the response is XML type + */ + XML("xml"), + /** + * Indicates the response is Other type + */ + OTHER("other"); + + private final String name; + + private PolicyType(final String typeName) { + this.name = typeName; + } + + /** + * Returns the String format of Type for this + * PolicyType + * + * @return the String of the Type for this + * PolicyType + */ + @Override + public String toString() { + return this.name; + } + + @JsonCreator + public static PolicyType create(final String value) { + for (final PolicyType type : values()) { + if (type.toString().equalsIgnoreCase(value) || type.name().equalsIgnoreCase(value)) { return type; } } - throw new IllegalArgumentException(); - } + throw new IllegalArgumentException("Invalid value: " + value); + } } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/api/RuleProvider.java b/PolicyEngineAPI/src/main/java/org/onap/policy/api/RuleProvider.java index 2e45549fc..913f32517 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/api/RuleProvider.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/api/RuleProvider.java @@ -29,50 +29,52 @@ import com.fasterxml.jackson.annotation.JsonCreator; * @version 0.1 */ public enum RuleProvider { - /** - * Indicates User will be defining the Rule information. - */ - CUSTOM("Custom"), - /** - * Indicates AAF will be providing the Rule information. - */ - AAF("AAF"), - /** - * Indicates Guard YAML will be providing the Rule information. - */ - GUARD_YAML("GUARD_YAML"), - /** - * Indicates Guard BLACKLIST YAML - */ - GUARD_BL_YAML("GUARD_BL_YAML"), - /** - * Indicates Guard BLACKLIST YAML - */ - RAINY_DAY("Rainy_Day") - ; - - private String name; - - private RuleProvider(String typeName){ - this.name = typeName; - } - - /** - * Returns the String format of Type for this AttributeType - * @return the String of the Type for this AttributeType - */ - @Override - public String toString() { - return this.name; - } - - @JsonCreator - public static RuleProvider create (String value) { - for(RuleProvider type: values()){ - if(type.toString().equals(value) || type.equals(RuleProvider.valueOf(value))){ + /** + * Indicates User will be defining the Rule information. + */ + CUSTOM("Custom"), + /** + * Indicates AAF will be providing the Rule information. + */ + AAF("AAF"), + /** + * Indicates Guard YAML will be providing the Rule information. + */ + GUARD_YAML("GUARD_YAML"), + /** + * Indicates Guard BLACKLIST YAML + */ + GUARD_BL_YAML("GUARD_BL_YAML"), + /** + * Indicates Guard BLACKLIST YAML + */ + RAINY_DAY("Rainy_Day"); + + private final String name; + + private RuleProvider(final String typeName) { + this.name = typeName; + } + + /** + * Returns the String format of Type for this + * AttributeType + * + * @return the String of the Type for this + * AttributeType + */ + @Override + public String toString() { + return this.name; + } + + @JsonCreator + public static RuleProvider create(final String value) { + for (final RuleProvider type : values()) { + if (type.toString().equalsIgnoreCase(value) || type.name().equalsIgnoreCase(value)) { return type; } } - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Invalid value: " + value); } } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/models/APIDictionaryResponse.java b/PolicyEngineAPI/src/main/java/org/onap/policy/models/APIDictionaryResponse.java index 050f36bf4..79c6196e6 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/models/APIDictionaryResponse.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/models/APIDictionaryResponse.java @@ -21,33 +21,43 @@ package org.onap.policy.models; public class APIDictionaryResponse { private Object dictionaryData; + private Object dictionaryJson; + private int responseCode; + private String responseMessage; + public Object getDictionaryData() { return dictionaryData; } - public void setDictionaryData(Object dictionaryData) { + + public void setDictionaryData(final Object dictionaryData) { this.dictionaryData = dictionaryData; } + public Object getDictionaryJson() { return dictionaryJson; } - public void setDictionaryJson(Object dictionaryJson) { + + public void setDictionaryJson(final Object dictionaryJson) { this.dictionaryJson = dictionaryJson; } + public int getResponseCode() { return responseCode; } - public void setResponseCode(int responseCode) { + + public void setResponseCode(final int responseCode) { this.responseCode = responseCode; } + public String getResponseMessage() { return responseMessage; } - public void setResponseMessage(String responseMessage) { + + public void setResponseMessage(final String responseMessage) { this.responseMessage = responseMessage; } - - + } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/std/MatchStore.java b/PolicyEngineAPI/src/main/java/org/onap/policy/std/MatchStore.java index e5204d894..fa8029908 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/std/MatchStore.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/std/MatchStore.java @@ -21,7 +21,9 @@ package org.onap.policy.std; import java.util.Collection; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import org.onap.policy.api.LoadedPolicy; @@ -29,220 +31,132 @@ import org.onap.policy.api.NotificationType; import org.onap.policy.api.PDPNotification; import org.onap.policy.api.RemovedPolicy; import org.onap.policy.common.logging.flexlogger.FlexLogger; -import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.common.logging.flexlogger.Logger; public class MatchStore { - private static HashSet matchStore = new HashSet<>(); - private static Logger logger = FlexLogger.getLogger(MatchStore.class.getName()); - - private MatchStore() { - // Empty Constructor - } - - public static Set getMatchStore() { - return matchStore; - } - - public static void storeMatch(Matches newMatch){ - // Initialization.. - if(newMatch!=null){ - if(matchStore.isEmpty()){ - matchStore.add(newMatch); - }else{ - // Check if it is a new Match - Boolean match = false; - for(Matches oldMatch: matchStore){ - // Compare ONAPName - if(oldMatch.getOnapName().equals(newMatch.getOnapName())){ - // Compare ConfigName if it exists. - if(newMatch.getConfigName()!=null && oldMatch.getConfigName()!=null){ - if(oldMatch.getConfigName().equals(newMatch.getConfigName())){ - // Compare the Config Attributes if they exist. - if(newMatch.getConfigAttributes()!= null && oldMatch.getConfigAttributes()!=null) { - //Simple thing would be comparing their size. - if(newMatch.getConfigAttributes().size()==oldMatch.getConfigAttributes().size()){ - // Now need to compare each of them.. - int count= 0; - for(String oldkey: oldMatch.getConfigAttributes().keySet()){ - boolean check = false; - for(String newKey: newMatch.getConfigAttributes().keySet()){ - if(oldkey.equals(newKey)){ - if(oldMatch.getConfigAttributes().get(oldkey).equals(newMatch.getConfigAttributes().get(newKey))){ - check = true; - } - } - } - if(check){ - count++; - }else{ - break; - } - } - if(count==oldMatch.getConfigAttributes().size()){ - match = true; - break; - } - } - }else if(newMatch.getConfigAttributes()== null && oldMatch.getConfigAttributes()==null){ - match = true; - break; - } - } - }else if(newMatch.getConfigName()==null && oldMatch.getConfigName()==null){ - match = true; - break; - } - } - - } - // IF not a match then add it to the MatchStore - if(! match){ - matchStore.add(newMatch); - } - } - } - } - - //Logic changes for Requested Policies notifications.. - public static PDPNotification checkMatch(PDPNotification oldNotification) { - boolean removed = false; - boolean updated = false; - if(oldNotification==null){ - return null; - } - StdPDPNotification newNotification = new StdPDPNotification(); - if(matchStore.isEmpty()) { - logger.debug("No Success Config Calls made yet.. "); - return null; - } - if(oldNotification.getRemovedPolicies()!=null && !oldNotification.getRemovedPolicies().isEmpty()){ - // send all removed policies to client. - Collection removedPolicies = new HashSet<>(); - StdRemovedPolicy newRemovedPolicy; - for(RemovedPolicy removedPolicy: oldNotification.getRemovedPolicies()){ - newRemovedPolicy = new StdRemovedPolicy(); - newRemovedPolicy.setPolicyName(removedPolicy.getPolicyName()); - newRemovedPolicy.setVersionNo(removedPolicy.getVersionNo()); - removedPolicies.add(newRemovedPolicy); - } - newNotification.setRemovedPolicies(removedPolicies); - removed = true; - } - if(oldNotification.getLoadedPolicies()!=null && !oldNotification.getLoadedPolicies().isEmpty()){ - Collection updatedPolicies = new HashSet<>(); - StdLoadedPolicy newUpdatedPolicy; - for(LoadedPolicy updatedPolicy: oldNotification.getLoadedPolicies()){ - // if it is config policies check their matches.. - if(updatedPolicy.getMatches()!=null && !updatedPolicy.getMatches().isEmpty()){ - boolean matched; - for(Matches match : matchStore){ - matched = false; - // Again Better way would be comparing sizes first. - // Matches are different need to check if has configAttributes - if(match.getConfigAttributes()!=null && !match.getConfigAttributes().isEmpty()){ - // adding onap and config to config-attributes. - int compValues = match.getConfigAttributes().size() + 2; - if(updatedPolicy.getMatches().size()== compValues){ - // Comparing both the values.. - boolean matchAttributes = false; - for(String newKey: updatedPolicy.getMatches().keySet()){ - if("ONAPName".equals(newKey)){ - if(updatedPolicy.getMatches().get(newKey).equals(match.getOnapName())){ - matchAttributes = true; - }else { - matchAttributes = false; - break; - } - }else if("ConfigName".equals(newKey)) { - if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigName())){ - matchAttributes = true; - }else{ - matchAttributes = false; - break; - } - }else { - if(match.getConfigAttributes().containsKey(newKey)){ - if(updatedPolicy.getMatches().get(newKey).equals(match.getConfigAttributes().get(newKey))){ - matchAttributes = true; - }else{ - matchAttributes = false; - break; - } - }else{ - matchAttributes = false; - break; - } - } - } - if(matchAttributes){ - // Match.. - matched = true; - }else{ - break; - } - }else { - break; - } - }else if(match.getConfigName()!=null){ - // If there are no config Attributes then check if it has Config Name - if(updatedPolicy.getMatches().size()== 2){ - if(updatedPolicy.getMatches().get("ONAPName").equals(match.getOnapName())){ - if(updatedPolicy.getMatches().get("ConfigName").equals(match.getConfigName())){ - // Match.. - matched = true; - }else{ - break; - } - }else { - break; - } - }else{ - break; - } - }else { - // If non exist then assuming the ONAP Name to be there. - if(updatedPolicy.getMatches().size()== 1){ - if(updatedPolicy.getMatches().get("ONAPName").equals(match.getOnapName())){ - // Match.. - matched = true; - }else { - break; - } - }else{ - break; - } - } - // Add logic to add the policy. - if(matched){ - newUpdatedPolicy = new StdLoadedPolicy(); - newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName()); - newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo()); - newUpdatedPolicy.setMatches(updatedPolicy.getMatches()); - updatedPolicies.add(newUpdatedPolicy); - updated = true; - } - } - - }else { - //send all non config notifications to client. - newUpdatedPolicy = new StdLoadedPolicy(); - newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName()); - newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo()); - updatedPolicies.add(newUpdatedPolicy); - updated = true; - } - } - newNotification.setLoadedPolicies(updatedPolicies); - } - // Need to set the type of Update.. - if(removed && updated) { - newNotification.setNotificationType(NotificationType.BOTH); - }else if(removed){ - newNotification.setNotificationType(NotificationType.REMOVE); - }else if(updated){ - newNotification.setNotificationType(NotificationType.UPDATE); - } - return newNotification; - } - + private static final String CONFIG_NAME = "ConfigName"; + + private static final String ONAP_NAME = "ONAPName"; + + private static final Set MATCH_STORE = new HashSet<>(); + + private static final Logger LOGGER = FlexLogger.getLogger(MatchStore.class.getName()); + + private MatchStore() { + // Empty Constructor + } + + public static Set getMatchStore() { + return MATCH_STORE; + } + + public static void storeMatch(final Matches newMatch) { + if (newMatch != null && !MATCH_STORE.contains(newMatch)) { + MATCH_STORE.add(newMatch); + } + } + + // Logic changes for Requested Policies notifications.. + public static PDPNotification checkMatch(final PDPNotification oldNotification) { + if (oldNotification == null) { + return null; + } + if (MATCH_STORE.isEmpty()) { + LOGGER.debug("No Success Config Calls made yet.. "); + return null; + } + return getPDPNotification(oldNotification); + } + + private static PDPNotification getPDPNotification(final PDPNotification oldNotification) { + boolean removed = false; + boolean updated = false; + final StdPDPNotification newNotification = new StdPDPNotification(); + if (isValid(oldNotification.getRemovedPolicies())) { + // send all removed policies to client. + newNotification.setRemovedPolicies(getStdRemovedPolicies(oldNotification.getRemovedPolicies())); + removed = true; + } + if (isValid(oldNotification.getLoadedPolicies())) { + final Collection updatedPolicies = new HashSet<>(); + for (final LoadedPolicy updatedPolicy : oldNotification.getLoadedPolicies()) { + updated = updateStdLoadedPolicy(updated, updatedPolicies, updatedPolicy); + } + newNotification.setLoadedPolicies(updatedPolicies); + } + // Need to set the type of Update.. + if (removed && updated) { + newNotification.setNotificationType(NotificationType.BOTH); + } else if (removed) { + newNotification.setNotificationType(NotificationType.REMOVE); + } else if (updated) { + newNotification.setNotificationType(NotificationType.UPDATE); + } + return newNotification; + } + + private static boolean updateStdLoadedPolicy(boolean updated, final Collection updatedPolicies, + final LoadedPolicy updatedPolicy) { + // if it is config policies check their matches + if (isValid(updatedPolicy.getMatches())) { + final Map matchesMap = updatedPolicy.getMatches(); + final Matches policyMatches = getMatches(matchesMap); + for (final Matches match : MATCH_STORE) { + if (match.equals(policyMatches)) { + final StdLoadedPolicy newUpdatedPolicy = new StdLoadedPolicy(); + newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName()); + newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo()); + newUpdatedPolicy.setMatches(updatedPolicy.getMatches()); + updatedPolicies.add(newUpdatedPolicy); + updated = true; + } else { + break; + } + } + + } else { + // send all non config notifications to client. + final StdLoadedPolicy newUpdatedPolicy = new StdLoadedPolicy(); + newUpdatedPolicy.setPolicyName(updatedPolicy.getPolicyName()); + newUpdatedPolicy.setVersionNo(updatedPolicy.getVersionNo()); + updatedPolicies.add(newUpdatedPolicy); + updated = true; + } + return updated; + } + + private static Matches getMatches(final Map attributes) { + final Matches matches = new Matches(); + matches.setOnapName(attributes.get(ONAP_NAME)); + matches.setConfigName(attributes.get(CONFIG_NAME)); + + final Map configAttributes = new HashMap<>(attributes); + // remove onap and config to config-attributes. + configAttributes.remove(ONAP_NAME); + configAttributes.remove(CONFIG_NAME); + + matches.setConfigAttributes(configAttributes); + + return matches; + } + + private static boolean isValid(final Map map) { + return map != null && !map.isEmpty(); + } + + private static Collection getStdRemovedPolicies(final Collection policies) { + final Set removedPolicies = new HashSet<>(); + for (final RemovedPolicy removedPolicy : policies) { + final StdRemovedPolicy newRemovedPolicy = new StdRemovedPolicy(); + newRemovedPolicy.setPolicyName(removedPolicy.getPolicyName()); + newRemovedPolicy.setVersionNo(removedPolicy.getVersionNo()); + removedPolicies.add(newRemovedPolicy); + } + return removedPolicies; + } + + private static boolean isValid(final Collection collection) { + return collection != null && !collection.isEmpty(); + } + } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/std/Matches.java b/PolicyEngineAPI/src/main/java/org/onap/policy/std/Matches.java index eb382a63d..b3486e45b 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/std/Matches.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/std/Matches.java @@ -23,26 +23,61 @@ package org.onap.policy.std; import java.util.Map; public class Matches { - private String onapName = null; - private String configName = null; - private Map configAttributes = null; - public String getOnapName() { - return onapName; - } - public void setOnapName(String onapName) { - this.onapName = onapName; - } - public String getConfigName() { - return configName; - } - public void setConfigName(String configName) { - this.configName = configName; - } - public Map getConfigAttributes() { - return configAttributes; - } - public void setConfigAttributes(Map configAttributes) { - this.configAttributes = configAttributes; - } - + private String onapName = null; + + private String configName = null; + + private Map configAttributes = null; + + public String getOnapName() { + return onapName; + } + + public void setOnapName(final String onapName) { + this.onapName = onapName; + } + + public String getConfigName() { + return configName; + } + + public void setConfigName(final String configName) { + this.configName = configName; + } + + public Map getConfigAttributes() { + return configAttributes; + } + + public void setConfigAttributes(final Map configAttributes) { + this.configAttributes = configAttributes; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((configAttributes == null) ? 0 : configAttributes.hashCode()); + result = prime * result + ((configName == null) ? 0 : configName.hashCode()); + result = prime * result + ((onapName == null) ? 0 : onapName.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (obj instanceof Matches) { + final Matches other = (Matches) obj; + return isEqual(onapName, other.onapName) && isEqual(configName, other.configName) + && isEqual(configAttributes, other.configAttributes); + } + return false; + } + + private boolean isEqual(final Object objectA, final Object objectB) { + if (objectA == null) { + return objectB == null; + } + return objectA.equals(objectB); + } + } diff --git a/PolicyEngineAPI/src/main/java/org/onap/policy/std/NotificationUnMarshal.java b/PolicyEngineAPI/src/main/java/org/onap/policy/std/NotificationUnMarshal.java index 6772f5a9b..fb7029800 100644 --- a/PolicyEngineAPI/src/main/java/org/onap/policy/std/NotificationUnMarshal.java +++ b/PolicyEngineAPI/src/main/java/org/onap/policy/std/NotificationUnMarshal.java @@ -31,40 +31,48 @@ import org.onap.policy.api.UpdateType; import com.fasterxml.jackson.databind.ObjectMapper; public class NotificationUnMarshal { - - private NotificationUnMarshal() { - // Empty constructor - } - - public static StdPDPNotification notificationJSON(String json) throws IOException{ - ObjectMapper mapper = new ObjectMapper(); - StdPDPNotification notification = mapper.readValue(json, StdPDPNotification.class); - if(notification == null || notification.getLoadedPolicies() == null){ - return notification; - } - Collection stdLoadedPolicies = new ArrayList<>(); - for(LoadedPolicy loadedPolicy: notification.getLoadedPolicies()){ - StdLoadedPolicy stdLoadedPolicy = (StdLoadedPolicy) loadedPolicy; - if(notification.getRemovedPolicies()!=null){ - Boolean updated = false; - for(RemovedPolicy removedPolicy: notification.getRemovedPolicies()){ - String regex = ".(\\d)*.xml"; - if(removedPolicy.getPolicyName().replaceAll(regex, "").equals(stdLoadedPolicy.getPolicyName().replaceAll(regex, ""))){ - updated = true; - break; - } - } - if(updated){ - stdLoadedPolicy.setUpdateType(UpdateType.UPDATE); - }else{ - stdLoadedPolicy.setUpdateType(UpdateType.NEW); - } - }else{ - stdLoadedPolicy.setUpdateType(UpdateType.NEW); - } - stdLoadedPolicies.add(stdLoadedPolicy); - } - notification.setLoadedPolicies(stdLoadedPolicies); - return notification; - } + + private static final String EMPTY_STRING = ""; + + private static final String REGEX = ".(\\d)*.xml"; + + private NotificationUnMarshal() { + // Empty constructor + } + + public static StdPDPNotification notificationJSON(final String json) throws IOException { + final ObjectMapper mapper = new ObjectMapper(); + final StdPDPNotification notification = mapper.readValue(json, StdPDPNotification.class); + if (notification == null || notification.getLoadedPolicies() == null) { + return notification; + } + final Collection stdLoadedPolicies = new ArrayList<>(); + for (final LoadedPolicy loadedPolicy : notification.getLoadedPolicies()) { + final StdLoadedPolicy stdLoadedPolicy = (StdLoadedPolicy) loadedPolicy; + if (notification.getRemovedPolicies() != null) { + if (isUpdated(notification, stdLoadedPolicy)) { + stdLoadedPolicy.setUpdateType(UpdateType.UPDATE); + } else { + stdLoadedPolicy.setUpdateType(UpdateType.NEW); + } + } else { + stdLoadedPolicy.setUpdateType(UpdateType.NEW); + } + stdLoadedPolicies.add(stdLoadedPolicy); + } + notification.setLoadedPolicies(stdLoadedPolicies); + return notification; + } + + private static Boolean isUpdated(final StdPDPNotification notification, final StdLoadedPolicy stdLoadedPolicy) { + for (final RemovedPolicy removedPolicy : notification.getRemovedPolicies()) { + final String removedPolicyName = removedPolicy.getPolicyName(); + final String loadedPolicyName = stdLoadedPolicy.getPolicyName(); + if (removedPolicyName.replaceAll(REGEX, EMPTY_STRING) + .equals(loadedPolicyName.replaceAll(REGEX, EMPTY_STRING))) { + return true; + } + } + return false; + } } -- cgit 1.2.3-korg