From 548ce6873fcb20f593f582c3f68a7bc6f1bde09d Mon Sep 17 00:00:00 2001 From: geethkokila Date: Thu, 21 May 2015 17:27:17 +0530 Subject: [PATCH] Changing the get effective policy method to store the policy as a operation, and adding ownership type to the policy database --- .../org.wso2.carbon.policy.mgt.common/pom.xml | 1 + .../mgt/core/PolicyManagerServiceImpl.java | 77 ++++++++++++++++--- .../mgt/core/dao/impl/PolicyDAOImpl.java | 4 +- .../core/util/PolicyManagementConstants.java | 1 + .../policy/mgt/core/util/PolicyCreator.java | 5 ++ .../src/test/resources/sql/CreateH2TestDB.sql | 1 + .../point/PolicyEvaluationServiceImpl.java | 18 ++++- .../src/main/resources/dbscripts/cdm/h2.sql | 1 + 8 files changed, 95 insertions(+), 13 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml index f63a2aafe6..86e3b3e3a0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/pom.xml @@ -57,6 +57,7 @@ org.apache.commons.logging, org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.device.mgt.core.dto.*, + org.wso2.carbon.device.mgt.core.operation.*, javax.xml.bind.*, 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 522e00eee7..d05dd69bb7 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 @@ -22,19 +22,15 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.Feature; -import org.wso2.carbon.policy.mgt.common.FeatureManagementException; -import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; -import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; -import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; -import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; -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.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; +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 org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; +import java.util.ArrayList; import java.util.List; public class PolicyManagerServiceImpl implements PolicyManagerService { @@ -85,13 +81,45 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { @Override public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { try { - return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(). + + + Policy policy = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(). getEffectivePolicy(deviceIdentifier); + + List effectiveFeatures =policy.getProfile().getProfileFeaturesList(); + + PolicyOperation policyOperation = new PolicyOperation(); + + List profileOperationList = new ArrayList(); + for (ProfileFeature feature : effectiveFeatures) { + ProfileOperation operation = new ProfileOperation(); + + operation.setCode(feature.getFeatureCode()); + operation.setPayLoad(feature.getContent()); + profileOperationList.add(operation); + } + policyOperation.setProfileOperations(profileOperationList); + policyOperation.setCode(PolicyManagementConstants.POLICY_BUNDLE); + + List deviceIdentifiers = new ArrayList(); + deviceIdentifiers.add(deviceIdentifier); + + PolicyManagementDataHolder.getInstance().getDeviceManagementService(). + addOperation(policyOperation, deviceIdentifiers); + + + + return policy; } 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); + } catch (OperationManagementException e) { + String msg = "Error occurred while adding the effective feature to database." + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyManagementException(msg, e); } } @@ -99,13 +127,40 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException { try { - return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(). + + List effectiveFeatures = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(). getEffectiveFeatures(deviceIdentifier); + + PolicyOperation policyOperation = new PolicyOperation(); + + List profileOperationList = new ArrayList(); + for (ProfileFeature feature : effectiveFeatures) { + ProfileOperation operation = new ProfileOperation(); + + operation.setCode(feature.getFeatureCode()); + operation.setPayLoad(feature.getContent()); + profileOperationList.add(operation); + } + policyOperation.setProfileOperations(profileOperationList); + policyOperation.setCode(PolicyManagementConstants.POLICY_BUNDLE); + + List deviceIdentifiers = new ArrayList(); + deviceIdentifiers.add(deviceIdentifier); + + PolicyManagementDataHolder.getInstance().getDeviceManagementService(). + addOperation(policyOperation, deviceIdentifiers); + + return effectiveFeatures; } 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); + } catch (OperationManagementException e) { + String msg = "Error occurred while adding the effective feature to database." + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); + log.error(msg, e); + throw new FeatureManagementException(msg, e); } } 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 604403950d..156bceeeb8 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 @@ -670,6 +670,7 @@ public class PolicyDAOImpl implements PolicyDAO { policy.setTenantId(resultSet.getInt("TENANT_ID")); policy.setPriorityId(resultSet.getInt("PRIORITY")); policy.setCompliance(resultSet.getString("COMPLIANCE")); + policy.setOwnershipType(resultSet.getString("OWNERSHIP_TYPE")); policies.add(policy); } return policies; @@ -1157,7 +1158,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE) VALUES (?, ?, ?, ?, ?)"; + String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE) VALUES (?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setString(1, policy.getPolicyName()); @@ -1165,6 +1166,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(3, tenantId); stmt.setInt(4, readHighestPriorityOfPolicies()); stmt.setString(5, policy.getCompliance()); + stmt.setString(6, policy.getOwnershipType()); int affectedRows = stmt.executeUpdate(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java index c8c26213b2..fa390ffdb2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -22,5 +22,6 @@ public final class PolicyManagementConstants { public static final String DEVICE_CONFIG_XML_NAME = "cdm-config.xml"; public static final String ANY = "ANY"; + public static final String POLICY_BUNDLE = "POLICY_BUNDLE"; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java index cf68ff1600..24d0e2d444 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/PolicyCreator.java @@ -38,6 +38,7 @@ public class PolicyCreator { users.add("Dilshan"); policy.setUsers(users); policy.setCompliance("ENFORCE"); + policy.setOwnershipType("COPE"); return policy; } @@ -83,6 +84,8 @@ public class PolicyCreator { criteria.add(criterion); + policy.setOwnershipType("COPE"); + policy.setPolicyCriterias(criteria); // // policy.setLatitude("6.927079"); @@ -111,6 +114,7 @@ public class PolicyCreator { policy.setRoles(roles); policy.setCompliance("ENFORCE"); + policy.setOwnershipType("BYOD"); // List users = new ArrayList(); // users.add("Geeth"); @@ -155,6 +159,7 @@ public class PolicyCreator { policy.setDevices(DeviceCreator.getDeviceList(DeviceTypeCreator.getDeviceType())); policy.setCompliance("MONITOR"); + policy.setOwnershipType("BYOD"); List roles = new ArrayList(); roles.add("Role_04"); 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 ca2409c8e6..7f291415de 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 @@ -72,6 +72,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( NAME VARCHAR(45) NULL DEFAULT NULL , TENANT_ID INT(11) NOT NULL , PROFILE_ID INT(11) NOT NULL , + OWNERSHIP_TYPE VARCHAR(45) NULL, COMPLIANCE VARCHAR(100) NULL, PRIORITY INT NOT NULL , PRIMARY KEY (ID) , 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 f77c792188..f6fbd82d69 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 @@ -41,6 +41,22 @@ public class PolicyEvaluationServiceImpl implements PolicyEvaluationPoint { @Override public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { - return evaluation.getEffectivePolicy(deviceIdentifier).getProfile().getProfileFeaturesList(); + + List effectiveFeatures = evaluation.getEffectivePolicy(deviceIdentifier). + getProfile().getProfileFeaturesList(); + +/* PolicyOperation policyOperation = new PolicyOperation(); + + List profileOperationList = new ArrayList(); + for (ProfileFeature feature : effectiveFeatures) { + ProfileOperation operation = new ProfileOperation(); + + operation.setCode(feature.getFeatureCode()); + operation.setPayLoad(feature.getContent()); + profileOperationList.add(operation); + } + policyOperation.setProfileOperations(profileOperationList); + policyOperation.setCode(PolicyManagementConstants.POLICY_BUNDLE);*/ + return effectiveFeatures; } } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 25ef125323..bc58da9eac 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -96,6 +96,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( NAME VARCHAR(45) NULL DEFAULT NULL , TENANT_ID INT(11) NOT NULL , PROFILE_ID INT(11) NOT NULL , + OWNERSHIP_TYPE VARCHAR(45) NULL, COMPLIANCE VARCHAR(100) NULL, PRIORITY INT NOT NULL , PRIMARY KEY (ID) ,