Adding the final changes of the policy implementation, Added the simple policy evaluation implementation

revert-70aa11f8
Geeth Munasinghe 10 years ago
parent bddf71f2de
commit 90ff502b21

@ -22,13 +22,15 @@ package org.wso2.carbon.policy.mgt.common;
import org.wso2.carbon.device.mgt.core.dto.Device;
import java.sql.Date;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
/**
* This class will be the used to create policy object with relevant information for evaluating.
*/
public class Policy {
public class Policy implements Comparable<Policy> {
private int id; // Identifier of the policy.
private int priorityId; // Priority of the policies. This will be used only for simple evaluation.
private Profile profile; // Profile id
@ -186,4 +188,27 @@ public class Policy {
public void setTenantId(int tenantId) {
this.tenantId = tenantId;
}
/* static final Comparator<Policy> PRIORITY_ORDER =
new Comparator<Policy>() {
public int compare(Policy p1, Policy p2) {
int dateCmp = new Integer(p2.getId()).compareTo(new Integer(p1.getId()));
if (dateCmp != 0)
return dateCmp;
return (p1.getId() < p2.getId() ? -1 :
(p1.getId() == p2.getId() ? 0 : 1));
}
};*/
@Override
public int compareTo(Policy o) {
if (this.priorityId == o.priorityId)
return 0;
else if ((this.priorityId) > o.priorityId)
return 1;
else
return -1;
}
}

@ -34,7 +34,7 @@ public interface PolicyEvaluationPoint {
* @param deviceIdentifier device information.
* @return returns the effective policy.
*/
Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier);
Policy getEffectivePolicies(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException;
/**
@ -42,5 +42,5 @@ public interface PolicyEvaluationPoint {
* @param deviceIdentifier device information.
* @return returns the effective feature set.
*/
List<Feature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier);
List<ProfileFeature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException ;
}

@ -641,12 +641,13 @@ public class PolicyDAOImpl implements PolicyDAO {
int tenantId = -1234;
try {
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID) VALUES (?, ?, ?)";
String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS);
stmt.setString(1, policy.getPolicyName());
stmt.setInt(2, policy.getProfile().getProfileId());
stmt.setInt(3, tenantId);
stmt.setInt(4, readHighestPriorityOfPolicies());
int affectedRows = stmt.executeUpdate();
@ -715,4 +716,30 @@ public class PolicyDAOImpl implements PolicyDAO {
return deviceTypeId;
}
private int readHighestPriorityOfPolicies() throws PolicyManagerDAOException {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = this.getConnection();
String query = "SELECT MAX(PRIORITY) PRIORITY FROM DM_POLICY;";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
return resultSet.getInt("PRIORITY");
}
} catch (SQLException e) {
String msg = "Error occurred while reading the highest priority of the policies.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet);
}
return 0;
}
}

@ -18,6 +18,8 @@
package org.wso2.carbon.policy.mgt.core.internal;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.user.core.service.RealmService;
import org.wso2.carbon.user.core.tenant.TenantManager;
@ -25,6 +27,8 @@ public class PolicyManagementDataHolder {
private RealmService realmService;
private TenantManager tenantManager;
private PolicyEvaluationPoint policyEvaluationPoint;
private PolicyInformationPoint policyInformationPoint;
private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder();
private PolicyManagementDataHolder() {}
@ -53,5 +57,19 @@ public class PolicyManagementDataHolder {
return tenantManager;
}
public PolicyEvaluationPoint getPolicyEvaluationPoint() {
return policyEvaluationPoint;
}
public void setPolicyEvaluationPoint(PolicyEvaluationPoint policyEvaluationPoint) {
this.policyEvaluationPoint = policyEvaluationPoint;
}
public PolicyInformationPoint getPolicyInformationPoint() {
return policyInformationPoint;
}
public void setPolicyInformationPoint(PolicyInformationPoint policyInformationPoint) {
this.policyInformationPoint = policyInformationPoint;
}
}

@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.core.internal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager;
@ -44,6 +45,12 @@ import org.wso2.carbon.user.core.service.RealmService;
* policy="dynamic"
* bind="setPIPService"
* unbind="unsetPIPService"
* @scr.reference name="org.wso2.carbon.devicemgt.simple.policy.evaluation.manager"
* interface="org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint"
* cardinality="1..1"
* policy="dynamic"
* bind="setPEPService"
* unbind="unsetPEPService"
*/
public class PolicyManagementServiceComponent {
@ -94,16 +101,33 @@ public class PolicyManagementServiceComponent {
}
protected void setPIPService(PolicyInformationPoint policyInformationService) {
protected void setPIPService(PolicyInformationPoint pipService) {
if (log.isDebugEnabled()) {
log.debug("Setting Policy Information Service");
}
PolicyManagementDataHolder.getInstance().setPolicyInformationPoint(pipService);
}
protected void unsetPIPService(PolicyInformationPoint pipService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting Policy Information Service");
}
PolicyManagementDataHolder.getInstance().setPolicyInformationPoint(null);
}
protected void setPEPService(PolicyEvaluationPoint pepService) {
if (log.isDebugEnabled()) {
log.debug("Setting Policy Information Service");
}
PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(pepService);
}
protected void unsetPIPService(PolicyInformationPoint policyInformationService) {
protected void unsetPEPService(PolicyEvaluationPoint pepService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting Policy Information Service");
}
PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(null);
}
}

