From 9f76f51ee08cd206b48735a64a35f6aa2c0abc5b Mon Sep 17 00:00:00 2001 From: Chathura Ekanayake Date: Tue, 8 Aug 2017 16:13:49 +0530 Subject: [PATCH] lifecycle management changes --- .../api/services/LifecycleManagementAPI.java | 111 +++++++++++++++++- .../impl/LifecycleManagementAPIImpl.java | 6 +- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 2 + .../mgt/core/util/ConnectionManagerUtil.java | 13 ++ 4 files changed, 123 insertions(+), 9 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java index b2f56851007..fdd33f1a29b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/LifecycleManagementAPI.java @@ -18,6 +18,9 @@ package org.wso2.carbon.device.application.mgt.api.services; import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; import io.swagger.annotations.Extension; import io.swagger.annotations.ExtensionProperty; import io.swagger.annotations.Info; @@ -25,13 +28,19 @@ import io.swagger.annotations.SwaggerDefinition; import io.swagger.annotations.Tag; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; +import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; +import org.wso2.carbon.device.application.mgt.common.Application; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.Platform; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.Path; +import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.List; @SwaggerDefinition( info = @Info( @@ -40,7 +49,7 @@ import javax.ws.rs.core.Response; extensions = { @Extension(properties = { @ExtensionProperty(name = "name", value = "LifecycleManagementService"), - @ExtensionProperty(name = "context", value = "/api/application-mgt/v1.0/lifecycle"), + @ExtensionProperty(name = "context", value = "/api/application-mgt/v1.0/lifecycles"), }) } ), @@ -54,23 +63,115 @@ import javax.ws.rs.core.Response; name = "Get Lifecycle Details", description = "Get lifecycle details", key = "perm:lifecycle:get", - permissions = {"/device-mgt/lifecycle/get"} + permissions = {"/device-mgt/lifecycles/get"} ), @Scope( name = "Add a lifecycle state", description = "Add a lifecycle state", key = "perm:lifecycle:add", - permissions = {"/device-mgt/lifecycle/add"} + permissions = {"/device-mgt/lifecycles/add"} ), + @Scope( + name = "Delete a lifecycle state", + description = "Delete a lifecycle state", + key = "perm:lifecycle:delete", + permissions = {"/device-mgt/lifecycles/delete"} + ) } ) -@Path("/lifecycle") +@Path("/lifecycles") @Api(value = "Lifecycle Management", description = "This API carries all lifecycle management related operations.") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public interface LifecycleManagementAPI { + String SCOPE = "scope"; + @GET - @Path("/states") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get lifecycle states", + notes = "Get all lifecycle states", + tags = "Lifecycle Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:lifecycle:get") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved lifecycle states.", + response = List.class, + responseContainer = "List"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the lifecycle list.", + response = ErrorResponse.class) + }) Response getLifecycleStates(); + + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Add a lifecycle state", + notes = "This will add a new lifecycle state", + tags = "Lifecycle Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:lifecycle:add") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 201, + message = "OK. \n Successfully add a lifecycle state.", + response = Application.class), + @ApiResponse( + code = 304, + message = "Not Modified. \n " + + "Empty body because the client already has the latest version of the requested resource."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred adding a lifecycle state.", + response = ErrorResponse.class) + }) + Response addLifecycleState(LifecycleState state); + + @Path("/{identifier}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "DELETE", + value = "Remove lifecycle state", + notes = "Remove lifecycle state", + tags = "Lifecycle Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:platform:remove") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully deleted the platform"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while deleting the platform.", + response = ErrorResponse.class) + }) + Response deleteLifecycleState(@PathParam("identifier") String identifier); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java index 4be4b90ac7b..b310d31dfdb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/LifecycleManagementAPIImpl.java @@ -38,13 +38,12 @@ import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.List; -@Path("/lifecycle") +@Path("/lifecycles") public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { private static Log log = LogFactory.getLog(LifecycleManagementAPIImpl.class); @GET - @Path("/states") public Response getLifecycleStates() { LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); List lifecycleStates = new ArrayList<>(); @@ -59,7 +58,6 @@ public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { } @POST - @Path("/states") public Response addLifecycleState(LifecycleState state) { LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); try { @@ -73,7 +71,7 @@ public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { } @DELETE - @Path("/states/{identifier}") + @Path("/{identifier}") public Response deleteLifecycleState(@PathParam("identifier") String identifier) { LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); try { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml index 1347461c54a..5b4af27dbe8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -27,6 +27,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> + @@ -35,6 +36,7 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java index d96854bb9f3..ce957da19e5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java @@ -44,6 +44,19 @@ public class ConnectionManagerUtil { private static ThreadLocal currentTxState = new ThreadLocal<>(); private static DataSource dataSource; + public static void openDBConnection() throws DBConnectionException { + Connection conn = currentConnection.get(); + if (conn != null) { + throw new IllegalTransactionStateException("Database connection has already been obtained."); + } + try { + conn = dataSource.getConnection(); + } catch (SQLException e) { + throw new DBConnectionException("Failed to get a database connection.", e); + } + currentConnection.set(conn); + } + public static Connection getDBConnection() throws DBConnectionException { Connection conn = currentConnection.get(); if (conn == null) {