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.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..9fb7cbdb24 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(). + getEffectivePolicies(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/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/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..120937a0fe 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 @@ -20,23 +20,22 @@ 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.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.ProfileFeature; 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 +199,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 +270,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 +303,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 +337,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 +435,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 +469,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 +504,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 +523,116 @@ 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.executeQuery(); + + } 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.executeQuery(); + + } 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.executeQuery(); + + } 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 +823,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 43a4e34df3..b578f5580f 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..75b212a389 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,7 +59,6 @@ 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()); 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..cb227200ef 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 @@ -40,12 +40,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" @@ -108,7 +102,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 +114,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..63ddc5ff0a 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 @@ -30,6 +30,7 @@ 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.ProfileFeature; import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; @@ -565,4 +566,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.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..7e8a4e50e7 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,10 +21,7 @@ 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; @@ -43,20 +40,31 @@ public class SimpleEvaluationImpl implements SimpleEvaluation { @Override public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { + Policy policy = new Policy(); + PolicyAdministratorPoint policyAdministratorPoint; + PolicyInformationPoint policyInformationPoint; try { - if (policyManagerService == null && policyList == null) { - PIPDevice pipDevice = policyManagerService.getPIP().getDeviceData(deviceIdentifier); - policyList = policyManagerService.getPIP().getRelatedPolicies(pipDevice); + if (policyManagerService != null && policyList == null) { + + policyInformationPoint = policyManagerService.getPIP(); + PIPDevice pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier); + policyList = policyInformationPoint.getRelatedPolicies(pipDevice); + + sortPolicy(); + 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; }