From 5831d277daa6d8283e9228e41e372b79252cc5be Mon Sep 17 00:00:00 2001 From: warunalakshitha Date: Mon, 27 Mar 2017 11:48:30 +0530 Subject: [PATCH] Fixing https://github.com/wso2/carbon-device-mgt-plugins/issues/613 --- .../mgt/core/enforcement/DelegationTask.java | 7 +- .../PolicyEnforcementDelegatorImpl.java | 36 +++++++++- .../policy/mgt/core/mgt/PolicyManager.java | 3 +- .../mgt/bean/UpdatedPolicyDeviceListBean.java | 65 +++++++++++++++++++ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 9 +-- 5 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/bean/UpdatedPolicyDeviceListBean.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java index dea10f5fac2..adccce97230 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java @@ -29,6 +29,7 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; +import org.wso2.carbon.policy.mgt.core.mgt.bean.UpdatedPolicyDeviceListBean; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import java.util.ArrayList; @@ -54,7 +55,8 @@ public class DelegationTask implements Task { try { PolicyManager policyManager = new PolicyManagerImpl(); - List deviceTypes = policyManager.applyChangesMadeToPolicies(); + UpdatedPolicyDeviceListBean updatedPolicyDeviceList = policyManager.applyChangesMadeToPolicies(); + List deviceTypes = updatedPolicyDeviceList.getChangedDeviceTypes(); PolicyCacheManagerImpl.getInstance().rePopulateCache(); @@ -78,7 +80,8 @@ public class DelegationTask implements Task { // } } if (!toBeNotified.isEmpty()) { - PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(toBeNotified); + PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl + (toBeNotified, updatedPolicyDeviceList.getUpdatedPolicyIds()); enforcementDelegator.delegate(); } } catch (DeviceManagementException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java index 9a3bcfa8948..fa02314460f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java @@ -44,8 +44,9 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato private static final Log log = LogFactory.getLog(PolicyEnforcementDelegatorImpl.class); private List devices; + private List updatedPolicyIds; - public PolicyEnforcementDelegatorImpl(List devices) { + public PolicyEnforcementDelegatorImpl(List devices, List updatedPolicyIds) { log.info("Policy re-enforcing stared due to change of the policies."); @@ -56,6 +57,7 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato } } this.devices = devices; + this.updatedPolicyIds = updatedPolicyIds; } @@ -66,12 +68,22 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato identifier.setId(device.getDeviceIdentifier()); identifier.setType(device.getType()); + Policy devicePolicy = this.getAppliedPolicyToDevice(identifier); Policy policy = this.getEffectivePolicy(identifier); List deviceIdentifiers = new ArrayList<>(); deviceIdentifiers.add(identifier); if (policy != null) { - this.addPolicyRevokeOperation(deviceIdentifiers); - this.addPolicyOperation(deviceIdentifiers, policy); + /* + We add policy operation for the device if, + 1) Device does not have any policy or + 2) New Policy or + 3) Device existing policy has changed + */ + if (devicePolicy == null || devicePolicy.getId() != policy.getId() || updatedPolicyIds.contains + (policy.getId())) { + this.addPolicyRevokeOperation(deviceIdentifiers); + this.addPolicyOperation(deviceIdentifiers, policy); + } } else { //This means all the applicable policies have been removed from device. Hence calling a policy revoke. this.addPolicyRevokeOperation(deviceIdentifiers); @@ -154,4 +166,22 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato policyRevokeOperation.setType(Operation.Type.COMMAND); return policyRevokeOperation; } + + /** + * Provides the applied policy for give device + * + * @param identifier Device Identifier + * @return Applied Policy + * @throws PolicyDelegationException exception throws when retrieving applied policy for given device + */ + public Policy getAppliedPolicyToDevice(DeviceIdentifier identifier) throws PolicyDelegationException { + try { + PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); + return policyManagerService.getAppliedPolicyToDevice(identifier); + } catch (PolicyManagementException e) { + String msg = "Error occurred while retrieving the applied policy for devices."; + log.error(msg, e); + throw new PolicyDelegationException(msg, e); + } + } } 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 8f18a89ffd4..68c57b5049f 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 @@ -21,6 +21,7 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.core.mgt.bean.UpdatedPolicyDeviceListBean; import java.util.HashMap; import java.util.List; @@ -67,7 +68,7 @@ public interface PolicyManager { void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; - List applyChangesMadeToPolicies() throws PolicyManagementException; + UpdatedPolicyDeviceListBean applyChangesMadeToPolicies() throws PolicyManagementException; void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/bean/UpdatedPolicyDeviceListBean.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/bean/UpdatedPolicyDeviceListBean.java new file mode 100644 index 00000000000..2071d1ff5bd --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/bean/UpdatedPolicyDeviceListBean.java @@ -0,0 +1,65 @@ +/* +* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ +package org.wso2.carbon.policy.mgt.core.mgt.bean; + +import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; +import org.wso2.carbon.device.mgt.core.dto.DeviceType; + + +import java.util.List; + +/** + * This class stores list of updated policies and list of changed devices for Policy Manager + */ +public class UpdatedPolicyDeviceListBean { + + private List updatedPolicies; + private List updatedPolicyIds; + private List changedDeviceTypes; + + public UpdatedPolicyDeviceListBean(List updatedPolicies, List updatedPolicyIds, List + deviceTypes) { + this.updatedPolicies = updatedPolicies; + this.updatedPolicyIds = updatedPolicyIds; + this.changedDeviceTypes = deviceTypes; + } + + public List getUpdatedPolicies() { + return updatedPolicies; + } + + public void setUpdatedPolicies(List updatedPolicies) { + this.updatedPolicies = updatedPolicies; + } + + public List getUpdatedPolicyIds() { + return updatedPolicyIds; + } + + public void setUpdatedPolicyIds(List updatedPolicyIds) { + this.updatedPolicyIds = updatedPolicyIds; + } + + public List getChangedDeviceTypes() { + return changedDeviceTypes; + } + + public void setChangedDeviceTypes(List changedDeviceTypes) { + this.changedDeviceTypes = changedDeviceTypes; + } +} 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 3614f756970..b8c0efd98f1 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 @@ -39,6 +39,7 @@ import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl; 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 org.wso2.carbon.policy.mgt.core.mgt.bean.UpdatedPolicyDeviceListBean; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.sql.SQLException; @@ -830,15 +831,15 @@ public class PolicyManagerImpl implements PolicyManager { } @Override - public List applyChangesMadeToPolicies() throws PolicyManagementException { + public UpdatedPolicyDeviceListBean applyChangesMadeToPolicies() throws PolicyManagementException { List changedDeviceTypes = new ArrayList<>(); + List updatedPolicies = new ArrayList<>(); + List updatedPolicyIds = new ArrayList<>(); try { //HashMap map = policyDAO.getUpdatedPolicyIdandDeviceTypeId(); - List updatedPolicies = new ArrayList<>(); // List activePolicies = new ArrayList<>(); // List inactivePolicies = new ArrayList<>(); - List updatedPolicyIds = new ArrayList<>(); // List allPolicies = this.getPolicies(); List allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies(); @@ -867,7 +868,7 @@ public class PolicyManagerImpl implements PolicyManager { } finally { PolicyManagementDAOFactory.closeConnection(); } - return changedDeviceTypes; + return new UpdatedPolicyDeviceListBean(updatedPolicies, updatedPolicyIds, changedDeviceTypes); }