From 6263a550ffc5f7b550751237ced5f67f2bd18e4d Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 25 Jun 2015 20:25:08 +0530 Subject: [PATCH 01/10] Set device enrolment status --- .../device/mgt/common/EnrolmentInfo.java | 4 ++-- .../mgt/core/dao/impl/DeviceDAOImpl.java | 10 +++++--- .../DeviceManagementProviderServiceImpl.java | 24 +++++++++++++++++-- .../mgt/core/util/DeviceManagerUtil.java | 1 + 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java index f002c617b1..997c445271 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/EnrolmentInfo.java @@ -26,11 +26,11 @@ public class EnrolmentInfo { private Status status; private String owner; - public enum Status { + public static enum Status { CREATED, ACTIVE, INACTIVE, UNCLAIMED, SUSPENDED, BLOCKED, REMOVED } - public enum OwnerShip { + public static enum OwnerShip { BYOD, COPE } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 9ca8317ac1..0d64e67e6e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -76,12 +76,16 @@ public class DeviceDAOImpl implements DeviceDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String sql = "UPDATE DM_DEVICE SET STATUS = ?, OWNER = ? WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?"; + String sql = "UPDATE DM_DEVICE SET STATUS = ?, OWNER = ?, DATE_OF_ENROLLMENT=?, " + + "DATE_OF_LAST_UPDATE=? WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ? AND DEVICE_TYPE_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, device.getEnrolmentInfo().getStatus().toString()); stmt.setString(2, device.getEnrolmentInfo().getOwner()); - stmt.setString(3, device.getDeviceIdentifier()); - stmt.setInt(4, tenantId); + stmt.setLong(3, device.getEnrolmentInfo().getDateOfEnrolment()); + stmt.setLong(4, device.getEnrolmentInfo().getDateOfLastUpdate()); + stmt.setString(5, device.getDeviceIdentifier()); + stmt.setInt(6, typeId); + stmt.setInt(7, tenantId); stmt.executeUpdate(); } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while enrolling device '" + 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 38d6a6f8f2..5b768e7ae8 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 @@ -44,6 +44,7 @@ import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; +import java.util.Date; import java.util.List; public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, PluginInitializationListener { @@ -100,6 +101,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.beginTransaction(); DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); + device.getEnrolmentInfo().setDateOfEnrolment(new Date().getTime()); + device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); deviceDAO.addDevice(type.getId(), device, tenantId); DeviceManagementDAOFactory.commitTransaction(); @@ -121,6 +124,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return status; } + + @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { DeviceManager dms = @@ -130,9 +135,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManagementDAOFactory.beginTransaction(); - DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); - deviceDAO.updateDevice(type.getId(), device, tenantId); + deviceDAO.updateDevice(type.getId(),device, tenantId); DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { @@ -155,8 +159,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManager dms = this.getPluginRepository().getDeviceManagementService(deviceId.getType()); + try { + Device device = deviceDAO.getDevice(deviceId,tenantId); + DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); + + device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); + device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); + deviceDAO.updateDevice(deviceType.getId(), device, tenantId); + + } catch (DeviceManagementDAOException e) { + String errorMsg = "Error occurred while fetch device for device Identifier:"; + log.error(errorMsg + deviceId.toString(),e); + throw new DeviceManagementException(errorMsg, e); + + } return dms.disenrollDevice(deviceId); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 126d60b543..4824537db4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -30,6 +30,7 @@ import org.wso2.carbon.apimgt.impl.APIConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.core.api.mgt.APIConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; From f3dd402e4bde8a8070ebf0feeafc40096de1aff3 Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 25 Jun 2015 21:13:55 +0530 Subject: [PATCH 02/10] Set device status to active when poll operations --- .../operation/mgt/OperationManagerImpl.java | 34 +++++++++++++++++++ .../DeviceManagementProviderService.java | 1 + .../DeviceManagementProviderServiceImpl.java | 17 +++++++++- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 148e32a124..da551394e3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -24,12 +24,15 @@ import org.wso2.carbon.context.CarbonContext; 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.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; @@ -57,6 +60,7 @@ public class OperationManagerImpl implements OperationManager { private OperationMappingDAO operationMappingDAO; private OperationDAO operationDAO; private DeviceDAO deviceDAO; + private DeviceTypeDAO deviceTypeDAO; public OperationManagerImpl() { commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); @@ -66,6 +70,7 @@ public class OperationManagerImpl implements OperationManager { operationMappingDAO = OperationManagementDAOFactory.getOperationMappingDAO(); operationDAO = OperationManagementDAOFactory.getOperationDAO(); deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); } @Override @@ -169,6 +174,21 @@ public class OperationManagerImpl implements OperationManager { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); device = deviceDAO.getDevice(deviceId, tenantId); + if (device.getEnrolmentInfo().getStatus() !=null && !device.getEnrolmentInfo().getStatus().equals( + EnrolmentInfo.Status.ACTIVE)){ + try { + DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .updateDeviceEnrolmentInfo(device, + EnrolmentInfo.Status.ACTIVE); + }catch (DeviceManagementException deviceMgtEx){ + String errorMsg = "Error occurred while update enrol status: "+deviceId.toString(); + log.error(errorMsg, deviceMgtEx); + throw new OperationManagementException(errorMsg, deviceMgtEx); + } + } + + + if (device == null) { throw new OperationManagementException("Device not found for given device " + "Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); @@ -225,6 +245,20 @@ public class OperationManagerImpl implements OperationManager { } org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO .getNextOperation(device.getId()); + + if (device.getEnrolmentInfo().getStatus() !=null && !device.getEnrolmentInfo().getStatus().equals( + EnrolmentInfo.Status.ACTIVE)){ + try { + DeviceManagementDataHolder.getInstance().getDeviceManagementProvider() + .updateDeviceEnrolmentInfo(device, + EnrolmentInfo.Status.ACTIVE); + }catch (DeviceManagementException deviceMgtEx){ + String errorMsg = "Error occurred while update enrol status: "+deviceId.toString(); + log.error(errorMsg, deviceMgtEx); + throw new OperationManagementException(errorMsg, deviceMgtEx); + } + } + if (dtoOperation != null) { if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND .equals(dtoOperation.getType())) { 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 b1f3e28947..9602ad390b 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 @@ -87,4 +87,5 @@ public interface DeviceManagementProviderService extends DeviceManager, LicenseM */ List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; + void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) 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 5b768e7ae8..ad002b76cd 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 @@ -137,7 +137,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.beginTransaction(); DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); deviceDAO.updateDevice(type.getId(),device, tenantId); - DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { try { @@ -652,6 +651,22 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return null; } + @Override + public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { + + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); + device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); + device.getEnrolmentInfo().setStatus(status); + deviceDAO.updateDevice(deviceType.getId(), device, tenantId); + }catch (DeviceManagementDAOException deviceDaoEx){ + String errorMsg = "Error occured update device enrolment status:"+device.getId(); + log.error(errorMsg, deviceDaoEx); + throw new DeviceManagementException(errorMsg, deviceDaoEx); + } + } + @Override public void registerDeviceManagementService(DeviceManagementService deviceManagementService) { try { From d28de90679d5c4a0efacf9c116d51b4cc7298b9f Mon Sep 17 00:00:00 2001 From: prabathabey Date: Tue, 30 Jun 2015 12:35:25 +0530 Subject: [PATCH 03/10] Fixing an invalid database table name used in DeviceDAOImpl --- .../org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 0d64e67e6e..16a5a4604f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -145,7 +145,7 @@ public class DeviceDAOImpl implements DeviceDAO { conn = this.getConnection(); String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DATE_OF_ENROLLMENT, " + "d.DATE_OF_LAST_UPDATE, d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, " + - "d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID, t.NAME AS DEVICE_TYPE_NAME FROM DM_DEVICE d, DEVICE_TYPE t " + + "d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID, t.NAME AS DEVICE_TYPE_NAME FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "WHERE d.DEVICE_TYPE_ID = t.ID AND d.TENANT_ID = ? "; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); From 567f78abb6ae3ecafc8e74f1df1cd4b3fe7507b9 Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 2 Jul 2015 15:06:18 +0530 Subject: [PATCH 04/10] Add additional fields to load device --- .../device/mgt/core/dao/impl/DeviceDAOImpl.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index 16a5a4604f..ad447730e1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -116,7 +116,7 @@ public class DeviceDAOImpl implements DeviceDAO { conn = this.getConnection(); String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, d.OWNERSHIP, d.STATUS, " + - "d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DM_DEVICE_TYPE dt WHERE " + + "d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID, dt.NAME AS DEVICE_TYPE_NAME FROM DM_DEVICE d, DM_DEVICE_TYPE dt WHERE " + "dt.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getType()); @@ -173,7 +173,7 @@ public class DeviceDAOImpl implements DeviceDAO { try { conn = this.getConnection(); String selectDBQueryForType = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, " + - "d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM DM_DEVICE d, DM_DEVICE_TYPE t " + + "d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID,t.NAME AS DEVICE_TYPE_NAME FROM DM_DEVICE d, DM_DEVICE_TYPE t " + "WHERE d.DM_DEVICE.DEVICE_TYPE_ID = t.ID AND t.NAME = ? AND d.TENANT_ID = ?"; stmt = conn.prepareStatement(selectDBQueryForType); stmt.setString(1, type); @@ -200,7 +200,7 @@ public class DeviceDAOImpl implements DeviceDAO { try { conn = this.getConnection(); stmt = conn.prepareStatement( - "SELECT t.NAME AS DEVICE_TYPE, d.ID AS DEVICE_ID, d.DESCRIPTION, " + + "SELECT t.NAME AS DEVICE_TYPE_NAME, d.ID AS DEVICE_ID, d.DESCRIPTION, " + "d.NAME AS DEVICE_NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, " + "d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, " + "d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM " + @@ -273,7 +273,7 @@ public class DeviceDAOImpl implements DeviceDAO { conn = this.getConnection(); stmt = conn.prepareStatement( "SELECT d.ID AS DEVICE_ID, d.NAME AS DEVICE_NAME, t.ID AS DEVICE_TYPE_ID, d.DESCRIPTION, " + - "t.NAME AS DEVICE_TYPE, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, " + + "t.NAME AS DEVICE_TYPE_NAME, d.DATE_OF_ENROLLMENT, d.DATE_OF_LAST_UPDATE, " + "d.OWNERSHIP, d.STATUS, d.DEVICE_TYPE_ID, " + "d.DEVICE_IDENTIFICATION, d.OWNER, d.TENANT_ID FROM " + "DM_DEVICE d, DM_DEVICE_TYPE t WHERE d.DEVICE_TYPE_ID = t.ID " + @@ -318,13 +318,14 @@ public class DeviceDAOImpl implements DeviceDAO { } private Device loadDevice(ResultSet rs) throws SQLException { + Device device = new Device(); DeviceType deviceType = new DeviceType(); - deviceType.setId(rs.getInt("ID")); - deviceType.setName(rs.getString("DEVICE_NAME")); - device.setId(rs.getInt("DEVICE_TYPE_ID")); + deviceType.setId(rs.getInt("DEVICE_TYPE_ID")); + deviceType.setName(rs.getString("DEVICE_TYPE_NAME")); + device.setId(rs.getInt("DEVICE_ID")); device.setDescription(rs.getString("DESCRIPTION")); - device.setType(rs.getString("DEVICE_TYPE")); + device.setType(rs.getString("DEVICE_TYPE_NAME")); device.setDeviceIdentifier(rs.getString("DEVICE_IDENTIFICATION")); EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); From 65b1ea70abea82d7c0b2ced54d45f4b97d099b72 Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 2 Jul 2015 18:07:35 +0530 Subject: [PATCH 05/10] Add properties to store mobile app specific meta data --- .../device/mgt/common/app/mgt/Application.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java index 11a2313022..c279ab9934 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java @@ -18,6 +18,9 @@ */ package org.wso2.carbon.device.mgt.common.app.mgt; +import java.util.List; +import java.util.Properties; + public class Application { private String id; @@ -29,6 +32,7 @@ public class Application { private String imageUrl; private String version; private String type; + private List appProperties; public String getType() { return type; @@ -105,4 +109,12 @@ public class Application { this.category = category; } + public List getAppProperties() { + return appProperties; + } + + public void setAppProperties(List appProperties) { + this.appProperties = appProperties; + } + } From d30048d94de6ca9c1cd8fd699d43c6ce2245f5d5 Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 3 Jul 2015 11:43:59 +0530 Subject: [PATCH 06/10] change properties --- .../wso2/carbon/device/mgt/common/app/mgt/Application.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java index c279ab9934..3762f37906 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java @@ -32,7 +32,7 @@ public class Application { private String imageUrl; private String version; private String type; - private List appProperties; + private Properties appProperties; public String getType() { return type; @@ -109,12 +109,13 @@ public class Application { this.category = category; } - public List getAppProperties() { + public Properties getAppProperties() { return appProperties; } - public void setAppProperties(List appProperties) { + public void setAppProperties(Properties appProperties) { this.appProperties = appProperties; } + } From e4d156a77e0a7dd5468f0bacce4ccd4c8ac2c8f4 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Fri, 3 Jul 2015 13:03:27 +0530 Subject: [PATCH 07/10] Adding policy monitoring to core, this is only having the monitoring daos and manager class and exceptions --- .../mgt/common/Monitor/ComplianceData.java | 80 ++++++ .../mgt/common/Monitor/ComplianceFeature.java | 63 +++++ .../Monitor/PolicyComplianceException.java | 56 +++++ .../mgt/common/PolicyAdministratorPoint.java | 6 +- .../common/spi/PolicyMonitoringService.java | 33 +++ .../policy/mgt/core/PolicyManagerService.java | 12 + .../mgt/core/PolicyManagerServiceImpl.java | 43 +++- .../policy/mgt/core/dao/FeatureDAO.java | 4 +- .../policy/mgt/core/dao/MonitoringDAO.java | 42 ++++ .../mgt/core/dao/MonitoringDAOException.java | 57 +++++ .../carbon/policy/mgt/core/dao/PolicyDAO.java | 5 + .../core/dao/PolicyManagementDAOFactory.java | 5 + .../mgt/core/dao/impl/FeatureDAOImpl.java | 12 +- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 235 ++++++++++++++++++ .../mgt/core/dao/impl/PolicyDAOImpl.java | 106 +++++++- .../impl/PolicyAdministratorPointImpl.java | 4 +- .../internal/PolicyManagementDataHolder.java | 17 ++ .../PolicyManagementServiceComponent.java | 24 ++ .../policy/mgt/core/mgt/FeatureManager.java | 4 +- .../mgt/core/mgt/MonitoringManager.java | 39 +++ .../policy/mgt/core/mgt/PolicyManager.java | 2 + .../mgt/core/mgt/impl/FeatureManagerImpl.java | 12 +- .../core/mgt/impl/MonitoringManagerImpl.java | 183 ++++++++++++++ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 37 ++- .../core/service/PolicyManagementService.java | 28 ++- .../mgt/core/util/PolicyManagerUtil.java | 15 +- .../policy/mgt/core/PolicyDAOTestCase.java | 2 + .../src/test/resources/sql/CreateH2TestDB.sql | 204 ++++++++------- .../src/test/resources/testng.xml | 2 +- .../decision/point/SimpleEvaluationImpl.java | 4 +- 30 files changed, 1184 insertions(+), 152 deletions(-) create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceFeature.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/PolicyComplianceException.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAOException.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java create mode 100644 components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java new file mode 100644 index 0000000000..85234fff61 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java @@ -0,0 +1,80 @@ +/* + * 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.mgt.common.Monitor; + +import java.util.List; + +public class ComplianceData { + + private int id; + private int deviceId; + private int policyId; + List complianceFeatures; + private boolean status; + private String message; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getDeviceId() { + return deviceId; + } + + public void setDeviceId(int deviceId) { + this.deviceId = deviceId; + } + + public int getPolicyId() { + return policyId; + } + + public void setPolicyId(int policyId) { + this.policyId = policyId; + } + + public List getComplianceFeatures() { + return complianceFeatures; + } + + public void setComplianceFeatures(List complianceFeatures) { + this.complianceFeatures = complianceFeatures; + } + + public boolean isStatus() { + return status; + } + + public void setStatus(boolean status) { + this.status = status; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceFeature.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceFeature.java new file mode 100644 index 0000000000..7c4589a8ab --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceFeature.java @@ -0,0 +1,63 @@ +/* + * 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.mgt.common.Monitor; + +import org.wso2.carbon.policy.mgt.common.ProfileFeature; + +public class ComplianceFeature { + + private ProfileFeature feature; + private String featureCode; + private boolean compliance; + private String message; + + + public ProfileFeature getFeature() { + return feature; + } + + public void setFeature(ProfileFeature feature) { + this.feature = feature; + } + + public String getFeatureCode() { + return featureCode; + } + + public void setFeatureCode(String featureCode) { + this.featureCode = featureCode; + } + + public boolean isCompliance() { + return compliance; + } + + public void setCompliance(boolean compliance) { + this.compliance = compliance; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/PolicyComplianceException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/PolicyComplianceException.java new file mode 100644 index 0000000000..d579546dd9 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/PolicyComplianceException.java @@ -0,0 +1,56 @@ +/* + * 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.mgt.common.Monitor; + +public class PolicyComplianceException extends Exception { + + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public PolicyComplianceException(String message) { + super(message); + setErrorMessage(message); + } + + public PolicyComplianceException(String message, Exception ex) { + super(message, ex); + setErrorMessage(message); + } + + public PolicyComplianceException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public PolicyComplianceException() { + super(); + } + + public PolicyComplianceException(Throwable cause) { + super(cause); + } +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java index 1910ee4615..f59e722d5f 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyAdministratorPoint.java @@ -140,9 +140,9 @@ public interface PolicyAdministratorPoint { List getProfiles() throws PolicyManagementException; - Feature addFeature(Feature feature) throws FeatureManagementException; - - Feature updateFeature(Feature feature) throws FeatureManagementException; +// Feature addFeature(Feature feature) throws FeatureManagementException; +// +// Feature updateFeature(Feature feature) throws FeatureManagementException; boolean deleteFeature(int featureId) throws FeatureManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java new file mode 100644 index 0000000000..4d5e34b368 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java @@ -0,0 +1,33 @@ +/* + * 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.mgt.common.spi; + +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; +import org.wso2.carbon.policy.mgt.common.Policy; + +import java.util.List; + +public interface PolicyMonitoringService { + + List checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object response) + throws PolicyComplianceException; +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java index 0214e34448..3a5a48ab52 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java @@ -22,6 +22,9 @@ package org.wso2.carbon.policy.mgt.core; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; @@ -34,9 +37,11 @@ import java.util.List; public interface PolicyManagerService { +/* Feature addFeature(Feature feature) throws FeatureManagementException; Feature updateFeature(Feature feature) throws FeatureManagementException; +*/ Profile addProfile(Profile profile) throws PolicyManagementException; @@ -65,4 +70,11 @@ public interface PolicyManagerService { PolicyEvaluationPoint getPEP() throws PolicyManagementException; int getPolicyCount() throws PolicyManagementException; + + List CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object + deviceResponse) throws PolicyComplianceException; + + boolean checkCompliance(DeviceIdentifier deviceIdentifier, Object response) throws PolicyComplianceException; + + ComplianceData getDeviceCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; } 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 c6da23346d..c4fcd2a4e8 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 @@ -27,9 +27,14 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.policy.mgt.common.*; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.impl.PolicyAdministratorPointImpl; import org.wso2.carbon.policy.mgt.core.impl.PolicyInformationPointImpl; 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.util.PolicyManagementConstants; import java.util.ArrayList; @@ -41,19 +46,11 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { private static final Log log = LogFactory.getLog(PolicyManagerServiceImpl.class); PolicyAdministratorPointImpl policyAdministratorPoint; + MonitoringManager monitoringManager; public PolicyManagerServiceImpl() { policyAdministratorPoint = new PolicyAdministratorPointImpl(); - } - - @Override - public Feature addFeature(Feature feature) throws FeatureManagementException { - return policyAdministratorPoint.addFeature(feature); - } - - @Override - public Feature updateFeature(Feature feature) throws FeatureManagementException { - return policyAdministratorPoint.updateFeature(feature); + monitoringManager = new MonitoringManagerImpl(); } @Override @@ -144,7 +141,8 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { FeatureManagementException { try { - List effectiveFeatures = PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(). + List effectiveFeatures = PolicyManagementDataHolder.getInstance() + .getPolicyEvaluationPoint(). getEffectiveFeatures(deviceIdentifier); List deviceIdentifiers = new ArrayList(); @@ -211,4 +209,27 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { public int getPolicyCount() throws PolicyManagementException { return policyAdministratorPoint.getPolicyCount(); } + + @Override + public List CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object + deviceResponse) throws PolicyComplianceException { + return monitoringManager.checkPolicyCompliance(deviceIdentifier, deviceResponse); + } + + @Override + public boolean checkCompliance(DeviceIdentifier deviceIdentifier, Object response) throws + PolicyComplianceException { + + List complianceFeatures = + monitoringManager.checkPolicyCompliance(deviceIdentifier, response); + if (complianceFeatures == null || complianceFeatures.isEmpty()) { + return false; + } + return true; + } + + @Override + public ComplianceData getDeviceCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { + return monitoringManager.getDevicePolicyCompliance(deviceIdentifier); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java index f35e5070ea..1c08a18789 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/FeatureDAO.java @@ -27,11 +27,11 @@ import java.util.List; public interface FeatureDAO { - Feature addFeature(Feature feature) throws FeatureManagerDAOException; +/* Feature addFeature(Feature feature) throws FeatureManagerDAOException; List addFeatures(List feature) throws FeatureManagerDAOException; - Feature updateFeature(Feature feature) throws FeatureManagerDAOException; + Feature updateFeature(Feature feature) throws FeatureManagerDAOException;*/ ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java new file mode 100644 index 0000000000..e97501dc05 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java @@ -0,0 +1,42 @@ +/* + * 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.mgt.core.dao; + +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; + +import java.util.List; +import java.util.Map; + +public interface MonitoringDAO { + + int setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException; + + void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException; + + void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List complianceFeatures) + throws MonitoringDAOException; + + ComplianceData getCompliance(int deviceId) throws MonitoringDAOException; + + List getNoneComplianceFeatures(int policyComplianceStatusId) throws MonitoringDAOException; + + void deleteNoneComplianceData(int deviceId) throws MonitoringDAOException; +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAOException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAOException.java new file mode 100644 index 0000000000..5ab28f187f --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAOException.java @@ -0,0 +1,57 @@ +/* + * 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.mgt.core.dao; + +public class MonitoringDAOException extends Exception{ + + private String monitoringDAOErrorMessage; + + public String getMonitoringDAOErrorMessage() { + return monitoringDAOErrorMessage; + } + + public void setMonitoringDAOErrorMessage(String monitoringDAOErrorMessage) { + this.monitoringDAOErrorMessage = monitoringDAOErrorMessage; + } + + public MonitoringDAOException(String message) { + super(message); + setMonitoringDAOErrorMessage(message); + } + + public MonitoringDAOException(String message, Exception ex) { + super(message, ex); + setMonitoringDAOErrorMessage(message); + } + + public MonitoringDAOException(String message, Throwable cause) { + super(message, cause); + setMonitoringDAOErrorMessage(message); + } + + public MonitoringDAOException() { + super(); + } + + public MonitoringDAOException(Throwable cause) { + super(cause); + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index b8c3fb32d5..92940de313 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -19,6 +19,7 @@ package org.wso2.carbon.policy.mgt.core.dao; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Criterion; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyCriterion; @@ -99,4 +100,8 @@ public interface PolicyDAO { boolean checkPolicyAvailable(int deviceId) throws PolicyManagerDAOException; int getPolicyCount() throws PolicyManagerDAOException; + + int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException; + + Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java index ab0c5e3697..d7d74daf50 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyManagementDAOFactory.java @@ -24,6 +24,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOE import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.policy.mgt.core.dao.impl.FeatureDAOImpl; +import org.wso2.carbon.policy.mgt.core.dao.impl.MonitoringDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; @@ -67,6 +68,10 @@ public class PolicyManagementDAOFactory { return new FeatureDAOImpl(); } + public static MonitoringDAO getMonitoringDAO() { + return new MonitoringDAOImpl(); + } + /** * Resolve data source from the data source definition * diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index 9324d3ba45..1c4b67a564 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -42,7 +42,7 @@ public class FeatureDAOImpl implements FeatureDAO { private static final Log log = LogFactory.getLog(FeatureDAOImpl.class); - @Override +/* @Override public Feature addFeature(Feature feature) throws FeatureManagerDAOException { Connection conn; @@ -75,9 +75,9 @@ public class FeatureDAOImpl implements FeatureDAO { PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); } return feature; - } + }*/ - @Override + /* @Override public List addFeatures(List features) throws FeatureManagerDAOException { Connection conn; @@ -119,10 +119,10 @@ public class FeatureDAOImpl implements FeatureDAO { PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); } return featureList; - } + }*/ - @Override + /* @Override public Feature updateFeature(Feature feature) throws FeatureManagerDAOException { Connection conn; @@ -147,7 +147,7 @@ public class FeatureDAOImpl implements FeatureDAO { } return feature; - } + }*/ @Override public ProfileFeature addProfileFeature(ProfileFeature feature, int profileId) throws FeatureManagerDAOException { 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 new file mode 100644 index 0000000000..e34f4eab84 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -0,0 +1,235 @@ +/* + * 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.mgt.core.dao.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAO; +import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAOException; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class MonitoringDAOImpl implements MonitoringDAO { + + private static final Log log = LogFactory.getLog(MonitoringDAOImpl.class); + + @Override + public int setDeviceAsNoneCompliance(int deviceId, int policyId) throws MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet generatedKeys = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS) VALUES" + + " (?, ?, ?) "; + stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt.setInt(1, deviceId); + stmt.setInt(2, policyId); + stmt.setInt(3, 0); + stmt.executeUpdate(); + + generatedKeys = stmt.getGeneratedKeys(); + if (generatedKeys.next()) { + return generatedKeys.getInt(1); + } else { + return 0; + } + + } catch (SQLException e) { + String msg = "Error occurred while adding the none compliance to the database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys); + } + + } + + @Override + public void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + + stmt.executeUpdate(); + + } catch (SQLException e) { + String msg = "Error occurred while deleting the none compliance to the database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List + complianceFeatures) throws MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS) " + + "VALUES" + + " (?, ?, ?) "; + stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + for (ComplianceFeature feature : complianceFeatures) { + stmt.setInt(1, policyComplianceStatusId); + stmt.setString(2, feature.getFeatureCode()); + stmt.setString(3, String.valueOf(feature.isCompliance())); + stmt.addBatch(); + } + stmt.executeBatch(); + + } catch (SQLException e) { + String msg = "Error occurred while adding the none compliance features to the database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public ComplianceData getCompliance(int deviceId) throws MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + ComplianceData complianceData = null; + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + complianceData.setId(resultSet.getInt("ID")); + complianceData.setDeviceId(resultSet.getInt("DEVICE_ID")); + complianceData.setPolicyId(resultSet.getInt("POLICY_ID")); + complianceData.setStatus(resultSet.getBoolean("STATUS")); + } + return complianceData; + + } catch (SQLException e) { + String msg = "Unable to retrieve compliance data from database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + } + + @Override + public List getNoneComplianceFeatures(int policyComplianceStatusId) throws + MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List complianceFeatures = new ArrayList(); + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_POLICY_COMPLIANCE_FEATURES WHERE COMPLIANCE_STATUS_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, policyComplianceStatusId); + + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + ComplianceFeature feature = new ComplianceFeature(); + feature.setFeatureCode(resultSet.getString("FEATURE_CODE")); + feature.setMessage(resultSet.getString("STATUS")); + complianceFeatures.add(feature); + } + return complianceFeatures; + + } catch (SQLException e) { + String msg = "Unable to retrieve compliance features data from database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + + } + + @Override + public void deleteNoneComplianceData(int deviceId) throws MonitoringDAOException { + + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String query = "DELETE FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + stmt.executeUpdate(); + + } catch (SQLException e) { + String msg = "Unable to delete compliance data from database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + + } + + + private Connection getConnection() throws MonitoringDAOException { + try { + return PolicyManagementDAOFactory.getConnection(); + } catch (PolicyManagerDAOException e) { + throw new MonitoringDAOException("Error occurred while obtaining a connection from the policy " + + "management metadata repository datasource", e); + } + } + + + private void closeConnection() { + try { + PolicyManagementDAOFactory.closeConnection(); + } catch (PolicyManagerDAOException e) { + log.warn("Unable to close the database connection."); + } + } +} 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 551243e318..cf82d26ce9 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 @@ -21,16 +21,21 @@ package org.wso2.carbon.policy.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Criterion; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyCriterion; import org.wso2.carbon.policy.mgt.common.ProfileFeature; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; import java.sql.*; import java.util.ArrayList; import java.util.Calendar; @@ -429,7 +434,8 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, CONTENT) VALUES (?, ?, ?, ?)"; + String query = "INSERT INTO DM_POLICY_CRITERIA_PROPERTIES (POLICY_CRITERION_ID, PROP_KEY, PROP_VALUE, " + + "CONTENT) VALUES (?, ?, ?, ?)"; stmt = conn.prepareStatement(query); for (PolicyCriterion criterion : policyCriteria) { @@ -517,7 +523,8 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ? WHERE ID = ?"; + String query = "UPDATE DM_POLICY SET NAME= ?, TENANT_ID = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ?" + + " WHERE ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getTenantId()); @@ -740,7 +747,6 @@ public class PolicyDAOImpl implements PolicyDAO { } - @Override public void addEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) throws PolicyManagerDAOException { @@ -1060,7 +1066,8 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE) VALUES (?, ?, ?, ?, ?, ?)"; + String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE)" + + " VALUES (?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setString(1, policy.getPolicyName()); @@ -1191,4 +1198,95 @@ public class PolicyDAOImpl implements PolicyDAO { } } + @Override + public int getAppliedPolicyId(int deviceId) throws PolicyManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + return resultSet.getInt("POLICY_ID"); + } + + } catch (SQLException e) { + String msg = "Error occurred while getting the applied policy id."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + + return 0; + } + + @Override + public Policy getAppliedPolicy(int deviceId) throws PolicyManagerDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + + Policy policy = null; + + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setInt(1, deviceId); + resultSet = stmt.executeQuery(); + + + ByteArrayInputStream bais = null; + ObjectInputStream ois = null; + byte[] contentBytes; + try { + contentBytes = (byte[]) resultSet.getBytes("POLICY_CONTENT"); + bais = new ByteArrayInputStream(contentBytes); + ois = new ObjectInputStream(bais); + policy = (Policy) ois.readObject(); + } finally { + if (bais != null) { + try { + bais.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } + } + if (ois != null) { + try { + ois.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } + } + } + + } catch (SQLException e) { + String msg = "Error occurred while getting the applied policy."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } catch (IOException e) { + String msg = "Unable to read the byte stream for content"; + log.error(msg); + throw new PolicyManagerDAOException(msg, e); + } catch (ClassNotFoundException e) { + String msg = "Class not found while converting the object"; + log.error(msg); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + return policy; + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index 2041d99713..052199f4e0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -188,7 +188,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { } } - @Override + /* @Override public Feature addFeature(Feature feature) throws FeatureManagementException { return featureManager.addFeature(feature); } @@ -197,7 +197,7 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { public Feature updateFeature(Feature feature) throws FeatureManagementException { return featureManager.updateFeature(feature); - } + }*/ @Override public boolean deleteFeature(int featureId) throws FeatureManagementException { 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 506488f762..4f2d664494 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 @@ -21,9 +21,12 @@ package org.wso2.carbon.policy.mgt.core.internal; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; +import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; +import java.util.Map; + public class PolicyManagementDataHolder { private RealmService realmService; @@ -31,6 +34,8 @@ public class PolicyManagementDataHolder { private PolicyEvaluationPoint policyEvaluationPoint; private PolicyInformationPoint policyInformationPoint; private DeviceManagementProviderService deviceManagementService; + private Map policyMonitoringServiceMap; + private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder(); private PolicyManagementDataHolder() {} @@ -82,4 +87,16 @@ public class PolicyManagementDataHolder { public void setDeviceManagementService(DeviceManagementProviderService deviceManagementService) { this.deviceManagementService = deviceManagementService; } + + public PolicyMonitoringService getPolicyMonitoringService(String deviceType) { + return policyMonitoringServiceMap.get(deviceType); + } + + public void setPolicyMonitoringService(String deviceType, PolicyMonitoringService policyMonitoringService) { + this.policyMonitoringServiceMap.put(deviceType, policyMonitoringService); + } + + public void unsetPolicyMonitoringService(String deviceType) { + this.policyMonitoringServiceMap.remove(deviceType); + } } 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 c0f8450ac1..d6f72dba74 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 @@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; +import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager; @@ -51,6 +52,12 @@ import org.wso2.carbon.user.core.service.RealmService; * policy="dynamic" * bind="setDeviceManagementService" * unbind="unsetDeviceManagementService" + * @scr.reference name="org.wso2.carbon.policy.mgt.common.policy.monitor" + * interface="org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService" + * cardinality="0..n" + * policy="dynamic" + * bind="setPolicyMonitoringService" + * unbind="unsetPolicyMonitoringService" */ @SuppressWarnings("unused") public class PolicyManagementServiceComponent { @@ -143,4 +150,21 @@ public class PolicyManagementServiceComponent { PolicyManagementDataHolder.getInstance().setDeviceManagementService(null); } + + protected void setPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) { + if (log.isDebugEnabled()) { + log.debug("Setting Policy Monitoring Service"); + } + // TODO: FIX THE device type by taking from properties + PolicyManagementDataHolder.getInstance().setPolicyMonitoringService("", policyMonitoringService); + } + + protected void unsetPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) { + if (log.isDebugEnabled()) { + log.debug("Setting Policy Monitoring Service"); + } + // TODO: FIX THE device type by taking from properties + PolicyManagementDataHolder.getInstance().unsetPolicyMonitoringService(""); + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java index 5af6049f5b..8ccab296e5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/FeatureManager.java @@ -29,11 +29,11 @@ import java.util.List; public interface FeatureManager { - Feature addFeature(Feature feature) throws FeatureManagementException; + /*Feature addFeature(Feature feature) throws FeatureManagementException; public List addFeatures(List features) throws FeatureManagementException; - Feature updateFeature(Feature feature) throws FeatureManagementException; + Feature updateFeature(Feature feature) throws FeatureManagementException;*/ boolean deleteFeature(Feature feature) throws FeatureManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java new file mode 100644 index 0000000000..f0ca764d8c --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java @@ -0,0 +1,39 @@ +/* + * 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.mgt.core.mgt; + +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; + +import java.util.List; + +public interface MonitoringManager { + + List checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Object deviceResponse) + throws PolicyComplianceException; + + + boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; + + ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index 924a57116b..0aa8c11ac7 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -68,4 +68,6 @@ public interface PolicyManager { boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; int getPolicyCount() throws PolicyManagementException; + + Policy getAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java index 7b9f9aa801..94ee440a10 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/FeatureManagerImpl.java @@ -41,7 +41,7 @@ public class FeatureManagerImpl implements FeatureManager { featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); } - @Override + /*@Override public Feature addFeature(Feature feature) throws FeatureManagementException { try { PolicyManagementDAOFactory.beginTransaction(); @@ -68,9 +68,9 @@ public class FeatureManagerImpl implements FeatureManager { throw new FeatureManagementException(msg, e); } return feature; - } + }*/ - @Override + /*@Override public List addFeatures(List features) throws FeatureManagementException { try { PolicyManagementDAOFactory.beginTransaction(); @@ -98,9 +98,9 @@ public class FeatureManagerImpl implements FeatureManager { } return features; - } + }*/ - @Override + /* @Override public Feature updateFeature(Feature feature) throws FeatureManagementException { try { PolicyManagementDAOFactory.beginTransaction(); @@ -127,7 +127,7 @@ public class FeatureManagerImpl implements FeatureManager { throw new FeatureManagementException(msg, e); } return feature; - } + }*/ @Override public boolean deleteFeature(Feature feature) throws FeatureManagementException { 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 new file mode 100644 index 0000000000..56c4f593b8 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -0,0 +1,183 @@ +/* + * 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.mgt.core.mgt.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; +import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.ProfileFeature; +import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; +import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAO; +import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAOException; +import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +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.util.PolicyManagerUtil; + +import java.util.List; + +public class MonitoringManagerImpl implements MonitoringManager { + + private PolicyDAO policyDAO; + private DeviceDAO deviceDAO; + private MonitoringDAO monitoringDAO; + + private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class); + + public MonitoringManagerImpl() { + this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO(); + this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + this.monitoringDAO = PolicyManagementDAOFactory.getMonitoringDAO(); + } + + @Override + public List checkPolicyCompliance(DeviceIdentifier deviceIdentifier, + Object deviceResponse) throws PolicyComplianceException { + + List complianceFeatures; + try { + PolicyManagementDAOFactory.beginTransaction(); + int tenantId = PolicyManagerUtil.getTenantId(); + + Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + Policy policy = policyDAO.getAppliedPolicy(device.getId()); + PolicyMonitoringService monitoringService = PolicyManagementDataHolder.getInstance(). + getPolicyMonitoringService(deviceIdentifier.getType()); + + complianceFeatures = monitoringService.checkPolicyCompliance(deviceIdentifier, + policy, deviceResponse); + + if (!complianceFeatures.isEmpty()) { + int complianceId = monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId()); + monitoringDAO.addNoneComplianceFeatures(complianceId, device.getId(), complianceFeatures); + + List profileFeatures = policy.getProfile().getProfileFeaturesList(); + for (ComplianceFeature compFeature : complianceFeatures) { + for (ProfileFeature profFeature : profileFeatures) { + if (profFeature.getFeatureCode().equalsIgnoreCase(compFeature.getFeatureCode())) { + compFeature.setFeature(profFeature); + } + } + } + + } else { + monitoringDAO.setDeviceAsCompliance(device.getId(), policy.getId()); + } + PolicyManagementDAOFactory.commitTransaction(); + + } catch (DeviceManagementDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Unable tor retrieve device data from DB for " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } catch (PolicyManagerDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Unable tor retrieve policy data from DB for device " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } catch (MonitoringDAOException e) { + try { + PolicyManagementDAOFactory.rollbackTransaction(); + } catch (PolicyManagerDAOException e1) { + log.warn("Error occurred while roll backing the transaction."); + } + String msg = "Unable to add the none compliance features to database for device " + deviceIdentifier. + getId() + " - " + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } + return complianceFeatures; + } + + + @Override + public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { + + try { + int tenantId = PolicyManagerUtil.getTenantId(); + Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + ComplianceData complianceData = monitoringDAO.getCompliance(device.getId()); + if (complianceData == null || !complianceData.isStatus()) { + return false; + } + + } catch (DeviceManagementDAOException e) { + String msg = "Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + + } catch (MonitoringDAOException e) { + String msg = "Unable to retrieve compliance status for " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } + return true; + } + + @Override + public ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws + PolicyComplianceException { + + ComplianceData complianceData; + try { + int tenantId = PolicyManagerUtil.getTenantId(); + Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + complianceData = monitoringDAO.getCompliance(device.getId()); + List complianceFeatures = + monitoringDAO.getNoneComplianceFeatures(complianceData.getId()); + complianceData.setComplianceFeatures(complianceFeatures); + + } catch (DeviceManagementDAOException e) { + String msg = "Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + + } catch (MonitoringDAOException e) { + String msg = "Unable to retrieve compliance data for " + deviceIdentifier.getId() + " - " + + deviceIdentifier.getType(); + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } + return complianceData; + } +} 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 0426a0fad2..15013de7f0 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 @@ -30,6 +30,7 @@ import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.sql.Timestamp; import java.util.ArrayList; @@ -288,7 +289,7 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicy(policy); } List deviceList = new ArrayList(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { deviceList.add(deviceDAO.getDevice(deviceIdentifier, tenantId)); } @@ -517,7 +518,7 @@ public class PolicyManagerImpl implements PolicyManager { List policyIdList; List policies = new ArrayList(); try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); policyIdList = policyDAO.getPolicyIdsOfDevice(device); List tempPolicyList = this.getPolicies(); @@ -632,7 +633,7 @@ public class PolicyManagerImpl implements PolicyManager { List deviceIds; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId); for (int deviceId : deviceIds) { //TODO FIX ME @@ -654,9 +655,10 @@ public class PolicyManagerImpl implements PolicyManager { @Override public void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, int policyId, List profileFeatures) throws PolicyManagementException { + int deviceId = -1; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); deviceId = device.getId(); boolean exist = policyDAO.checkPolicyAvailable(deviceId); @@ -690,7 +692,7 @@ public class PolicyManagerImpl implements PolicyManager { boolean exist; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); exist = policyDAO.checkPolicyAvailable(device.getId()); } catch (PolicyManagerDAOException e) { @@ -709,7 +711,7 @@ public class PolicyManagerImpl implements PolicyManager { public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = PolicyManagerUtil.getTenantId(); Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); policyDAO.setPolicyApplied(device.getId()); return true; @@ -727,6 +729,7 @@ public class PolicyManagerImpl implements PolicyManager { @Override public int getPolicyCount() throws PolicyManagementException { + int policyCount = 0; try { policyCount = policyDAO.getPolicyCount(); @@ -737,4 +740,26 @@ public class PolicyManagerImpl implements PolicyManager { throw new PolicyManagementException(msg, e); } } + + @Override + public Policy getAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { + + Policy policy; + try { + int tenantId = PolicyManagerUtil.getTenantId(); + Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + int policyId = policyDAO.getAppliedPolicyId(device.getId()); + policy = policyDAO.getPolicy(policyId); + + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while getting device id."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while getting policy id or policy."; + log.error(msg, e); + throw new PolicyManagementException(msg, e); + } + return policy; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java index 46b51c7352..a6cebdd59c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java @@ -21,6 +21,9 @@ package org.wso2.carbon.policy.mgt.core.service; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceData; +import org.wso2.carbon.policy.mgt.common.Monitor.ComplianceFeature; +import org.wso2.carbon.policy.mgt.common.Monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; @@ -42,15 +45,6 @@ public class PolicyManagementService implements PolicyManagerService { policyManagerService = new PolicyManagerServiceImpl(); } - @Override - public Feature addFeature(Feature feature) throws FeatureManagementException { - return policyManagerService.addFeature(feature); - } - - @Override - public Feature updateFeature(Feature feature) throws FeatureManagementException { - return policyManagerService.updateFeature(feature); - } @Override public Profile addProfile(Profile profile) throws PolicyManagementException { @@ -122,4 +116,20 @@ public class PolicyManagementService implements PolicyManagerService { public int getPolicyCount() throws PolicyManagementException { return policyManagerService.getPolicyCount(); } + + @Override + public List CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object + deviceResponse) throws PolicyComplianceException { + return policyManagerService.CheckPolicyCompliance(deviceIdentifier, deviceResponse); + } + + @Override + public boolean checkCompliance(DeviceIdentifier deviceIdentifier, Object response) throws PolicyComplianceException { + return policyManagerService.checkCompliance(deviceIdentifier, response); + } + + @Override + public ComplianceData getDeviceCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { + return policyManagerService.getDeviceCompliance(deviceIdentifier); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 08acd03c79..8e2ca941d0 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -90,17 +90,12 @@ public class PolicyManagerUtil { //TODO: Get the tenant id proper way. This is has to be fix for test to run. int tenantId; - tenantId = MultitenantConstants.SUPER_TENANT_ID; -/* try { - if (PrivilegedCarbonContext.getThreadLocalCarbonContext() != null) { - tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - } else { - tenantId = MultitenantConstants.SUPER_TENANT_ID; - } - } catch (Exception e) { - - }*/ + if ("Super".equalsIgnoreCase(System.getProperty("GetTenantIDForTest"))) { + tenantId = MultitenantConstants.SUPER_TENANT_ID; + } else { + tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + } return tenantId; } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index a3138da535..f3fc808127 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -77,6 +77,8 @@ public class PolicyDAOTestCase { TestDBConfiguration dbConfig = getTestDBConfiguration(dbType); PoolProperties properties = new PoolProperties(); + System.setProperty("GetTenantIDForTest","Super"); + log.info("Database Type : " + dbTypeStr); switch (dbType) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index 7f291415de..b4bfe487a2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -1,49 +1,105 @@ +CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( + ID INT auto_increment NOT NULL, + NAME VARCHAR(300) NULL DEFAULT NULL, + PRIMARY KEY (ID) +); --- ----------------------------------------------------- --- Table DM_DEVICE_TYPE --- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS DM_DEVICE ( + ID INTEGER auto_increment NOT NULL, + DESCRIPTION TEXT NULL DEFAULT NULL, + NAME VARCHAR(100) NULL DEFAULT NULL, + DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL, + DATE_OF_LAST_UPDATE BIGINT NULL DEFAULT NULL, + OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, + STATUS VARCHAR(15) NULL DEFAULT NULL, + DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL, + OWNER VARCHAR(45) NULL DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) + REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); +CREATE TABLE IF NOT EXISTS DM_OPERATION ( + ID INTEGER AUTO_INCREMENT NOT NULL, + TYPE VARCHAR(50) NOT NULL, + CREATED_TIMESTAMP TIMESTAMP NOT NULL, + RECEIVED_TIMESTAMP TIMESTAMP NULL, + OPERATION_CODE VARCHAR(1000) NOT NULL, + PRIMARY KEY (ID) +); -CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( - ID INT(11) AUTO_INCREMENT , - NAME VARCHAR(300) NULL DEFAULT NULL , - PRIMARY KEY (ID) ) +CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + OPERATION_CONFIG BLOB DEFAULT NULL, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); -; +CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + ENABLED BOOLEAN NOT NULL DEFAULT FALSE, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); ---INSERT INTO DM_DEVICE_TYPE (NAME) VALUES ('ANDROID'); ---INSERT INTO DM_DEVICE_TYPE (NAME) VALUES ('IOS'); +CREATE TABLE IF NOT EXISTS DM_POLICY_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + ENABLED INTEGER NOT NULL DEFAULT 0, + OPERATION_DETAILS BLOB DEFAULT NULL, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_policy FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_DEVICE --- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS DM_PROFILE_OPERATION ( + OPERATION_ID INTEGER NOT NULL, + ENABLED INTEGER NOT NULL DEFAULT 0, + OPERATION_DETAILS BLOB DEFAULT NULL, + PRIMARY KEY (OPERATION_ID), + CONSTRAINT fk_dm_operation_profile FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); +CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING ( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + OPERATION_ID INTEGER NOT NULL, + STATUS VARCHAR(50) NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); -CREATE TABLE IF NOT EXISTS DM_DEVICE ( - ID INT(11) AUTO_INCREMENT NOT NULL , - DESCRIPTION TEXT NULL DEFAULT NULL , - NAME VARCHAR(100) NULL DEFAULT NULL , - DATE_OF_ENROLLMENT BIGINT(20) NULL DEFAULT NULL , - DATE_OF_LAST_UPDATE BIGINT(20) NULL DEFAULT NULL , - OWNERSHIP VARCHAR(45) NULL DEFAULT NULL , - STATUS VARCHAR(15) NULL DEFAULT NULL , - DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL , - DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL , - OWNER VARCHAR(45) NULL DEFAULT NULL , - TENANT_ID INT(11) NULL DEFAULT '0' , - PRIMARY KEY (ID) , - CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 - FOREIGN KEY (DEVICE_TYPE_ID ) - REFERENCES DM_DEVICE_TYPE (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION) +CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_RESPONSE ( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + OPERATION_ID INTEGER NOT NULL, + OPERATION_RESPONSE BLOB DEFAULT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_operation_response_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_device_operation_response_operation FOREIGN KEY (OPERATION_ID) REFERENCES + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); -; +CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATIONS ( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + APPLICATIONS BLOB DEFAULT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_applications_device FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, +); + +--- POLICY RELATED TABLES ---- --- ----------------------------------------------------- --- Table DM_PROFILE --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_PROFILE ( @@ -58,13 +114,11 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE ( FOREIGN KEY (DEVICE_TYPE_ID ) REFERENCES DM_DEVICE_TYPE (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) -; + ON UPDATE NO ACTION +); + --- ----------------------------------------------------- --- Table DM_POLICY --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_POLICY ( @@ -80,14 +134,10 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( FOREIGN KEY (PROFILE_ID ) REFERENCES DM_PROFILE (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) - -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_DEVICE_POLICY --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( @@ -104,14 +154,10 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( FOREIGN KEY (DEVICE_ID ) REFERENCES DM_DEVICE (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) - -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_DEVICE_TYPE_POLICY --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( @@ -128,15 +174,11 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( FOREIGN KEY (DEVICE_TYPE_ID ) REFERENCES DM_DEVICE_TYPE (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) - -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_PROFILE_FEATURES --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( @@ -150,14 +192,10 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( FOREIGN KEY (PROFILE_ID) REFERENCES DM_PROFILE (ID) ON DELETE NO ACTION - ON UPDATE NO ACTION) - -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_ROLE_POLICY --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY ( @@ -169,15 +207,11 @@ CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY ( FOREIGN KEY (POLICY_ID ) REFERENCES DM_POLICY (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) - -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table .DM_USER_POLICY --- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS DM_USER_POLICY ( ID INT NOT NULL AUTO_INCREMENT , @@ -188,10 +222,10 @@ CREATE TABLE IF NOT EXISTS DM_USER_POLICY ( FOREIGN KEY (POLICY_ID ) REFERENCES DM_POLICY (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) - ; - - + ON UPDATE NO ACTION +); + + CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY_APPLIED ( ID INT NOT NULL AUTO_INCREMENT , DEVICE_ID INT NOT NULL , @@ -211,14 +245,10 @@ CREATE TABLE IF NOT EXISTS DM_USER_POLICY ( FOREIGN KEY (POLICY_ID ) REFERENCES DM_POLICY (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION) -; + ON UPDATE NO ACTION +); --- ----------------------------------------------------- --- Table DM_CRITERIA --- ----------------------------------------------------- -DROP TABLE IF EXISTS DM_CRITERIA ; CREATE TABLE IF NOT EXISTS DM_CRITERIA ( ID INT NOT NULL AUTO_INCREMENT, @@ -228,10 +258,6 @@ CREATE TABLE IF NOT EXISTS DM_CRITERIA ( ); --- ----------------------------------------------------- --- Table DM_POLICY_CRITERIA --- ----------------------------------------------------- -DROP TABLE IF EXISTS DM_POLICY_CRITERIA ; CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA ( ID INT NOT NULL AUTO_INCREMENT, @@ -251,10 +277,6 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA ( ); --- ----------------------------------------------------- --- Table DM_POLICY_CRITERIA_PROPERTIES --- ----------------------------------------------------- -DROP TABLE IF EXISTS DM_POLICY_CRITERIA_PROPERTIES ; CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( ID INT NOT NULL AUTO_INCREMENT, @@ -271,3 +293,9 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( ); +-- POLICY RELATED TABLES FINISHED -- + + +-- TO:DO - Remove this INSERT sql statement. +--Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (1, 'android'); +--Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (2, 'ios'); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml index 474d352e7a..02107984d5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml @@ -25,7 +25,7 @@ - + \ No newline at end of file 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.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java index 188a742f3d..f7b7135eb5 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.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java @@ -61,8 +61,8 @@ public class SimpleEvaluationImpl implements SimpleEvaluation { return null; } //TODO : UNCOMMENT THE FOLLOWING CASE -// policyAdministratorPoint = policyManagerService.getPAP(); -// policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy); + policyAdministratorPoint = policyManagerService.getPAP(); + policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy); } From 20a9d1222f3224b3433744d1f2c446e279b33e88 Mon Sep 17 00:00:00 2001 From: harshanl Date: Fri, 3 Jul 2015 14:35:26 +0530 Subject: [PATCH 08/10] Added a method to retrieve list of applications installed on a device --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 17 ++++++ .../mgt/core/dao/impl/DeviceDAOImpl.java | 52 +++++++++++++++++++ .../DeviceManagementProviderService.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 12 ++++- 4 files changed, 80 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 4e13a34f89..fc4e34dfd8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -75,5 +75,22 @@ public interface DeviceDAO { */ List getDevicesByName(String deviceName, int tenantId) throws DeviceManagementDAOException; + /** + * Get the list of devices that matches with the given device name. + * + * @param id Name of the device + * @param applications List of applications + * @throws DeviceManagementDAOException + */ void addDeviceApplications(int id, Object applications) throws DeviceManagementDAOException; + + /** + * Get the list of devices that matches with the given device name. + * + * @param deviceId device id of the device + * @return List of Applications that are installed on the given device. + * @throws DeviceManagementDAOException + */ + List getInstalledApplications(int deviceId) + throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index ad447730e1..9a2d11528f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -18,6 +18,8 @@ package org.wso2.carbon.device.mgt.core.dao.impl; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; @@ -29,7 +31,11 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -40,6 +46,8 @@ import java.util.List; public class DeviceDAOImpl implements DeviceDAO { + private static final Log log = LogFactory.getLog(DeviceDAOImpl.class); + @Override public void addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { Connection conn; @@ -317,6 +325,50 @@ public class DeviceDAOImpl implements DeviceDAO { } } + @Override + public List getInstalledApplications(int deviceId) + throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + List applications = new ArrayList(); + Application application; + ByteArrayInputStream bais; + ObjectInputStream ois; + + try { + conn = this.getConnection(); + stmt = conn.prepareStatement( + "SELECT DEVICE_ID, APPLICATIONS FROM DM_DEVICE_APPLICATIONS WHERE DEVICE_ID = ?"); + stmt.setInt(1, deviceId); + ResultSet rs = stmt.executeQuery(); + + while (rs.next()) { + byte[] applicationDetails = rs.getBytes("APPLICATIONS"); + bais = new ByteArrayInputStream(applicationDetails); + ois = new ObjectInputStream(bais); + application = (Application) ois.readObject(); + applications.add(application); + } + + }catch (IOException e) { + String errorMsg = "IO Error occurred while de serialize the Application object"; + log.error(errorMsg, e); + throw new DeviceManagementDAOException(errorMsg, e); + } catch (ClassNotFoundException e) { + String errorMsg = "Class not found error occurred while de serialize the Application object"; + log.error(errorMsg, e); + throw new DeviceManagementDAOException(errorMsg, e); + } catch (SQLException e) { + String errorMsg = "SQL Error occurred while retrieving the list of Applications installed in device id '" + + deviceId; + log.error(errorMsg, e); + throw new DeviceManagementDAOException(errorMsg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + return applications; + } + private Device loadDevice(ResultSet rs) throws SQLException { Device device = new Device(); 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 9602ad390b..ba8fd26d59 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 @@ -82,7 +82,7 @@ public interface DeviceManagementProviderService extends DeviceManager, LicenseM * The method to get application list installed for the device. * * @param deviceIdentifier - * @return + * @return List of applications installed on the device * @throws DeviceManagementException */ List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) 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 ad002b76cd..09c090e117 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 @@ -648,7 +648,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { - return null; + Device device = null; + try { + device = this.getDevice(deviceIdentifier); + return deviceDAO.getInstalledApplications(device.getId()); + }catch (DeviceManagementDAOException deviceDaoEx){ + String errorMsg = "Error occured while fetching the Application List of device : " + device.getId(); + log.error(errorMsg, deviceDaoEx); + throw new DeviceManagementException(errorMsg, deviceDaoEx); + } } @Override @@ -661,7 +669,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv device.getEnrolmentInfo().setStatus(status); deviceDAO.updateDevice(deviceType.getId(), device, tenantId); }catch (DeviceManagementDAOException deviceDaoEx){ - String errorMsg = "Error occured update device enrolment status:"+device.getId(); + String errorMsg = "Error occured update device enrolment status : "+device.getId(); log.error(errorMsg, deviceDaoEx); throw new DeviceManagementException(errorMsg, deviceDaoEx); } From 8c59d82708809a0f8904ed3835a75e2ce4591973 Mon Sep 17 00:00:00 2001 From: Dilshan Edirisuriya Date: Fri, 3 Jul 2015 16:30:55 +0530 Subject: [PATCH 09/10] Making serializable --- .../wso2/carbon/device/mgt/common/app/mgt/Application.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java index 3762f37906..f699fd5ee3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/Application.java @@ -18,10 +18,10 @@ */ package org.wso2.carbon.device.mgt.common.app.mgt; -import java.util.List; +import java.io.Serializable; import java.util.Properties; -public class Application { +public class Application implements Serializable { private String id; private String packageName; From 952db0183decc7f51ffe559a3bdd1016822c9305 Mon Sep 17 00:00:00 2001 From: harshanl Date: Fri, 3 Jul 2015 18:13:02 +0530 Subject: [PATCH 10/10] Moved updateApplicationListOfDevice method to DeviceMgtService --- .../mgt/ApplicationManagementProviderService.java | 7 ------- .../ApplicationManagerProviderServiceImpl.java | 15 --------------- .../service/DeviceManagementProviderService.java | 12 ++++++++++++ .../DeviceManagementProviderServiceImpl.java | 15 +++++++++++++++ 4 files changed, 27 insertions(+), 22 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/ApplicationManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/ApplicationManagementProviderService.java index 12e9cf74b0..7ed2af3ba6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/ApplicationManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/api/mgt/ApplicationManagementProviderService.java @@ -1,14 +1,7 @@ package org.wso2.carbon.device.mgt.core.api.mgt; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.app.mgt.Application; -import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; -import java.util.List; - public interface ApplicationManagementProviderService extends ApplicationManager { - void updateApplicationListInstallInDevice(DeviceIdentifier deviceIdentifier, List applications) - throws ApplicationManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index 2034ea61dd..c51429b773 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -159,21 +159,6 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem return pluginRepository; } - @Override - public void updateApplicationListInstallInDevice(DeviceIdentifier deviceIdentifier,List applications) - throws ApplicationManagementException { - - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { - Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); - deviceDAO.addDeviceApplications(device.getId(), applications); - }catch (DeviceManagementDAOException deviceDaoEx){ - String errorMsg = "Error occurred saving application list to the device"; - log.error(errorMsg+":"+deviceIdentifier.toString()); - throw new ApplicationManagementException(errorMsg, deviceDaoEx); - } - } - @Override public void registerDeviceManagementService(DeviceManagementService deviceManagementService) { try { 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 ba8fd26d59..fbe5dfd957 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 @@ -87,5 +87,17 @@ public interface DeviceManagementProviderService extends DeviceManager, LicenseM */ List getApplicationListForDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException; + + /** + * The method to get application list installed for the device. + * + * @param deviceIdentifier device identifier of the device + * @param applications List of installed Applications + * + * @throws DeviceManagementException + */ + void updateInstalledApplicationListOfDevice(DeviceIdentifier deviceIdentifier, List applications) + throws DeviceManagementException; + void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status active) 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 09c090e117..cc74de3e89 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 @@ -659,6 +659,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } + @Override + public void updateInstalledApplicationListOfDevice(DeviceIdentifier deviceIdentifier, + List applications) + throws DeviceManagementException { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); + deviceDAO.addDeviceApplications(device.getId(), applications); + }catch (DeviceManagementDAOException deviceDaoEx){ + String errorMsg = "Error occurred saving application list to the device"; + log.error(errorMsg+":"+deviceIdentifier.toString()); + throw new DeviceManagementException(errorMsg, deviceDaoEx); + } + } + @Override public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException {