From c8d56f66ead2ff661ab049e02e81cd343ec866cc Mon Sep 17 00:00:00 2001 From: sameera910409 Date: Wed, 29 Nov 2017 19:07:11 +0530 Subject: [PATCH] added update device properties functionality for device types --- .../jaxrs/service/api/DeviceAgentService.java | 58 +++++++++++++++++++ .../service/impl/DeviceAgentServiceImpl.java | 33 +++++++++++ .../device/mgt/common/DeviceManager.java | 10 ++++ .../DeviceManagementProviderService.java | 2 + .../DeviceManagementProviderServiceImpl.java | 22 +++++++ .../device/mgt/core/TestDeviceManager.java | 5 ++ .../type/template/DeviceTypeManager.java | 21 +++++++ .../dao/PropertyBasedPluginDAOImpl.java | 8 +-- .../type/template/DeviceTypeManagerTest.java | 18 ++++++ .../mgt/core/mock/TypeXDeviceManager.java | 5 ++ 10 files changed, 178 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceAgentService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceAgentService.java index ce442701e1..1d6d6a4728 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceAgentService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceAgentService.java @@ -546,6 +546,64 @@ public interface DeviceAgentService { @ApiParam(name = "operation", value = "Operation object with data.", required = true) @Valid Operation operation); + @PUT + @Path("/properties/{type}/{id}") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + consumes = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "Update Properties", + notes = "Update device properties.", + tags = "Device Agent Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:device:modify") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully updated the operations.", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. Empty body because the client already has the latest " + + "version of the requested resource."), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found. \n No device is found under the provided type and id.", + response = ErrorResponse.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while retrieving information requested device.", + response = ErrorResponse.class) + }) + Response updateDeviceProperties(@ApiParam(name = "type", value = "The device type, such as ios, android or windows.", required = true) + @PathParam("type") String type, + @ApiParam(name = "id", value = "The device id.", required = true) + @PathParam("id") String deviceId, + @ApiParam(name = "properties", value = "device properties list.", required = true) + @Valid List properties); + @GET @Path("/status/operations/{type}/{id}") @ApiOperation( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java index c72dfd9197..08328c424d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java @@ -22,6 +22,7 @@ import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonParser; +import io.swagger.annotations.ApiParam; import org.apache.axis2.AxisFault; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -518,6 +519,38 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { } } + @Override + @PUT + @Path("/operations/{type}/{id}") + public Response updateDeviceProperties(@PathParam("type") String type, @PathParam("id") String deviceId, + @Valid List properties) { + try { + if (!DeviceMgtAPIUtils.getDeviceManagementService().getAvailableDeviceTypes().contains(type)) { + String errorMessage = "Device type is invalid"; + log.error(errorMessage); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + if(properties == null) { + String errorMessage = "Properties cannot be empty"; + log.error(errorMessage); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(deviceId, type); + if (!DeviceMgtAPIUtils.isValidDeviceIdentifier(deviceIdentifier)) { + String msg = "Device not found for identifier '" + deviceId + "'"; + log.error(msg); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } + + DeviceMgtAPIUtils.getDeviceManagementService().updateProperties(deviceIdentifier, properties); + } catch (DeviceManagementException e) { + String errorMessage = "Issue in retrieving device management service instance"; + log.error(errorMessage, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + } + return null; + } + @GET @Path("/status/operations/{type}/{id}") public Response getOperationsByDeviceAndStatus(@PathParam("type") String type, @PathParam("id") String deviceId, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java index 31b1aea61e..ebb225ae4f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java @@ -128,6 +128,16 @@ public interface DeviceManager { */ Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException; + /** + * Method to update device properties. + * + * @param deviceId identifier to identify the device + * @param list device properties list + * @return A boolean indicating the status of the operation. + * @throws DeviceManagementException If some unusual behaviour is observed while updating the device properties + */ + boolean updateDeviceProperties(DeviceIdentifier deviceId, List list) throws DeviceManagementException; + /** * Method to update device information. * 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 a1ff76a3e7..b3730d34b8 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 @@ -552,6 +552,8 @@ public interface DeviceManagementProviderService { void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException; + boolean updateProperties(DeviceIdentifier deviceId, List properties) throws DeviceManagementException; + Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException; 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 a3652faa16..c49ef81340 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 @@ -1426,6 +1426,28 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv .updateOperation(deviceId, operation); } + @Override + public boolean updateProperties(DeviceIdentifier deviceId, List properties) + throws DeviceManagementException { + if (deviceId == null || properties == null) { + String msg = "Received incomplete data for updateDeviceInfo"; + log.error(msg); + throw new DeviceManagementException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Update device info of device: " + deviceId.getId()); + } + DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); + if (deviceManager == null) { + if (log.isDebugEnabled()) { + log.debug("Device Manager associated with the device type '" + deviceId.getType() + "' is null. " + + "Therefore, not attempting method 'updateProperties'"); + } + return false; + } + return deviceManager.updateDeviceProperties(deviceId, properties); + } + @Override public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java index b67a0ceb27..47a01c9e59 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java @@ -92,6 +92,11 @@ public class TestDeviceManager implements DeviceManager { return device; } + @Override public boolean updateDeviceProperties(DeviceIdentifier deviceId, List list) + throws DeviceManagementException { + return false; + } + @Override public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java index 6e2aff8b35..83f6481401 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java @@ -385,6 +385,27 @@ public class DeviceTypeManager implements DeviceManager { return null; } + @Override + public boolean updateDeviceProperties(DeviceIdentifier deviceId, List propertyList) + throws DeviceManagementException { + boolean status = false; + if (propertiesExist) { + try { + if (log.isDebugEnabled()) { + log.debug("Getting the details of " + deviceType + " device : '" + deviceId.getId() + "'"); + } + Device updatedDevice = new Device(); + updatedDevice.setDeviceIdentifier(deviceId.getId()); + updatedDevice.setProperties(propertyList); + status = deviceTypePluginDAOManager.getDeviceDAO().updateDevice(updatedDevice); + } catch (DeviceTypeMgtPluginException e) { + throw new DeviceManagementException( + "Error occurred while fetching the " + deviceType + " device: '" + deviceId.getId() + "'", e); + } + } + return status; + } + @Override public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java index 0e1502f34a..ac90fb1052 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PropertyBasedPluginDAOImpl.java @@ -139,10 +139,10 @@ public class PropertyBasedPluginDAOImpl implements PluginDAO { continue; } stmt.setString(1, property.getValue()); - stmt.setString(1, deviceType); - stmt.setString(2, device.getDeviceIdentifier()); - stmt.setString(3, property.getName()); - stmt.setInt(4, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true)); + stmt.setString(2, deviceType); + stmt.setString(3, device.getDeviceIdentifier()); + stmt.setString(4, property.getName()); + stmt.setInt(5, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true)); stmt.addBatch(); } stmt.executeBatch(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerTest.java index 49a6a7c9ff..0e61a40e8b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerTest.java @@ -71,6 +71,7 @@ public class DeviceTypeManagerTest { private Field deviceTypePluginDAOField; private Field deviceTypeDAOHandlerField; private String[] customDeviceTypeProperties = {"custom_property", "custom_property2"}; + private String updatedDeviceTypePropertyValue = "custom_property_updated"; private final String SQL_FOLDER = "sql-files" + File.separator; @BeforeClass(description = "Mocking the classes for testing") @@ -221,6 +222,23 @@ public class DeviceTypeManagerTest { "Existing device update failed"); } + @Test (description = "This test case tests the updateDeviceProperties method") + public void testUpdateDeviceProperties() throws DeviceManagementException { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(customDeviceType, customDeviceType); + Device customDevice = customDeviceTypeManager + .getDevice(deviceIdentifier); + List list = customDevice.getProperties(); + Assert.assertEquals(customDevice.getProperties().size(), 2, + "GetDevice call" + " failed in custom deviceTypeManager"); + Device.Property property = list.get(0); + property.setValue(updatedDeviceTypePropertyValue); + customDeviceTypeManager.updateDeviceProperties(deviceIdentifier, list); + customDevice = customDeviceTypeManager + .getDevice(deviceIdentifier); + Assert.assertEquals(customDevice.getProperties().get(0).getValue(), updatedDeviceTypePropertyValue, + "GetDevice call" + " failed in custom deviceTypeManager"); + } + /** * To create sample android devices to add to DAO Layer. */ diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManager.java index 575a75162b..d9477b5119 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManager.java @@ -86,6 +86,11 @@ public class TypeXDeviceManager implements DeviceManager { return null; } + @Override public boolean updateDeviceProperties(DeviceIdentifier deviceId, List list) + throws DeviceManagementException { + return false; + } + @Override public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) throws DeviceManagementException {