From c7dd386c9460e76e8a0d4476f1db854ae601de1e Mon Sep 17 00:00:00 2001 From: geethkokila Date: Wed, 18 May 2016 14:56:46 +0530 Subject: [PATCH] Adding new apis for device informations --- .../mgt/jaxrs/api/DeviceInformation.java | 23 +++++++++++ .../jaxrs/api/impl/DeviceInformationImpl.java | 19 +++++++++ .../src/main/webapp/META-INF/permissions.xml | 7 ++++ .../details/mgt/DeviceInformationManager.java | 10 +++++ .../impl/DeviceInformationManagerImpl.java | 41 +++++++++++++++++++ 5 files changed, 100 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java index 0d20230610..864eda023a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/DeviceInformation.java @@ -19,14 +19,17 @@ package org.wso2.carbon.device.mgt.jaxrs.api; import io.swagger.annotations.*; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.List; /** * Device information related operations. @@ -55,6 +58,26 @@ public interface DeviceInformation { @ApiParam(name = "id", value = "Provide the device identifier", required = true) @PathParam("id") String id); + + @POST + @Path("{list}") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Get devices information from the supplied device identifies", + notes = "This will return device information such as CPU usage, memory usage etc for supplied device " + + "identifiers.", + response = DeviceInfo.class, + responseContainer = "List") + @ApiResponses(value = { + @ApiResponse(code = 200, message = ""), + @ApiResponse(code = 400, message = ""), + @ApiResponse(code = 400, message = ""), + @ApiResponse(code = 500, message = "Internal Server Error") + }) + Response getDevicesInfo(@ApiParam(name = "deviceIdentifiers", value = "List of device identifiers", + required = true) List deviceIdentifiers); + @GET @Path("location/{type}/{id}") @ApiOperation( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java index 17b59364f6..a59c81be54 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/impl/DeviceInformationImpl.java @@ -30,9 +30,11 @@ import org.wso2.carbon.device.mgt.jaxrs.api.DeviceInformation; import org.wso2.carbon.device.mgt.jaxrs.api.util.DeviceMgtAPIUtils; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; +import java.util.List; @SuppressWarnings("NonJaxWsWebServices") public class DeviceInformationImpl implements DeviceInformation { @@ -59,6 +61,23 @@ public class DeviceInformationImpl implements DeviceInformation { } + @POST + @Path("list") + public Response getDevicesInfo(List deviceIdentifiers) { + DeviceInformationManager informationManager; + List deviceInfos; + try { + informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); + deviceInfos = informationManager.getDevicesInfo(deviceIdentifiers); + } catch (DeviceDetailsMgtException e) { + String msg = "Error occurred while getting the device information."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + return Response.status(Response.Status.OK).entity(deviceInfos).build(); + } + + @GET @Path("location/{type}/{id}") public Response getDeviceLocation(@PathParam("type") String type, @PathParam("id") String id) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml index ca4664f9cb..9b4e086221 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/webapp/META-INF/permissions.xml @@ -876,6 +876,13 @@ GET + + Device Information + /device-mgt/admin/information/list + /information/list + POST + + Device Search /device-mgt/admin/search diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java index e30652a870..5b62a0f8dd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/DeviceInformationManager.java @@ -23,6 +23,8 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; +import java.util.List; + /** * This class will manage the storing of device details related generic information such as cpu/memory utilization, battery level, * plugged in to a power source or operation on battery. @@ -47,6 +49,14 @@ public interface DeviceInformationManager { */ DeviceInfo getDeviceInfo(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException; + /** + * This method will return device information for the supplied devices list. + * @param deviceIdentifiers + * @return List of device info objects + * @throws DeviceDetailsMgtException + */ + List getDevicesInfo(List deviceIdentifiers) throws DeviceDetailsMgtException; + /** * This method will manage storing the device location as latitude, longitude, address, zip, country etc.. * @param deviceLocation - Device location object. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index fb7e163767..b2266d35cd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -35,6 +35,10 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDA import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class DeviceInformationManagerImpl implements DeviceInformationManager { @@ -97,6 +101,43 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } + @Override + public List getDevicesInfo(List deviceIdentifiers) throws DeviceDetailsMgtException { + + List deviceInfos = new ArrayList<>(); + + Map identifierMap = new HashMap<>(); + for (DeviceIdentifier identifier : deviceIdentifiers) { + identifierMap.put(identifier.getId(), identifier); + } + try { + List deviceIds = new ArrayList<>(); + List devices = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider().getAllDevices(); + for (Device device : devices) { + if (identifierMap.containsKey(device.getDeviceIdentifier()) && + device.getType().equals(identifierMap.get(device.getDeviceIdentifier()))) { + deviceIds.add(device.getId()); + } + } + DeviceManagementDAOFactory.openConnection(); + for(Integer id : deviceIds) { + DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(id); + deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(id)); + deviceInfos.add(deviceInfo); + } + } catch (SQLException e) { + throw new DeviceDetailsMgtException("SQL error occurred while retrieving devices from database.", e); + } catch (DeviceManagementException e) { + throw new DeviceDetailsMgtException("Exception occurred while retrieving the devices.", e); + } catch (DeviceDetailsMgtDAOException e) { + throw new DeviceDetailsMgtException("Exception occurred while retrieving devices details.", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return deviceInfos; + } + @Override public void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtException {