From 1100db4d6763712c891c4a312eac18ca631d005f Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 3 May 2019 17:01:40 +0530 Subject: [PATCH] Add lifecycle retrieving API --- .../mgt/api/services/ConfigRetrieveAPI.java | 28 ++++++ .../services/impl/ConfigRetrieveAPIImpl.java | 25 +++++ .../mgt/common/config/LifecycleGraph.java | 70 ++++++++++++++ .../common/config/LifecycleStateVertex.java | 91 +++++++++++++++++++ .../mgt/common/services/AppmDataHandler.java | 4 + .../mgt/core/impl/AppmDataHandlerImpl.java | 15 ++- .../core/lifecycle/LifecycleStateManager.java | 33 +++++++ 7 files changed, 265 insertions(+), 1 deletion(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java index 15c76d251e..f4e3130c97 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ConfigRetrieveAPI.java @@ -81,4 +81,32 @@ public interface ConfigRetrieveAPI { }) Response getUiConfig(); + @GET + @Path("/lifecycle-config") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get application management UI configuration", + notes = "This will get all UI configuration of application management" + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully got Lifecycle Config.", + response = ApplicationList.class), + @ApiResponse( + code = 404, + message = "Not Found. There doesn't have an defined in app management " + + "configuration file." + + "query."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the lifecycle config.", + response = ErrorResponse.class) + }) + Response getLifecycleConfig(); + } 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/ConfigRetrieveAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java index fe7c97ef64..b00ee9a9b9 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ConfigRetrieveAPIImpl.java @@ -20,7 +20,9 @@ package org.wso2.carbon.device.application.mgt.api.services.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.api.services.ConfigRetrieveAPI; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleStateVertex; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; @@ -29,6 +31,9 @@ import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * Implementation of ApplicationDTO Management related APIs. @@ -49,4 +54,24 @@ public class ConfigRetrieveAPIImpl implements ConfigRetrieveAPI { return Response.status(Response.Status.OK).entity(uiConfiguration).build(); } + @GET + @Override + @Consumes("application/json") + @Path("/lifecycle-config") + public Response getLifecycleConfig() { + AppmDataHandler dataHandler = APIUtil.getDataHandler(); + Map> verticesObject = new HashMap<>(); + Map> vertices = null; + try { + vertices = dataHandler.getLifecycleConfiguration().getAdjVertices(); + for (LifecycleStateVertex vt : vertices.keySet()) { + verticesObject.put(vt.getLabel(), vertices.get(vt)); + } + return Response.status(Response.Status.OK).entity(verticesObject).build(); + } catch (LifecycleManagementException e) { + String msg = "Error Occurred while accessing lifecycle manager."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java new file mode 100644 index 0000000000..9b3116c7a7 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleGraph.java @@ -0,0 +1,70 @@ +package org.wso2.carbon.device.application.mgt.common.config;/* 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. + */ + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class LifecycleGraph { + + private Map> adjVertices; + + public LifecycleGraph() { + this.adjVertices = new HashMap<>() ; + } + + public void addVertex(String label) { + adjVertices.putIfAbsent(new LifecycleStateVertex(label), new ArrayList<>()); + } + + public void addVertex(LifecycleStateVertex vertext) { + adjVertices.putIfAbsent(vertext, new ArrayList<>()); + } + + void removeVertex(String label) { + LifecycleStateVertex v = new LifecycleStateVertex(label); + adjVertices.values() + .stream() + .map(e -> e.remove(v)) + .collect(Collectors.toList()); + adjVertices.remove(new LifecycleStateVertex(label)); + } + + public void addEdge(String label1, String label2) { + LifecycleStateVertex v1 = new LifecycleStateVertex(label1); + LifecycleStateVertex v2 = new LifecycleStateVertex(label2); + adjVertices.get(v1).add(v2); +// adjVertices.get(v2).add(v1); + } + + public void removeEdge(String label1, String label2) { + LifecycleStateVertex v1 = new LifecycleStateVertex(label1); + LifecycleStateVertex v2 = new LifecycleStateVertex(label2); + List eV1 = adjVertices.get(v1); + List eV2 = adjVertices.get(v2); + if (eV1 != null) + eV1.remove(v2); + if (eV2 != null) + eV2.remove(v1); + } + + public Map> getAdjVertices() { + return adjVertices; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java new file mode 100644 index 0000000000..17743db9d8 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/config/LifecycleStateVertex.java @@ -0,0 +1,91 @@ +package org.wso2.carbon.device.application.mgt.common.config;/* 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. + */ + +public class LifecycleStateVertex { + private String label; + private boolean isAppUpdatable; + private boolean isAppInstallable; + private boolean isInitialState; + private boolean isEndState; + + public LifecycleStateVertex(String label) { + this.label = label; + } + + public String getLabel() { + return label; + } + + public boolean isAppUpdatable() { + return isAppUpdatable; + } + + public void setAppUpdatable(boolean appUpdatable) { + isAppUpdatable = appUpdatable; + } + + public boolean isAppInstallable() { + return isAppInstallable; + } + + public void setAppInstallable(boolean appInstallable) { + isAppInstallable = appInstallable; + } + + public boolean isInitialState() { + return isInitialState; + } + + public void setInitialState(boolean initialState) { + isInitialState = initialState; + } + + public boolean isEndState() { + return isEndState; + } + + public void setEndState(boolean endState) { + isEndState = endState; + } + + @Override + public int hashCode(){ + return label == null ? 0 : label.hashCode(); + } + + // Overriding equals() to compare two LifecycleStateVertex objects + @Override + public boolean equals(Object o) { + + // If the object is compared with itself then return true + if (o == this) { + return true; + } + + /* Check if o is an instance of LifecycleStateVertex or not + "null instanceof [type]" also returns false */ + if (!(o instanceof LifecycleStateVertex)) { + return false; + } + + // typecast o to Complex so that we can compare data members + LifecycleStateVertex c = (LifecycleStateVertex) o; + + // Compare the equality of label name and return accordingly + return label.equals(c.label); + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java index 508d7afa3b..f633c2e359 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/AppmDataHandler.java @@ -17,8 +17,10 @@ package org.wso2.carbon.device.application.mgt.common.services; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleGraph; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import java.io.InputStream; @@ -30,5 +32,7 @@ public interface AppmDataHandler { */ UIConfiguration getUIConfiguration(); + LifecycleGraph getLifecycleConfiguration() throws LifecycleManagementException; + InputStream getArtifactStream(String uuid, String artifactName) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java index 9a0aee2efe..5de4f7f134 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/AppmDataHandlerImpl.java @@ -20,8 +20,10 @@ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleGraph; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.common.services.AppmDataHandler; import org.wso2.carbon.device.application.mgt.common.config.UIConfiguration; @@ -30,6 +32,8 @@ import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagem import org.wso2.carbon.device.application.mgt.core.dao.common.Util; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.device.application.mgt.core.lifecycle.LifecycleStateManager; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.Constants; @@ -37,12 +41,16 @@ import java.io.InputStream; public class AppmDataHandlerImpl implements AppmDataHandler { - private UIConfiguration uiConfiguration; private static final Log log = LogFactory.getLog(AppmDataHandlerImpl.class); + private UIConfiguration uiConfiguration; + private LifecycleStateManager lifecycleStateManager; + public AppmDataHandlerImpl(UIConfiguration config) { this.uiConfiguration = config; + lifecycleStateManager = DataHolder.getInstance().getLifecycleStateManager(); + } @Override @@ -50,6 +58,11 @@ public class AppmDataHandlerImpl implements AppmDataHandler { return this.uiConfiguration; } + @Override + public LifecycleGraph getLifecycleConfiguration() throws LifecycleManagementException { + return lifecycleStateManager.getLifecyccleStateGraph(); + } + @Override public InputStream getArtifactStream(String uuid, String artifactName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java index 03598a7f43..a33eaedab1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/lifecycle/LifecycleStateManager.java @@ -19,6 +19,8 @@ package org.wso2.carbon.device.application.mgt.core.lifecycle; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleGraph; +import org.wso2.carbon.device.application.mgt.common.config.LifecycleStateVertex; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.lifecycle.config.LifecycleState; @@ -62,6 +64,37 @@ public class LifecycleStateManager { } } + public LifecycleGraph getLifecyccleStateGraph() throws LifecycleManagementException { + LifecycleGraph lifecycleGraph = new LifecycleGraph(); + Map lifecycleStatesDup = lifecycleStates; + + for (State state : lifecycleStatesDup.values()) { + Set proceedingStateNames = state.getProceedingStates(); + String stateName = state.getState(); + LifecycleStateVertex lifecycleStateVertex = new LifecycleStateVertex(stateName); + if (isInitialState(stateName)) { + lifecycleStateVertex.setInitialState(true); + } + if (isUpdatableState(stateName)) { + lifecycleStateVertex.setAppUpdatable(true); + } + if (isEndState(stateName)) { + lifecycleStateVertex.setEndState(true); + } + if (isInstallableState(stateName)) { + lifecycleStateVertex.setAppInstallable(true); + } + lifecycleGraph.addVertex(lifecycleStateVertex); + if (proceedingStateNames != null) { + proceedingStateNames.forEach(proceedingStateName -> { + lifecycleGraph.addVertex(proceedingStateName); + lifecycleGraph.addEdge(stateName, proceedingStateName); + }); + } + } + return lifecycleGraph; + } + public Set getNextLifecycleStates(String currentLifecycleState) { return lifecycleStates.get(currentLifecycleState.toUpperCase()).getProceedingStates();