diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java index 8e12a2ab9a..1ba3bea1f2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/ConfigOperation.java @@ -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; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 241b73858b..5000bd56a6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -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; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDevice.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDevice.java index 2605895a2c..2bef8e1497 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDevice.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PIPDevice.java @@ -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 userIds; - String roles []; - String altitude; - String longitude; - Timestamp timestamp; + private Device device; + private DeviceType deviceType; + private DeviceIdentifier deviceIdentifier; + private String ownershipType; + private List 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 attributes; 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 82d8601011..714bceecab 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 @@ -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 { +public class Policy implements Comparable, 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 roleList; // Roles which this policy should be applied. @@ -57,6 +57,7 @@ public class Policy implements Comparable { 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*/ diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index dd29e6bf57..da9ca06add 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -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 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; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java index 0ee56812fe..ef4f84988d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java @@ -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 getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException ; + List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/resources/mysql.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/resources/mysql.sql index 9102ed00c1..bd85b06e14 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/resources/mysql.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/resources/mysql.sql @@ -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; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml index 4cba9ee01b..bd87dba8f4 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml @@ -119,6 +119,10 @@ org.wso2.carbon org.wso2.carbon.core + + org.wso2.carbon + org.wso2.carbon.base + org.wso2.carbon org.wso2.carbon.ndatasource.core diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java index b38a5b2c01..c4c5c7f0eb 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java @@ -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,15 +38,19 @@ public interface PolicyManagerService { Policy updatePolicy(Policy policy) throws PolicyManagementException; - Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; + boolean deletePolicy(Policy policy) throws PolicyManagementException; - Policy getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException; + Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; + + List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException; List getPolicies(String deviceType) throws PolicyManagementException; List getFeatures() throws FeatureManagementException; - PolicyAdministratorPoint getPAP() throws PolicyManagementException; + PolicyAdministratorPoint getPAP() throws PolicyManagementException; + + PolicyInformationPoint getPIP() throws PolicyManagementException; - PolicyInformationPoint getPIP() throws PolicyManagementException; + PolicyEvaluationPoint getPEP() throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index 39065e764e..f1e67cd26a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -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 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 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(); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java index 3f2f130a83..79a0e4f826 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java @@ -38,9 +38,11 @@ public interface FeatureDAO { ProfileFeature updateProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException; - List addProfileFeatures(List features, int profileId) throws FeatureManagerDAOException; + List addProfileFeatures(List features, int profileId) throws + FeatureManagerDAOException; - List updateProfileFeatures(List features, int profileId) throws FeatureManagerDAOException; + List updateProfileFeatures(List features, int profileId) throws + FeatureManagerDAOException; List getAllFeatures() throws FeatureManagerDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index 5a63fdaf12..604f201543 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -73,4 +73,13 @@ public interface PolicyDAO { PolicyLocations getLocationsOfPolicy(Policy policy) throws PolicyManagerDAOException; + void addEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) + throws PolicyManagerDAOException; + + void setPolicyApplied(int deviceId) throws PolicyManagerDAOException; + + void updateEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) + throws PolicyManagerDAOException; + + boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index ed740f4ab6..59485bd155 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -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); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 21f29a8c62..a475279e99 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -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 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 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 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 (?, ?, ?, ?)"; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index 5d8ced9d23..f12400b0fb 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -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(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index fc2b6bb4d5..f4dad99f00 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -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 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 diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java index 132679db9e..12cd31c126 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java @@ -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(); + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java index ccd6d42508..fcf9247d03 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java @@ -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) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index e11321d993..f7640093a3 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -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 deviceIdentifierList, Policy policy) throws PolicyManagementException; + Policy addPolicyToDevice(List deviceIdentifierList, Policy policy) throws + PolicyManagementException; Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagementException; @@ -54,4 +56,11 @@ public interface PolicyManager { List getPoliciesOfUser(String username) throws PolicyManagementException; List getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException; + + void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, int policyId, List profileFeatures) throws + PolicyManagementException; + + boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; + + boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index b37c50ff9a..89cd3eefae 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -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 policies = new ArrayList(); try { - DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); - List profileList = profileDAO.getProfilesOfDeviceType(deviceType); +// DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); + + List profileList = profileManager.getProfilesOfDeviceType(deviceTypeName); List 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 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); + } + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java index 434c75b2d3..511b4db8b8 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java @@ -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 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(); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 54ef4aa79d..08acd03c79 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -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; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index 12312d12de..7e0621e89a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -235,7 +235,7 @@ public class PolicyDAOTestCase { List deviceIdentifierList = new ArrayList(); 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 policyList = policyAdministratorPoint.getPoliciesOfDeviceType("ANDROID"); + List policyList = policyAdministratorPoint.getPoliciesOfDeviceType("android"); log.debug("----------Device type related policy---------"); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java index e8e2188ddd..5dc9e981e2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java @@ -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; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java index 0095f8d987..332e464422 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java @@ -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"); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index d6eb8f0a73..9762f4d8a2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -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 ) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql index e0959de557..e5aa29f0bf 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql @@ -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; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml index 6241a0aefe..936272480e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml @@ -19,10 +19,10 @@ - jdbc:mysql://10.100.0.47:3306/WSO2CDM + jdbc:mysql://localhost:3306/WSO2CDM com.mysql.jdbc.Driver root - root + jdbc:h2:mem:WSO2_TEST_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000 diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java index 4fbacba173..f77c792188 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java @@ -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); } diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java index 2019f3f632..fb96100a06 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java @@ -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; diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java index 8359768e5c..bd15caec36 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java @@ -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 policyList; + private List policyList = new ArrayList(); - 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(); + } }