Adding partially completed policy evaluation

merge-requests/7/head
Geeth Munasinghe 10 years ago
parent 771575c408
commit ce3149bc45

@ -78,6 +78,10 @@
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.logging</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.policy.mgt.common</artifactId>
</dependency>
</dependencies>
</project>

@ -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<Feature> evaluate(List<Policy> policyList, List<FeatureRules> featureRulesList);
List<Feature> extractFeatures(List<Policy> policyList);
List<Feature> evaluateFeatures(List<Feature> featureList, List<FeatureRules> featureRulesList);
void getDenyOverridesFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getPermitOverridesFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getFirstApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getLastApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getAllApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getHighestApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
void getLowestApplicableFeatures(String featureName, List<Feature> featureList, List<Feature> effectiveFeatureList);
}

@ -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<Feature> evaluate(List<Policy> policyList, List<FeatureRules> 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<Feature> extractFeatures(List<Policy> policyList) {
List<Feature> featureList = new ArrayList<Feature>();
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<Feature> evaluateFeatures(List<Feature> featureList, List<FeatureRules> featureRulesList) {
List<Feature> effectiveFeatureList = new ArrayList<Feature>();
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<Feature> featureList, List<Feature> 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<Feature> featureList, List<Feature> 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<Feature> featureList, List<Feature> 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<Feature> featureList, List<Feature> 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<Feature> featureList, List<Feature> 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<Feature> featureList, List<Feature> 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<Feature> featureList, List<Feature> 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);
}
}
}

@ -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;
}
}

@ -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<Policy> getEffectivePolicyList(List<Policy> policies, List<String> roles, String deviceType) {
return null;
}
@Override
public List<Feature> getEffectiveFeatureList(List<Policy> policies, List<FeatureRules> featureRulesList) {
return null;
}
}

@ -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<Policy> extractPoliciesRelatedToRoles(List<Policy> policyList, List<String> roles);
/**
* This mehtod extract the policies related to a given device type from policy list.
* @param policyList
* @param deviceType
* @return
*/
public List<Policy> extractPoliciesRelatedToDeviceType(List<Policy> policyList, String deviceType);
}

@ -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<Policy> extractPoliciesRelatedToRoles(List<Policy> policyList, List<String> roles) {
List<Policy> policies = new ArrayList<Policy>();
for (Policy policy : policyList) {
List<String> 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<Policy> extractPoliciesRelatedToDeviceType(List<Policy> policyList, String deviceType) {
List<Policy> policies = new ArrayList<Policy>();
for (Policy policy : policyList) {
if (policy.getDeviceType().equalsIgnoreCase(deviceType)) {
policies.add(policy);
}
}
return policies;
}
}

@ -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<Policy> getEffectivePolicyList(List<Policy> policies, List<String> roles, String deviceType);
List<Feature> getEffectiveFeatureList(List<Policy> policies, List<FeatureRules> featureRulesList);
}

@ -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";
}

@ -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;

@ -23,8 +23,35 @@ import java.util.List;
public class Policy {
private int id;
private String policyName;
private List<Feature> featuresList;
private List<Feature> featuresList;
private boolean generic;
private List<String> roleList;
private List<String> DeviceList;
private String deviceType;
public List<String> getRoleList() {
return roleList;
}
public void setRoleList(List<String> roleList) {
this.roleList = roleList;
}
public List<String> getDeviceList() {
return DeviceList;
}
public void setDeviceList(List<String> deviceList) {
DeviceList = deviceList;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public boolean isGeneric() {
return generic;

Loading…
Cancel
Save