diff --git a/components/policy-mgt/org.wso2.carbon.policy.evalutor/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.evalutor/pom.xml index 790e29527..56c0590f0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.evalutor/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.evalutor/pom.xml @@ -78,6 +78,10 @@ org.wso2.carbon org.wso2.carbon.logging + + org.wso2.carbon + org.wso2.carbon.policy.mgt.common + diff --git a/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/FeatureFilter.java b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/FeatureFilter.java new file mode 100644 index 000000000..0636e1689 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/FeatureFilter.java @@ -0,0 +1,49 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you 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. +*/ + + +package org.wso2.carbon.policy.evaluator; + +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.List; +import java.util.Map; + +public interface FeatureFilter { + + List evaluate(List policyList, List featureRulesList); + + List extractFeatures(List policyList); + + List evaluateFeatures(List featureList, List featureRulesList); + + void getDenyOverridesFeatures(String featureName, List featureList, List effectiveFeatureList); + + void getPermitOverridesFeatures(String featureName, List featureList, List effectiveFeatureList); + + void getFirstApplicableFeatures(String featureName, List featureList, List effectiveFeatureList); + + void getLastApplicableFeatures(String featureName, List featureList, List effectiveFeatureList); + + void getAllApplicableFeatures(String featureName, List featureList, List effectiveFeatureList); + + void getHighestApplicableFeatures(String featureName, List featureList, List effectiveFeatureList); + + void getLowestApplicableFeatures(String featureName, List featureList, List effectiveFeatureList); +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/FeatureFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/FeatureFilterImpl.java new file mode 100644 index 000000000..04faae5f0 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/FeatureFilterImpl.java @@ -0,0 +1,250 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you 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. +*/ + +package org.wso2.carbon.policy.evaluator; + +import org.wso2.carbon.policy.evaluator.utils.Constants; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.ArrayList; +import java.util.List; + +/** + * This class is responsible for evaluating the policy (Configurations sets) and returning + * the effective features set. + */ + +public class FeatureFilterImpl implements FeatureFilter { + + /** + * This method returns the effective feature list when policy list and feature aggregation rules are supplied. + * @param policyList + * @param featureRulesList + * @return + */ + @Override + public List evaluate(List policyList, List featureRulesList) { + return evaluateFeatures(extractFeatures(policyList), featureRulesList); + } + + /** + * This method extract the features from the given policy list in the order they are provided in the list. + * @param policyList + * @return + */ + public List extractFeatures(List policyList) { + List featureList = new ArrayList(); + for (Policy policy : policyList) { + featureList.addAll(policy.getFeaturesList()); + } + return featureList; + } + + /** + * This method is responsible for supplying tasks to other methods to evaluate given features. + * @param featureList + * @param featureRulesList + * @return + */ + public List evaluateFeatures(List featureList, List featureRulesList) { + List effectiveFeatureList = new ArrayList(); + for (FeatureRules rule : featureRulesList) { + String ruleName = rule.getEvaluationCriteria(); + String featureName = rule.getName(); + if (ruleName.equalsIgnoreCase(Constants.DENY_OVERRIDES)) { + getDenyOverridesFeatures(featureName, featureList, effectiveFeatureList); + } + if (ruleName.equalsIgnoreCase(Constants.PERMIT_OVERRIDES)) { + getPermitOverridesFeatures(featureName, featureList, effectiveFeatureList); + } + if (ruleName.equalsIgnoreCase(Constants.FIRST_APPLICABLE)) { + getFirstApplicableFeatures(featureName, featureList, effectiveFeatureList); + } + if (ruleName.equalsIgnoreCase(Constants.LAST_APPLICABLE)) { + getLastApplicableFeatures(featureName, featureList, effectiveFeatureList); + } + if (ruleName.equalsIgnoreCase(Constants.ALL_APPLICABLE)) { + getAllApplicableFeatures(featureName, featureList, effectiveFeatureList); + } + if (ruleName.equalsIgnoreCase(Constants.HIGHEST_APPLICABLE)) { + getHighestApplicableFeatures(featureName, featureList, effectiveFeatureList); + } + if (ruleName.equalsIgnoreCase(Constants.LOWEST_APPLICABLE)) { + getLowestApplicableFeatures(featureName, featureList, effectiveFeatureList); + } + } + return effectiveFeatureList; + } + + /** + * This method picks up denied features, if there is no denied features it will add to the list, the final permitted feature. + * But if given policies do not have features of given type, it will not add anything. + * + * @param featureName + * @param featureList + * @param effectiveFeatureList + */ + public void getDenyOverridesFeatures(String featureName, List featureList, List effectiveFeatureList) { + Feature evaluatedFeature = null; + for (Feature feature : featureList) { + if (feature.getName().equalsIgnoreCase(featureName)) { + if (feature.getRuleValue().equalsIgnoreCase("Deny")) { + evaluatedFeature = feature; + effectiveFeatureList.add(evaluatedFeature); + return; + } else { + evaluatedFeature = feature; + } + } + } + if (evaluatedFeature != null) { + effectiveFeatureList.add(evaluatedFeature); + } + + } + + /** + * This method picks up permitted features, if there is no permitted features it will add to the list, the final denied feature. + * But if given policies do not have features of given type, it will not add anything. + * + * @param featureName + * @param featureList + * @param effectiveFeatureList + */ + public void getPermitOverridesFeatures(String featureName, List featureList, List effectiveFeatureList) { + Feature evaluatedFeature = null; + for (Feature feature : featureList) { + if (feature.getName().equalsIgnoreCase(featureName)) { + if (feature.getRuleValue().equalsIgnoreCase("Permit")) { + evaluatedFeature = feature; + effectiveFeatureList.add(evaluatedFeature); + return; + } else { + evaluatedFeature = feature; + } + } + } + if (evaluatedFeature != null) { + effectiveFeatureList.add(evaluatedFeature); + } + + } + + /** + * This method picks the first features of the give type. + * But if given policies do not have features of given type, it will not add anything. + * + * @param featureName + * @param featureList + * @param effectiveFeatureList + */ + public void getFirstApplicableFeatures(String featureName, List featureList, List effectiveFeatureList) { + for (Feature feature : featureList) { + if (feature.getName().equalsIgnoreCase(featureName)) { + effectiveFeatureList.add(feature); + return; + + } + } + } + + /** + * This method picks the last features of the give type. + * But if given policies do not have features of given type, it will not add anything. + * + * @param featureName + * @param featureList + * @param effectiveFeatureList + */ + public void getLastApplicableFeatures(String featureName, List featureList, List effectiveFeatureList) { + Feature evaluatedFeature = null; + for (Feature feature : featureList) { + if (feature.getName().equalsIgnoreCase(featureName)) { + evaluatedFeature = feature; + } + } + if (evaluatedFeature != null) { + effectiveFeatureList.add(evaluatedFeature); + } + } + + /** + * This method picks the all features of the give type. + * But if given policies do not have features of given type, it will not add anything. + * + * @param featureName + * @param featureList + * @param effectiveFeatureList + */ + public void getAllApplicableFeatures(String featureName, List featureList, List effectiveFeatureList) { + for (Feature feature : featureList) { + if (feature.getName().equalsIgnoreCase(featureName)) { + effectiveFeatureList.add(feature); + } + } + } + + /** + * This method picks the feature with the highest value of given type. + * But if given policies do not have features of given type, it will not add anything. + * + * @param featureName + * @param featureList + * @param effectiveFeatureList + */ + public void getHighestApplicableFeatures(String featureName, List featureList, List effectiveFeatureList) { + Feature evaluatedFeature = null; + int intValve = 0; + for (Feature feature : featureList) { + if (feature.getName().equalsIgnoreCase(featureName)) { + if (Integer.parseInt(feature.getRuleValue()) > intValve) { + intValve = Integer.parseInt(feature.getRuleValue()); + evaluatedFeature = feature; + } + } + } + if (evaluatedFeature != null) { + effectiveFeatureList.add(evaluatedFeature); + } + } + + /** + * This method picks the feature with the lowest value of given type. + * But if given policies do not have features of given type, it will not add anything. + * + * @param featureName + * @param featureList + * @param effectiveFeatureList + */ + public void getLowestApplicableFeatures(String featureName, List featureList, List effectiveFeatureList) { + Feature evaluatedFeature = null; + int intValve = 0; + for (Feature feature : featureList) { + if (feature.getName().equalsIgnoreCase(featureName)) { + if (Integer.parseInt(feature.getRuleValue()) < intValve) { + intValve = Integer.parseInt(feature.getRuleValue()); + evaluatedFeature = feature; + } + } + } + if (evaluatedFeature != null) { + effectiveFeatureList.add(evaluatedFeature); + } + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/FeatureRules.java b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/FeatureRules.java new file mode 100644 index 000000000..f706cf947 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/FeatureRules.java @@ -0,0 +1,41 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you 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. +*/ + +package org.wso2.carbon.policy.evaluator; + +public class FeatureRules { + + private String name; + private String evaluationCriteria; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEvaluationCriteria() { + return evaluationCriteria; + } + + public void setEvaluationCriteria(String evaluationCriteria) { + this.evaluationCriteria = evaluationCriteria; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/PDPServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/PDPServiceImpl.java new file mode 100644 index 000000000..04c9b4728 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/PDPServiceImpl.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you 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. + */ + +package org.wso2.carbon.policy.evaluator; + +import org.wso2.carbon.policy.evaluator.spi.PDPService; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.List; + +public class PDPServiceImpl implements PDPService { + @Override + public List getEffectivePolicyList(List policies, List roles, String deviceType) { + return null; + } + + @Override + public List getEffectiveFeatureList(List policies, List featureRulesList) { + return null; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilter.java b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilter.java new file mode 100644 index 000000000..6a3244879 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilter.java @@ -0,0 +1,43 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you 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. +*/ + + +package org.wso2.carbon.policy.evaluator; + +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.List; + +public interface PolicyFilter { + + /** + * This method will extract the policies related a given roles list from the policy list available. + * @param policyList + * @param roles + * @return + */ + public List extractPoliciesRelatedToRoles(List policyList, List roles); + + /** + * This mehtod extract the policies related to a given device type from policy list. + * @param policyList + * @param deviceType + * @return + */ + public List extractPoliciesRelatedToDeviceType(List policyList, String deviceType); +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java new file mode 100644 index 000000000..92775e7ce --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/PolicyFilterImpl.java @@ -0,0 +1,72 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you 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. +*/ + +package org.wso2.carbon.policy.evaluator; + +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.ArrayList; +import java.util.List; + +public class PolicyFilterImpl implements PolicyFilter { + + + /** + * This method will extract the policies related a given roles list from the policy list available. + * + * @param policyList + * @param roles + * @return + */ + @Override + public List extractPoliciesRelatedToRoles(List policyList, List roles) { + + List policies = new ArrayList(); + + for (Policy policy : policyList) { + List roleList = policy.getRoleList(); + + for (String role : roleList) { + if (roles.contains(role)) { + policies.add(policy); + break; + } + } + } + return policies; + } + + /** + * This mehtod extract the policies related to a given device type from policy list. + * + * @param policyList + * @param deviceType + * @return + */ + @Override + public List extractPoliciesRelatedToDeviceType(List policyList, String deviceType) { + List policies = new ArrayList(); + + for (Policy policy : policyList) { + if (policy.getDeviceType().equalsIgnoreCase(deviceType)) { + policies.add(policy); + } + } + return policies; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/spi/PDPService.java b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/spi/PDPService.java index 9c3f33da3..0b2b65d44 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/spi/PDPService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/spi/PDPService.java @@ -19,7 +19,16 @@ package org.wso2.carbon.policy.evaluator.spi; +import org.wso2.carbon.policy.evaluator.FeatureRules; +import org.wso2.carbon.policy.mgt.common.Feature; +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.List; + public interface PDPService { + List getEffectivePolicyList(List policies, List roles, String deviceType); + + List getEffectiveFeatureList(List policies, List featureRulesList); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/utils/Constants.java b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/utils/Constants.java new file mode 100644 index 000000000..eb5326b15 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.evalutor/src/main/java/org/wso2/carbon/policy/evaluator/utils/Constants.java @@ -0,0 +1,30 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you 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. +*/ + +package org.wso2.carbon.policy.evaluator.utils; + +public class Constants { + + public static final String DENY_OVERRIDES="deny_overrides"; + public static final String PERMIT_OVERRIDES="permit_overrides"; + public static final String FIRST_APPLICABLE="first_applicable"; + public static final String LAST_APPLICABLE="last_applicable"; + public static final String ALL_APPLICABLE="all_applicable"; + public static final String HIGHEST_APPLICABLE="highest_applicable"; + public static final String LOWEST_APPLICABLE="lowest_applicable"; +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java index 9183c810b..b6dbdf52c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Feature.java @@ -24,6 +24,15 @@ public class Feature { private String code; private String name; private Object attribute; + private String ruleValue; + + public String getRuleValue() { + return ruleValue; + } + + public void setRuleValue(String ruleValue) { + this.ruleValue = ruleValue; + } public int getId() { return id; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java index a52bd487e..576a26e75 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java @@ -23,8 +23,35 @@ import java.util.List; public class Policy { private int id; private String policyName; - private List featuresList; + private List featuresList; private boolean generic; + private List roleList; + private List DeviceList; + private String deviceType; + + public List getRoleList() { + return roleList; + } + + public void setRoleList(List roleList) { + this.roleList = roleList; + } + + public List getDeviceList() { + return DeviceList; + } + + public void setDeviceList(List deviceList) { + DeviceList = deviceList; + } + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } public boolean isGeneric() { return generic;