revert-70aa11f8
manoj 10 years ago
commit 60f50a9242

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.operation.mgt;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@ -39,7 +40,7 @@ public class ConfigOperation extends Operation {
properties.add(new Property(name, value, type));
}
public class Property {
public class Property implements Serializable {
private String name;
private Object value;
private Class<?> type;

@ -76,6 +76,8 @@ public class OperationManagerImpl implements OperationManager {
OperationManagementDAOFactory.beginTransaction();
org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto =
OperationDAOUtil.convertOperation(operation);
operationDto.setStatus(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING);
int operationId = this.lookupOperationDAO(operation).addOperation(operationDto);
org.wso2.carbon.device.mgt.common.Device device;

@ -27,17 +27,18 @@ import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
//TODO :
public class PIPDevice {
Device device;
DeviceType deviceType;
DeviceIdentifier deviceIdentifier;
String ownershipType;
List<String> userIds;
String roles [];
String altitude;
String longitude;
Timestamp timestamp;
private Device device;
private DeviceType deviceType;
private DeviceIdentifier deviceIdentifier;
private String ownershipType;
private List<String> userIds;
private String roles[];
private String altitude;
private String longitude;
private Timestamp timestamp;
/*This will be used to record attributes to which would come from other PDPs*/
Map<String, Object> attributes;

@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.common;
import org.wso2.carbon.device.mgt.core.dto.Device;
import java.io.Serializable;
import java.sql.Date;
import java.util.List;
import java.util.Map;
@ -28,12 +29,11 @@ import java.util.Map;
/**
* This class will be the used to create policy object with relevant information for evaluating.
*/
public class Policy implements Comparable<Policy> {
public class Policy implements Comparable<Policy>, Serializable {
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
private int profileId;
private String policyName; // Name of the policy.
private boolean generic; // If true, this should be applied to all related device.
private List<String> roleList; // Roles which this policy should be applied.
@ -57,6 +57,7 @@ public class Policy implements Comparable<Policy> {
private String longitude; // Longitude
private int tenantId;
private int profileId;
/*This will be used to record attributes which will be used by customer extended PDPs and PIPs*/

@ -31,8 +31,6 @@ public interface PolicyAdministratorPoint {
/**
* This method adds a policy to the platform
*
* @param policy
* @return primary key (generated key)
*/
Policy addPolicy(Policy policy) throws PolicyManagementException;
@ -40,13 +38,10 @@ public interface PolicyAdministratorPoint {
Policy updatePolicy(Policy policy) throws PolicyManagementException;
boolean deletePolicy(Policy policy) throws PolicyManagementException;
/**
* This method adds a policy per device which should be implemented by the related plugins.
*
* @param deviceIdentifierList
* @param policy
* @return
* @throws PolicyManagementException
*/
Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws PolicyManagementException;
@ -132,7 +127,7 @@ public interface PolicyAdministratorPoint {
*/
Profile addProfile(Profile profile) throws PolicyManagementException;
boolean deleteProfile(int profileId) throws PolicyManagementException;
boolean deleteProfile(Profile profile) throws PolicyManagementException;
Profile updateProfile(Profile profile) throws PolicyManagementException;

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

@ -124,7 +124,7 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_FEATURES` (
`NAME` VARCHAR(256) NOT NULL ,
`CODE` VARCHAR(45) NULL DEFAULT NULL ,
`DESCRIPTION` TEXT NULL DEFAULT NULL ,
`EVALUVATION_RULE` VARCHAR(60) NOT NULL ,
`EVALUATION_RULE` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`ID`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;

@ -119,6 +119,10 @@
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.base</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.ndatasource.core</artifactId>

@ -19,7 +19,6 @@
package org.wso2.carbon.policy.mgt.core;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.*;
@ -39,9 +38,11 @@ public interface PolicyManagerService {
Policy updatePolicy(Policy policy) throws PolicyManagementException;
boolean deletePolicy(Policy policy) throws PolicyManagementException;
Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
Policy getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException;
List<ProfileFeature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException;
List<Policy> getPolicies(String deviceType) throws PolicyManagementException;
@ -50,4 +51,6 @@ public interface PolicyManagerService {
PolicyAdministratorPoint getPAP() throws PolicyManagementException;
PolicyInformationPoint getPIP() throws PolicyManagementException;
PolicyEvaluationPoint getPEP() throws PolicyManagementException;
}

@ -24,6 +24,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl;
import org.wso2.carbon.policy.mgt.core.impl.PolicyInformationPointImpl;
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
import java.util.List;
@ -67,19 +68,41 @@ public class PolicyManagerServiceImpl implements PolicyManagerService {
return policyAdministratorPoint.updatePolicy(policy);
}
@Override
public boolean deletePolicy(Policy policy) throws PolicyManagementException {
return policyAdministratorPoint.deletePolicy(policy);
}
@Override
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
return null;
try {
return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint().
getEffectivePolicy(deviceIdentifier);
} catch (PolicyEvaluationException e) {
String msg = "Error occurred while getting the effective policies from the PEP service for device " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
}
@Override
public Policy getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException {
return null;
public List<ProfileFeature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws
FeatureManagementException {
try {
return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint().
getEffectiveFeatures(deviceIdentifier);
} catch (PolicyEvaluationException e) {
String msg = "Error occurred while getting the effective features from the PEP service " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType();
log.error(msg, e);
throw new FeatureManagementException(msg, e);
}
}
@Override
public List<Policy> getPolicies(String deviceType) throws PolicyManagementException {
return null;
return policyAdministratorPoint.getPoliciesOfDeviceType(deviceType);
}
@Override
@ -96,4 +119,9 @@ public class PolicyManagerServiceImpl implements PolicyManagerService {
public PolicyInformationPoint getPIP() throws PolicyManagementException {
return new PolicyInformationPointImpl();
}
@Override
public PolicyEvaluationPoint getPEP() throws PolicyManagementException {
return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint();
}
}

@ -38,9 +38,11 @@ public interface FeatureDAO {
ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException;
List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagerDAOException;
List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws
FeatureManagerDAOException;
List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId) throws FeatureManagerDAOException;
List<ProfileFeature> updateProfileFeatures(List<ProfileFeature> features, int profileId) throws
FeatureManagerDAOException;
List<Feature> getAllFeatures() throws FeatureManagerDAOException;

@ -73,4 +73,13 @@ public interface PolicyDAO {
PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException;
void addEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures)
throws PolicyManagerDAOException;
void setPolicyApplied(int deviceId) throws PolicyManagerDAOException;
void updateEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures)
throws PolicyManagerDAOException;
boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException;
}

@ -50,7 +50,7 @@ public class FeatureDAOImpl implements FeatureDAO {
try {
conn = this.getConnection();
String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION, EVALUVATION_RULE, DEVICE_TYPE_ID) VALUES (?, ?, ?, ?, ?)";
String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION, EVALUATION_RULE, DEVICE_TYPE_ID) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt.setString(1, feature.getName());
stmt.setString(2, feature.getCode());
@ -88,7 +88,7 @@ public class FeatureDAOImpl implements FeatureDAO {
try {
conn = this.getConnection();
String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION, EVALUVATION_RULE, DEVICE_TYPE_ID) VALUES (?, ?, ?, ?, ?)";
String query = "INSERT INTO DM_FEATURES (NAME, CODE, DESCRIPTION, EVALUATION_RULE, DEVICE_TYPE_ID) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
for (Feature feature : features) {
@ -133,7 +133,7 @@ public class FeatureDAOImpl implements FeatureDAO {
try {
conn = this.getConnection();
String query = "UPDATE DM_FEATURES SET NAME = ?, CODE = ?, DESCRIPTION = ?, EVALUVATION_RULE = ? WHERE ID = ?";
String query = "UPDATE DM_FEATURES SET NAME = ?, CODE = ?, DESCRIPTION = ?, EVALUATION_RULE = ? WHERE ID = ?";
stmt = conn.prepareStatement(query);
stmt.setString(1, feature.getName());
stmt.setString(2, feature.getCode());
@ -266,7 +266,7 @@ public class FeatureDAOImpl implements FeatureDAO {
try {
conn = this.getConnection();
String query = "SELECT ID, NAME, CODE, DEVICE_TYPE_ID, EVALUVATION_RULE FROM DM_FEATURES";
String query = "SELECT ID, NAME, CODE, DEVICE_TYPE_ID, EVALUATION_RULE FROM DM_FEATURES";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
@ -277,7 +277,7 @@ public class FeatureDAOImpl implements FeatureDAO {
feature.setCode(resultSet.getString("CODE"));
feature.setName(resultSet.getString("NAME"));
feature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID"));
feature.setRuleValue(resultSet.getString("EVALUVATION_RULE"));
feature.setRuleValue(resultSet.getString("EVALUATION_RULE"));
featureList.add(feature);
}
@ -303,7 +303,7 @@ public class FeatureDAOImpl implements FeatureDAO {
try {
conn = this.getConnection();
String query = "SELECT PF.ID AS ID, PF.FEATURE_ID FEATURE_ID, F.NAME NAME, F.CODE CODE, " +
"F.EVALUVATION_RULE RULE, F.CONTENT AS CONTENT FROM DM_PROFILE_FEATURES AS PF " +
"F.EVALUATION_RULE RULE, PF.CONTENT AS CONTENT, PF.PROFILE_ID PROFILE_ID FROM DM_PROFILE_FEATURES AS PF " +
"JOIN DM_FEATURES AS F ON F.ID = PF.FEATURE_ID";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
@ -320,6 +320,7 @@ public class FeatureDAOImpl implements FeatureDAO {
profileFeature.setFeature(feature);
profileFeature.setId(resultSet.getInt("ID"));
profileFeature.setContent(resultSet.getObject("CONTENT"));
profileFeature.setProfileId(resultSet.getInt("PROFILE_ID"));
featureList.add(profileFeature);
}
@ -345,7 +346,7 @@ public class FeatureDAOImpl implements FeatureDAO {
try {
conn = this.getConnection();
String query = "SELECT f.ID ID, f.NAME NAME, f.CODE CODE, f.DEVICE_TYPE_ID DEVICE_TYPE_ID," +
" f.EVALUVATION_RULE EVALUVATION_RULE FROM DM_FEATURES f INNER JOIN DM_DEVICE_TYPE d " +
" f.EVALUATION_RULE EVALUATION_RULE FROM DM_FEATURES f INNER JOIN DM_DEVICE_TYPE d " +
"ON d.ID=f.DEVICE_TYPE_ID WHERE d.NAME = ?";
stmt = conn.prepareStatement(query);
stmt.setString(1, deviceType);
@ -358,7 +359,7 @@ public class FeatureDAOImpl implements FeatureDAO {
feature.setCode(resultSet.getString("CODE"));
feature.setName(resultSet.getString("NAME"));
feature.setDeviceTypeId(resultSet.getInt("DEVICE_TYPE_ID"));
feature.setRuleValue(resultSet.getString("EVALUVATION_RULE"));
feature.setRuleValue(resultSet.getString("EVALUATION_RULE"));
featureList.add(feature);
}
@ -384,7 +385,7 @@ public class FeatureDAOImpl implements FeatureDAO {
try {
conn = this.getConnection();
String query = "SELECT PF.ID AS ID, PF.FEATURE_ID FEATURE_ID, F.NAME NAME, F.CODE CODE, " +
"F.EVALUVATION_RULE RULE, F.CONTENT AS CONTENT FROM DM_PROFILE_FEATURES AS PF " +
"F.EVALUATION_RULE RULE, F.CONTENT AS CONTENT FROM DM_PROFILE_FEATURES AS PF " +
"JOIN DM_FEATURES AS F ON F.ID = PF.FEATURE_ID WHERE PROFILE_ID=?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, profileId);

@ -21,22 +21,16 @@ package org.wso2.carbon.policy.mgt.core.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyDates;
import org.wso2.carbon.policy.mgt.common.PolicyLocations;
import org.wso2.carbon.policy.mgt.common.PolicyTimes;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class PolicyDAOImpl implements PolicyDAO {
@ -200,7 +194,8 @@ public class PolicyDAOImpl implements PolicyDAO {
}
@Override
public Policy addLocationToPolicy(String latitude, String longitude, Policy policy) throws PolicyManagerDAOException {
public Policy addLocationToPolicy(String latitude, String longitude, Policy policy) throws
PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
@ -270,6 +265,7 @@ public class PolicyDAOImpl implements PolicyDAO {
policy.setId(policyId);
policy.setPolicyName(resultSet.getString("NAME"));
policy.setTenantId(resultSet.getInt("TENANT_ID"));
policy.setPriorityId(resultSet.getInt("PRIORITY"));
}
return policy;
@ -302,6 +298,7 @@ public class PolicyDAOImpl implements PolicyDAO {
policy.setId(resultSet.getInt("ID"));
policy.setPolicyName(resultSet.getString("NAME"));
policy.setTenantId(resultSet.getInt("TENANT_ID"));
policy.setPriorityId(resultSet.getInt("PRIORITY"));
}
return policy;
@ -335,6 +332,7 @@ public class PolicyDAOImpl implements PolicyDAO {
policy.setProfileId(resultSet.getInt("PROFILE_ID"));
policy.setPolicyName(resultSet.getString("NAME"));
policy.setTenantId(resultSet.getInt("TENANT_ID"));
policy.setPriorityId(resultSet.getInt("PRIORITY"));
policies.add(policy);
}
return policies;
@ -432,7 +430,6 @@ public class PolicyDAOImpl implements PolicyDAO {
resultSet = stmt.executeQuery();
while (resultSet.next()) {
//TODO:
policy.setStartTime(resultSet.getInt("STARTING_TIME"));
policy.setEndTime(resultSet.getInt("ENDING_TIME"));
@ -467,7 +464,7 @@ public class PolicyDAOImpl implements PolicyDAO {
resultSet = stmt.executeQuery();
while (resultSet.next()) {
//TODO:
policy.setStartDate(resultSet.getDate("START_DATE"));
policy.setEndDate(resultSet.getDate("END_DATE"));
@ -502,7 +499,7 @@ public class PolicyDAOImpl implements PolicyDAO {
resultSet = stmt.executeQuery();
while (resultSet.next()) {
//TODO:
policy.setLatitude(resultSet.getString("LATITUDE"));
policy.setLongitude(resultSet.getString("LONGITUDE"));
@ -521,6 +518,117 @@ public class PolicyDAOImpl implements PolicyDAO {
return locations;
}
@Override
public void addEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures)
throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
try {
conn = this.getConnection();
String query = "INSERT INTO DM_DEVICE_POLICY_APPLIED " +
"(DEVICE_ID, POLICY_ID, POLICY_CONTENT, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId);
stmt.setInt(2, policyId);
stmt.setObject(3, profileFeatures);
stmt.setTimestamp(4, currentTimestamp);
stmt.setTimestamp(5, currentTimestamp);
stmt.executeUpdate();
} catch (SQLException e) {
String msg = "Error occurred while adding the evaluated feature list to device.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public void setPolicyApplied(int deviceId) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
try {
conn = this.getConnection();
String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET APPLIED_TIME = ?, APPLIED = ? WHERE DEVICE_ID = ? ";
stmt = conn.prepareStatement(query);
stmt.setTimestamp(1, currentTimestamp);
stmt.setBoolean(2, true);
stmt.setInt(3, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
String msg = "Error occurred while updating applied policy to device (" + deviceId + ")";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public void updateEffectivePolicyToDevice(int deviceId, int policyId, List<ProfileFeature> profileFeatures)
throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
try {
conn = this.getConnection();
String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " +
"APPLIED = ? WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, policyId);
stmt.setObject(2, profileFeatures);
stmt.setTimestamp(3, currentTimestamp);
stmt.setBoolean(4, false);
stmt.setInt(5, deviceId);
stmt.executeUpdate();
} catch (SQLException e) {
String msg = "Error occurred while updating the evaluated feature list to device.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
boolean exist = false;
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId);
resultSet = stmt.executeQuery();
exist = resultSet.next();
} catch (SQLException e) {
String msg = "Error occurred while checking whether device (" + deviceId + ") has a policy to apply.";
log.error(msg, e);
throw new PolicyManagerDAOException(msg, e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, resultSet);
this.closeConnection();
}
return exist;
}
@Override
public List<Integer> getPolicyIdsOfDevice(Device device) throws PolicyManagerDAOException {
@ -711,8 +819,8 @@ public class PolicyDAOImpl implements PolicyDAO {
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
int tenantId = PolicyManagerUtil.getTenantId();
int tenantId = MultitenantConstants.SUPER_TENANT_ID;
try {
conn = this.getConnection();
String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY) VALUES (?, ?, ?, ?)";

@ -20,6 +20,7 @@ package org.wso2.carbon.policy.mgt.core.dao.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
@ -27,7 +28,7 @@ import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
import org.wso2.carbon.policy.mgt.core.dao.ProfileDAO;
import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
@ -45,7 +46,7 @@ public class ProfileDAOImpl implements ProfileDAO {
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
int tenantId = MultitenantConstants.SUPER_TENANT_ID;
int tenantId = PolicyManagerUtil.getTenantId();
try {
conn = this.getConnection();
@ -91,7 +92,7 @@ public class ProfileDAOImpl implements ProfileDAO {
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
int tenantId = MultitenantConstants.SUPER_TENANT_ID;
int tenantId = PolicyManagerUtil.getTenantId();
try {
conn = this.getConnection();

@ -57,6 +57,11 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
return policyManager.updatePolicy(policy);
}
@Override
public boolean deletePolicy(Policy policy) throws PolicyManagementException {
return policyManager.deletePolicy(policy);
}
@Override
public Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws PolicyManagementException {
return policyManager.addPolicyToDevice(deviceIdentifierList, policy);
@ -94,17 +99,17 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
@Override
public boolean isPolicyAvailableForDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
return false;
return policyManager.checkPolicyAvailable(deviceIdentifier);
}
@Override
public boolean isPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
return false;
return policyManager.setPolicyApplied(deviceIdentifier);
}
@Override
public void setPolicyUsed(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException {
policyManager.addAppliedPolicyToDevice(deviceIdentifier, policy.getId(), policy.getProfile().getProfileFeaturesList());
}
@Override
@ -119,8 +124,14 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
}
@Override
public boolean deleteProfile(int profileId) throws PolicyManagementException {
return false;
public boolean deleteProfile(Profile profile) throws PolicyManagementException {
try {
return profileManager.deleteProfile(profile);
} catch (ProfileManagementException e) {
String msg = "Error occurred while deleting the profile.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
}
@Override

@ -59,9 +59,9 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
PIPDevice pipDevice = new PIPDevice();
org.wso2.carbon.device.mgt.common.Device device;
// TODO : Find
DeviceType deviceType = new DeviceType();
deviceType.setName(deviceIdentifier.getType());
deviceManagementService = getDeviceManagementService();
try {
device = deviceManagementService.getDevice(deviceIdentifier);
@ -147,4 +147,8 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint {
return finalPolicies;
}
private DeviceManagementService getDeviceManagementService() {
return PolicyManagementDataHolder.getInstance().getDeviceManagementService();
}
}

@ -23,13 +23,12 @@ import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementService;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl;
import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager;
import org.wso2.carbon.policy.mgt.core.config.PolicyManagementConfig;
import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.service.PolicyManagementService;
import org.wso2.carbon.user.core.service.RealmService;
/**
@ -40,12 +39,6 @@ import org.wso2.carbon.user.core.service.RealmService;
* policy="dynamic"
* bind="setRealmService"
* unbind="unsetRealmService"
* @scr.reference name="org.wso2.carbon.devicemgt.policy.information.point.default"
* interface="org.wso2.carbon.policy.mgt.common.PolicyInformationPoint"
* cardinality="1..1"
* 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"
@ -59,7 +52,7 @@ import org.wso2.carbon.user.core.service.RealmService;
* bind="setDeviceManagementService"
* unbind="unsetDeviceManagementService"
*/
@SuppressWarnings("unused")
public class PolicyManagementServiceComponent {
private static Log log = LogFactory.getLog(PolicyManagementServiceComponent.class);
@ -73,11 +66,10 @@ public class PolicyManagementServiceComponent {
PolicyManagementDAOFactory.init(dsConfig);
componentContext.getBundleContext().registerService(
PolicyManagerService.class.getName(), new PolicyManagementService(), null);
PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null);
} catch (Throwable t) {
String msg = "Error occurred while initializing the Policy management core.";
log.error(msg, t);
log.error("Error occurred while initializing the Policy management core.", t);
}
}
@ -108,7 +100,7 @@ public class PolicyManagementServiceComponent {
}
protected void setPIPService(PolicyInformationPoint pipService) {
/* protected void setPIPService(PolicyInformationPoint pipService) {
if (log.isDebugEnabled()) {
log.debug("Setting Policy Information Service");
}
@ -120,7 +112,7 @@ public class PolicyManagementServiceComponent {
log.debug("Unsetting Policy Information Service");
}
PolicyManagementDataHolder.getInstance().setPolicyInformationPoint(null);
}
}*/
protected void setPEPService(PolicyEvaluationPoint pepService) {

@ -22,6 +22,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import java.util.List;
@ -33,7 +34,8 @@ public interface PolicyManager {
boolean deletePolicy(Policy policy) throws PolicyManagementException;
Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws PolicyManagementException;
Policy addPolicyToDevice(List<DeviceIdentifier> deviceIdentifierList, Policy policy) throws
PolicyManagementException;
Policy addPolicyToRole(List<String> roleNames, Policy policy) throws PolicyManagementException;
@ -54,4 +56,11 @@ public interface PolicyManager {
List<Policy> getPoliciesOfUser(String username) throws PolicyManagementException;
List<Device> getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException;
void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, int policyId, List<ProfileFeature> profileFeatures) throws
PolicyManagementException;
boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
}

@ -27,11 +27,10 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO;
import org.wso2.carbon.device.mgt.core.dto.Device;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.common.Profile;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.dao.*;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager;
import java.util.ArrayList;
import java.util.List;
@ -43,6 +42,7 @@ public class PolicyManagerImpl implements PolicyManager {
private FeatureDAO featureDAO;
private DeviceDAO deviceDAO;
private DeviceTypeDAO deviceTypeDAO;
private ProfileManager profileManager;
private static Log log = LogFactory.getLog(PolicyManagerImpl.class);
public PolicyManagerImpl() {
@ -51,6 +51,7 @@ public class PolicyManagerImpl implements PolicyManager {
this.featureDAO = PolicyManagementDAOFactory.getFeatureDAO();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
this.profileManager = new ProfileManagerImpl();
}
@Override
@ -462,13 +463,16 @@ public class PolicyManagerImpl implements PolicyManager {
List<Policy> policies = new ArrayList<Policy>();
try {
DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
List<Profile> profileList = profileDAO.getProfilesOfDeviceType(deviceType);
// DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName);
List<Profile> profileList = profileManager.getProfilesOfDeviceType(deviceTypeName);
List<Policy> allPolicies = policyDAO.getAllPolicies();
for (Profile profile : profileList) {
for (Policy policy : allPolicies) {
if (policy.getProfileId() == profile.getProfileId()) {
policy.setProfile(profile);
policies.add(policy);
}
}
@ -478,12 +482,16 @@ public class PolicyManagerImpl implements PolicyManager {
String msg = "Error occurred while getting all the policies.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (ProfileManagerDAOException e) {
String msg = "Error occurred while getting the profiles related to device type (" + deviceTypeName + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting device type object related to (" + deviceTypeName + ")";
// } catch (ProfileManagerDAOException e) {
// String msg = "Error occurred while getting the profiles related to device type (" + deviceTypeName + ")";
// log.error(msg, e);
// throw new PolicyManagementException(msg, e);
// } catch (DeviceManagementDAOException e) {
// String msg = "Error occurred while getting device type object related to (" + deviceTypeName + ")";
// log.error(msg, e);
// throw new PolicyManagementException(msg, e);
} catch (ProfileManagementException e) {
String msg = "Error occurred while getting all the profile features.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
@ -565,4 +573,75 @@ public class PolicyManagerImpl implements PolicyManager {
}
return deviceList;
}
@Override
public void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, int policyId, List<ProfileFeature> profileFeatures) throws
PolicyManagementException {
int deviceId = -1;
try {
Device device = deviceDAO.getDevice(deviceIdentifier);
deviceId = device.getId();
boolean exist = policyDAO.checkPolicyAvailable(deviceId);
PolicyManagementDAOFactory.beginTransaction();
if (exist) {
policyDAO.updateEffectivePolicyToDevice(deviceId, policyId, profileFeatures);
} else {
policyDAO.addEffectivePolicyToDevice(deviceId, policyId, profileFeatures);
}
PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) {
try {
PolicyManagementDAOFactory.rollbackTransaction();
} catch (PolicyManagerDAOException e1) {
log.warn("Error occurred while roll backing the transaction.");
}
String msg = "Error occurred while adding the evaluated policy to device (" +
deviceId + " - " + policyId + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
}
@Override
public boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
boolean exist;
try {
Device device = deviceDAO.getDevice(deviceIdentifier);
exist = policyDAO.checkPolicyAvailable(device.getId());
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while checking whether device has a policy to apply.";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
return exist;
}
@Override
public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
try {
Device device = deviceDAO.getDevice(deviceIdentifier);
policyDAO.setPolicyApplied(device.getId());
return true;
} catch (PolicyManagerDAOException e) {
String msg = "Error occurred while setting the policy has applied to device (" +
deviceIdentifier.getId() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while getting the device details (" + deviceIdentifier.getId() + ")";
log.error(msg, e);
throw new PolicyManagementException(msg, e);
}
}
}

@ -64,13 +64,19 @@ public class PolicyManagementService implements PolicyManagerService {
return policyManagerService.updatePolicy(policy);
}
@Override
public boolean deletePolicy(Policy policy) throws PolicyManagementException {
return policyManagerService.deletePolicy(policy);
}
@Override
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
return policyManagerService.getEffectivePolicy(deviceIdentifier);
}
@Override
public Policy getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException {
public List<ProfileFeature> getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws
FeatureManagementException {
return policyManagerService.getEffectiveFeatures(deviceIdentifier);
}
@ -93,4 +99,9 @@ public class PolicyManagementService implements PolicyManagerService {
public PolicyInformationPoint getPIP() throws PolicyManagementException {
return policyManagerService.getPIP();
}
@Override
public PolicyEvaluationPoint getPEP() throws PolicyManagementException {
return policyManagerService.getPEP();
}
}

@ -21,10 +21,12 @@ package org.wso2.carbon.policy.mgt.core.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
import javax.sql.DataSource;
import javax.xml.parsers.DocumentBuilder;
@ -82,4 +84,23 @@ public class PolicyManagerUtil {
}
return dataSource;
}
public static int getTenantId() {
//TODO: Get the tenant id proper way. This is has to be fix for test to run.
int tenantId;
tenantId = MultitenantConstants.SUPER_TENANT_ID;
/* try {
if (PrivilegedCarbonContext.getThreadLocalCarbonContext() != null) {
tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
} else {
tenantId = MultitenantConstants.SUPER_TENANT_ID;
}
} catch (Exception e) {
}*/
return tenantId;
}
}

@ -235,7 +235,7 @@ public class PolicyDAOTestCase {
List<DeviceIdentifier> deviceIdentifierList = new ArrayList<DeviceIdentifier>();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(device.getDeviceIdentificationId());
deviceIdentifier.setType("ANDROID");
deviceIdentifier.setType("android");
deviceIdentifierList.add(deviceIdentifier);
policyManager.addPolicyToDevice(deviceIdentifierList, policy);
@ -266,7 +266,7 @@ public class PolicyDAOTestCase {
public void getDeviceTypeRelatedPolicy() throws PolicyManagementException {
PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl();
List<Policy> policyList = policyAdministratorPoint.getPoliciesOfDeviceType("ANDROID");
List<Policy> policyList = policyAdministratorPoint.getPoliciesOfDeviceType("android");
log.debug("----------Device type related policy---------");

@ -25,7 +25,7 @@ public class DeviceTypeCreator {
public static DeviceType getDeviceType(){
DeviceType deviceType = new DeviceType();
deviceType.setName("ANDROID");
deviceType.setName("android");
deviceType.setId(1);
return deviceType;

@ -32,7 +32,7 @@ public class ProfileCreator {
DeviceType deviceType = new DeviceType();
deviceType.setId(1);
deviceType.setName("ANDROID");
deviceType.setName("android");
profile.setProfileFeaturesList(ProfileFeatureCreator.getProfileFeature(features));
profile.setProfileName("Test Profile");

@ -142,7 +142,7 @@ CREATE TABLE IF NOT EXISTS DM_FEATURES (
CODE VARCHAR(45) NULL DEFAULT NULL ,
DEVICE_TYPE_ID INT NOT NULL ,
DESCRIPTION TEXT NULL DEFAULT NULL ,
EVALUVATION_RULE VARCHAR(60) NOT NULL ,
EVALUATION_RULE VARCHAR(60) NOT NULL ,
PRIMARY KEY (ID) ,
CONSTRAINT DM_FEATURES_DEVICE_TYPE
FOREIGN KEY (DEVICE_TYPE_ID )

@ -154,7 +154,7 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_FEATURES` (
`CODE` VARCHAR(45) NULL DEFAULT NULL ,
`DEVICE_TYPE_ID` INT NOT NULL ,
`DESCRIPTION` TEXT NULL DEFAULT NULL ,
`EVALUVATION_RULE` VARCHAR(60) NOT NULL ,
`EVALUATION_RULE` VARCHAR(60) NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `DM_FEATURES_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC) ,
CONSTRAINT `DM_FEATURES_DEVICE_TYPE`
@ -286,6 +286,36 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_USER_POLICY` (
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `WSO2CDM`.`DM_DEVICE_POLICY_APPLIED`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `WSO2CDM`.`DM_DEVICE_POLICY_APPLIED` ;
CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_DEVICE_POLICY_APPLIED` (
`ID` INT NOT NULL AUTO_INCREMENT ,
`DEVICE_ID` INT NOT NULL ,
`POLICY_ID` INT NOT NULL ,
`POLICY_CONTENT` BLOB NULL ,
`APPLIED` TINYINT(1) NULL ,
`CREATED_TIME` TIMESTAMP NULL ,
`UPDATED_TIME` TIMESTAMP NULL ,
`APPLIED_TIME` TIMESTAMP NULL ,
PRIMARY KEY (`ID`) ,
INDEX `FK_DM_POLICY_DEVCIE_APPLIED` (`DEVICE_ID` ASC) ,
INDEX `FK_DM_POLICY_DEVICE_APPLIED_POLICY` (`POLICY_ID` ASC) ,
CONSTRAINT `FK_DM_POLICY_DEVCIE_APPLIED`
FOREIGN KEY (`DEVICE_ID` )
REFERENCES `WSO2CDM`.`DM_DEVICE` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `FK_DM_POLICY_DEVICE_APPLIED_POLICY`
FOREIGN KEY (`POLICY_ID` )
REFERENCES `WSO2CDM`.`DM_POLICY` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;

@ -19,10 +19,10 @@
<DeviceMgtTestDBConfigurations>
<DBType typeName="MySql">
<connectionurl>jdbc:mysql://10.100.0.47:3306/WSO2CDM</connectionurl>
<connectionurl>jdbc:mysql://localhost:3306/WSO2CDM</connectionurl>
<driverclass>com.mysql.jdbc.Driver</driverclass>
<userName>root</userName>
<pwd>root</pwd>
<pwd></pwd>
</DBType>
<DBType typeName="H2">
<connectionurl>jdbc:h2:mem:WSO2_TEST_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000</connectionurl>

@ -35,7 +35,7 @@ public class PolicyEvaluationServiceImpl implements PolicyEvaluationPoint {
}
@Override
public Policy getEffectivePolicies(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
return evaluation.getEffectivePolicy(deviceIdentifier);
}

@ -25,7 +25,7 @@ import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
public interface SimpleEvaluation {
void sortPolicy() throws PolicyEvaluationException;
void sortPolicies() throws PolicyEvaluationException;
Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException;

@ -21,47 +21,62 @@ 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.common.PolicyEvaluationException;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.common.*;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.simple.policy.decision.point.internal.PolicyDecisionPointDataHolder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SimpleEvaluationImpl implements SimpleEvaluation {
private static final Log log = LogFactory.getLog(SimpleEvaluationImpl.class);
//TODO : to revove the stale reference
private PolicyManagerService policyManagerService;
private List<Policy> policyList;
private List<Policy> policyList = new ArrayList<Policy>();
public SimpleEvaluationImpl() {
policyManagerService = PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService();
}
// public SimpleEvaluationImpl() {
// policyManagerService = PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService();
// }
@Override
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
Policy policy = new Policy();
PolicyAdministratorPoint policyAdministratorPoint;
PolicyInformationPoint policyInformationPoint;
policyManagerService = getPolicyManagerService();
try {
if (policyManagerService == null && policyList == null) {
PIPDevice pipDevice = policyManagerService.getPIP().getDeviceData(deviceIdentifier);
policyList = policyManagerService.getPIP().getRelatedPolicies(pipDevice);
if (policyManagerService != null) {
policyInformationPoint = policyManagerService.getPIP();
PIPDevice pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier);
policyList = policyInformationPoint.getRelatedPolicies(pipDevice);
sortPolicies();
policy = policyList.get(0);
policyAdministratorPoint = policyManagerService.getPAP();
policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy);
}
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);
return policy;
}
@Override
public void sortPolicy() throws PolicyEvaluationException {
public void sortPolicies() throws PolicyEvaluationException {
Collections.sort(policyList);
}
private PolicyManagerService getPolicyManagerService(){
return PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService();
}
}

Loading…
Cancel
Save