diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java index e68fef5489f..331c81b749d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/Device.java @@ -131,4 +131,19 @@ public class Device { } } + @Override + public String toString() { + return "Device[" + + "name=" + name + ";" + + "type=" + type + ";" + + "description=" + description + ";" + + "identifier=" + deviceIdentifier + ";" + +// "EnrolmentInfo[" + +// "owner=" + enrolmentInfo.getOwner() + ";" + +// "ownership=" + enrolmentInfo.getOwnership() + ";" + +// "status=" + enrolmentInfo.getStatus() + ";" + +// "]" + + "]"; + } + } 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 63366318ed7..2281dd0749b 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,7 +18,10 @@ */ package org.wso2.carbon.device.mgt.common.app.mgt; -public class Application { +import java.io.Serializable; +import java.util.Properties; + +public class Application implements Serializable { private int id; private String packageName; @@ -29,6 +32,7 @@ public class Application { private String imageUrl; private String version; private String type; + private Properties appProperties; public String getType() { return type; @@ -113,5 +117,12 @@ public class Application { return packageName.equals(target.getPackageName()); } + public Properties getAppProperties() { + return appProperties; + } + + public void setAppProperties(Properties appProperties) { + this.appProperties = appProperties; + } } 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 12e9cf74b07..7ed2af3ba64 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 eece3cef25f..01b344aea4b 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 @@ -160,7 +160,6 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem return pluginRepository; } - @Override public 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/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 34488b0db0a..ac28c97b596 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 @@ -60,4 +60,21 @@ public interface DeviceDAO { EnrolmentInfo getEnrolment(DeviceIdentifier deviceId, String currentUser, 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/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index a56e39b26c5..d5527cefbc7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -71,7 +71,7 @@ public class DeviceManagementDAOFactory { conn.setAutoCommit(false); currentConnection.set(conn); } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving datasource connection", e); + throw new DeviceManagementDAOException("Error occurred while retrieving config.datasource connection", e); } } @@ -79,7 +79,7 @@ public class DeviceManagementDAOFactory { try { currentConnection.set(dataSource.getConnection()); } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while acquiring datasource connection", e); + throw new DeviceManagementDAOException("Error occurred while acquiring config.datasource connection", e); } } 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 61b15f0d5c7..549a27630ec 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,18 +18,21 @@ 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; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; -import org.wso2.carbon.device.mgt.common.EnrolmentInfo.OwnerShip; import org.wso2.carbon.device.mgt.common.app.mgt.Application; 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.util.DeviceManagementDAOUtil; -import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; import java.sql.*; import java.util.ArrayList; import java.util.Date; @@ -37,6 +40,8 @@ import java.util.List; public class DeviceDAOImpl implements DeviceDAO { + private static final Log log = LogFactory.getLog(DeviceDAOImpl.class); + @Override public int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { Connection conn; @@ -115,8 +120,8 @@ public class DeviceDAOImpl implements DeviceDAO { conn = this.getConnection(); String sql = "SELECT d.ID AS DEVICE_ID, d.DESCRIPTION, d.NAME AS DEVICE_NAME, " + - "d.DEVICE_TYPE_ID, d.DEVICE_IDENTIFICATION, d.TENANT_ID FROM DM_DEVICE d, DM_DEVICE_TYPE dt WHERE " + - "dt.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?"; + "t.NAME AS DEVICE_TYPE_NAME, d.DEVICE_IDENTIFICATION, d.TENANT_ID FROM DM_DEVICE d, DM_DEVICE_TYPE t WHERE " + + "t.NAME = ? AND d.DEVICE_IDENTIFICATION = ? AND d.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, deviceId.getType()); stmt.setString(2, deviceId.getId()); @@ -409,17 +414,61 @@ public class DeviceDAOImpl implements DeviceDAO { } } + @Override + public void addDeviceApplications(int id, Object applications) throws DeviceManagementDAOException { + + } + + @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(); - DeviceType deviceType = new DeviceType(); - deviceType.setId(rs.getInt("ID")); - deviceType.setName(rs.getString("DEVICE_NAME")); - device.setId(rs.getInt("DEVICE_TYPE_ID")); + 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")); - device.setEnrolmentInfo(this.loadEnrolment(rs)); - return device; } 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 148e32a1245..da551394e30 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/operation/mgt/dao/OperationManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java index bb5a088b379..e5de73ede09 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationManagementDAOFactory.java @@ -74,7 +74,7 @@ public class OperationManagementDAOFactory { try { currentConnection.set(dataSource.getConnection()); } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while retrieving datasource connection", e); + throw new OperationManagementDAOException("Error occurred while retrieving config.datasource connection", e); } } 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 b1f3e28947c..fbe5dfd957e 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,9 +82,22 @@ 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; + + /** + * 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 0203efe18b2..c67aa18cffc 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 @@ -43,6 +43,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 { @@ -127,6 +128,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return status; } + + @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { DeviceManager dms = @@ -135,7 +138,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); DeviceManagementDAOFactory.beginTransaction(); - DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); int deviceId = deviceDAO.updateDevice(type.getId(), device, tenantId); enrolmentDAO.updateEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); @@ -161,8 +163,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); } @@ -635,7 +653,46 @@ 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 + 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 { + + 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 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 126d60b543e..4824537db42 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; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementBaseTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementBaseTest.java deleted file mode 100644 index 97204528f11..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceManagementBaseTest.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * 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.device.mgt.core; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.tomcat.jdbc.pool.PoolProperties; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Parameters; -import org.w3c.dom.Document; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.common.DBTypes; -import org.wso2.carbon.device.mgt.core.common.TestDBConfiguration; -import org.wso2.carbon.device.mgt.core.common.TestDBConfigurations; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; -import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; - -import javax.sql.DataSource; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import java.io.File; -import java.sql.Connection; -import java.sql.Statement; - -public class DeviceManagementBaseTest { - - private DataSource dataSource; - private static final Log log = LogFactory.getLog(DeviceManagementBaseTest.class); - - @BeforeClass(alwaysRun = true) - @Parameters("dbType") - public void setupDatabase(String dbTypeName) throws Exception { - DBTypes type = DBTypes.valueOf(dbTypeName); - TestDBConfiguration config = getTestDBConfiguration(type); - switch (type) { - case H2: - PoolProperties properties = new PoolProperties(); - properties.setUrl(config.getConnectionUrl()); - properties.setDriverClassName(config.getDriverClass()); - properties.setUsername(config.getUserName()); - properties.setPassword(config.getPwd()); - dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties); - this.initSQLScript(); - default: - } - } - private TestDBConfiguration getTestDBConfiguration(DBTypes dbType) throws DeviceManagementDAOException, - DeviceManagementException { - File dbConfig = new File("src/test/resources/data-source-config.xml"); - Document doc = DeviceManagerUtil.convertToDocument(dbConfig); - TestDBConfigurations dbConfigs; - JAXBContext testDBContext; - - try { - testDBContext = JAXBContext.newInstance(TestDBConfigurations.class); - Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); - dbConfigs = (TestDBConfigurations) unmarshaller.unmarshal(doc); - for (TestDBConfiguration config : dbConfigs.getDbTypesList()) { - if (config.getDbType().equals(dbType.toString())) { - return config; - } - } - return null; - } catch (JAXBException e) { - throw new DeviceManagementDAOException("Error parsing test db configurations", e); - } - } - - private void initSQLScript() throws Exception { - Connection conn = null; - Statement stmt = null; - try { - conn = this.getDataSource().getConnection(); - stmt = conn.createStatement(); - stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/h2.sql'"); - } catch(Exception e){ - log.error(e); - throw e; - }finally { - TestUtils.cleanupResources(conn, stmt, null); - } - } - - protected DataSource getDataSource() { - return dataSource; - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java deleted file mode 100644 index 764124a7f79..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/DeviceOperationManagementTests.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * 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.device.mgt.core; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.testng.Assert; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.Test; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -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.internal.DeviceManagementDataHolder; -import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; -import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; -import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; - -import java.util.ArrayList; -import java.util.List; - -public class DeviceOperationManagementTests extends DeviceManagementBaseTest { - - private OperationManager operationManager; - private static final Log log = LogFactory.getLog(DeviceOperationManagementTests.class); - - @BeforeClass(alwaysRun = true) - public void init() throws Exception{ - OperationManagementDAOFactory.init(this.getDataSource()); - this.initOperationManager(); - this.setupData(); - } - - private void setupData() throws Exception { - String deviceSql = "INSERT INTO DM_DEVICE(DESCRIPTION, NAME, DATE_OF_ENROLLMENT, DATE_OF_LAST_UPDATE, " + - "OWNERSHIP, STATUS, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, OWNER, TENANT_ID) " + - "VALUES ('Galaxy Tab', 'Samsung', 1425467382, 1425467382, 'BYOD', 'ACTIVE', 1, " + - "'4892813d-0b18-4a02-b7b1-61775257400e', 'admin@wso2.com', '-1234');"; - String typeSql = "Insert into DM_DEVICE_TYPE (ID,NAME) VALUES (1, 'android');"; - this.getDataSource().getConnection().createStatement().execute(typeSql); - this.getDataSource().getConnection().createStatement().execute(deviceSql); - } - - private void initOperationManager() { - this.operationManager = new OperationManagerImpl(); - DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(new DeviceManagementProviderServiceImpl()); - } - -// @Test -// public void testAddOperation() throws Exception { -// CommandOperation op = new CommandOperation(); -// op.setEnabled(true); -// op.setType(Operation.Type.COMMAND); -// op.setCode("OPCODE1"); -// -// List deviceIds = new ArrayList(); -// DeviceIdentifier deviceId = new DeviceIdentifier(); -// deviceId.setId("4892813d-0b18-4a02-b7b1-61775257400e"); -// deviceId.setType("android"); -// deviceIds.add(deviceId); -// -// try { -// boolean isAdded = operationManager.addOperation(op, deviceIds); -// Assert.assertTrue(isAdded); -// } catch (OperationManagementException e) { -// e.printStackTrace(); -// throw new Exception(e); -// } -// } - - public void testGetOperations() { - try { - //TODO:- operationManager.getOperations is not implemented - DeviceIdentifier deviceId = new DeviceIdentifier(); - deviceId.setId("4892813d-0b18-4a02-b7b1-61775257400e"); - deviceId.setType("android"); - List operations = operationManager.getOperations(deviceId); - Assert.assertNotNull(operations); - boolean notEmpty = operations.size() > 0; - Assert.assertTrue(notEmpty); - } catch (OperationManagementException e) { - e.printStackTrace(); - } - } - - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/DBTypes.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/DBTypes.java deleted file mode 100644 index 89c5683ff63..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/DBTypes.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.device.mgt.core.common; - - -public enum DBTypes { - Oracle("Oracle"),H2("H2"),MySql("MySql"); - - String dbName ; - DBTypes(String dbStrName) { - dbName = dbStrName; - } -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/DataSourceConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/DataSourceConfig.java new file mode 100644 index 00000000000..25ecd976dea --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/DataSourceConfig.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.device.mgt.core.common; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "DataSourceConfig") +public class DataSourceConfig { + + private String url; + private String driverClassName; + private String user; + private String password; + + @Override public String toString() { + return "DataSourceConfig[" + + " Url ='" + url + '\'' + + ", DriverClassName ='" + driverClassName + '\'' + + ", UserName ='" + user + '\'' + + ", Password ='" + password + '\'' + + "]"; + } + + @XmlElement(name = "Url", nillable = false) + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + @XmlElement(name = "DriverClassName", nillable = false) + public String getDriverClassName() { + return driverClassName; + } + + public void setDriverClassName(String driverClassName) { + this.driverClassName = driverClassName; + } + + @XmlElement(name = "User", nillable = false) + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + @XmlElement(name = "Password", nillable = false) + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDBConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDBConfiguration.java deleted file mode 100644 index d33bfeba940..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDBConfiguration.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.device.mgt.core.common; - -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement(name = "DBType") -public class TestDBConfiguration { - - private String connectionUrl; - private String driverClass; - private String userName; - private String pwd; - - @Override public String toString() { - return "TestDBConfiguration{" + - "connectionUrl='" + connectionUrl + '\'' + - ", driverClass='" + driverClass + '\'' + - ", userName='" + userName + '\'' + - ", pwd='" + pwd + '\'' + - ", dbType='" + dbType + '\'' + - '}'; - } - - private String dbType; - - @XmlElement(name = "connectionurl", nillable = false) - public String getConnectionUrl() { - return connectionUrl; - } - - public void setConnectionUrl(String connectionUrl) { - this.connectionUrl = connectionUrl; - } - - @XmlElement(name = "driverclass", nillable = false) - public String getDriverClass() { - return driverClass; - } - - public void setDriverClass(String driverClass) { - this.driverClass = driverClass; - } - - @XmlElement(name = "userName", nillable = false) - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - @XmlElement(name = "pwd", nillable = false) - public String getPwd() { - return pwd; - } - - public void setPwd(String pwd) { - this.pwd = pwd; - } - - @XmlAttribute(name = "typeName") - public String getDbType() { - return dbType; - } - - public void setDbType(String dbType) { - this.dbType = dbType; - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDBConfigurations.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDBConfigurations.java deleted file mode 100644 index d1551cca0e6..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/common/TestDBConfigurations.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.device.mgt.core.common; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import java.util.List; - -@XmlRootElement(name = "DeviceMgtTestDBConfigurations") -public class TestDBConfigurations { - - private List dbTypesList; - - @XmlElement(name = "DBType") - public List getDbTypesList() { - return dbTypesList; - } - - public void setDbTypesList(List dbTypesList) { - this.dbTypesList = dbTypesList; - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java index 1c717507aac..6522191686c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceDAOTests.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.core.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.Assert; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.app.mgt.Application; @@ -80,4 +81,9 @@ public class ApplicationPersistenceDAOTests extends BaseDeviceManagementDAOTest } } + @BeforeClass + @Override + public void init() throws Exception { + this.initDatSource(); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/BaseDeviceManagementDAOTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/BaseDeviceManagementDAOTest.java index a211bb894d9..bf4a4605345 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/BaseDeviceManagementDAOTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/BaseDeviceManagementDAOTest.java @@ -23,14 +23,12 @@ import org.apache.commons.logging.LogFactory; import org.apache.tomcat.jdbc.pool.PoolProperties; import org.testng.Assert; import org.testng.annotations.AfterSuite; +import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeSuite; -import org.testng.annotations.Parameters; import org.w3c.dom.Document; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.TestUtils; -import org.wso2.carbon.device.mgt.core.common.DBTypes; -import org.wso2.carbon.device.mgt.core.common.TestDBConfiguration; -import org.wso2.carbon.device.mgt.core.common.TestDBConfigurations; +import org.wso2.carbon.device.mgt.core.common.DataSourceConfig; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import javax.sql.DataSource; @@ -43,44 +41,44 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; -public class BaseDeviceManagementDAOTest { +public abstract class BaseDeviceManagementDAOTest { private DataSource dataSource; private static final Log log = LogFactory.getLog(BaseDeviceManagementDAOTest.class); @BeforeSuite public void setupDataSource() throws Exception { - this.dataSource = this.getDataSource(this.readDataSourceConfig()); + this.initDatSource(); this.initSQLScript(); + } + + public void initDatSource() throws Exception { + this.dataSource = this.getDataSource(this.readDataSourceConfig()); DeviceManagementDAOFactory.init(dataSource); } - private DataSource getDataSource(TestDBConfiguration config) { + @BeforeClass + public abstract void init() throws Exception; + + private DataSource getDataSource(DataSourceConfig config) { PoolProperties properties = new PoolProperties(); - properties.setUrl(config.getConnectionUrl()); - properties.setDriverClassName(config.getDriverClass()); - properties.setUsername(config.getUserName()); - properties.setPassword(config.getPwd()); + properties.setUrl(config.getUrl()); + properties.setDriverClassName(config.getDriverClassName()); + properties.setUsername(config.getUser()); + properties.setPassword(config.getPassword()); return new org.apache.tomcat.jdbc.pool.DataSource(properties); } - private TestDBConfiguration readDataSourceConfig() throws DeviceManagementDAOException, - DeviceManagementException { - File config = new File("src/test/resources/data-source-config.xml"); - Document doc; - TestDBConfigurations dbConfigs; - - doc = DeviceManagerUtil.convertToDocument(config); - JAXBContext testDBContext; - + private DataSourceConfig readDataSourceConfig() throws DeviceManagementException { try { - testDBContext = JAXBContext.newInstance(TestDBConfigurations.class); + File file = new File("src/test/resources/config/datasource/data-source-config.xml"); + Document doc = DeviceManagerUtil.convertToDocument(file); + JAXBContext testDBContext = JAXBContext.newInstance(DataSourceConfig.class); Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); - dbConfigs = (TestDBConfigurations) unmarshaller.unmarshal(doc); + return (DataSourceConfig) unmarshaller.unmarshal(doc); } catch (JAXBException e) { - throw new DeviceManagementDAOException("Error parsing test db configurations", e); + throw new DeviceManagementException("Error occurred while reading data source configuration", e); } - } private void initSQLScript() throws Exception { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java index 926fe389819..464bcfa0a32 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOTests.java @@ -44,8 +44,9 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { private static final Log log = LogFactory.getLog(DeviceManagementDAOTests.class); @BeforeClass - public void init() { - + @Override + public void init() throws Exception { + initDatSource(); } @Test @@ -142,6 +143,7 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { int id = -1; try { + Assert.assertNotNull(getDataSource(), "Data Source is not initialized properly"); conn = getDataSource().getConnection(); String sql = "SELECT ID FROM DM_DEVICE WHERE DEVICE_IDENTIFICATION = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); @@ -169,6 +171,7 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { DeviceType deviceType = this.loadDummyDeviceType(); try { + Assert.assertNotNull(getDataSource(), "Data Source is not initialized properly"); conn = getDataSource().getConnection(); stmt = conn.prepareStatement(sql); stmt.setString(1, deviceType.getName()); @@ -188,6 +191,7 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { @Test(dependsOnMethods = "testAddDeviceTest") public void testSetEnrolmentStatus() { + System.out.println("ENROLLLLLLLLLLLLLL"); Device device = this.loadDummyDevice(); try { DeviceManagementDAOFactory.openConnection(); @@ -247,6 +251,7 @@ public class DeviceManagementDAOTests extends BaseDeviceManagementDAOTest { device.setEnrolmentInfo(enrolmentInfo); device.setDescription("Test Description"); device.setDeviceIdentifier("1234"); + device.setType(this.loadDummyDeviceType().getName()); return device; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceDAOTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceDAOTests.java index f1b21c252fb..91c4bad0374 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceDAOTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceDAOTests.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.core.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.testng.Assert; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.app.mgt.Application; @@ -82,4 +83,9 @@ public class EnrolmentPersistenceDAOTests extends BaseDeviceManagementDAOTest { } } + @BeforeClass + @Override + public void init() throws Exception { + this.initDatSource(); + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/data-source-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/data-source-config.xml similarity index 100% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/data-source-config.xml rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/datasource/data-source-config.xml diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index d83ab8e300a..1a82c6d651f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -23,14 +23,12 @@ - + - - \ No newline at end of file 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 00000000000..85234fff619 --- /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 00000000000..7c4589a8ab2 --- /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 00000000000..d579546dd92 --- /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 1910ee46158..f59e722d5f7 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 00000000000..4d5e34b3681 --- /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 0214e344481..3a5a48ab521 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 c6da23346d9..c4fcd2a4e8a 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 f35e5070ea7..1c08a18789e 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 00000000000..e97501dc053 --- /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 00000000000..5ab28f187f5 --- /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 b8c3fb32d52..92940de313a 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 ab0c5e3697e..e59aa9b82de 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 * @@ -107,7 +112,7 @@ public class PolicyManagementDAOFactory { conn.setAutoCommit(false); currentConnection.set(conn); } catch (SQLException e) { - throw new PolicyManagerDAOException("Error occurred while retrieving datasource connection", e); + throw new PolicyManagerDAOException("Error occurred while retrieving config.datasource connection", e); } } 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 9324d3ba45f..e6e5b93abe3 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 { @@ -466,7 +466,7 @@ public class FeatureDAOImpl implements FeatureDAO { return PolicyManagementDAOFactory.getConnection(); } catch (PolicyManagerDAOException e) { throw new FeatureManagerDAOException("Error occurred while obtaining a connection from the policy " + - "management metadata repository datasource", e); + "management metadata repository config.datasource", e); } } 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 00000000000..5c03d43058b --- /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 config.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 551243e318a..cf82d26ce90 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/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index f12400b0fbd..1ec9c10e00e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -279,7 +279,7 @@ public class ProfileDAOImpl implements ProfileDAO { return PolicyManagementDAOFactory.getConnection(); } catch (PolicyManagerDAOException e) { throw new ProfileManagerDAOException("Error occurred while obtaining a connection from the policy " + - "management metadata repository datasource", e); + "management metadata repository config.datasource", e); } } 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 2041d997130..052199f4e0a 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 506488f762a..4f2d664494c 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 c0f8450ac15..d6f72dba744 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 5af6049f5b6..8ccab296e56 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 00000000000..f0ca764d8cd --- /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 924a57116b6..0aa8c11ac77 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 7b9f9aa8016..94ee440a109 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 00000000000..56c4f593b82 --- /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 0426a0fad21..15013de7f02 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 46b51c73529..a6cebdd59cf 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 08acd03c796..8e2ca941d08 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/BasePolicyManagementDAOTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java new file mode 100644 index 00000000000..4fe1b712c03 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java @@ -0,0 +1,98 @@ +/* + * 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; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.tomcat.jdbc.pool.PoolProperties; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.BeforeSuite; +import org.w3c.dom.Document; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.core.common.DataSourceConfig; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; + +import javax.sql.DataSource; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.File; +import java.sql.Connection; +import java.sql.Statement; + +public abstract class BasePolicyManagementDAOTest { + + private DataSource dataSource; + private static final Log log = LogFactory.getLog(BasePolicyManagementDAOTest.class); + + @BeforeSuite + public void setupDataSource() throws Exception { + this.initDatSource(); + this.initSQLScript(); + } + + public void initDatSource() throws Exception { + this.dataSource = this.getDataSource(this.readDataSourceConfig()); + DeviceManagementDAOFactory.init(dataSource); + PolicyManagementDAOFactory.init(dataSource); + } + + @BeforeClass + public abstract void init() throws Exception; + + private DataSource getDataSource(DataSourceConfig config) { + PoolProperties properties = new PoolProperties(); + properties.setUrl(config.getUrl()); + properties.setDriverClassName(config.getDriverClassName()); + properties.setUsername(config.getUser()); + properties.setPassword(config.getPassword()); + return new org.apache.tomcat.jdbc.pool.DataSource(properties); + } + + private DataSourceConfig readDataSourceConfig() throws PolicyManagementException { + try { + File file = new File("src/test/resources/config/datasource/data-source-config.xml"); + Document doc = PolicyManagerUtil.convertToDocument(file); + JAXBContext testDBContext = JAXBContext.newInstance(DataSourceConfig.class); + Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); + return (DataSourceConfig) unmarshaller.unmarshal(doc); + } catch (JAXBException e) { + throw new PolicyManagementException("Error occurred while reading data source configuration", e); + } + } + + private void initSQLScript() throws Exception { + Connection conn = null; + Statement stmt = null; + try { + conn = this.getDataSource().getConnection(); + stmt = conn.createStatement(); + stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'"); + } finally { + TestUtils.cleanupResources(conn, stmt, null); + } + } + + public DataSource getDataSource() { + return dataSource; + } + +} 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 279b61e6f78..b7bfd89035b 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 @@ -15,30 +15,21 @@ * specific language governing permissions and limitations * under the License. */ - package org.wso2.carbon.policy.mgt.core; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.tomcat.jdbc.pool.PoolProperties; import org.testng.annotations.BeforeClass; -import org.testng.annotations.Parameters; import org.testng.annotations.Test; -import org.w3c.dom.Document; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.Feature; 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.common.Feature; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.policy.mgt.common.*; -import org.wso2.carbon.policy.mgt.core.common.DBTypes; -import org.wso2.carbon.policy.mgt.core.common.TestDBConfiguration; -import org.wso2.carbon.policy.mgt.core.common.TestDBConfigurations; -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.impl.PolicyAdministratorPointImpl; import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; @@ -48,21 +39,12 @@ import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import org.wso2.carbon.policy.mgt.core.mgt.impl.ProfileManagerImpl; import org.wso2.carbon.policy.mgt.core.util.*; -import javax.sql.DataSource; -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import java.io.File; -import java.sql.Connection; -import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.util.Properties; -public class PolicyDAOTestCase { +public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { - - private static DataSource dataSource; private List featureList; private List profileFeatureList; private Profile profile; @@ -71,100 +53,13 @@ public class PolicyDAOTestCase { private static final Log log = LogFactory.getLog(PolicyDAOTestCase.class); @BeforeClass - @Parameters("dbType") - public void setUpDB(String dbTypeStr) throws Exception { - DBTypes dbType = DBTypes.valueOf(dbTypeStr); - TestDBConfiguration dbConfig = getTestDBConfiguration(dbType); - PoolProperties properties = new PoolProperties(); - - log.info("Database Type : " + dbTypeStr); - - switch (dbType) { - - case MySql: - - log.info("Mysql Called..................................................." + dbTypeStr); - - properties.setUrl(dbConfig.getConnectionUrl()); - properties.setDriverClassName(dbConfig.getDriverClass()); - properties.setUsername(dbConfig.getUserName()); - properties.setPassword(dbConfig.getPwd()); - dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties); - PolicyManagementDAOFactory.init(dataSource); - DeviceManagementDAOFactory.init(dataSource); - break; - - case H2: - - properties.setUrl(dbConfig.getConnectionUrl()); - properties.setDriverClassName(dbConfig.getDriverClass()); - properties.setUsername(dbConfig.getUserName()); - properties.setPassword(dbConfig.getPwd()); - dataSource = new org.apache.tomcat.jdbc.pool.DataSource(properties); - this.initH2SQLScript(); - PolicyManagementDAOFactory.init(dataSource); - DeviceManagementDAOFactory.init(dataSource); - break; - - default: - } - } - - private TestDBConfiguration getTestDBConfiguration(DBTypes dbType) throws PolicyManagerDAOException, - PolicyManagementException { - File deviceMgtConfig = new File("src/test/resources/data-source-config.xml"); - Document doc; - TestDBConfigurations dbConfigs; - - doc = PolicyManagerUtil.convertToDocument(deviceMgtConfig); - JAXBContext testDBContext; - - try { - testDBContext = JAXBContext.newInstance(TestDBConfigurations.class); - Unmarshaller unmarshaller = testDBContext.createUnmarshaller(); - dbConfigs = (TestDBConfigurations) unmarshaller.unmarshal(doc); - } catch (JAXBException e) { - throw new PolicyManagerDAOException("Error parsing test db configurations", e); - } - for (TestDBConfiguration config : dbConfigs.getDbTypesList()) { - if (config.getDbType().equals(dbType.toString())) { - return config; - } - } - return null; - } - - private void initH2SQLScript() throws Exception { - Connection conn = null; - Statement stmt = null; - try { - conn = this.getDataSource().getConnection(); - stmt = conn.createStatement(); - stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateH2TestDB.sql'"); - } finally { - TestUtils.cleanupResources(conn, stmt, null); - } - } - - private void initMySQlSQLScript() throws Exception { - Connection conn = null; - Statement stmt = null; - try { - conn = this.getDataSource().getConnection(); - stmt = conn.createStatement(); - stmt.executeUpdate("RUNSCRIPT FROM './src/test/resources/sql/CreateMySqlTestDB.sql'"); - } finally { - TestUtils.cleanupResources(conn, stmt, null); - } - } - - private DataSource getDataSource() { - return dataSource; + @Override + public void init() throws Exception { + initDatSource(); } @Test public void addDeviceType() throws DeviceManagementDAOException { - DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); deviceTypeDAO.addDeviceType(DeviceTypeCreator.getDeviceType()); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/DBTypes.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/DBTypes.java deleted file mode 100644 index 7362d80451f..00000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/DBTypes.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2014, 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.common; - - -public enum DBTypes { - Oracle("Oracle"),H2("H2"),MySql("MySql"); - - String dbName ; - DBTypes(String dbStrName) { - dbName = dbStrName; - } -} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/DataSourceConfig.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/DataSourceConfig.java new file mode 100644 index 00000000000..726405df962 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/DataSourceConfig.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2014, 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.common; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "DataSourceConfig") +public class DataSourceConfig { + + private String url; + private String driverClassName; + private String user; + private String password; + + @Override public String toString() { + return "DataSourceConfig[" + + " Url ='" + url + '\'' + + ", DriverClassName ='" + driverClassName + '\'' + + ", UserName ='" + user + '\'' + + ", Password ='" + password + '\'' + + "]"; + } + + @XmlElement(name = "Url", nillable = false) + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + @XmlElement(name = "DriverClassName", nillable = false) + public String getDriverClassName() { + return driverClassName; + } + + public void setDriverClassName(String driverClassName) { + this.driverClassName = driverClassName; + } + + @XmlElement(name = "User", nillable = false) + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + @XmlElement(name = "Password", nillable = false) + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfiguration.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfiguration.java deleted file mode 100644 index 633041fdd26..00000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfiguration.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2014, 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.common; - -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement(name = "DBType") -public class TestDBConfiguration { - - private String connectionUrl; - private String driverClass; - private String userName; - private String pwd; - - @Override public String toString() { - return "TestDBConfiguration{" + - "connectionUrl='" + connectionUrl + '\'' + - ", driverClass='" + driverClass + '\'' + - ", userName='" + userName + '\'' + - ", pwd='" + pwd + '\'' + - ", dbType='" + dbType + '\'' + - '}'; - } - - private String dbType; - - @XmlElement(name = "connectionurl", nillable = false) - public String getConnectionUrl() { - return connectionUrl; - } - - public void setConnectionUrl(String connectionUrl) { - this.connectionUrl = connectionUrl; - } - - @XmlElement(name = "driverclass", nillable = false) - public String getDriverClass() { - return driverClass; - } - - public void setDriverClass(String driverClass) { - this.driverClass = driverClass; - } - - @XmlElement(name = "userName", nillable = false) - public String getUserName() { - return userName; - } - - public void setUserName(String userName) { - this.userName = userName; - } - - @XmlElement(name = "pwd", nillable = false) - public String getPwd() { - return pwd; - } - - public void setPwd(String pwd) { - this.pwd = pwd; - } - - @XmlAttribute(name = "typeName") - public String getDbType() { - return dbType; - } - - public void setDbType(String dbType) { - this.dbType = dbType; - } - -} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfigurations.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfigurations.java deleted file mode 100644 index 629b8106a15..00000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/common/TestDBConfigurations.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2014, 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.common; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import java.util.List; - -@XmlRootElement(name = "DeviceMgtTestDBConfigurations") -public class TestDBConfigurations { - - private List dbTypesList; - - @XmlElement(name = "DBType") - public List getDbTypesList() { - return dbTypesList; - } - - public void setDbTypesList(List dbTypesList) { - this.dbTypesList = dbTypesList; - } - -} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml similarity index 56% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml rename to components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml index 936272480ef..b07866513ac 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/config/datasource/data-source-config.xml @@ -17,17 +17,9 @@ ~ under the License. --> - - - jdbc:mysql://localhost:3306/WSO2CDM - com.mysql.jdbc.Driver - root - - - - jdbc:h2:mem:WSO2_TEST_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000 - org.h2.Driver - wso2carbon - wso2carbon - - + + jdbc:h2:mem:cdm-test-db;DB_CLOSE_DELAY=-1 + org.h2.Driver + wso2carbon + wso2carbon + 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 7f291415dea..b4bfe487a21 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 474d352e7ac..222c07c7f49 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 188a742f3d1..f7b7135eb5f 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); }