@ -79,6 +79,7 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY` (
`NAME` VARCHAR(45) NULL DEFAULT NULL ,
`TENANT_ID` INT(11) NOT NULL ,
`PROFILE_ID` INT(11) NOT NULL ,
`PRIORITY` INT NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `FK_DM_PROFILE_DM_POLICY` (`PROFILE_ID` ASC) ,
CONSTRAINT `FK_DM_PROFILE_DM_POLICY`
@ -151,9 +152,16 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_FEATURES` (
`ID` INT(11) NOT NULL AUTO_INCREMENT ,
`NAME` VARCHAR(256) NOT NULL ,
`CODE` VARCHAR(45) NULL DEFAULT NULL ,
`DEVICE_TYPE_ID` INT NOT NULL ,
`DESCRIPTION` TEXT NULL DEFAULT NULL ,
`EVALUVATION_RULE` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`ID`) )
PRIMARY KEY (`ID`) ,
INDEX `DM_FEATURES_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC) ,
CONSTRAINT `DM_FEATURES_DEVICE_TYPE`
FOREIGN KEY (`DEVICE_TYPE_ID` )
REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

@ -34,6 +34,15 @@
<Bundle-Version>${carbon.device.mgt.version}</Bundle-Version>
<Bundle-Description>Simple Policy Decision Point Bundle</Bundle-Description>
<Private-Package>org.wso2.carbon.simple.policy.decision.point.internal</Private-Package>
<Import-Package>
org.osgi.framework,
org.osgi.service.component,
org.apache.commons.logging,
org.wso2.carbon.policy.mgt.common.*,
org.wso2.carbon.policy.mgt.core.*,
org.wso2.carbon.user.core.*,
org.wso2.carbon.device.mgt.common.*
</Import-Package>
<Export-Package>
org.wso2.carbon.simple.policy.decision.point.*
</Export-Package>

@ -19,20 +19,28 @@
package org.wso2.carbon.simple.policy.decision.point;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Feature;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import java.util.List;
public class PolicyEvaluationServiceImpl implements PolicyEvaluationPoint {
private SimpleEvaluationImpl evaluation;
public PolicyEvaluationServiceImpl() {
evaluation = new SimpleEvaluationImpl();
}
@Override
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) {
return null;
public Policy getEffectivePolicies(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
return evaluation.getEffectivePolicy(deviceIdentifier);
}
@Override
public List<Feature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) {
return null;
public List<ProfileFeature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
return evaluation.getEffectivePolicy(deviceIdentifier).getProfile().getProfileFeaturesList();
}
}

@ -19,13 +19,14 @@
package org.wso2.carbon.simple.policy.decision.point;
import org.wso2.carbon.policy.mgt.common.PIPDevice;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
public interface SimpleEvaluation {
void sortPolicy(Policy policy) throws PolicyEvaluationException;
void sortPolicy() throws PolicyEvaluationException;
Policy getEffectivePolicy(PIPDevice pipDevice) throws PolicyEvaluationException;
Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException;
}

@ -20,28 +20,48 @@ package org.wso2.carbon.simple.policy.decision.point;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.PIPDevice;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.simple.policy.decision.point.internal.PolicyDecisionPointDataHolder;
import java.util.Collections;
import java.util.List;
public class SimpleEvaluationImpl implements SimpleEvaluation {
private static final Log log = LogFactory.getLog(SimpleEvaluationImpl.class);
PolicyDAOImpl policyDAO;
private PolicyManager policyManager;
private List<Policy> policyList;
public SimpleEvaluationImpl() {
policyDAO = new PolicyDAOImpl();
policyManager = PolicyDecisionPointDataHolder.getInstance().getPolicyManager();
}
@Override
public void sortPolicy(Policy policy) throws PolicyEvaluationException {
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
try {
if (policyManager == null && policyList == null) {
PIPDevice pipDevice = policyManager.getPIP().getDeviceData(deviceIdentifier);
policyList = policyManager.getPIP().getRelatedPolicies(pipDevice);
}
sortPolicy();
} catch (PolicyManagementException e) {
String msg = "Error occurred when retrieving the policy related data from policy management service.";
log.error(msg, e);
throw new PolicyEvaluationException(msg, e);
}
return policyList.get(0);
}
@Override
public Policy getEffectivePolicy(PIPDevice pipDevice) throws PolicyEvaluationException {
return null;
public void sortPolicy() throws PolicyEvaluationException {
Collections.sort(policyList);
}
}

@ -0,0 +1,53 @@
/*
* 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.simple.policy.decision.point.internal;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.user.core.service.RealmService;
public class PolicyDecisionPointDataHolder {
private RealmService realmService;
private PolicyManager policyManager;
private static PolicyDecisionPointDataHolder dataHolder = new PolicyDecisionPointDataHolder();
private PolicyDecisionPointDataHolder() {
}
public static PolicyDecisionPointDataHolder getInstance() {
return dataHolder;
}
public RealmService getRealmService() {
return realmService;
}
public void setRealmService(RealmService realmService) {
this.realmService = realmService;
}
public PolicyManager getPolicyManager() {
return policyManager;
}
public void setPolicyManager(PolicyManager policyManager) {
this.policyManager = policyManager;
}
}

@ -0,0 +1,109 @@
/*
* 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.simple.policy.decision.point.internal;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.core.PolicyManager;
import org.wso2.carbon.simple.policy.decision.point.PolicyEvaluationServiceImpl;
import org.wso2.carbon.user.core.service.RealmService;
/**
* @scr.component name="org.wso2.carbon.simple.policy.decision.PolicyEvaluationServiceComponent" immediate="true"
* @scr.reference name="user.realmservice.default"
* interface="org.wso2.carbon.user.core.service.RealmService"
* cardinality="1..1"
* policy="dynamic"
* bind="setRealmService"
* unbind="unsetRealmService"
* @scr.reference name="org.wso2.carbon.devicemgt.policy.manager"
* interface="org.wso2.carbon.policy.mgt.core.PolicyManager"
* cardinality="0..1"
* policy="dynamic"
* bind="setPolicyManagerService"
* unbind="unsetPolicyManagerService"
*/
public class PolicyEvaluationServiceComponent {
private static Log log = LogFactory.getLog(PolicyEvaluationServiceComponent.class);
protected void activate(ComponentContext componentContext) {
if (log.isDebugEnabled()) {
log.debug("Activating the simple policy evaluation bundle.");
}
try {
componentContext.getBundleContext().registerService(PolicyEvaluationPoint.class.getName(),
new PolicyEvaluationServiceImpl(), null);
} catch (Throwable t) {
log.error("Error occurred while initializing the simple policy evaluation bundle");
}
}
protected void deactivate(ComponentContext componentContext) {
if (log.isDebugEnabled()) {
log.debug("De-activating the simple policy evaluation bundle.");
}
}
/**
* Sets Realm Service
*
* @param realmService An instance of RealmService
*/
protected void setRealmService(RealmService realmService) {
if (log.isDebugEnabled()) {
log.debug("Setting Realm Service");
}
PolicyDecisionPointDataHolder.getInstance().setRealmService(realmService);
}
/**
* Unsets Realm Service
*
* @param realmService An instance of RealmService
*/
protected void unsetRealmService(RealmService realmService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting Realm Service");
}
PolicyDecisionPointDataHolder.getInstance().setRealmService(null);
}
protected void setPolicyManagerService(PolicyManager policyManagerService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting PolicyManager Service");
}
PolicyDecisionPointDataHolder.getInstance().setPolicyManager(policyManagerService);
}
protected void unsetPolicyManagerService(PolicyManager policyManagerService) {
if (log.isDebugEnabled()) {
log.debug("Unsetting PolicyManager Service");
}
PolicyDecisionPointDataHolder.getInstance().setPolicyManager(null);
}
}

@ -47,7 +47,7 @@
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.complex.policy.decision.point</artifactId>
<artifactId>org.wso2.carbon.simple.policy.decision.point</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
@ -110,7 +110,7 @@
org.wso2.carbon.devicemgt:org.wso2.carbon.policy.mgt.common:${carbon.device.mgt.version}
</bundleDef>
<bundleDef>
org.wso2.carbon.devicemgt:org.wso2.carbon.complex.policy.decision.point:${carbon.device.mgt.version}
org.wso2.carbon.devicemgt:org.wso2.carbon.simple.policy.decision.point:${carbon.device.mgt.version}
</bundleDef>
<bundleDef>
org.wso2.carbon.devicemgt:org.wso2.carbon.policy.information.point:${carbon.device.mgt.version}

@ -141,6 +141,11 @@
<artifactId>org.wso2.carbon.complex.policy.decision.point</artifactId>
<version>${carbon.device.mgt.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.simple.policy.decision.point</artifactId>
<version>${carbon.device.mgt.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.policy.information.point</artifactId>

Loading…
Cancel
Save