|
|
|
@ -23,14 +23,18 @@ import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.InvalidDeviceException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants;
|
|
|
|
|
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.core.PolicyManagerService;
|
|
|
|
|
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl;
|
|
|
|
|
import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder;
|
|
|
|
|
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
@ -58,7 +62,6 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void delegate() throws PolicyDelegationException {
|
|
|
|
|
|
|
|
|
|
for (Device device : devices) {
|
|
|
|
|
DeviceIdentifier identifier = new DeviceIdentifier();
|
|
|
|
|
identifier.setId(device.getDeviceIdentifier());
|
|
|
|
@ -70,14 +73,17 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
|
|
|
|
|
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
|
|
|
|
|
deviceIdentifiers.add(identifier);
|
|
|
|
|
this.addPolicyOperation(deviceIdentifiers, policy);
|
|
|
|
|
} else {
|
|
|
|
|
//This means all the applicable policies have been removed. Hence sending policy-revoke operation.
|
|
|
|
|
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
|
|
|
|
|
deviceIdentifiers.add(identifier);
|
|
|
|
|
this.addPolicyRevokeOperation(deviceIdentifiers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Policy getEffectivePolicy(DeviceIdentifier identifier) throws PolicyDelegationException {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl();
|
|
|
|
|
return policyManagerService.getPEP().getEffectivePolicy(identifier);
|
|
|
|
@ -96,11 +102,13 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
|
|
|
|
|
@Override
|
|
|
|
|
public void addPolicyOperation(List<DeviceIdentifier> deviceIdentifiers, Policy policy) throws
|
|
|
|
|
PolicyDelegationException {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
//ToDo Need to fix this to fetch OSGi service
|
|
|
|
|
OperationManager operationManager = new OperationManagerImpl();
|
|
|
|
|
operationManager.addOperation(PolicyManagerUtil.transformPolicy(policy), deviceIdentifiers);
|
|
|
|
|
String type = null;
|
|
|
|
|
if (deviceIdentifiers.size() > 0) {
|
|
|
|
|
type = deviceIdentifiers.get(0).getType();
|
|
|
|
|
}
|
|
|
|
|
PolicyManagementDataHolder.getInstance().getDeviceManagementService().addOperation(type,
|
|
|
|
|
PolicyManagerUtil.transformPolicy(policy), deviceIdentifiers);
|
|
|
|
|
} catch (InvalidDeviceException e) {
|
|
|
|
|
String msg = "Invalid DeviceIdentifiers found.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
@ -110,7 +118,33 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new PolicyDelegationException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addPolicyRevokeOperation(List<DeviceIdentifier> deviceIdentifiers) throws PolicyDelegationException {
|
|
|
|
|
try {
|
|
|
|
|
String type = null;
|
|
|
|
|
if (deviceIdentifiers.size() > 0) {
|
|
|
|
|
type = deviceIdentifiers.get(0).getType();
|
|
|
|
|
}
|
|
|
|
|
PolicyManagementDataHolder.getInstance().getDeviceManagementService().addOperation(type,
|
|
|
|
|
this.getPolicyRevokeOperation(), deviceIdentifiers);
|
|
|
|
|
} catch (InvalidDeviceException e) {
|
|
|
|
|
String msg = "Invalid DeviceIdentifiers found.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new PolicyDelegationException(msg, e);
|
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
|
String msg = "Error occurred while adding the operation to device.";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new PolicyDelegationException(msg, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Operation getPolicyRevokeOperation() {
|
|
|
|
|
CommandOperation policyRevokeOperation = new CommandOperation();
|
|
|
|
|
policyRevokeOperation.setEnabled(true);
|
|
|
|
|
policyRevokeOperation.setCode(OperationMgtConstants.OperationCodes.POLICY_REVOKE);
|
|
|
|
|
policyRevokeOperation.setType(Operation.Type.COMMAND);
|
|
|
|
|
return policyRevokeOperation;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|