From 1a957666e3bbebb9243f9d05dcbd8487948aa713 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 22 Jan 2018 14:39:16 +0530 Subject: [PATCH 1/7] Improvements to policy monitoring framework This fixes the isseu where policy monitoring is configured per device type, they were not used. With this fix, main policy monitoring config is with cdm-config.xml and per device type can be enabled separately in the device type related xml file. --- .../common/spi/DeviceManagementService.java | 3 ++ .../DeviceManagementProviderService.java | 2 + .../DeviceManagementProviderServiceImpl.java | 29 +++++++++-- .../mgt/core/TestDeviceManagementService.java | 6 +++ .../template/DeviceTypeManagerService.java | 18 ++++++- .../config/InitialOperationConfig.java | 2 +- .../template/config/PolicyMonitoring.java | 3 ++ .../DeviceTypeManagerServiceTest.java | 11 +++++ .../test/resources/device-types/arduino.xml | 1 + .../mgt/core/PolicyManagerServiceImpl.java | 7 ++- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 3 ++ .../core/mgt/impl/MonitoringManagerImpl.java | 49 ++++++++++--------- .../core/PolicyManagerServiceImplTest.java | 26 +++++++++- .../mock/TypeXDeviceManagementService.java | 6 +++ 14 files changed, 135 insertions(+), 31 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java index 8cbe049245..b14a5ab920 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.common.spi; import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; +import org.wso2.carbon.device.mgt.common.general.GeneralConfig; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; @@ -52,4 +53,6 @@ public interface DeviceManagementService { DeviceStatusTaskPluginConfig getDeviceStatusTaskPluginConfig(); + GeneralConfig getGeneralConfig(); + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 07ff4ba382..864b962528 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -522,6 +522,8 @@ public interface DeviceManagementProviderService { List getAvailableDeviceTypes() throws DeviceManagementException; + List getPolicyMonitoringEnableDeviceTypes() throws DeviceManagementException; + boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException; boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException; 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 9f2d4674a8..2d86ff9861 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 @@ -1177,6 +1177,29 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return deviceTypesResponse; } + @Override + public List getPolicyMonitoringEnableDeviceTypes() throws DeviceManagementException { + + List deviceTypes = this.getAvailableDeviceTypes(); + List deviceTyepsToMonitor = new ArrayList<>(); + int tenantId = this.getTenantId(); + Map registeredTypes = + pluginRepository.getAllDeviceManagementServices(tenantId); + + List services = new ArrayList<>(registeredTypes.values()); + for (DeviceManagementService deviceType : services) { + if (deviceType != null && deviceType.getGeneralConfig() != null && + deviceType.getGeneralConfig().isPolicyMonitoringEnabled()) { + for (String type : deviceTypes) { + if (type.equalsIgnoreCase(deviceType.getType())) { + deviceTyepsToMonitor.add(type); + } + } + } + } + return deviceTyepsToMonitor; + } + @Override public boolean updateDeviceInfo(DeviceIdentifier deviceId, Device device) throws DeviceManagementException { if (deviceId == null || device == null) { @@ -1489,13 +1512,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public List getFilteredActivities(String operationCode, int limit, int offset) throws OperationManagementException{ + public List getFilteredActivities(String operationCode, int limit, int offset) throws OperationManagementException { limit = DeviceManagerUtil.validateActivityListPageSize(limit); return DeviceManagementDataHolder.getInstance().getOperationManager().getFilteredActivities(operationCode, limit, offset); } @Override - public int getTotalCountOfFilteredActivities(String operationCode) throws OperationManagementException{ + public int getTotalCountOfFilteredActivities(String operationCode) throws OperationManagementException { return DeviceManagementDataHolder.getInstance().getOperationManager().getTotalCountOfFilteredActivities(operationCode); } @@ -2565,7 +2588,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } try { DeviceManagementDAOFactory.openConnection(); - return deviceDAO.findGeoClusters(southWest,northEast,geohashLength,this.getTenantId()); + return deviceDAO.findGeoClusters(southWest, northEast, geohashLength, this.getTenantId()); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving the geo clusters."; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java index f83100b442..29ebe9c493 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core; import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; +import org.wso2.carbon.device.mgt.common.general.GeneralConfig; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; @@ -108,4 +109,9 @@ public class TestDeviceManagementService implements DeviceManagementService { public DeviceStatusTaskPluginConfig getDeviceStatusTaskPluginConfig() { return null; } + + @Override + public GeneralConfig getGeneralConfig() { + return null; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java index 9ff760b4c9..48f011f8db 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java @@ -30,6 +30,7 @@ import org.wso2.carbon.device.mgt.common.ProvisioningConfig; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.common.general.GeneralConfig; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; @@ -69,6 +70,7 @@ public class DeviceTypeManagerService implements DeviceManagementService { private InitialOperationConfig initialOperationConfig; private PullNotificationSubscriber pullNotificationSubscriber; private DeviceStatusTaskPluginConfig deviceStatusTaskPluginConfig; + private GeneralConfig generalConfig; public DeviceTypeManagerService(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier, DeviceTypeConfiguration deviceTypeConfiguration) { @@ -84,6 +86,7 @@ public class DeviceTypeManagerService implements DeviceManagementService { this.setDeviceStatusTaskPluginConfig(deviceTypeConfiguration.getDeviceStatusTaskConfiguration()); this.setPolicyMonitoringManager(deviceTypeConfiguration.getPolicyMonitoring()); this.setPullNotificationSubscriber(deviceTypeConfiguration.getPullNotificationSubscriberConfig()); + this.setGeneralConfig(deviceTypeConfiguration); } @Override @@ -92,7 +95,7 @@ public class DeviceTypeManagerService implements DeviceManagementService { } @Override - public OperationMonitoringTaskConfig getOperationMonitoringConfig(){ + public OperationMonitoringTaskConfig getOperationMonitoringConfig() { return operationMonitoringConfigs; } @@ -193,6 +196,11 @@ public class DeviceTypeManagerService implements DeviceManagementService { return deviceStatusTaskPluginConfig; } + @Override + public GeneralConfig getGeneralConfig() { + return generalConfig; + } + private void setProvisioningConfig(String tenantDomain, DeviceTypeConfiguration deviceTypeConfiguration) { if (deviceTypeConfiguration.getProvisioningConfig() != null) { boolean sharedWithAllTenants = deviceTypeConfiguration.getProvisioningConfig().isSharedWithAllTenants(); @@ -264,4 +272,12 @@ public class DeviceTypeManagerService implements DeviceManagementService { } } } + + + public void setGeneralConfig(DeviceTypeConfiguration deviceTypeConfiguration) { + this.generalConfig = new GeneralConfig(); + if (deviceTypeConfiguration.getPolicyMonitoring() != null) { + this.generalConfig.setPolicyMonitoringEnabled(deviceTypeConfiguration.getPolicyMonitoring().isEnabled()); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/InitialOperationConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/InitialOperationConfig.java index cb45a1b212..30cb5b255a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/InitialOperationConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/InitialOperationConfig.java @@ -33,7 +33,7 @@ public class InitialOperationConfig { return operations; } - public void setOperationsll(List operations) { + public void setOperations(List operations) { this.operations = operations; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java index 251c598666..a45dcb1303 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java @@ -52,6 +52,9 @@ public class PolicyMonitoring { protected String value; @XmlAttribute(name = "enabled") protected boolean enabled; +// protected String policyEvaluationPoint; +// protected String cacheEnable; + /** * Gets the value of the value property. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java index b512a2965d..e957bfd0db 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java @@ -33,6 +33,7 @@ import org.wso2.carbon.device.mgt.common.ProvisioningConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.common.general.GeneralConfig; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; @@ -76,6 +77,7 @@ public class DeviceTypeManagerServiceTest { private Method populatePushNotificationConfig; private Method setPolicyMonitoringManager; private Method setPullNotificationSubscriber; + private Method setGeneralConfig; @BeforeClass public void setup() throws NoSuchMethodException, SAXException, JAXBException, ParserConfigurationException, @@ -102,10 +104,19 @@ public class DeviceTypeManagerServiceTest { .getDeclaredMethod("setPullNotificationSubscriber", PullNotificationSubscriberConfig.class); setPullNotificationSubscriber.setAccessible(true); + setGeneralConfig = DeviceTypeManagerService.class + .getDeclaredMethod("setGeneralConfig", DeviceTypeConfiguration.class); + setGeneralConfig.setAccessible(true); + Field deviceStatusTaskPluginConfig = DeviceTypeManagerService.class .getDeclaredField("deviceStatusTaskPluginConfig"); deviceStatusTaskPluginConfig.setAccessible(true); + + Field generalConfig = DeviceTypeManagerService.class + .getDeclaredField("generalConfig"); + generalConfig.setAccessible(true); + Field operationMonitoringConfigs = DeviceTypeManagerService.class .getDeclaredField("operationMonitoringConfigs"); operationMonitoringConfigs.setAccessible(true); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/arduino.xml b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/arduino.xml index eb12e9ccb3..5b1ed2d468 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/arduino.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/arduino.xml @@ -30,6 +30,7 @@ + true 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 11e69580e0..946f1c49a6 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 @@ -209,7 +209,12 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { List complianceFeatures = monitoringManager.checkPolicyCompliance(deviceIdentifier, response); - return !(complianceFeatures == null || complianceFeatures.isEmpty()); + if(complianceFeatures == null || complianceFeatures.isEmpty()) { + return true; + } else { + return false; + } + } @Override diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index edc064b6d0..e88e4b62a7 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -242,6 +242,9 @@ public class MonitoringDAOImpl implements MonitoringDAO { PreparedStatement stmt = null; ResultSet resultSet = null; NonComplianceData complianceData = new NonComplianceData(); + // Setting the initial compliance status as true; +// complianceData.setStatus(true); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 447db27d7d..2ccbe94778 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -20,6 +20,7 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl; 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.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; @@ -92,19 +93,21 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.openConnection(); NonComplianceData cmd = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId()); complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier, - policy, deviceResponse); + policy, deviceResponse); + if (cmd != null) { + complianceData.setId(cmd.getId()); + complianceData.setPolicy(policy); + complianceFeatures = complianceData.getComplianceFeatures(); + complianceData.setDeviceId(device.getId()); + complianceData.setPolicyId(policy.getId()); + } - complianceData.setId(cmd.getId()); - complianceData.setPolicy(policy); - complianceFeatures = complianceData.getComplianceFeatures(); - complianceData.setDeviceId(device.getId()); - complianceData.setPolicyId(policy.getId()); } catch (SQLException e) { throw new PolicyComplianceException("Error occurred while opening a data source connection", e); } catch (MonitoringDAOException e) { throw new PolicyComplianceException( "Unable to add the none compliance features to database for device " + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -115,7 +118,7 @@ public class MonitoringManagerImpl implements MonitoringManager { try { PolicyManagementDAOFactory.beginTransaction(); monitoringDAO.setDeviceAsNoneCompliance(device.getId(), device.getEnrolmentInfo().getId(), - policy.getId()); + policy.getId()); if (log.isDebugEnabled()) { log.debug("Compliance status primary key " + complianceData.getId()); } @@ -181,19 +184,19 @@ public class MonitoringManagerImpl implements MonitoringManager { device = service.getDevice(deviceIdentifier, false); } catch (DeviceManagementException e) { throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + - " - " + deviceIdentifier.getType(), e); + " - " + deviceIdentifier.getType(), e); } try { PolicyManagementDAOFactory.openConnection(); NonComplianceData complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo() - .getId()); - if (complianceData == null || !complianceData.isStatus()) { + .getId()); + if (complianceData != null && !complianceData.isStatus()) { return false; } } catch (MonitoringDAOException e) { throw new PolicyComplianceException("Unable to retrieve compliance status for " + deviceIdentifier.getId() + - " - " + deviceIdentifier.getType(), e); + " - " + deviceIdentifier.getType(), e); } catch (SQLException e) { throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); } finally { @@ -204,7 +207,7 @@ public class MonitoringManagerImpl implements MonitoringManager { @Override public NonComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws - PolicyComplianceException { + PolicyComplianceException { NonComplianceData complianceData; try { PolicyManagementDAOFactory.openConnection(); @@ -218,11 +221,11 @@ public class MonitoringManagerImpl implements MonitoringManager { } catch (DeviceManagementException e) { throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + - " - " + deviceIdentifier.getType(), e); + " - " + deviceIdentifier.getType(), e); } catch (MonitoringDAOException e) { throw new PolicyComplianceException("Unable to retrieve compliance data for " + deviceIdentifier.getId() + - " - " + deviceIdentifier.getType(), e); + " - " + deviceIdentifier.getType(), e); } catch (SQLException e) { throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); } finally { @@ -288,10 +291,10 @@ public class MonitoringManagerImpl implements MonitoringManager { if (complianceData.getAttempts() == 0) { deviceIdsToAddOperation.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); + deviceIds.get(complianceData.getDeviceId())); } else { deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); + deviceIds.get(complianceData.getDeviceId())); } } } @@ -315,7 +318,7 @@ public class MonitoringManagerImpl implements MonitoringManager { log.debug("These devices are in the system for the first time"); for (PolicyDeviceWrapper wrapper : firstTimeDevices) { log.debug("First time device primary key : " + wrapper.getDeviceId() + " & policy id " + - wrapper.getPolicyId()); + wrapper.getPolicyId()); } } @@ -358,11 +361,11 @@ public class MonitoringManagerImpl implements MonitoringManager { List deviceTypes = new ArrayList<>(); try { - //when shutdown, it sets DeviceManagementService to null, therefore need to have a null check - if (PolicyManagementDataHolder.getInstance().getDeviceManagementService() != null) { - deviceTypes = - PolicyManagementDataHolder.getInstance().getDeviceManagementService().getAvailableDeviceTypes(); - } + //when shutdown, it sets DeviceManagementService to null, therefore need to have a null check + if (PolicyManagementDataHolder.getInstance().getDeviceManagementService() != null) { + deviceTypes = + PolicyManagementDataHolder.getInstance().getDeviceManagementService().getPolicyMonitoringEnableDeviceTypes(); + } } catch (DeviceManagementException e) { throw new PolicyComplianceException("Error occurred while getting the device types.", e); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java index bf24d0e536..78a98c229a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java @@ -46,6 +46,8 @@ import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; 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.mgt.MonitoringManager; +import org.wso2.carbon.policy.mgt.core.mgt.impl.MonitoringManagerImpl; import org.wso2.carbon.policy.mgt.core.mock.TypeXDeviceManagementService; import org.wso2.carbon.policy.mgt.core.task.MonitoringTask; import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService; @@ -232,7 +234,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = "applyPolicy") - public void checkCompliance() throws PolicyComplianceException { + public void checkCompliance() throws PolicyComplianceException, DeviceManagementException { new MonitoringTask().execute(); List complianceFeatures = new ArrayList<>(); @@ -246,6 +248,15 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { complianceFeature.setMessage("Test message"); complianceFeature.setCompliance(true); complianceFeatures.add(complianceFeature); + + Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). + getDevice(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), false); + List deviceList = new ArrayList<>(); + deviceList.add(device); + + MonitoringManager mm = new MonitoringManagerImpl(); + mm.addMonitoringOperation(deviceList); + policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); @@ -253,7 +264,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { } @Test(dependsOnMethods = "checkCompliance") - public void checkNonCompliance() throws PolicyComplianceException { + public void checkNonCompliance() throws PolicyComplianceException, DeviceManagementException { new MonitoringTask().execute(); List complianceFeatures = new ArrayList<>(); @@ -267,6 +278,17 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { complianceFeature.setMessage("Test message"); complianceFeature.setCompliance(false); complianceFeatures.add(complianceFeature); + + + Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). + getDevice(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), false); + List deviceList = new ArrayList<>(); + deviceList.add(device); + + MonitoringManager mm = new MonitoringManagerImpl(); + mm.addMonitoringOperation(deviceList); + + policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); Assert.assertFalse(deviceCompliance, "Policy was compliant even though the response was not compliant"); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManagementService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManagementService.java index 012bfd7b6b..0f76246da6 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManagementService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManagementService.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.MonitoringOperation; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; +import org.wso2.carbon.device.mgt.common.general.GeneralConfig; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; @@ -96,4 +97,9 @@ public class TypeXDeviceManagementService implements DeviceManagementService { public DeviceStatusTaskPluginConfig getDeviceStatusTaskPluginConfig() { return null; } + + @Override + public GeneralConfig getGeneralConfig() { + return null; + } } From 03570f3ad74d073c4ed5b902d1d7f603e73f047a Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 22 Jan 2018 14:39:29 +0530 Subject: [PATCH 2/7] Improvements to policy monitoring framework This fixes the isseu where policy monitoring is configured per device type, they were not used. With this fix, main policy monitoring config is with cdm-config.xml and per device type can be enabled separately in the device type related xml file. --- .../mgt/common/general/GeneralConfig.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/general/GeneralConfig.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/general/GeneralConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/general/GeneralConfig.java new file mode 100644 index 0000000000..f9e75b4450 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/general/GeneralConfig.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2018, 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.device.mgt.common.general; + +public class GeneralConfig { + + private boolean policyMonitoringEnabled; + + public boolean isPolicyMonitoringEnabled() { + return policyMonitoringEnabled; + } + + public void setPolicyMonitoringEnabled(boolean policyMonitoringEnabled) { + this.policyMonitoringEnabled = policyMonitoringEnabled; + } +} + From 8f1d15c6903eead754ffaa294532196135895f21 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 22 Jan 2018 15:10:47 +0530 Subject: [PATCH 3/7] Removing commented lines --- .../device/type/template/config/PolicyMonitoring.java | 2 -- .../wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java index a45dcb1303..4abb126ce0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java @@ -52,8 +52,6 @@ public class PolicyMonitoring { protected String value; @XmlAttribute(name = "enabled") protected boolean enabled; -// protected String policyEvaluationPoint; -// protected String cacheEnable; /** diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index e88e4b62a7..779232bfee 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -242,8 +242,6 @@ public class MonitoringDAOImpl implements MonitoringDAO { PreparedStatement stmt = null; ResultSet resultSet = null; NonComplianceData complianceData = new NonComplianceData(); - // Setting the initial compliance status as true; -// complianceData.setStatus(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); From 5c2c7dfc2aabadc52d9e55e04619c581eb5b8fc4 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 22 Jan 2018 15:11:29 +0530 Subject: [PATCH 4/7] Removing commented lines --- .../device/type/template/config/PolicyMonitoring.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java index 4abb126ce0..99a3030245 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java @@ -52,8 +52,7 @@ public class PolicyMonitoring { protected String value; @XmlAttribute(name = "enabled") protected boolean enabled; - - + /** * Gets the value of the value property. * From 7c4ca89beb30ae2de66d14ee134672e22e87d056 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 22 Jan 2018 15:13:43 +0530 Subject: [PATCH 5/7] Removing extra lines --- .../device/type/template/config/PolicyMonitoring.java | 2 +- .../carbon/policy/mgt/core/PolicyManagerServiceImplTest.java | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java index 99a3030245..251c598666 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/PolicyMonitoring.java @@ -52,7 +52,7 @@ public class PolicyMonitoring { protected String value; @XmlAttribute(name = "enabled") protected boolean enabled; - + /** * Gets the value of the value property. * diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java index 78a98c229a..adddbfd3f0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java @@ -279,7 +279,6 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { complianceFeature.setCompliance(false); complianceFeatures.add(complianceFeature); - Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider(). getDevice(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), false); List deviceList = new ArrayList<>(); @@ -287,8 +286,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { MonitoringManager mm = new MonitoringManagerImpl(); mm.addMonitoringOperation(deviceList); - - + policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); Assert.assertFalse(deviceCompliance, "Policy was compliant even though the response was not compliant"); From a0a2817730b5d02f449f13bd107c300e8a8854b2 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 22 Jan 2018 15:19:24 +0530 Subject: [PATCH 6/7] Changing the variable name --- .../policy/mgt/core/PolicyManagerServiceImplTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java index adddbfd3f0..0f17393128 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java @@ -254,8 +254,8 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { List deviceList = new ArrayList<>(); deviceList.add(device); - MonitoringManager mm = new MonitoringManagerImpl(); - mm.addMonitoringOperation(deviceList); + MonitoringManager manager = new MonitoringManagerImpl(); + manager.addMonitoringOperation(deviceList); policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); @@ -284,9 +284,9 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { List deviceList = new ArrayList<>(); deviceList.add(device); - MonitoringManager mm = new MonitoringManagerImpl(); - mm.addMonitoringOperation(deviceList); - + MonitoringManager manager = new MonitoringManagerImpl(); + manager.addMonitoringOperation(deviceList); + policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); Assert.assertFalse(deviceCompliance, "Policy was compliant even though the response was not compliant"); From ca92c5029fe30bbfd117972378f515b05ba44c69 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 22 Jan 2018 15:25:40 +0530 Subject: [PATCH 7/7] Changing the variable name --- .../core/service/DeviceManagementProviderServiceImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 2d86ff9861..439448b382 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 @@ -1181,7 +1181,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public List getPolicyMonitoringEnableDeviceTypes() throws DeviceManagementException { List deviceTypes = this.getAvailableDeviceTypes(); - List deviceTyepsToMonitor = new ArrayList<>(); + List deviceTypesToMonitor = new ArrayList<>(); int tenantId = this.getTenantId(); Map registeredTypes = pluginRepository.getAllDeviceManagementServices(tenantId); @@ -1192,12 +1192,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceType.getGeneralConfig().isPolicyMonitoringEnabled()) { for (String type : deviceTypes) { if (type.equalsIgnoreCase(deviceType.getType())) { - deviceTyepsToMonitor.add(type); + deviceTypesToMonitor.add(type); } } } } - return deviceTyepsToMonitor; + return deviceTypesToMonitor; } @Override