warunalakshitha 8 years ago
parent 52455b4c40
commit 5831d277da

@ -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<String> deviceTypes = policyManager.applyChangesMadeToPolicies();
UpdatedPolicyDeviceListBean updatedPolicyDeviceList = policyManager.applyChangesMadeToPolicies();
List<String> 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) {

@ -44,8 +44,9 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
private static final Log log = LogFactory.getLog(PolicyEnforcementDelegatorImpl.class);
private List<Device> devices;
private List<Integer> updatedPolicyIds;
public PolicyEnforcementDelegatorImpl(List<Device> devices) {
public PolicyEnforcementDelegatorImpl(List<Device> devices, List<Integer> 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<DeviceIdentifier> 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);
}
}
}

@ -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<String> applyChangesMadeToPolicies() throws PolicyManagementException;
UpdatedPolicyDeviceListBean applyChangesMadeToPolicies() throws PolicyManagementException;
void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException;

@ -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<Policy> updatedPolicies;
private List<Integer> updatedPolicyIds;
private List<String> changedDeviceTypes;
public UpdatedPolicyDeviceListBean(List<Policy> updatedPolicies, List<Integer> updatedPolicyIds, List<String>
deviceTypes) {
this.updatedPolicies = updatedPolicies;
this.updatedPolicyIds = updatedPolicyIds;
this.changedDeviceTypes = deviceTypes;
}
public List<Policy> getUpdatedPolicies() {
return updatedPolicies;
}
public void setUpdatedPolicies(List<Policy> updatedPolicies) {
this.updatedPolicies = updatedPolicies;
}
public List<Integer> getUpdatedPolicyIds() {
return updatedPolicyIds;
}
public void setUpdatedPolicyIds(List<Integer> updatedPolicyIds) {
this.updatedPolicyIds = updatedPolicyIds;
}
public List<String> getChangedDeviceTypes() {
return changedDeviceTypes;
}
public void setChangedDeviceTypes(List<String> changedDeviceTypes) {
this.changedDeviceTypes = changedDeviceTypes;
}
}

@ -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<String> applyChangesMadeToPolicies() throws PolicyManagementException {
public UpdatedPolicyDeviceListBean applyChangesMadeToPolicies() throws PolicyManagementException {
List<String> changedDeviceTypes = new ArrayList<>();
List<Policy> updatedPolicies = new ArrayList<>();
List<Integer> updatedPolicyIds = new ArrayList<>();
try {
//HashMap<Integer, Integer> map = policyDAO.getUpdatedPolicyIdandDeviceTypeId();
List<Policy> updatedPolicies = new ArrayList<>();
// List<Policy> activePolicies = new ArrayList<>();
// List<Policy> inactivePolicies = new ArrayList<>();
List<Integer> updatedPolicyIds = new ArrayList<>();
// List<Policy> allPolicies = this.getPolicies();
List<Policy> allPolicies = PolicyCacheManagerImpl.getInstance().getAllPolicies();
@ -867,7 +868,7 @@ public class PolicyManagerImpl implements PolicyManager {
} finally {
PolicyManagementDAOFactory.closeConnection();
}
return changedDeviceTypes;
return new UpdatedPolicyDeviceListBean(updatedPolicies, updatedPolicyIds, changedDeviceTypes);
}

Loading…
Cancel
Save