diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java index 6a77938b0f..c4c3d60294 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/PolicyManagementService.java @@ -31,6 +31,7 @@ import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import io.swagger.annotations.ResponseHeader; +import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper; @@ -562,5 +563,9 @@ public interface PolicyManagementService { required = true) List priorityUpdatedPolicies); + @GET + @Path("/effective-policy/{deviceType}/{deviceId}") + @Permission(name = "Get Effective Policy of Devices", permission = "/device-mgt/policies/view") + Response getEffectivePolicy(@PathParam("deviceId") String deviceId, @PathParam("deviceType") String deviceType); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index 16e7a8eb06..73ed8523a2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -373,4 +373,29 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } } + @GET + @Path("/effective-policy/{deviceType}/{deviceId}") + @Override + public Response getEffectivePolicy(@PathParam("deviceId") String deviceId, @PathParam("deviceType") String deviceType) { + PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); + final org.wso2.carbon.policy.mgt.common.Policy policy; + try { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(deviceType); + policy = policyManagementService.getAppliedPolicyToDevice(deviceIdentifier); + if (policy == null) { + return Response.status(Response.Status.NOT_FOUND).entity( + new ErrorResponse.ErrorResponseBuilder().setMessage( + "No policy found for device ID '" + deviceId + "'"+ deviceId).build()).build(); + } + } catch (PolicyManagementException e) { + String msg = "Error occurred while retrieving policy corresponding to the id '" + deviceType + "'"+ deviceId; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + return Response.status(Response.Status.OK).entity(policy).build(); + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java index 7ce7000d81..8d5a8cc434 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java @@ -42,6 +42,5 @@ public class MonitoringOperation { this.recurrentTimes = recurrentTimes; } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationMonitoringTaskConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationMonitoringTaskConfig.java index ec50a02b6f..237516b26a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationMonitoringTaskConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationMonitoringTaskConfig.java @@ -52,6 +52,6 @@ public class OperationMonitoringTaskConfig { public void setMonitoringOperation(List monitoringOperation) { this.monitoringOperation = monitoringOperation; - } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index 9e2a81d5c3..ca32fa037b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -195,7 +195,6 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis } else { deviceTaskManagerService.startTask(deviceManagementService.getType(), operationMonitoringTaskConfig); - // TODO: In here a race condition can arise. Need to handle it. } } } catch (DeviceMgtTaskException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/policy/PolicyConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/policy/PolicyConfiguration.java index 73d3517d83..1f52150f3f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/policy/PolicyConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/policy/PolicyConfiguration.java @@ -33,6 +33,7 @@ public class PolicyConfiguration { private int minRetriesToMarkUnreachable; private int minRetriesToMarkInactive; private List platforms; + private String policyEvaluationPoint; @XmlElement(name = "MonitoringClass", required = true) public String getMonitoringClass() { @@ -98,4 +99,13 @@ public class PolicyConfiguration { this.platforms = platforms; } + @XmlElement(name = "PolicyEvaluationPoint", required = true) + public String getPolicyEvaluationPointName() { + return policyEvaluationPoint; + } + + public void setPolicyEvaluationPointName(String policyEvaluationPointName) { + this.policyEvaluationPoint = policyEvaluationPointName; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java index 7dab541321..344d567f0f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java @@ -58,11 +58,11 @@ public class DeviceTaskManagerServiceComponent { log.debug("Initializing device details retrieving task manager bundle."); } // This will start the device details retrieving task. -// DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl(); -// DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService( -// deviceTaskManagerService); -// componentContext.getBundleContext().registerService(DeviceTaskManagerService.class, -// deviceTaskManagerService, null); + // DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl(); + // DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService( + // deviceTaskManagerService); + // componentContext.getBundleContext().registerService(DeviceTaskManagerService.class, + // deviceTaskManagerService, null); getDeviceOperationMonitoringConfig(componentContext); @@ -76,12 +76,12 @@ public class DeviceTaskManagerServiceComponent { } } - private void getDeviceOperationMonitoringConfig(ComponentContext componentContext) throws DeviceMgtTaskException { + private void getDeviceOperationMonitoringConfig(ComponentContext componentContext) + throws DeviceMgtTaskException { DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl(); - DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService( - deviceTaskManagerService); + DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(deviceTaskManagerService); componentContext.getBundleContext().registerService(DeviceTaskManagerService.class, deviceTaskManagerService, null); @@ -89,11 +89,10 @@ public class DeviceTaskManagerServiceComponent { Map deviceConfigMap = DeviceMonitoringOperationDataHolder .getInstance().getOperationMonitoringConfigFromMap(); - for (String platformType : new ArrayList(deviceConfigMap.keySet())) { + for (String platformType : new ArrayList<>(deviceConfigMap.keySet())) { deviceTaskManagerService.startTask(platformType, deviceConfigMap.get(platformType)); deviceConfigMap.remove(platformType); } - } @SuppressWarnings("unused") diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 8ad213c0ae..521bcefdf6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1232,17 +1232,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getMonitoringOperationList(String deviceType) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// Map deviceManagementServiceMap = -// pluginRepository.getAllDeviceManagementServices(tenantId); + DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceType, tenantId); - // ; - // OperationMonitoringTaskConfig operationMonitoringTaskConfig; - //Map> deviceTypeSpecificMonitoringOperations = new HashMap<>(); -// for(DeviceTypeIdentifier dti : deviceManagementServiceMap.keySet()){ -// dms = deviceManagementServiceMap.get(dti); -// -// } OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getOperationMonitoringConfig(); return operationMonitoringTaskConfig.getMonitoringOperation(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java index 37a90f6f82..69e49b7874 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java @@ -19,8 +19,10 @@ package org.wso2.carbon.device.mgt.core.task.impl; +import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager; import org.wso2.carbon.ntask.core.Task; @@ -32,10 +34,18 @@ public class DeviceDetailsRetrieverTask implements Task { private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); // private DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(); private String deviceType; + private String oppConfig; + private OperationMonitoringTaskConfig operationMonitoringTaskConfig; @Override public void setProperties(Map map) { deviceType = map.get("DEVICE_TYPE"); + oppConfig = map.get("OPPCONFIG"); + + Gson gson = new Gson(); + + operationMonitoringTaskConfig = gson.fromJson(oppConfig, + OperationMonitoringTaskConfig.class); } @Override @@ -49,14 +59,18 @@ public class DeviceDetailsRetrieverTask implements Task { log.debug("Device details retrieving task started to run."); } - DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType); - + DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, + operationMonitoringTaskConfig); + //pass the configurations also from here, monitoring tasks try { deviceTaskManager.addOperations(); } catch (DeviceMgtTaskException e) { - log.error("Error occurred while trying to add the operations to device to retrieve device details.", e); + log.error( + "Error occurred while trying to add the operations to device to retrieve device details.", + e); } } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java index 2606f07fd3..16fff9ce13 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.MonitoringOperation; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; 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.core.internal.DeviceManagementDataHolder; @@ -46,30 +47,26 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { private static Log log = LogFactory.getLog(DeviceTaskManagerImpl.class); private String deviceType; private static Map> map = new HashMap<>(); + private OperationMonitoringTaskConfig operationMonitoringTaskConfig; + + public DeviceTaskManagerImpl(String deviceType, + OperationMonitoringTaskConfig operationMonitoringTaskConfig) { + this.operationMonitoringTaskConfig = operationMonitoringTaskConfig; + this.deviceType = deviceType; + } public DeviceTaskManagerImpl(String deviceType) { this.deviceType = deviceType; } //get device type specific operations - public List getOperationList() throws DeviceMgtTaskException { - - DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder - .getInstance(). - getDeviceManagementProvider(); - - return deviceManagementProviderService.getMonitoringOperationList( - deviceType);//Get task list from each device type + private List getOperationList() throws DeviceMgtTaskException { + return operationMonitoringTaskConfig.getMonitoringOperation(); } @Override public int getTaskFrequency() throws DeviceMgtTaskException { - DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder - .getInstance(). - getDeviceManagementProvider(); - - return deviceManagementProviderService.getDeviceMonitoringFrequency(deviceType); - + return operationMonitoringTaskConfig.getFrequency(); } // @Override @@ -80,11 +77,7 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { @Override public boolean isTaskEnabled() throws DeviceMgtTaskException { - DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder - .getInstance(). - getDeviceManagementProvider(); - - return deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType); + return operationMonitoringTaskConfig.isEnabled(); } @@ -149,13 +142,22 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { return opNames; } + private List getOperationListforTask() throws DeviceMgtTaskException { + + DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder + .getInstance(). + getDeviceManagementProvider(); + + return deviceManagementProviderService.getMonitoringOperationList( + deviceType);//Get task list from each device type + } @Override public boolean isTaskOperation(String opName) { try { - List monitoringOperations = this.getOperationList(); + List monitoringOperations = this.getOperationListforTask(); for (MonitoringOperation taop : monitoringOperations) { if (taop.getTaskName().equalsIgnoreCase(opName)) { return true; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java index 7a47f8d6bf..d23939f866 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.task.impl; +import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -46,9 +47,8 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { private static Log log = LogFactory.getLog(DeviceTaskManagerServiceImpl.class); @Override - public void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException { - -// String TASK_NAME = deviceType; + public void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + throws DeviceMgtTaskException { log.info("Task adding for " + deviceType); @@ -60,45 +60,52 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { if (log.isDebugEnabled()) { log.debug("Device details retrieving task is started for the tenant id " + tenantId); -// log.debug("Device details retrieving task is at frequency of : " + deviceTaskManager -// .getTaskFrequency()); - log.debug("Device details retrieving task is at frequency of : " + operationMonitoringTaskConfig - .getFrequency()); + // log.debug("Device details retrieving task is at frequency of : " + deviceTaskManager + // .getTaskFrequency()); + log.debug( + "Device details retrieving task is at frequency of : " + operationMonitoringTaskConfig + .getFrequency()); } TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); -// triggerInfo.setIntervalMillis(deviceTaskManager.getTaskFrequency()); + // triggerInfo.setIntervalMillis(deviceTaskManager.getTaskFrequency()); triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency()); triggerInfo.setRepeatCount(-1); + Gson gson = new Gson(); + String operationConfigs = gson.toJson(operationMonitoringTaskConfig); + Map properties = new HashMap<>(); + properties.put(TENANT_ID, String.valueOf(tenantId)); properties.put("DEVICE_TYPE", deviceType); + properties.put("OPPCONFIG", operationConfigs); + String taskName = deviceType + String.valueOf(tenantId); if (!taskManager.isTaskScheduled(deviceType)) { - TaskInfo taskInfo = new TaskInfo(deviceType, TASK_CLASS, properties, triggerInfo); + TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo); taskManager.registerTask(taskInfo); taskManager.rescheduleTask(taskInfo.getName()); } else { - throw new DeviceMgtTaskException("Device details retrieving task is already started for this tenant " + - tenantId); + throw new DeviceMgtTaskException( + "Device details retrieving task is already started for this tenant " + tenantId); } } catch (TaskException e) { - throw new DeviceMgtTaskException("Error occurred while creating the task for tenant " + tenantId, e); + throw new DeviceMgtTaskException("Error occurred while creating the task for tenant " + tenantId, + e); } } @Override - public void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException { - -// String TASK_NAME = deviceType; + public void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + throws DeviceMgtTaskException { try { TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); @@ -108,18 +115,18 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { } } catch (TaskException e) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - throw new DeviceMgtTaskException("Error occurred while deleting the task for tenant " + tenantId, e); + throw new DeviceMgtTaskException("Error occurred while deleting the task for tenant " + tenantId, + e); } } @Override - public void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException { - -// String TASK_NAME = deviceType; + public void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + throws DeviceMgtTaskException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// deviceTaskManager = new DeviceTaskManagerImpl(); + // deviceTaskManager = new DeviceTaskManagerImpl(); try { TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); @@ -134,21 +141,20 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { Map properties = new HashMap<>(); properties.put(TENANT_ID, String.valueOf(tenantId)); - TaskInfo taskInfo = new TaskInfo(deviceType, TASK_CLASS, properties, - triggerInfo); + TaskInfo taskInfo = new TaskInfo(deviceType, TASK_CLASS, properties, triggerInfo); taskManager.registerTask(taskInfo); taskManager.rescheduleTask(taskInfo.getName()); } else { - throw new DeviceMgtTaskException("Device details retrieving task has not been started for this tenant " + - tenantId + ". Please start the task first."); + throw new DeviceMgtTaskException( + "Device details retrieving task has not been started for this tenant " + + tenantId + ". Please start the task first."); } } catch (TaskException e) { - throw new DeviceMgtTaskException("Error occurred while updating the task for tenant " + tenantId, e); + throw new DeviceMgtTaskException("Error occurred while updating the task for tenant " + tenantId, + e); } - - } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js index 65c74e886c..9121f1d3a9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.devices/public/js/listing.js @@ -258,8 +258,10 @@ function loadDevices(searchType, searchParam) { data: 'name', class: 'remove-padding icon-only content-fill', render: function (data, type, row, meta) { - return '
'; + return '
'; } }, { @@ -333,22 +335,14 @@ function loadDevices(searchType, searchParam) { var deviceIdentifier = row.deviceIdentifier; var html = ''; if (status != 'REMOVED') { - html = - '' - + - ''; + html = ''; if (analyticsEnabled(row.deviceType)) { - html += - '' - + - '' - + - ''; + html += '' + + '' + + ''; } if ((!groupName || !groupId) && groupingEnabled(row.deviceType)) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.effective-policy.view/view.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.effective-policy.view/view.hbs new file mode 100644 index 0000000000..4fbf555f53 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.effective-policy.view/view.hbs @@ -0,0 +1,43 @@ +{{! + Copyright (c) 2016, 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. +}} + +{{unit "cdmf.unit.ui.title" pageTitle="Policy Management | View Policy"}} + +{{#zone "breadcrumbs"}} +
  • + + + +
  • +
  • + + + Policies + +
  • +
  • + + View + +
  • +{{/zone}} + +{{#zone "content"}} + {{unit "cdmf.unit.device.operation-mod"}} + {{unit "cdmf.unit.effective-policy.view"}} +{{/zone}} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.effective-policy.view/view.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.effective-policy.view/view.js new file mode 100644 index 0000000000..46b4ed70b7 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.effective-policy.view/view.js @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2016, 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. + */ + +function onRequest(context) { + var utility = require("/app/modules/utility.js")["utility"]; + var deviceType = context.uriParams.deviceType; + var deviceId = context.uriParams.deviceId; + return {"deviceTypePolicyView": utility.getTenantedDeviceUnitName(deviceType, deviceId,"policy-view")}; +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.effective-policy.view/view.json b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.effective-policy.view/view.json new file mode 100644 index 0000000000..ec79d2b203 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/pages/cdmf.page.effective-policy.view/view.json @@ -0,0 +1,5 @@ +{ + "version": "1.0.0", + "uri": "/policy/effective-policy/", + "layout": "cdmf.layout.default" +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/js/device-view.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/js/device-view.js index 0ca5ddc820..3672656fd4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/js/device-view.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/js/device-view.js @@ -16,7 +16,6 @@ * under the License. */ -(function () { var deviceId = $(".device-id"); var deviceIdentifier = deviceId.data("deviceid"); var deviceType = deviceId.data("type"); @@ -49,6 +48,7 @@ function loadOperationsLog(update) { var operationsLogTable = "#operations-log-table"; + if (update) { operationTable = $(operationsLogTable).DataTable(); $("#operations-spinner").removeClass("hidden"); @@ -163,6 +163,8 @@ var viewModel = {}; viewModel["policy"] = activePolicy; viewModel["deviceType"] = deviceType; + viewModel["deviceId"] = deviceId; + viewModel["appContext"] = context; data = JSON.parse(data); var content; if (data["complianceData"]) { @@ -206,5 +208,3 @@ } ); } - -}()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/templates/policy-compliance.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/templates/policy-compliance.hbs index 3313320afd..198741bc57 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/templates/policy-compliance.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.device.view/public/templates/policy-compliance.hbs @@ -37,7 +37,7 @@
    - diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/public/js/view.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/public/js/view.js new file mode 100644 index 0000000000..a41d89fa1b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/public/js/view.js @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2015, 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. + */ + +var policy = {}; +var displayPolicy = function (policyPayloadObj) { + policy["name"] = policyPayloadObj["policyName"]; + policy["platform"] = policyPayloadObj["profile"]["deviceType"]; + // updating next-page wizard title with selected platform + $("#policy-heading").text(policy["platform"].toUpperCase() + " POLICY - " + policy["name"].toUpperCase()); + $("#policy-platform").text(policy["platform"].toUpperCase()); + $("#policy-assignment").text(policyPayloadObj.deviceGroups); + $("#policy-action").text(policyPayloadObj.compliance.toUpperCase()); + $("#policy-description").text(policyPayloadObj["description"]); + var policyStatus = "Active"; + if (policyPayloadObj["active"] == true && policyPayloadObj["updated"] == true) { + policyStatus = ' Active/Updated'; + } else if (policyPayloadObj["active"] == true && policyPayloadObj["updated"] == false) { + policyStatus = ' Active'; + } else if (policyPayloadObj["active"] == false && policyPayloadObj["updated"] == true) { + policyStatus = ' Inactive/Updated'; + } else if (policyPayloadObj["active"] == false && policyPayloadObj["updated"] == false) { + policyStatus = ' Inactive'; + } + + $("#policy-status").html(policyStatus); + + if (policyPayloadObj.users == null) { + $("#policy-users").text("NONE"); + } + else if (policyPayloadObj.users.length > 0) { + $("#policy-users").text(policyPayloadObj.users.toString().split(",").join(", ")); + } else { + $("#users-row").addClass("hidden"); + } + + if (policyPayloadObj.deviceGroups == null) { + $("#policy-groups").text("NONE"); + } else if (policyPayloadObj.deviceGroups.length > 0) { + debugger; + var deviceGroups = policyPayloadObj.deviceGroups; + var assignedGroups = []; + for (var index in deviceGroups) { + if (deviceGroups.hasOwnProperty(index)) { + assignedGroups.push(deviceGroups[index].name); + } + } + $("#policy-groups").text(assignedGroups.toString().split(",").join(", ")); + } else { + $("#policy-groups").text("NONE"); + } + + if (policyPayloadObj.roles == null) { + $("#policy-roles").text("NONE"); + } + else if (policyPayloadObj.roles.length > 0) { + $("#policy-roles").text(policyPayloadObj.roles.toString().split(",").join(", ")); + } else { + $("#roles-row").addClass("hidden"); + } + + var deviceType = policy["platform"]; + var policyOperationsTemplateSrc = context + '/public/cdmf.unit.device.type.' + deviceType + + '.policy-view/templates/' + deviceType + '-policy-view.hbs'; + var policyOperationsScriptSrc = context + '/public/cdmf.unit.device.type.' + deviceType + + '.policy-view/js/' + deviceType + '-policy-view.js'; + var policyOperationsStylesSrc = context + '/public/cdmf.unit.device.type.' + deviceType + + '.policy-view/css/' + deviceType + '-policy-view.css'; + var policyOperationsTemplateCacheKey = deviceType + '-policy-operations'; + + $.isResourceExists(policyOperationsTemplateSrc, function (status) { + if (status) { + $.template(policyOperationsTemplateCacheKey, policyOperationsTemplateSrc, function (template) { + var content = template(); + $("#device-type-policy-operations").html(content).removeClass("hidden"); + $(".policy-platform").addClass("hidden"); + $.isResourceExists(policyOperationsScriptSrc, function (status) { + if (status) { + var script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = policyOperationsScriptSrc; + $(".wr-advance-operations").prepend(script); + /* + This method should be implemented in the relevant plugin side and should include the logic to + populate the policy profile in the plugin specific UI. + */ + polulateProfileOperations(policyPayloadObj["profile"]["profileFeaturesList"]); + } + }); + }); + + $.isResourceExists(policyOperationsStylesSrc, function (status) { + if (status) { + var style = document.createElement('link'); + style.type = 'text/css'; + style.rel = 'stylesheet'; + style.href = policyOperationsStylesSrc; + $(".wr-advance-operations").prepend(style); + } + }); + } else { + $("#generic-policy-operations").removeClass("hidden"); + } + $(".wr-advance-operations-init").addClass("hidden"); + }); +}; + +/** + * This method will return query parameter value given its name. + * @param name Query parameter name + * @returns {string} Query parameter value + */ +var getParameterByName = function (name) { + name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); + var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"), + results = regex.exec(location.search); + return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); +}; + +$(document).ready(function () { + var policyPayloadObj; + // Adding initial state of wizard-steps. + invokerUtil.get( + "/api/device-mgt/v1.0" + "/policies/effective-policy/" + getParameterByName("type") + "/" + getParameterByName("id"), + // on success + function (data, textStatus, jqXHR) { + if (jqXHR.status == 200 && data) { + policyPayloadObj = JSON.parse(data); + displayPolicy(policyPayloadObj); + } + }, + // on error + function (jqXHR) { + console.log(jqXHR); + // should be redirected to an error page + } + ); +}); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/view.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/view.hbs new file mode 100644 index 0000000000..025e51d81b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/view.hbs @@ -0,0 +1,83 @@ +{{#zone "content"}} +{{!--#if isAuthorized--}} + {{#defineZone "policy-profile-top"}} +
    +
    + +
    +
    + {{/defineZone}} + + +
    +
    +
    +
    + Policy Overview +
    + {{#defineZone "policy-detail-properties"}} + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    Platform
    Groups
    Action upon non-compliance
    Status
    Assigned Users
    Assigned Roles
    + {{/defineZone}} +
    Description +
    +
    +
    +
    +
    +
    + Profile Information +
    +
    + +
    +
    +
    + + Loading Platform Features . . . +
    +
    +
    + + +
    +
    +
    +
    +
    +{{/zone}} +{{#zone "bottomJs"}} + {{js "js/view.js"}} +{{/zone}} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/view.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/view.js new file mode 100644 index 0000000000..06aa7bf336 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/view.js @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2015, 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. + */ + +function onRequest(context) { + + var utility = require("/app/modules/utility.js")["utility"]; + var deviceType = context.uriParams.deviceType; + var deviceId = context.uriParams.deviceId; + + return {"deviceTypePolicyView": utility.getTenantedDeviceUnitName(deviceType, deviceId,"policy-view")}; + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/view.json b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/view.json new file mode 100644 index 0000000000..f706ffceea --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.effective-policy.view/view.json @@ -0,0 +1,3 @@ +{ + "version" : "1.0.0" +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.theme/public/css/custom-desktop.css b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.theme/public/css/custom-desktop.css index 5ecc4e2118..b143e04d3d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.theme/public/css/custom-desktop.css +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.ui.theme/public/css/custom-desktop.css @@ -6546,21 +6546,21 @@ select > option:hover { } @media (min-width: 1200px){ - table tbody tr { - width: 18% !important; - } - .table .fw-stack{ - font-size: 1.2vw; - } + table tbody tr { + width: 14% !important; + } + .table .fw-stack{ + font-size: 1.2vw; + } } @media (min-width: 1500px){ - table tbody tr { - width: 14% !important; - } - .table .fw-stack{ - font-size: 0.8vw; - } + table tbody tr { + width: 10% !important; + } + .table .fw-stack{ + font-size: 0.8vw; + } } /** End **/ diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.decision.point/pom.xml similarity index 84% rename from components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml rename to components/policy-mgt/org.wso2.carbon.policy.decision.point/pom.xml index 4690ad5c49..809f31731d 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.decision.point/pom.xml @@ -9,11 +9,11 @@ 4.0.0 org.wso2.carbon.devicemgt - org.wso2.carbon.simple.policy.decision.point + org.wso2.carbon.policy.decision.point 2.0.4-SNAPSHOT bundle - WSO2 Carbon - Simple Policy Decision Point - WSO2 Carbon - Simple Policy Decision Point + WSO2 Carbon - Policy Decision Point + WSO2 Carbon - Policy Decision Point http://wso2.org @@ -32,9 +32,10 @@ ${project.artifactId} ${project.artifactId} ${carbon.device.mgt.version} - Simple Policy Decision Point Bundle - org.wso2.carbon.simple.policy.decision.point.internal + Policy Decision Point Bundle + org.wso2.carbon.policy.decision.point.internal + org.wso2.carbon.context.*; org.osgi.framework, org.osgi.service.component, org.apache.commons.logging, @@ -44,7 +45,7 @@ org.wso2.carbon.device.mgt.common.* - org.wso2.carbon.simple.policy.decision.point.* + org.wso2.carbon.policy.decision.point.* diff --git a/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/internal/MergedPolicyEvaluationServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/internal/MergedPolicyEvaluationServiceComponent.java new file mode 100644 index 0000000000..3ec27e3918 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/internal/MergedPolicyEvaluationServiceComponent.java @@ -0,0 +1,112 @@ +/* +* Copyright (c) 2015 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.decision.point.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.policy.decision.point.merged.MergedEvaluationPoint; +import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; +import org.wso2.carbon.policy.mgt.core.PolicyManagerService; +import org.wso2.carbon.user.core.service.RealmService; + +/** + * @scr.component name="org.wso2.carbon.policy.decision.MergedPolicyEvaluationServiceComponent" immediate="true" + * @scr.reference name="user.realmservice.default" + * interface="org.wso2.carbon.user.core.service.RealmService" + * cardinality="1..1" + * policy="dynamic" + * bind="setRealmService" + * unbind="unsetRealmService" + * @scr.reference name="org.wso2.carbon.devicemgt.policy.manager" + * interface="org.wso2.carbon.policy.mgt.core.PolicyManagerService" + * cardinality="0..1" + * policy="dynamic" + * bind="setPolicyManagerService" + * unbind="unsetPolicyManagerService" + */ + +public class MergedPolicyEvaluationServiceComponent { + + private static Log log = LogFactory.getLog(MergedPolicyEvaluationServiceComponent.class); + + protected void activate(ComponentContext componentContext) { + if (log.isDebugEnabled()) { + log.debug("Activating the policy evaluation bundle."); + } + + try { + componentContext.getBundleContext().registerService(PolicyEvaluationPoint.class.getName(), + new MergedEvaluationPoint(), null); + } catch (Throwable t) { + log.error("Error occurred while initializing the policy evaluation bundle"); + } + } + + protected void deactivate(ComponentContext componentContext) { + if (log.isDebugEnabled()) { + log.debug("De-activating the policy evaluation bundle."); + } + } + + /** + * Sets Realm Service + * + * @param realmService An instance of RealmService + */ + protected void setRealmService(RealmService realmService) { + + if (log.isDebugEnabled()) { + log.debug("Setting Realm Service"); + } + PolicyDecisionPointDataHolder.getInstance().setRealmService(realmService); + } + + /** + * Unsets Realm Service + * + * @param realmService An instance of RealmService + */ + protected void unsetRealmService(RealmService realmService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting Realm Service"); + } + PolicyDecisionPointDataHolder.getInstance().setRealmService(null); + } + + protected void setPolicyManagerService(PolicyManagerService policyManagerService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting PolicyManagerService Service"); + } + PolicyDecisionPointDataHolder.getInstance().setPolicyManagerService(policyManagerService); + } + + protected void unsetPolicyManagerService(PolicyManagerService policyManagerService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting PolicyManagerService Service"); + } + PolicyDecisionPointDataHolder.getInstance().setPolicyManagerService(null); + } + +// protected String getName() { +// return MergedPolicyEvaluationServiceComponent.class.getName(); +// } + +} + diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/internal/PolicyDecisionPointDataHolder.java similarity index 96% rename from components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java rename to components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/internal/PolicyDecisionPointDataHolder.java index 7b5d104ea1..41909fbd49 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java +++ b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/internal/PolicyDecisionPointDataHolder.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.simple.policy.decision.point.internal; +package org.wso2.carbon.policy.decision.point.internal; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.user.core.service.RealmService; diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/internal/PolicyEvaluationServiceComponent.java similarity index 87% rename from components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java rename to components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/internal/PolicyEvaluationServiceComponent.java index 189534294f..931d1ec552 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java +++ b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/internal/PolicyEvaluationServiceComponent.java @@ -16,14 +16,16 @@ * under the License. */ -package org.wso2.carbon.simple.policy.decision.point.internal; +package org.wso2.carbon.policy.decision.point.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.policy.decision.point.merged.MergedEvaluationPoint; +import org.wso2.carbon.policy.decision.point.simple.PolicyEvaluationServiceImpl; +import org.wso2.carbon.policy.decision.point.simple.SimpleEvaluationImpl; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; -import org.wso2.carbon.simple.policy.decision.point.PolicyEvaluationServiceImpl; import org.wso2.carbon.user.core.service.RealmService; /** @@ -48,20 +50,20 @@ public class PolicyEvaluationServiceComponent { protected void activate(ComponentContext componentContext) { if (log.isDebugEnabled()) { - log.debug("Activating the simple policy evaluation bundle."); + log.debug("Activating the policy evaluation bundle."); } try { componentContext.getBundleContext().registerService(PolicyEvaluationPoint.class.getName(), new PolicyEvaluationServiceImpl(), null); } catch (Throwable t) { - log.error("Error occurred while initializing the simple policy evaluation bundle"); + log.error("Error occurred while initializing the policy evaluation bundle"); } } protected void deactivate(ComponentContext componentContext) { if (log.isDebugEnabled()) { - log.debug("De-activating the simple policy evaluation bundle."); + log.debug("De-activating the policy evaluation bundle."); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/merged/MergedEvaluationPoint.java b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/merged/MergedEvaluationPoint.java new file mode 100644 index 0000000000..07fd0b68d3 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/merged/MergedEvaluationPoint.java @@ -0,0 +1,134 @@ +/* +* Copyright (c) 2015 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.decision.point.merged; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.policy.mgt.common.*; +import org.wso2.carbon.policy.mgt.core.PolicyManagerService; +import org.wso2.carbon.policy.decision.point.internal.PolicyDecisionPointDataHolder; + +import java.sql.Timestamp; +import java.util.*; + +/** + * This class helps to merge related policies and return as a effective policy. + */ +public class MergedEvaluationPoint implements PolicyEvaluationPoint { + + private static final Log log = LogFactory.getLog(MergedEvaluationPoint.class); + private PolicyManagerService policyManagerService; + private static final String effectivePolicyName = "Effective-Policy"; + private static final String policyEvaluationPoint = "Merged"; + + @Override + public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) + throws PolicyEvaluationException { + return this.getEffectivePolicy(deviceIdentifier).getProfile().getProfileFeaturesList(); + } + + @Override + public String getName() { + return policyEvaluationPoint; + } + + @Override + public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { + PIPDevice pipDevice; + List policyList; + Policy policy; + try { + policyManagerService = getPolicyManagerService(); + if (policyManagerService == null) { + return null; + } + PolicyInformationPoint policyInformationPoint = policyManagerService.getPIP(); + pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier); + policyList = policyInformationPoint.getRelatedPolicies(pipDevice); + + if (policyList.size() == 0) { + return null; + } + + // Set effective-policy information + Profile profile = new Profile(); + policy = policyResolve(policyList); + profile.setProfileFeaturesList(policy.getProfile().getProfileFeaturesList()); + policy.setProfile(profile); + Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + profile.setCreatedDate(currentTimestamp); + profile.setUpdatedDate(currentTimestamp); + profile.setDeviceType(deviceIdentifier.getType()); + profile.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + // Set effective policy name + policy.setPolicyName(effectivePolicyName); + policy.setOwnershipType(pipDevice.getOwnershipType()); + // Set effective policy Active and Updated + policy.setActive(true); + policy.setUpdated(true); + policy.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); + String policyIds = ""; + Collections.sort(policyList); + for (Policy appliedPolicy : policyList) { + policyIds += appliedPolicy.getId() + ", "; + } + policyIds = policyIds.substring(0, policyIds.length() - 2); + policy.setDescription("This is a system generated effective policy by merging Policy Id : " + policyIds); + // Need to set compliance of the effective policy. Get compliance of first policy using priority order + policy.setCompliance(policyList.get(0).getCompliance()); + // Change default 0 effective policy id to (-1) + policy.setId(-1); + return policy; + } 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); + } + } + + private Policy policyResolve(List policyList) throws PolicyEvaluationException, PolicyManagementException { + Collections.sort(policyList, Collections.reverseOrder()); + + // Iterate through all policies + Map featureMap = new HashMap<>(); + for (Policy policy : policyList) { + List profileFeaturesList = policy.getProfile().getProfileFeaturesList(); + if (profileFeaturesList != null) { + for (ProfileFeature feature : profileFeaturesList) { + featureMap.put(feature.getFeatureCode(), feature); + } + } + } + + // Get prioritized features list + List newFeaturesList = new ArrayList<>(featureMap.values()); + Profile profile = new Profile(); + profile.setProfileFeaturesList(newFeaturesList); + Policy effectivePolicy = new Policy(); + effectivePolicy.setProfile(profile); + return effectivePolicy; + } + + private PolicyManagerService getPolicyManagerService() { + return PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService(); + } + +} 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.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/PolicyEvaluationServiceImpl.java similarity index 70% rename from components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java rename to components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/PolicyEvaluationServiceImpl.java index f6fbd82d69..d737cbaa3f 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.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/PolicyEvaluationServiceImpl.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.simple.policy.decision.point; +package org.wso2.carbon.policy.decision.point.simple; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Policy; @@ -29,6 +29,7 @@ import java.util.List; public class PolicyEvaluationServiceImpl implements PolicyEvaluationPoint { private SimpleEvaluationImpl evaluation; + private static final String policyEvaluationPoint = "Simple"; public PolicyEvaluationServiceImpl() { evaluation = new SimpleEvaluationImpl(); @@ -40,23 +41,16 @@ public class PolicyEvaluationServiceImpl implements PolicyEvaluationPoint { } @Override - public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { + public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) + throws PolicyEvaluationException { 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; } + + @Override + public String getName() { + return policyEvaluationPoint; + } } diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/SimpleEvaluation.java similarity index 95% rename from components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java rename to components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/SimpleEvaluation.java index fb96100a06..f8fd2a7bb7 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java +++ b/components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/SimpleEvaluation.java @@ -17,7 +17,7 @@ */ -package org.wso2.carbon.simple.policy.decision.point; +package org.wso2.carbon.policy.decision.point.simple; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Policy; 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.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/SimpleEvaluationImpl.java similarity index 89% rename from components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java rename to components/policy-mgt/org.wso2.carbon.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/SimpleEvaluationImpl.java index 13cde3e181..f8c75e3105 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.policy.decision.point/src/main/java/org/wso2/carbon/policy/decision/point/simple/SimpleEvaluationImpl.java @@ -16,14 +16,14 @@ * under the License. */ -package org.wso2.carbon.simple.policy.decision.point; +package org.wso2.carbon.policy.decision.point.simple; 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.decision.point.internal.PolicyDecisionPointDataHolder; 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; import java.util.ArrayList; import java.util.Collections; @@ -32,14 +32,9 @@ import java.util.List; public class SimpleEvaluationImpl implements SimpleEvaluation { private static final Log log = LogFactory.getLog(SimpleEvaluationImpl.class); - //TODO : to revove the stale reference private PolicyManagerService policyManagerService; private List policyList = new ArrayList(); -// public SimpleEvaluationImpl() { -// policyManagerService = PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService(); -// } - @Override public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { Policy policy = new Policy(); @@ -74,7 +69,6 @@ public class SimpleEvaluationImpl implements SimpleEvaluation { return policy; } - @Override public void sortPolicies() throws PolicyEvaluationException { Collections.sort(policyList); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java index ef4f84988d..74ce2adf1a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java @@ -39,8 +39,15 @@ public interface PolicyEvaluationPoint { /** * This class will return the effective feature set from the list. - * @param deviceIdentifier device information. - * @return returns the effective feature set. + * + * @param deviceIdentifier device information. + * @return returns the effective feature set. */ List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException; + + /** + * This method returns the name of the Policy Evaluation Point + * @return returns Policy Evaluation Point name + */ + String getName(); } 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 da127b70a9..7f00319052 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 @@ -91,11 +91,20 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { @Override public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { try { - Policy policy = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(). - getEffectivePolicy(deviceIdentifier); - - if (policy == null) { - return null; + PolicyEvaluationPoint policyEvaluationPoint = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(); + Policy policy; + + if (policyEvaluationPoint != null) { + policy = policyEvaluationPoint. + getEffectivePolicy(deviceIdentifier); + if (policy == null) { + policyAdministratorPoint.removePolicyUsed(deviceIdentifier); + return null; + } + this.getPAP().setPolicyUsed(deviceIdentifier, policy); + } else { + throw new PolicyEvaluationException("Error occurred while getting the policy evaluation point " + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType()); } List deviceIdentifiers = new ArrayList(); deviceIdentifiers.add(deviceIdentifier); @@ -129,8 +138,13 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws FeatureManagementException { try { - return PolicyManagementDataHolder.getInstance(). - getPolicyEvaluationPoint().getEffectiveFeatures(deviceIdentifier); + PolicyEvaluationPoint policyEvaluationPoint = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(); + if (policyEvaluationPoint != null) { + return policyEvaluationPoint.getEffectiveFeatures(deviceIdentifier); + } else { + throw new FeatureManagementException("Error occurred while getting the policy evaluation point " + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType()); + } } catch (PolicyEvaluationException e) { String msg = "Error occurred while getting the effective features from the PEP service " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); 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 7b6da5a882..700768fb0a 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 @@ -1361,6 +1361,11 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(1, policyId); stmt.executeUpdate(); + String deleteGroup = "DELETE FROM DM_DEVICE_GROUP_POLICY WHERE POLICY_ID = ?"; + stmt = conn.prepareStatement(deleteGroup); + stmt.setInt(1, policyId); + stmt.executeUpdate(); + if (log.isDebugEnabled()) { log.debug("Policy (" + policyId + ") related configs deleted from database."); } 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 2fa70bb3af..2d681cf93c 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 @@ -30,6 +30,7 @@ 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.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; @@ -84,7 +85,17 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato public Policy getEffectivePolicy(DeviceIdentifier identifier) throws PolicyDelegationException { try { PolicyManagerService policyManagerService = new PolicyManagerServiceImpl(); - return policyManagerService.getPEP().getEffectivePolicy(identifier); + PolicyAdministratorPoint policyAdministratorPoint; + + Policy policy = policyManagerService.getPEP().getEffectivePolicy(identifier); + policyAdministratorPoint = policyManagerService.getPAP(); + if (policy != null) { + policyAdministratorPoint.setPolicyUsed(identifier, policy); + } else { + policyAdministratorPoint.removePolicyUsed(identifier); + return null; + } + return policy; //return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint().getEffectivePolicy(identifier); } catch (PolicyEvaluationException e) { String msg = "Error occurred while retrieving the effective policy for devices."; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java index 584bd2ec08..27f9487ac6 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java @@ -18,6 +18,8 @@ package org.wso2.carbon.policy.mgt.core.internal; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; @@ -36,6 +38,7 @@ public class PolicyManagementDataHolder { private RealmService realmService; private TenantManager tenantManager; private PolicyEvaluationPoint policyEvaluationPoint; + private Map policyEvaluationPoints = new HashMap<>(); private PolicyInformationPoint policyInformationPoint; private DeviceManagementProviderService deviceManagementService; private MonitoringManager monitoringManager; @@ -88,13 +91,21 @@ public class PolicyManagementDataHolder { } public PolicyEvaluationPoint getPolicyEvaluationPoint() { - return policyEvaluationPoint; + PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance(). + getDeviceManagementConfig().getPolicyConfiguration(); + String policyEvaluationPointName = policyConfiguration.getPolicyEvaluationPointName(); + return policyEvaluationPoints.get(policyEvaluationPointName); } - public void setPolicyEvaluationPoint(PolicyEvaluationPoint policyEvaluationPoint) { - this.policyEvaluationPoint = policyEvaluationPoint; + public void setPolicyEvaluationPoint(String name, PolicyEvaluationPoint policyEvaluationPoint) { + policyEvaluationPoints.put(name,policyEvaluationPoint); } + public void removePolicyEvaluationPoint(PolicyEvaluationPoint policyEvaluationPoint) { + policyEvaluationPoints.remove(policyEvaluationPoint.getName()); + } + + public PolicyInformationPoint getPolicyInformationPoint() { return policyInformationPoint; } 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 0c456fce28..85bce85332 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 @@ -46,9 +46,9 @@ import org.wso2.carbon.user.core.service.RealmService; * policy="dynamic" * bind="setRealmService" * unbind="unsetRealmService" - * @scr.reference name="org.wso2.carbon.devicemgt.simple.policy.evaluation.manager" + * @scr.reference name="org.wso2.carbon.devicemgt.policy.evaluation.manager" * interface="org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint" - * cardinality="1..1" + * cardinality="1..n" * policy="dynamic" * bind="setPEPService" * unbind="unsetPEPService" @@ -159,14 +159,14 @@ public class PolicyManagementServiceComponent { if (log.isDebugEnabled()) { log.debug("Setting Policy Information Service"); } - PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(pepService); + PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(pepService.getName(), pepService); } protected void unsetPEPService(PolicyEvaluationPoint pepService) { if (log.isDebugEnabled()) { log.debug("Removing Policy Information Service"); } - PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(null); + PolicyManagementDataHolder.getInstance().removePolicyEvaluationPoint(pepService); } protected void setDeviceManagementService(DeviceManagementProviderService deviceManagerService) { 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 2fbb9d84e1..23b1a29420 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 @@ -880,9 +880,7 @@ public class PolicyManagerImpl implements PolicyManager { Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId()); if (policySaved != null && policySaved.getId() != 0) { -// if (policy.getId() != policySaved.getId()) { - policyDAO.updateEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); -// } + policyDAO.updateEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); } else { policyDAO.addEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); } @@ -912,17 +910,17 @@ public class PolicyManagerImpl implements PolicyManager { Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId()); if (policySaved != null) { - policyDAO.deleteEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId()); + policyDAO.deleteEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId()); } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while removing the applied policy to device (" + - deviceId + ")", e); + deviceId + ")", e); } catch (DeviceManagementException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while getting the device details (" + - deviceIdentifier.getId() + ")", e); + deviceIdentifier.getId() + ")", e); } finally { PolicyManagementDAOFactory.closeConnection(); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java index af0c3d604c..e97030301d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyEvaluationTestCase.java @@ -27,14 +27,10 @@ import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; -import org.wso2.carbon.device.mgt.core.authorization.DeviceAccessAuthorizationServiceImpl; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.ntask.common.TaskException; import org.wso2.carbon.policy.mgt.common.*; -import org.wso2.carbon.policy.mgt.core.enforcement.DelegationTask; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.services.SimplePolicyEvaluationTest; @@ -51,7 +47,7 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest { @Override public void init() throws Exception { PolicyEvaluationPoint evaluationPoint = new SimplePolicyEvaluationTest(); - PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(evaluationPoint); + PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(evaluationPoint.getName(), evaluationPoint); } @Test @@ -93,7 +89,7 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = ("activatePolicies")) - public void getEffectivePolicy() throws DeviceManagementException, PolicyEvaluationException { + public void getEffectivePolicy(DeviceIdentifier identifier) throws DeviceManagementException, PolicyEvaluationException { log.debug("Getting effective policy for device started .........."); @@ -103,7 +99,6 @@ public class PolicyEvaluationTestCase extends BasePolicyManagementDAOTest { PolicyEvaluationPoint evaluationPoint = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(); for (Device device : devices) { - DeviceIdentifier identifier = new DeviceIdentifier(); identifier.setType(device.getType()); identifier.setId(device.getDeviceIdentifier()); Policy policy = evaluationPoint.getEffectivePolicy(identifier); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java index b4bed5da4d..32f4efc212 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/services/SimplePolicyEvaluationTest.java @@ -29,7 +29,8 @@ import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; import java.util.Collections; import java.util.List; -public class SimplePolicyEvaluationTest implements PolicyEvaluationPoint { +public class +SimplePolicyEvaluationTest implements PolicyEvaluationPoint { private static final Log log = LogFactory.getLog(SimplePolicyEvaluationTest.class); @@ -76,6 +77,11 @@ public class SimplePolicyEvaluationTest implements PolicyEvaluationPoint { return null; } + @Override + public String getName() { + return "SimplePolicy"; + } + public void sortPolicies(List policyList) throws PolicyEvaluationException { Collections.sort(policyList); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml index 0f5861cc1d..98be922a1c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml @@ -42,6 +42,8 @@ 5 8 20 + + Simple diff --git a/components/policy-mgt/pom.xml b/components/policy-mgt/pom.xml index 97d2d59b09..e7451e8627 100644 --- a/components/policy-mgt/pom.xml +++ b/components/policy-mgt/pom.xml @@ -38,7 +38,7 @@ org.wso2.carbon.policy.mgt.common org.wso2.carbon.policy.mgt.core org.wso2.carbon.policy.information.point - org.wso2.carbon.simple.policy.decision.point + org.wso2.carbon.policy.decision.point org.wso2.carbon.complex.policy.decision.point diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/.cdm-config.xml.swp b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/.cdm-config.xml.swp new file mode 100644 index 0000000000..a8e6a11569 Binary files /dev/null and b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/.cdm-config.xml.swp differ diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml index 1f76b3dacd..e48531dd83 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml @@ -43,6 +43,10 @@ 5 8 20 + + + + Simple android ios diff --git a/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml b/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml index 2c140da41f..8740cff8d6 100644 --- a/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml +++ b/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml @@ -47,7 +47,7 @@ org.wso2.carbon.devicemgt - org.wso2.carbon.simple.policy.decision.point + org.wso2.carbon.policy.decision.point org.wso2.carbon.devicemgt @@ -118,7 +118,7 @@ org.wso2.carbon.devicemgt:org.wso2.carbon.policy.mgt.common:${carbon.device.mgt.version} - org.wso2.carbon.devicemgt:org.wso2.carbon.simple.policy.decision.point:${carbon.device.mgt.version} + org.wso2.carbon.devicemgt:org.wso2.carbon.policy.decision.point:${carbon.device.mgt.version} org.wso2.carbon.devicemgt:org.wso2.carbon.policy.information.point:${carbon.device.mgt.version} diff --git a/pom.xml b/pom.xml index cb38b4867c..2f3b5f5c42 100644 --- a/pom.xml +++ b/pom.xml @@ -195,7 +195,7 @@ org.wso2.carbon.devicemgt - org.wso2.carbon.simple.policy.decision.point + org.wso2.carbon.policy.decision.point ${carbon.device.mgt.version}