From 13960f77c9d0600003de2c46ebae935b5708b2ce Mon Sep 17 00:00:00 2001 From: Inosh Perara Date: Mon, 17 Jun 2019 08:09:00 +0000 Subject: [PATCH] add device type versioning APIs --- .../jaxrs/beans/DeviceTypeVersionWrapper.java | 40 ++++ .../DeviceTypeManagementAdminService.java | 211 ++++++++++++++++++ .../DeviceTypeManagementAdminServiceImpl.java | 136 +++++++++++ .../mgt/jaxrs/util/DeviceMgtAPIUtils.java | 12 + .../common/spi/DeviceManagementService.java | 3 + .../type/mgt/DeviceTypePlatformDetails.java | 44 ++++ .../type/mgt/DeviceTypePlatformVersion.java | 43 ++++ .../device/mgt/core/dao/DeviceTypeDAO.java | 41 ++++ .../mgt/core/dao/impl/DeviceTypeDAOImpl.java | 150 +++++++++++++ .../mgt/core/dto/DeviceTypeVersion.java | 66 ++++++ .../DeviceManagementProviderService.java | 12 + .../DeviceManagementProviderServiceImpl.java | 166 ++++++++++++++ .../mgt/core/TestDeviceManagementService.java | 6 + .../template/DeviceTypeManagerService.java | 16 ++ .../config/DeviceTypeConfiguration.java | 12 + .../test/resources/device-types/arduino.xml | 9 + .../mock/TypeXDeviceManagementService.java | 6 + .../src/main/resources/dbscripts/cdm/h2.sql | 17 ++ .../main/resources/dbscripts/cdm/mssql.sql | 17 ++ .../main/resources/dbscripts/cdm/mysql.sql | 18 ++ .../main/resources/dbscripts/cdm/oracle.sql | 25 +++ .../resources/dbscripts/cdm/postgresql.sql | 15 ++ 22 files changed, 1065 insertions(+) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeVersionWrapper.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/type/mgt/DeviceTypePlatformDetails.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/type/mgt/DeviceTypePlatformVersion.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceTypeVersion.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeVersionWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeVersionWrapper.java new file mode 100644 index 00000000000..28d1b377745 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceTypeVersionWrapper.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.jaxrs.beans; + +public class DeviceTypeVersionWrapper { + + String versionName; + String versionStatus; + + public String getVersionName() { + return versionName; + } + + public void setVersionName(String versionName) { + this.versionName = versionName; + } + + public String getVersionStatus() { + return versionStatus; + } + + public void setVersionStatus(String versionStatus) { + this.versionStatus = versionStatus; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java index d669b2c3099..b472b5832d6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/DeviceTypeManagementAdminService.java @@ -51,11 +51,13 @@ import org.wso2.carbon.apimgt.annotations.api.Scopes; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeList; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeVersionWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import javax.validation.constraints.Size; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -370,4 +372,213 @@ public interface DeviceTypeManagementAdminService { PlatformConfiguration config); + @Path("/versions") + @POST + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Add Device Type Version", + notes = "Add a new device type version.", + tags = "Device Type Management Administrative Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin:device-type") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully updated the device type version.", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body") + }), + @ApiResponse( + code = 304, + message = "Not Modified. Empty body because the client already has the latest version of the " + + "requested resource.\n"), + @ApiResponse( + code = 401, + message = "Unauthorized.\n The unauthorized access to the requested resource.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found.\n The specified device does not exist", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the device list.", + response = ErrorResponse.class) + }) + Response addDeviceTypeVersion( + @ApiParam( + name = "deviceTypeName", + value = "Device type name.", + required = true) + @PathParam("deviceTypeName") + String deviceTypeName, + @ApiParam( + name = "deviceTypeVersion", + value = "The device type version details.", + required = true) DeviceTypeVersionWrapper deviceTypeVersion); + + @Path("/{deviceTypeName}/versions") + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get Device Type Version", + notes = "Get a new device type version.", + tags = "Device Type Management Administrative Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin:device-type") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the device type version.", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body") + }), + @ApiResponse( + code = 304, + message = "Not Modified. Empty body because the client already has the latest version of the " + + "requested resource.\n"), + @ApiResponse( + code = 401, + message = "Unauthorized.\n The unauthorized access to the requested resource.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found.\n The specified device does not exist", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the device list.", + response = ErrorResponse.class) + }) + Response getDeviceTypeVersion( + @ApiParam( + name = "deviceTypeName", + value = "Device type name.", + required = true) + @PathParam("deviceTypeName") + String deviceTypeName); + + + @Path("{deviceTypeName}/versions") + @PUT + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "Update Device Type Version", + notes = "Update a new device type version.", + tags = "Device Type Management Administrative Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin:device-type") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully updated the device type version.", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body") + }), + @ApiResponse( + code = 304, + message = "Not Modified. Empty body because the client already has the latest version of the " + + "requested resource.\n"), + @ApiResponse( + code = 401, + message = "Unauthorized.\n The unauthorized access to the requested resource.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found.\n The specified device does not exist", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the device list.", + response = ErrorResponse.class) + }) + Response updateDeviceTypeVersion( + @ApiParam( + name = "deviceTypeName", + value = "Device type name.", + required = true) + @PathParam("deviceTypeName") + String deviceTypeName, + @ApiParam( + name = "deviceTypeVersion", + value = "The device type version details.", + required = true) DeviceTypeVersionWrapper deviceTypeVersion); + + + @Path("{deviceTypeName}/versions/{version}") + @DELETE + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "DELETE", + value = "Move Device Type Version to removed state", + notes = "Move Device Type Version to removed state.", + tags = "Device Type Management Administrative Service", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin:device-type") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully moved the device type version to removed.", + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body") + }), + @ApiResponse( + code = 304, + message = "Not Modified. Empty body because the client already has the latest version of the " + + "requested resource.\n"), + @ApiResponse( + code = 401, + message = "Unauthorized.\n The unauthorized access to the requested resource.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Not Found.\n The specified device does not exist", + response = ErrorResponse.class), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the device list.", + response = ErrorResponse.class) + }) + Response deleteDeviceTypeVersion(@ApiParam( + name = "deviceTypeName", + value = "Device type name.", + required = true) + @PathParam("deviceTypeName") String deviceTypeName, + @ApiParam( + name = "version", + value = "Device type version.", + required = true) + @PathParam("version")String version); + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java index 3edaf7af965..bdf1662687d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/DeviceTypeManagementAdminServiceImpl.java @@ -42,11 +42,14 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeVersionWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.DeviceTypeManagementAdminService; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -179,4 +182,137 @@ public class DeviceTypeManagementAdminServiceImpl implements DeviceTypeManagemen } return Response.status(isSaved ? Response.Status.OK : Response.Status.BAD_REQUEST).build(); } + + @Override + @Path("{deviceTypeName}/versions") + @POST + public Response addDeviceTypeVersion(@PathParam("deviceTypeName") String deviceTypeName, + DeviceTypeVersionWrapper versionWrapper) { + if (versionWrapper != null && deviceTypeName != null && !deviceTypeName.isEmpty() + && versionWrapper.getVersionName() != null && !versionWrapper.getVersionName().isEmpty()) { + try { + // Handle device type availability in current tenant. + DeviceTypeVersion deviceTypeVersion = DeviceMgtAPIUtils.getDeviceManagementService() + .getDeviceTypeVersion(deviceTypeName, versionWrapper.getVersionName()); + if (deviceTypeVersion != null) { + String msg = "Device type version already available, " + deviceTypeName; + return Response.status(Response.Status.CONFLICT).entity(msg).build(); + } + + // Handle device type availability in current tenant. + DeviceType deviceType = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceType(deviceTypeName); + if (deviceType != null) { + boolean result = DeviceMgtAPIUtils.getDeviceManagementService() + .addDeviceTypeVersion(DeviceMgtAPIUtils.convertDeviceTypeVersionWrapper(deviceTypeName, + deviceType.getId(), versionWrapper)); + if (result) { + return Response.serverError().entity("Could not add the version").build(); + } else { + return Response.status(Response.Status.OK).build(); + } + } else { + String msg = "Device type is not available " + versionWrapper.getVersionName(); + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } + + } catch (DeviceManagementException e) { + String msg = "Error occurred while adding a device type version."; + log.error(msg, e); + return Response.serverError().entity(msg).build(); + } + } else { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + } + + @GET + @Path("/{deviceTypeName}/versions") + @Override + public Response getDeviceTypeVersion(@PathParam("deviceTypeName") String deviceTypeName) { + try { + List deviceTypes = DeviceMgtAPIUtils.getDeviceManagementService() + .getDeviceTypeVersions(deviceTypeName); + return Response.status(Response.Status.OK).entity(deviceTypes).build(); + } catch (DeviceManagementException e) { + String msg = "Error occurred while getting device type version for device type: " + deviceTypeName; + log.error(msg, e); + return Response.serverError().entity(msg).build(); + } + } + + @PUT + @Override + @Path("{deviceTypeName}/versions") + public Response updateDeviceTypeVersion(@PathParam("deviceTypeName") String deviceTypeName, + DeviceTypeVersionWrapper deviceTypeVersion) { + if (deviceTypeVersion != null && deviceTypeVersion.getVersionName() == null || deviceTypeVersion + .getVersionName().isEmpty()) { + return Response.status(Response.Status.BAD_REQUEST).entity("Device type version cannot be empty.").build(); + } else if (deviceTypeName == null || deviceTypeName.isEmpty()) { + return Response.status(Response.Status.BAD_REQUEST).entity("Device type name cannot be empty.").build(); + } + + try { + boolean isAuthorized = DeviceMgtAPIUtils.getDeviceManagementService() + .isDeviceTypeVersionChangeAuthorized(deviceTypeName, deviceTypeVersion.getVersionName()); + if (!isAuthorized) { + return Response.status(Response.Status.UNAUTHORIZED).entity("Unauthorized to modify version.") + .build(); + } + + DeviceType deviceType = DeviceMgtAPIUtils.getDeviceManagementService().getDeviceType(deviceTypeName); + if (deviceType != null) { + boolean result = DeviceMgtAPIUtils.getDeviceManagementService().updateDeviceTypeVersion(DeviceMgtAPIUtils + .convertDeviceTypeVersionWrapper(deviceTypeName, deviceType.getId(), deviceTypeVersion)); + if (result) { + return Response.serverError().entity("Could not update the version").build(); + } else { + return Response.status(Response.Status.OK).build(); + } + } else { + String msg = "Device type is not available " + deviceTypeName; + return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); + } + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating device type: " + deviceTypeName ; + log.error(msg, e); + return Response.serverError().entity(msg).build(); + } + } + + @DELETE + @Override + @Path("{deviceTypeName}/versions/{version}") + public Response deleteDeviceTypeVersion(@PathParam("deviceTypeName") String deviceTypeName, + @PathParam("version") String version) { + if (version == null || version.isEmpty()) { + return Response.status(Response.Status.BAD_REQUEST).entity("Device type version cannot be empty.").build(); + } else if (deviceTypeName == null || deviceTypeName.isEmpty()) { + return Response.status(Response.Status.BAD_REQUEST).entity("Device type name cannot be empty.").build(); + } + + DeviceTypeVersion deviceTypeVersion = new DeviceTypeVersion(); + deviceTypeVersion.setDeviceTypeName(deviceTypeName); + deviceTypeVersion.setVersionName(version); + deviceTypeVersion.setVersionStatus("REMOVED"); + try { + boolean isAuthorized = DeviceMgtAPIUtils.getDeviceManagementService().isDeviceTypeVersionChangeAuthorized + (deviceTypeVersion.getDeviceTypeName(), deviceTypeVersion.getVersionName()); + if (!isAuthorized) { + return Response.status(Response.Status.UNAUTHORIZED).entity("Unauthorized to modify version.") + .build(); + } + boolean result = DeviceMgtAPIUtils.getDeviceManagementService().updateDeviceTypeVersion(deviceTypeVersion); + if (result) { + return Response.serverError().entity("Could not delete the version").build(); + } else { + return Response.status(Response.Status.OK).build(); + } + } catch (DeviceManagementException e) { + String msg = "Error occurred while updating device type: " + deviceTypeVersion.getDeviceTypeId() ; + log.error(msg, e); + return Response.serverError().entity(msg).build(); + } + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index eaaf3d3febd..2c7a3651eb9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -49,10 +49,12 @@ import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagement import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; +import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.privacy.PrivacyComplianceProvider; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeVersionWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.analytics.EventAttributeList; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.InputValidationException; @@ -717,4 +719,14 @@ public class DeviceMgtAPIUtils { } return false; } + + public static DeviceTypeVersion convertDeviceTypeVersionWrapper(String deviceTypeName, int deviceTypeId, + DeviceTypeVersionWrapper deviceTypeVersion) { + DeviceTypeVersion typeVersion = new DeviceTypeVersion(); + typeVersion.setDeviceTypeId(deviceTypeId); + typeVersion.setDeviceTypeName(deviceTypeName); + typeVersion.setVersionName(deviceTypeVersion.getVersionName()); + typeVersion.setVersionStatus(deviceTypeVersion.getVersionStatus()); + return typeVersion; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java index 33a9ac867bc..79426e808e9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.general.GeneralConfig; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; +import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypePlatformDetails; /** * Composite interface that acts as the SPI exposing all device management as well as application management @@ -56,4 +57,6 @@ public interface DeviceManagementService { GeneralConfig getGeneralConfig(); + DeviceTypePlatformDetails getDeviceTypePlatformDetails(); + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/type/mgt/DeviceTypePlatformDetails.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/type/mgt/DeviceTypePlatformDetails.java new file mode 100644 index 00000000000..61a57fac560 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/type/mgt/DeviceTypePlatformDetails.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.type.mgt; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlType; +import java.util.List; + +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "DeviceTypePlatformDetails", propOrder = { + "deviceTypePlatformVersion" +}) +public class DeviceTypePlatformDetails { + + @XmlElement(name = "DeviceTypePlatformVersion") + private List deviceTypePlatformVersion; + + public List getDeviceTypePlatformVersion() { + return deviceTypePlatformVersion; + } + + public void setDeviceTypePlatformVersion(List deviceTypePlatformVersion) { + this.deviceTypePlatformVersion = deviceTypePlatformVersion; + } +} + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/type/mgt/DeviceTypePlatformVersion.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/type/mgt/DeviceTypePlatformVersion.java new file mode 100644 index 00000000000..c7fe9ee9450 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/type/mgt/DeviceTypePlatformVersion.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.type.mgt; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlType; + +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "DeviceTypePlatformVersion", propOrder = { + "versionsName" +}) +public class DeviceTypePlatformVersion { + + @XmlElement(name = "VersionName", required = true) + private String versionsName; + + public String getVersionsName() { + return versionsName; + } + + public void setVersionsName(String versionsName) { + this.versionsName = versionsName; + } +} + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java index 337a5af133b..4ae4289993f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceTypeDAO.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.dao; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import java.util.List; @@ -63,6 +64,18 @@ public interface DeviceTypeDAO { */ List getSharedDeviceTypes() throws DeviceManagementDAOException; + /** + * Check if the private device types in the provided tenant matched the device type and version provided. + * If so, this is a device type version created by the tenant provided, hence modifiable. + * @param deviceTypeID device type ID of interest + * @param versionName name of the device type version ID of interest + * @param tenantId tenant ID of interest + * @return if modifiable + * @throws DeviceManagementDAOException + */ + boolean isDeviceTypeVersionModifiable(int deviceTypeID, String versionName, int tenantId) + throws DeviceManagementDAOException; + /** * @param id retrieve the device type with its id. * @return the device type associated with the id. @@ -87,4 +100,32 @@ public interface DeviceTypeDAO { */ void removeDeviceType(String name, int tenantId) throws DeviceManagementDAOException; + /** + * Add a set of device type versions to the database + * @param deviceTypeVersion device type version details + * @return success or failure + * @throws DeviceManagementDAOException + */ + boolean addDeviceTypeVersion(DeviceTypeVersion deviceTypeVersion) throws DeviceManagementDAOException; + + /** + * Get device type details + * @param deviceTypeId device type id + * @param typeName device type name + * @return list of versions of device types + * @throws DeviceManagementDAOException + */ + List getDeviceTypeVersions(int deviceTypeId, String typeName) + throws DeviceManagementDAOException; + + /** + * Update device type version + * @param deviceTypeVersion device type version details + * @return update status + * @throws DeviceManagementException + */ + boolean updateDeviceTypeVersion(DeviceTypeVersion deviceTypeVersion) throws DeviceManagementDAOException; + + DeviceTypeVersion getDeviceTypeVersion(int deviceTypeId, String version) + throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index a4ecacfd3a1..35e92651908 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -24,7 +24,14 @@ 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.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.file.Files; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -187,6 +194,34 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { } } + @Override + public boolean isDeviceTypeVersionModifiable(int deviceTypeID, String versionName, int tenantId) throws + DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getConnection(); + String sql = + "SELECT dt.ID as DEVICE_TYPE_IDENTIFIER, dt.NAME as DEVICE_TYPE_NAME " + + "FROM DM_DEVICE_TYPE_PLATFORM dv, DM_DEVICE_TYPE dt " + + "WHERE dt.ID = dv.DEVICE_TYPE_ID AND dv.DEVICE_TYPE_ID = ? AND dv.VERSION_NAME = ?" + + " AND dt.PROVIDER_TENANT_ID = ? AND dt.SHARED_WITH_ALL_TENANTS = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceTypeID); + stmt.setString(2, versionName); + stmt.setInt(3, tenantId); + stmt.setBoolean(4, false); + + rs = stmt.executeQuery(); + return rs.next(); + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while fetching the registered device types", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + @Override public DeviceType getDeviceType(int id) throws DeviceManagementDAOException { Connection conn; @@ -261,6 +296,121 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { } + @Override + public boolean addDeviceTypeVersion(DeviceTypeVersion deviceTypeVersion) throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "INSERT INTO DM_DEVICE_TYPE_PLATFORM (DEVICE_TYPE_ID, VERSION_NAME) VALUES (?,?)"; + if (deviceTypeVersion.getVersionStatus() != null) { + sql = "INSERT INTO DM_DEVICE_TYPE_PLATFORM (DEVICE_TYPE_ID, VERSION_NAME, VERSION_STATUS) " + + "VALUES (?,?,?)"; + } + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceTypeVersion.getDeviceTypeId()); + stmt.setString(2, deviceTypeVersion.getVersionName()); + if (deviceTypeVersion.getVersionStatus() != null) { + stmt.setString(3, deviceTypeVersion.getVersionStatus()); + } + return stmt.execute(); + } catch (SQLException e) { + throw new DeviceManagementDAOException( + "Error occurred while adding the version: " + deviceTypeVersion.getVersionName() + + " to device type: " + deviceTypeVersion.getDeviceTypeId(), e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public List getDeviceTypeVersions(int deviceTypeId, String typeName) + throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + List deviceTypesVersions = new ArrayList<>(); + try { + conn = this.getConnection(); + String sql = "SELECT * FROM DM_DEVICE_TYPE_PLATFORM where DEVICE_TYPE_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceTypeId); + rs = stmt.executeQuery(); + + while (rs.next()) { + DeviceTypeVersion deviceTypeVersion = new DeviceTypeVersion(); + deviceTypeVersion.setId(rs.getInt("ID")); + deviceTypeVersion.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID")); + deviceTypeVersion.setDeviceTypeName(typeName); // Adding this for the sake of completeness of DTO + deviceTypeVersion.setVersionName(rs.getString("VERSION_NAME")); + deviceTypeVersion.setVersionStatus(rs.getString("VERSION_STATUS")); + deviceTypesVersions.add(deviceTypeVersion); + } + return deviceTypesVersions; + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while fetching device type versions for device " + + "type: " + deviceTypeId, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + } + + @Override + public boolean updateDeviceTypeVersion(DeviceTypeVersion deviceTypeVersion) + throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + try { + conn = this.getConnection(); + String sql = "UPDATE DM_DEVICE_TYPE_PLATFORM SET " + + " VERSION_STATUS = ? WHERE DEVICE_TYPE_ID = ? AND VERSION_NAME = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, deviceTypeVersion.getVersionStatus()); + stmt.setInt(2, deviceTypeVersion.getDeviceTypeId()); + stmt.setString(3, deviceTypeVersion.getVersionName()); + return stmt.execute(); + } catch (SQLException e) { + throw new DeviceManagementDAOException( + "Error occurred while updating details of the version: " + deviceTypeVersion.getVersionName() + + " and device type: " + deviceTypeVersion.getDeviceTypeId(), e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, null); + } + } + + @Override + public DeviceTypeVersion getDeviceTypeVersion(int deviceTypeId, String version) + throws DeviceManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + DeviceTypeVersion deviceTypeVersion = null; + try { + conn = this.getConnection(); + String sql = + "SELECT * FROM DM_DEVICE_TYPE_PLATFORM WHERE DEVICE_TYPE_ID = ? AND VERSION_NAME = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceTypeId); + stmt.setString(2, version); + rs = stmt.executeQuery(); + + while (rs.next()) { + deviceTypeVersion = new DeviceTypeVersion(); + deviceTypeVersion.setId(rs.getInt("ID")); + deviceTypeVersion.setDeviceTypeId(rs.getInt("DEVICE_TYPE_ID")); + deviceTypeVersion.setVersionName(rs.getString("VERSION_NAME")); + deviceTypeVersion.setVersionStatus(rs.getString("VERSION_STATUS")); + } + return deviceTypeVersion; + } catch (SQLException e) { + throw new DeviceManagementDAOException("Error occurred while fetching device type version for device " + + "type: " + deviceTypeId + ", and version " + version, e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } + + } + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceTypeVersion.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceTypeVersion.java new file mode 100644 index 00000000000..c9d09bb0b73 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dto/DeviceTypeVersion.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2019, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.dto; + +public class DeviceTypeVersion { + int id; + int deviceTypeId; + String deviceTypeName; + String versionName; + String versionStatus; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getDeviceTypeId() { + return deviceTypeId; + } + + public void setDeviceTypeId(int deviceTypeId) { + this.deviceTypeId = deviceTypeId; + } + + public String getDeviceTypeName() { + return deviceTypeName; + } + + public void setDeviceTypeName(String deviceTypeName) { + this.deviceTypeName = deviceTypeName; + } + + public String getVersionName() { + return versionName; + } + + public void setVersionName(String versionName) { + this.versionName = versionName; + } + + public String getVersionStatus() { + return versionStatus; + } + + public void setVersionStatus(String versionStatus) { + this.versionStatus = versionStatus; + } +} 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 47be9083b79..07aedd30a19 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 @@ -40,6 +40,7 @@ import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecu import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.dto.DeviceType; +import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; @@ -671,4 +672,15 @@ public interface DeviceManagementProviderService { boolean updateEnrollment(String owner, List deviceIdentifiers) throws DeviceManagementException, UserNotFoundException, InvalidDeviceException; + + boolean addDeviceTypeVersion(DeviceTypeVersion deviceTypeVersion) throws DeviceManagementException; + + List getDeviceTypeVersions(String typeName) throws DeviceManagementException; + + boolean updateDeviceTypeVersion(DeviceTypeVersion deviceTypeVersion) throws DeviceManagementException; + + boolean isDeviceTypeVersionChangeAuthorized(String typeName, String version) throws DeviceManagementException; + + DeviceTypeVersion getDeviceTypeVersion(String deviceTypeName, String version) 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 965dffb2870..da01ffd7963 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 @@ -73,6 +73,8 @@ import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecu import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypePlatformDetails; +import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypePlatformVersion; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl; @@ -90,6 +92,7 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDA import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; +import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.geo.GeoCluster; import org.wso2.carbon.device.mgt.core.geo.geoHash.GeoCoordinate; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; @@ -2109,6 +2112,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } try { pluginRepository.addDeviceManagementProvider(deviceManagementService); + initializeDeviceTypeVersions(deviceManagementService); } catch (DeviceManagementException e) { String msg = "Error occurred while registering device management plugin '" + deviceManagementService.getType() + "'"; @@ -3034,4 +3038,166 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv + "constructing is failed", e); } } + + private void initializeDeviceTypeVersions(DeviceManagementService deviceManagementService) { + DeviceTypePlatformDetails deviceTypePlatformDetails = deviceManagementService.getDeviceTypePlatformDetails(); + String deviceType = deviceManagementService.getType(); + try { + if (deviceTypePlatformDetails != null && deviceTypePlatformDetails.getDeviceTypePlatformVersion() != null + && deviceTypePlatformDetails.getDeviceTypePlatformVersion().size() > 0) { + + List fromXML = deviceTypePlatformDetails.getDeviceTypePlatformVersion(); + List newPlatformsToBeAdded = new ArrayList<>(); + List existingPlatformVersions = getDeviceTypeVersions(deviceType); + + for (DeviceTypePlatformVersion versionFromXml : fromXML) { + boolean match = false; + if (existingPlatformVersions != null && existingPlatformVersions.size() > 0) { + for (DeviceTypeVersion existingVersion : existingPlatformVersions) { + if (existingVersion.getVersionName().equals(versionFromXml.getVersionsName())) { + match = true; + } + } + } + + if (!match) { + newPlatformsToBeAdded.add(versionFromXml); + } + } + + DeviceTypeVersion deviceTypeVersion; + for (DeviceTypePlatformVersion version : newPlatformsToBeAdded) { + deviceTypeVersion = new DeviceTypeVersion(); + deviceTypeVersion.setDeviceTypeId(getDeviceType(deviceType).getId()); + deviceTypeVersion.setDeviceTypeName(deviceManagementService.getType()); + deviceTypeVersion.setVersionName(version.getVersionsName()); + addDeviceTypeVersion(deviceTypeVersion); + } + } + } catch (DeviceManagementException e) { + log.error("Error while adding versions for device type: " + deviceManagementService.getType(), e); + } + } + + @Override + public boolean addDeviceTypeVersion(DeviceTypeVersion deviceTypeVersion) throws DeviceManagementException { + boolean success; + try { + DeviceManagementDAOFactory.beginTransaction(); + success = deviceTypeDAO.addDeviceTypeVersion(deviceTypeVersion); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while adding versions to device type: " + deviceTypeVersion.getDeviceTypeName(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return success; + } + + @Override + public List getDeviceTypeVersions(String typeName) throws + DeviceManagementException { + List versions = null; + DeviceType deviceType = getDeviceType(typeName); + if (deviceType != null) { + try { + DeviceManagementDAOFactory.openConnection(); + versions = deviceTypeDAO.getDeviceTypeVersions(deviceType.getId(), typeName); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while getting versions of device type: " + typeName; + 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); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + return versions; + } + + @Override + public boolean updateDeviceTypeVersion(DeviceTypeVersion deviceTypeVersion) throws DeviceManagementException { + boolean success; + try { + DeviceType deviceType = getDeviceType(deviceTypeVersion.getDeviceTypeName()); + DeviceManagementDAOFactory.beginTransaction(); + deviceTypeVersion.setDeviceTypeId(deviceType.getId()); + success = deviceTypeDAO.updateDeviceTypeVersion(deviceTypeVersion); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while updating versions to device type: " + + deviceTypeVersion.getDeviceTypeName(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return success; + } + + @Override + public boolean isDeviceTypeVersionChangeAuthorized(String deviceTypeName, String version) throws + DeviceManagementException { + boolean success = false; + try { + // Get the device type details of the deviceTypeName provided in current tenant. + DeviceType deviceType = getDeviceType(deviceTypeName); + DeviceManagementDAOFactory.openConnection(); + if (deviceType != null) { + success = deviceTypeDAO.isDeviceTypeVersionModifiable(deviceType.getId(), version, this.getTenantId()); + } + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while getting authorization details of device type : " + deviceTypeName; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while getting db connection to authorize details of device type : " + + deviceTypeName; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return success; + } + + public DeviceTypeVersion getDeviceTypeVersion(String deviceTypeName, String version) throws + DeviceManagementException { + DeviceTypeVersion versions = null; + DeviceType deviceType = getDeviceType(deviceTypeName); + if (deviceType != null) { + try { + DeviceManagementDAOFactory.openConnection(); + versions = deviceTypeDAO.getDeviceTypeVersion(deviceType.getId(), version); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while getting versions of device type: " + deviceTypeName + " ,version: " + + version; + 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); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + return versions; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java index e0b3de1c406..863297c3eeb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypePlatformDetails; import java.util.ArrayList; import java.util.List; @@ -115,4 +116,9 @@ public class TestDeviceManagementService implements DeviceManagementService { public GeneralConfig getGeneralConfig() { return null; } + + @Override + public DeviceTypePlatformDetails getDeviceTypePlatformDetails() { + return 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/DeviceTypeManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java index 72657f55771..a6a740137bd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java @@ -35,6 +35,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypePlatformDetails; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.ConfigProperties; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceStatusTaskConfiguration; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceTypeConfiguration; @@ -72,6 +73,7 @@ public class DeviceTypeManagerService implements DeviceManagementService { private InitialOperationConfig initialOperationConfig; private PullNotificationSubscriber pullNotificationSubscriber; private DeviceStatusTaskPluginConfig deviceStatusTaskPluginConfig; + private DeviceTypePlatformDetails deviceTypePlatformDetails; private GeneralConfig generalConfig; private boolean isRegistryBasedConfigs = false; private boolean isScheduled = false; @@ -88,6 +90,8 @@ public class DeviceTypeManagerService implements DeviceManagementService { this.initialOperationConfig = new InitialOperationConfig(); this.setInitialOperationConfig(deviceTypeConfiguration); this.deviceStatusTaskPluginConfig = new DeviceStatusTaskPluginConfig(); + this.deviceTypePlatformDetails = new DeviceTypePlatformDetails(); + this.setDeviceTypePlatformDetails(deviceTypeConfiguration); this.setDeviceStatusTaskPluginConfig(deviceTypeConfiguration.getDeviceStatusTaskConfiguration()); this.setPolicyMonitoringManager(deviceTypeConfiguration.getPolicyMonitoring()); this.setPullNotificationSubscriber(deviceTypeConfiguration.getPullNotificationSubscriberConfig()); @@ -219,6 +223,11 @@ public class DeviceTypeManagerService implements DeviceManagementService { return deviceStatusTaskPluginConfig; } + @Override + public DeviceTypePlatformDetails getDeviceTypePlatformDetails() { + return deviceTypePlatformDetails; + } + @Override public GeneralConfig getGeneralConfig() { return generalConfig; @@ -303,4 +312,11 @@ public class DeviceTypeManagerService implements DeviceManagementService { this.generalConfig.setPolicyMonitoringEnabled(deviceTypeConfiguration.getPolicyMonitoring().isEnabled()); } } + + protected void setDeviceTypePlatformDetails(DeviceTypeConfiguration deviceTypeConfiguration) { + DeviceTypePlatformDetails deviceTypeVersions = deviceTypeConfiguration.getDeviceTypePlatformDetails(); + if (deviceTypeVersions != null) { + deviceTypePlatformDetails.setDeviceTypePlatformVersion(deviceTypeVersions.getDeviceTypePlatformVersion()); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java index 46f442e89d8..103c731716c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java @@ -18,6 +18,8 @@ */ package org.wso2.carbon.device.mgt.extensions.device.type.template.config; +import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypePlatformDetails; + import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlAccessorType; @@ -86,6 +88,16 @@ public class DeviceTypeConfiguration { @XmlElementWrapper(name = "InitialOperationConfig") @XmlElement(name = "Operation", required = true) protected List operations; + @XmlElement(name = "DeviceTypePlatformDetails", required = true) + protected DeviceTypePlatformDetails deviceTypePlatformDetails; + + public DeviceTypePlatformDetails getDeviceTypePlatformDetails() { + return deviceTypePlatformDetails; + } + + public void setDeviceTypePlatformDetails(DeviceTypePlatformDetails deviceTypePlatformDetails) { + this.deviceTypePlatformDetails = deviceTypePlatformDetails; + } public List getOperations() { return operations; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/arduino.xml b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/arduino.xml index 5b1ed2d468b..bca211a30bd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/arduino.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/device-types/arduino.xml @@ -41,4 +41,13 @@ This is license text + + + 32bit + + + 64bit + + + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManagementService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManagementService.java index 4d40c434499..5c2d1e11a87 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManagementService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mock/TypeXDeviceManagementService.java @@ -30,6 +30,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypePlatformDetails; import java.util.ArrayList; @@ -102,4 +103,9 @@ public class TypeXDeviceManagementService implements DeviceManagementService { public GeneralConfig getGeneralConfig() { return null; } + + @Override + public DeviceTypePlatformDetails getDeviceTypePlatformDetails() { + return null; + } } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 6e4989721a6..0309c8b15c9 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -508,6 +508,23 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( ); -- END OF POLICY AND DEVICE GROUP MAPPING -- + +CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_PLATFORM( + ID int NOT NULL AUTO_INCREMENT, + DEVICE_TYPE_ID int NULL DEFAULT 0, + VERSION_NAME varchar(100) NULL, + VERSION_STATUS varchar(100) NULL DEFAULT 'ACTIVE', + PRIMARY KEY (ID), + CONSTRAINT DM_DEVICE_TYPE_DM_DEVICE_TYPE_PLATFORM_MAPPING + FOREIGN KEY (DEVICE_TYPE_ID) + REFERENCES DM_DEVICE_TYPE (ID) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT device_type_version_uk + UNIQUE ( + DEVICE_TYPE_ID, + VERSION_NAME + ) +) + -- DASHBOARD RELATED VIEWS -- CREATE VIEW POLICY_COMPLIANCE_INFO AS SELECT diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 8d6bb0fb7d1..3cb20bf48c1 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -540,6 +540,23 @@ CREATE TABLE DM_DEVICE_DETAIL ( ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_TYPE_PLATFORM]') AND TYPE IN (N'U')) +CREATE TABLE DM_DEVICE_TYPE_PLATFORM( + ID int IDENTITY(1, 1) NOT NULL, + DEVICE_TYPE_ID int NULL DEFAULT 0, + VERSION_NAME varchar(100) NULL, + VERSION_STATUS varchar(100) NULL DEFAULT 'ACTIVE', + PRIMARY KEY (ID), + CONSTRAINT DM_DEVICE_TYPE_DM_DEVICE_TYPE_PLATFORM_MAPPING + FOREIGN KEY (DEVICE_TYPE_ID) + REFERENCES DM_DEVICE_TYPE (ID) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT device_type_version_uk + UNIQUE ( + DEVICE_TYPE_ID, + VERSION_NAME + ) +); + -- DASHBOARD RELATED VIEWS -- IF NOT EXISTS (SELECT * FROM SYS.VIEWS WHERE NAME = 'POLICY_COMPLIANCE_INFO') diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 0ff005b6b92..d26be88d15d 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -556,6 +556,24 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ) ENGINE = InnoDB; +-- ----------------------------------------------------- +-- Table `DM_DEVICE_TYPE_PLATFORM` +-- ----------------------------------------------------- +CREATE TABLE IF NOT EXISTS `DM_DEVICE_TYPE_PLATFORM` ( + `ID` INT NOT NULL AUTO_INCREMENT, + `DEVICE_TYPE_ID` INT DEFAULT 0, + `VERSION_NAME` VARCHAR(100) NULL, + `VERSION_STATUS` VARCHAR(100) DEFAULT 'ACTIVE', + PRIMARY KEY (`ID`), + CONSTRAINT DM_DEVICE_TYPE_DM_DEVICE_TYPE_PLATFORM_MAPPING FOREIGN KEY (DEVICE_TYPE_ID) + REFERENCES DM_DEVICE_TYPE (ID) + ON DELETE CASCADE + ON UPDATE CASCADE, + UNIQUE KEY `device_type_version_uk` (`DEVICE_TYPE_ID`, `VERSION_NAME`) + ) +ENGINE = InnoDB; + + -- DASHBOARD RELATED VIEWS -- CREATE VIEW DEVICE_INFO_VIEW AS diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 9ebdcf56153..97791062722 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -891,6 +891,31 @@ WHEN (NEW.ID IS NULL) END; / + +CREATE TABLE `DM_DEVICE_TYPE_PLATFORM` ( + ID INT NOT NULL, + DEVICE_TYPE_ID INT DEFAULT 0, + VERSION_NAME VARCHAR(100) NULL, + VERSION_STATUS VARCHAR(100) DEFAULT 'ACTIVE', + PRIMARY KEY (ID), + CONSTRAINT DM_DEVICE_TYPE_DM_DEVICE_TYPE_PLATFORM_MAPPING FOREIGN KEY (DEVICE_TYPE_ID) + REFERENCES DM_DEVICE_TYPE (ID) + ON DELETE CASCADE + ON UPDATE CASCADE, + CONSTRAINT device_type_version_uk UNIQUE (DEVICE_TYPE_ID, VERSION_NAME) + ); + +-- Generate ID using sequence and trigger + CREATE SEQUENCE `DM_DEVICE_TYPE_PLATFORM_seq` START WITH 1 INCREMENT BY 1; + +CREATE OR REPLACE TRIGGER `DM_DEVICE_TYPE_PLATFORM_seq_tr` + BEFORE INSERT ON `DM_DEVICE_TYPE_PLATFORM` FOR EACH ROW + WHEN (NEW.ID IS NULL) +BEGIN + SELECT `DM_DEVICE_TYPE_PLATFORM_seq`.NEXTVAL INTO :NEW.ID FROM DUAL; +END; +/ + -- DASHBOARD RELATED VIEWS -- CREATE VIEW POLICY_COMPLIANCE_INFO AS diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 4ef06be29cd..6e6f3c3be2b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -491,6 +491,21 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( ON UPDATE NO ACTION ); +CREATE SEQUENCE `DM_DEVICE_TYPE_PLATFORM`_seq; + +CREATE TABLE IF NOT EXISTS `DM_DEVICE_TYPE_PLATFORM` ( + ID INT NOT NULL DEFAULT NEXTVAL ('`DM_DEVICE_TYPE_PLATFORM`_seq'), + DEVICE_TYPE_ID INT DEFAULT 0, + VERSION_NAME VARCHAR(100) NULL, + VERSION_STATUS VARCHAR(100) DEFAULT 'ACTIVE', + PRIMARY KEY (ID), + CONSTRAINT DM_DEVICE_TYPE_DM_DEVICE_TYPE_PLATFORM_MAPPING FOREIGN KEY (DEVICE_TYPE_ID) + REFERENCES DM_DEVICE_TYPE (ID) + ON DELETE CASCADE + ON UPDATE CASCADE, + CONSTRAINT device_type_version_uk UNIQUE (DEVICE_TYPE_ID, VERSION_NAME) + ) + -- DASHBOARD RELATED VIEWS -- CREATE VIEW POLICY_COMPLIANCE_INFO AS