From 192984e3c0036303bad8daedc042da6dbea84dc8 Mon Sep 17 00:00:00 2001 From: Ace Date: Thu, 27 Jun 2019 17:03:00 +0530 Subject: [PATCH] Adding new API to search devices based on device properties --- .../service/api/DeviceManagementService.java | 80 +++++++++++++++++++ .../impl/DeviceManagementServiceImpl.java | 26 ++++++ .../device/mgt/common/search/PropertyMap.java | 43 ++++++++++ .../carbon/device/mgt/core/dao/DeviceDAO.java | 10 +++ .../core/dao/impl/AbstractDeviceDAOImpl.java | 63 +++++++++++++++ .../DeviceManagementProviderService.java | 10 +++ .../DeviceManagementProviderServiceImpl.java | 40 ++++++++++ .../device/type/template/dao/PluginDAO.java | 1 + .../dao/PropertyBasedPluginDAOImpl.java | 1 - 9 files changed, 273 insertions(+), 1 deletion(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/search/PropertyMap.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 96991ed2ce..4cf332c0b8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -58,6 +58,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData; +import org.wso2.carbon.device.mgt.common.search.PropertyMap; import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; @@ -79,6 +80,7 @@ import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; +import java.util.Map; /** * Device related REST-API. This can be used to manipulated device related details. @@ -1074,6 +1076,84 @@ public interface DeviceManagementService { required = true) SearchContext searchContext); + @POST + @Path("/query-devices") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + consumes = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Property based Search for Devices", + notes = "Search for devices based on properties", + tags = "Device Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:devices:search") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved the device information.", + response = DeviceList.class, + 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 was last modified. \n" + + "Used by caches, or in conditional requests.")}), + @ApiResponse( + code = 304, + message = "Not Modified. \n " + + "Empty body because the client already has the latest version of the requested resource.\n"), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Acceptable.\n The existing device did not match the values specified in the device search.", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 415, + message = "Unsupported media type. \n The format of the requested entity was not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while getting the device details.", + response = ErrorResponse.class) + }) + Response queryDevicesByProperties( + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items.", + required = false, + defaultValue = "0") + @QueryParam("offset") + int offset, + @ApiParam( + name = "limit", + value = "Provide how many activity details you require from the starting pagination index/offset.", + required = false, + defaultValue = "5") + @QueryParam("limit") + int limit, + @ApiParam( + name = "device property map", + value = "properties by which devices need filtered", + required = true) + PropertyMap map); + @GET @Path("/{type}/{id}/applications") @ApiOperation( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 9175580ce9..2e7aeb9e53 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -63,6 +63,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; +import org.wso2.carbon.device.mgt.common.search.PropertyMap; import org.wso2.carbon.device.mgt.common.search.SearchContext; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceDetailsMgtException; @@ -104,6 +105,7 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Map; @Path("/devices") @Produces(MediaType.APPLICATION_JSON) @@ -652,6 +654,30 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.status(Response.Status.OK).entity(deviceList).build(); } + @POST + @Path("/query-devices") + @Override + public Response queryDevicesByProperties(@QueryParam("offset") int offset, + @QueryParam("limit") int limit, PropertyMap map) { + List devices; + DeviceList deviceList = new DeviceList(); + try { + DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); + devices = dms.getDevicesBasedOnProperties(map.getProperties()); + if(devices == null || devices.isEmpty()){ + return Response.status(Response.Status.OK).entity("No device found matching query criteria.").build(); + } + } catch (DeviceManagementException e) { + String msg = "Error occurred while searching for devices that matches the provided device properties"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } + deviceList.setList(devices); + deviceList.setCount(devices.size()); + return Response.status(Response.Status.OK).entity(deviceList).build(); + } + @GET @Path("/{type}/{id}/applications") @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/search/PropertyMap.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/search/PropertyMap.java new file mode 100644 index 0000000000..68df7c7bdc --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/search/PropertyMap.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.device.mgt.common.search; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.Map; + + +@ApiModel(value = "PropertyMap", description = "Search properties based on which devices need to be found.") +public class PropertyMap { + + @ApiModelProperty(name = "properties", value = "Contains the properties for search.", + required = true) + private Map properties; + + public Map getProperties() { + return properties; + } + + public void setProperties(Map properties) { + this.properties = properties; + } +} + 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 b089477f06..1a8888ae1b 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 @@ -47,6 +47,7 @@ import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Map; /** * This class represents the key operations associated with persisting device related information. @@ -180,6 +181,15 @@ public interface DeviceDAO { Device getDevice(DeviceIdentifier deviceIdentifier, Date ifModifiedSince, int tenantId) throws DeviceManagementDAOException; + /** + * Retrieves a list of devices based on a given criteria of properties + * @param deviceProps properties by which devices need to be filtered + * @param tenantId tenant id + * @return list of devices + * @throws DeviceManagementDAOException + */ + List getDeviceBasedOnDeviceProperties(Map deviceProps, int tenantId) throws DeviceManagementDAOException; + /** * This method is used to retrieve a device of a given device-identifier and tenant-id which modified * later than the ifModifiedSince param. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index aa07aba8b4..f241691ff6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -39,6 +39,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl; 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.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo.Status; import org.wso2.carbon.device.mgt.common.PaginationRequest; @@ -56,15 +57,22 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.StringJoiner; public abstract class AbstractDeviceDAOImpl implements DeviceDAO { private static final org.apache.commons.logging.Log log = LogFactory.getLog(AbstractDeviceDAOImpl.class); + private static final String PROPERTY_KEY_COLUMN_NAME = "PROPERTY_NAME"; + private static final String PROPERTY_VALUE_COLUMN_NAME = "PROPERTY_VALUE"; + private static final String PROPERTY_DEVICE_TYPE_NAME = "DEVICE_TYPE_NAME"; + private static final String PROPERTY_DEVICE_IDENTIFICATION = "DEVICE_IDENTIFICATION"; + @Override public int addDevice(int typeId, Device device, int tenantId) throws DeviceManagementDAOException { Connection conn; @@ -282,6 +290,61 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return device; } + public List getDeviceBasedOnDeviceProperties(Map deviceProps, int tenantId) + throws DeviceManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List devices = null; + try { + List> outputLists = new ArrayList<>(); + List deviceList = null; + conn = this.getConnection(); + for (Map.Entry entry : deviceProps.entrySet()) { + + stmt = conn.prepareStatement("SELECT DEVICE_IDENTIFICATION FROM DM_DEVICE_PROPERTIES " + + "WHERE (PROPERTY_NAME , PROPERTY_VALUE) IN " + + "((? , ?)) AND TENANT_ID = ?"); + stmt.setString(1, entry.getKey()); + stmt.setString(2, entry.getValue()); + stmt.setInt(3, tenantId); + resultSet = stmt.executeQuery(); + + deviceList = new ArrayList<>(); + while (resultSet.next()) { + deviceList.add(resultSet.getString(PROPERTY_DEVICE_IDENTIFICATION)); + } + outputLists.add(deviceList); + } + List deviceIds = findIntersection(outputLists); + for(String deviceId : deviceIds){ + devices.add(getDevice(deviceId, tenantId)); + } + } catch (SQLException e) { + String msg = "Error occurred while fetching devices against criteria : '" + deviceProps; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, resultSet); + } + return devices; + } + + private List findIntersection(List> collections) { + boolean first = true; + List intersectedResult = new ArrayList<>(); + for (Collection collection : collections) { + if (first) { + intersectedResult.addAll(collection); + first = false; + } else { + intersectedResult.retainAll(collection); + } + } + return intersectedResult; + } + + @Override public Device getDevice(String deviceIdentifier, Date since, int tenantId) throws DeviceManagementDAOException { 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 43ac2c8567..b32ed7ed12 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 @@ -65,6 +65,7 @@ import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Map; /** * Proxy class for all Device Management related operations that take the corresponding plugin type in @@ -254,6 +255,15 @@ public interface DeviceManagementProviderService { */ Device getDevice(DeviceIdentifier deviceId, Date since, boolean requireDeviceInfo) throws DeviceManagementException; + /** + * Retrieves a list of devices based on a given criteria of properties + * + * @param deviceProps properties by which devices need to be drawn + * @return list of devices + * @throws DeviceManagementException + */ + List getDevicesBasedOnProperties(Map deviceProps) throws DeviceManagementException; + /** * Returns the device of specified id. * 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 621cc966e3..b662585cc6 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 @@ -1198,6 +1198,46 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return device; } + + @Override + public List getDevicesBasedOnProperties(Map deviceProps) throws DeviceManagementException { + if (deviceProps == null || deviceProps.isEmpty()) { + String msg = "Devices retrieval criteria cannot be null or empty."; + log.error(msg); + throw new DeviceManagementException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Attempting to get devices based on criteria : " + deviceProps); + } + List devices; + try { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceManagementDAOFactory.openConnection(); + devices = deviceDAO.getDeviceBasedOnDeviceProperties(deviceProps, tenantId); + if (devices == null) { + if (log.isDebugEnabled()) { + log.debug("No device is found against criteria : " + deviceProps + " and tenantId "+ tenantId); + } + return null; + } + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while obtaining devices based on criteria : " + deviceProps; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred while obtaining devices based on criteria : " + deviceProps; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return devices; + } + @Override public Device getDevice(String deviceId, Date since, boolean requireDeviceInfo) throws DeviceManagementException { if (deviceId == null || since == null) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java index 47606fe6a6..51f1759fe0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/PluginDAO.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.extensions.device.type.template.dao; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException; + import java.util.List; public interface PluginDAO { 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 ac90fb1052..d1731d0999 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 @@ -31,7 +31,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map;