Merge pull request #1127 from sameeragunarathne/master

added update device properties functionality for device types
https://github.com/wso2/product-iots/issues/1505
4.x.x
Charitha Goonetilleke 7 years ago committed by GitHub
commit 2c9e9e2eab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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<Device.Property> properties);
@GET
@Path("/status/operations/{type}/{id}")
@ApiOperation(

@ -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<Device.Property> 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,

@ -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<Device.Property> list) throws DeviceManagementException;
/**
* Method to update device information.
*

@ -552,6 +552,8 @@ public interface DeviceManagementProviderService {
void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException;
boolean updateProperties(DeviceIdentifier deviceId, List<Device.Property> properties) throws DeviceManagementException;
Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId)
throws OperationManagementException;

@ -1426,6 +1426,28 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
.updateOperation(deviceId, operation);
}
@Override
public boolean updateProperties(DeviceIdentifier deviceId, List<Device.Property> 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 {

@ -92,6 +92,11 @@ public class TestDeviceManager implements DeviceManager {
return device;
}
@Override public boolean updateDeviceProperties(DeviceIdentifier deviceId, List<Device.Property> list)
throws DeviceManagementException {
return false;
}
@Override
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device)
throws DeviceManagementException {

@ -385,6 +385,27 @@ public class DeviceTypeManager implements DeviceManager {
return null;
}
@Override
public boolean updateDeviceProperties(DeviceIdentifier deviceId, List<Device.Property> 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 {

@ -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();

@ -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<Device.Property> 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.
*/

@ -86,6 +86,11 @@ public class TypeXDeviceManager implements DeviceManager {
return null;
}
@Override public boolean updateDeviceProperties(DeviceIdentifier deviceId, List<Device.Property> list)
throws DeviceManagementException {
return false;
}
@Override
public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device)
throws DeviceManagementException {

Loading…
Cancel
Save