From 2692693bba79b877a4cfe3278cba4f74a4064f2e Mon Sep 17 00:00:00 2001 From: "waqas.ikram" Date: Mon, 13 Feb 2023 16:52:50 +0000 Subject: Adding Delete AS Change-Id: I70836ab7da419b31ffd6f144fc3058ff440c0586 Issue-ID: SO-4054 Signed-off-by: waqas.ikram --- .../so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTask.java | 139 ++++++++++++ .../lcm/bpmn/flows/tasks/InstantiateAsTask.java | 11 +- .../flows/tasks/InstantiateDeploymentItemTask.java | 21 +- .../cnfm/lcm/bpmn/flows/tasks/TerminateAsTask.java | 12 +- .../src/main/resources/DeleteAs.bpmn | 251 +++++++++++++++++++++ .../lcm/bpmn/flows/tasks/DeleteAsTaskTest.java | 111 +++++++++ 6 files changed, 518 insertions(+), 27 deletions(-) create mode 100644 so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTask.java create mode 100644 so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/resources/DeleteAs.bpmn create mode 100644 so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/test/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTaskTest.java (limited to 'so-cnfm/so-cnfm-lcm') diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTask.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTask.java new file mode 100644 index 0000000..50ffa47 --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTask.java @@ -0,0 +1,139 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm.bpmn.flows.tasks; + +import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.AS_INSTANCE_ID_PARAM_NAME; +import static org.onap.so.cnfm.lcm.bpmn.flows.CamundaVariableNameConstants.AS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME; +import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.FINISHED; +import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.IN_PROGRESS; +import static org.onap.so.cnfm.lcm.database.beans.JobStatusEnum.STARTED; +import java.util.Optional; +import org.camunda.bpm.engine.delegate.DelegateExecution; +import org.onap.so.cnfm.lcm.bpmn.flows.extclients.aai.AaiServiceProvider; +import org.onap.so.cnfm.lcm.database.beans.AsInst; +import org.onap.so.cnfm.lcm.database.beans.State; +import org.onap.so.cnfm.lcm.database.service.DatabaseServiceProvider; +import org.onap.so.cnfm.lcm.model.ErrorDetails; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +/** + * + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +@Component +public class DeleteAsTask extends AbstractServiceTask { + private static final Logger logger = LoggerFactory.getLogger(DeleteAsTask.class); + + private static final String AS_INSTANCE_EXISTS_PARAM_NAME = "asInstanceExists"; + private static final String AS_INSTANCE_IS_IN_NOT_INSTANTIATED_STATE_PARAM_NAME = "isInNotInstantiatedState"; + + private final AaiServiceProvider aaiServiceProvider; + + protected DeleteAsTask(final DatabaseServiceProvider databaseServiceProvider, + final AaiServiceProvider aaiServiceProvider) { + super(databaseServiceProvider); + this.aaiServiceProvider = aaiServiceProvider; + } + + public void setJobStatusToStarted(final DelegateExecution execution) { + setJobStatus(execution, STARTED, "Delete AS workflow process started"); + } + + public void setJobStatusToFinished(final DelegateExecution execution) { + setJobStatus(execution, FINISHED, "Delete AS workflow process finished"); + } + + public void setJobStatusInProgress(final DelegateExecution execution, final String message) { + setJobStatus(execution, IN_PROGRESS, message); + } + + public void setJobStatusToError(final DelegateExecution execution) { + setJobStatusToError(execution, "Delete AS workflow process failed"); + } + + public void checkIfAsInstanceExistsInDb(final DelegateExecution execution) { + logger.info("Executing checkIfAsInstanceExistsInDb ..."); + setJobStatusInProgress(execution, "Checking that AS Instance Exists in DB"); + + final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME); + final Optional optionalAsInst = databaseServiceProvider.getAsInst(asInstId); + final boolean asInstanceExists = optionalAsInst.isPresent(); + logger.info("AS Instance entry with id: {} {} exist in database", asInstId, + asInstanceExists ? "does" : "doesn't"); + execution.setVariable(AS_INSTANCE_EXISTS_PARAM_NAME, asInstanceExists); + + if (!asInstanceExists) { + final String message = + "AS Instance with id: " + asInstId + " does not exist in database, so will not be deleted."; + logger.info(message); + execution.setVariable(AS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new ErrorDetails().detail(message)); + } + + logger.info("Finished executing checkIfAsInstanceExistsInDb ..."); + } + + public void checkifAsInstanceInDbIsInNotInstantiatedState(final DelegateExecution execution) { + logger.info("Executing checkifAsInstanceInDbIsInNotInstantiatedState ..."); + setJobStatusInProgress(execution, "Checking if AS Instance in database is in NOT_INSTANTIATED state"); + + final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME); + final AsInst asInst = getAsInst(execution, asInstId); + final State state = asInst.getStatus(); + final boolean isInNotInstantiatedState = State.NOT_INSTANTIATED.equals(state); + logger.info("As Instance entry with asInstId: {} is in state: {}", asInstId, state); + execution.setVariable(AS_INSTANCE_IS_IN_NOT_INSTANTIATED_STATE_PARAM_NAME, isInNotInstantiatedState); + + if (!isInNotInstantiatedState) { + final String message = "Cannot Delete AS Instance with id: " + asInstId + " in the state: " + state; + logger.info(message); + execution.setVariable(AS_WORKFLOW_PROCESSING_EXCEPTION_PARAM_NAME, new ErrorDetails().detail(message)); + } + + logger.info("Finished executing checkifAsInstanceInDbIsInNotInstantiatedState ..."); + } + + public void deleteGenericVnfFromAai(final DelegateExecution execution) { + logger.info("Executing deleteGenericVnfFromAai ..."); + try { + final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME); + aaiServiceProvider.deleteGenericVnf(asInstId); + } catch (final Exception exception) { + final String message = "Unable to Delete GenericVnf from AAI "; + logger.error(message, exception); + abortOperation(execution, message); + } + + + logger.info("Finished executing deleteGenericVnfFromAai ..."); + } + + public void deleteAsInstanceFromDb(final DelegateExecution execution) { + logger.info("Executing deleteAsInstanceFromDb ..."); + setJobStatusInProgress(execution, "Deleting AS Instance from Db"); + + final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME); + databaseServiceProvider.deleteAsInst(asInstId); + + logger.info("Finished executing deleteAsInstanceFromDb ..."); + } +} diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateAsTask.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateAsTask.java index 789f1be..9fa3100 100644 --- a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateAsTask.java +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateAsTask.java @@ -161,7 +161,7 @@ public class InstantiateAsTask extends AbstractServiceTask { if (!Files.exists(path.getParent())) { final File parentDir = path.getParent().toFile(); - logger.debug("Creating sub directories to download helm chart file {}", parentDir.toString()); + logger.debug("Creating sub directories to download helm chart file {}", parentDir); parentDir.mkdirs(); } @@ -381,10 +381,9 @@ public class InstantiateAsTask extends AbstractServiceTask { final List asDeploymentItems = databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId); if (asDeploymentItems != null) { - asDeploymentItems.stream().forEach(asDeploymentItem -> { - logger.info("Current status {} of asDeploymentItem: {}", asDeploymentItem.getStatus(), - asDeploymentItem.getName()); - }); + asDeploymentItems.stream() + .forEach(asDeploymentItem -> logger.info("Current status {} of asDeploymentItem: {}", + asDeploymentItem.getStatus(), asDeploymentItem.getName())); } } @@ -396,7 +395,7 @@ public class InstantiateAsTask extends AbstractServiceTask { final Path dirPath = Paths.get(parentDir, dirname); final File dir = dirPath.toFile(); if (!dir.exists()) { - logger.debug("Creating directory to download helm chart file {}", dir.toString()); + logger.debug("Creating directory to download helm chart file {}", dir); dir.mkdir(); } return dir; diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateDeploymentItemTask.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateDeploymentItemTask.java index a2cf976..4017946 100644 --- a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateDeploymentItemTask.java +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/InstantiateDeploymentItemTask.java @@ -65,7 +65,6 @@ import org.springframework.stereotype.Component; * @author Waqas Ikram (waqas.ikram@est.tech) * */ - @Component public class InstantiateDeploymentItemTask extends AbstractServiceTask { @@ -200,9 +199,7 @@ public class InstantiateDeploymentItemTask extends AbstractServiceTask { execution.setVariable(KUBE_KINDS_PARAM_NAME, kubeKinds); final Map result = new HashMap<>(); - kubeKinds.forEach(kind -> { - result.put(kind, false); - }); + kubeKinds.forEach(kind -> result.put(kind, false)); execution.setVariable(KUBE_KINDS_RESULT_PARAM_NAME, result); } catch (final BpmnError bpmnError) { @@ -303,7 +300,7 @@ public class InstantiateDeploymentItemTask extends AbstractServiceTask { resources.addAll(kubernetesClient.getStatefulSetResources(apiClient, labelSelector)); break; default: - logger.warn("Unknown resource type {} setting {} skipping it", kind); + logger.warn("Unknown resource type {} found skipping it ...", kind); break; } } catch (final Exception exception) { @@ -373,7 +370,6 @@ public class InstantiateDeploymentItemTask extends AbstractServiceTask { } }); - logger.info("Finished executing createK8sResourcesInAai ..."); } @@ -386,20 +382,17 @@ public class InstantiateDeploymentItemTask extends AbstractServiceTask { (Map) execution.getVariable(KUBE_KINDS_RESULT_PARAM_NAME); if (kubeKindResult != null) { - kubeKindResult.entrySet().forEach(entry -> { - logger.info("Current status {} of resource type: {}", entry.getValue(), entry.getKey()); - }); + kubeKindResult.entrySet().forEach( + entry -> logger.info("Current status {} of resource type: {}", entry.getValue(), entry.getKey())); } - final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME); final List asDeploymentItems = databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId); if (asDeploymentItems != null) { - asDeploymentItems.stream().forEach(asDeploymentItem -> { - logger.info("Current status {} of asDeploymentItem: {}", asDeploymentItem.getStatus(), - asDeploymentItem.getName()); - }); + asDeploymentItems.stream() + .forEach(asDeploymentItem -> logger.info("Current status {} of asDeploymentItem: {}", + asDeploymentItem.getStatus(), asDeploymentItem.getName())); } } diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/TerminateAsTask.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/TerminateAsTask.java index d834c4d..4ffcb2d 100644 --- a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/TerminateAsTask.java +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/TerminateAsTask.java @@ -59,7 +59,7 @@ public class TerminateAsTask extends AbstractServiceTask { @Autowired protected TerminateAsTask(final DatabaseServiceProvider databaseServiceProvider, - final KubConfigProvider kubConfigProvider, final AaiServiceProvider aaiServiceProvider) { + final KubConfigProvider kubConfigProvider, final AaiServiceProvider aaiServiceProvider) { super(databaseServiceProvider); this.kubConfigProvider = kubConfigProvider; this.aaiServiceProvider = aaiServiceProvider; @@ -97,10 +97,9 @@ public class TerminateAsTask extends AbstractServiceTask { final List asDeploymentItems = databaseServiceProvider.getAsDeploymentItemByAsInstId(asInstId); if (asDeploymentItems != null) { - asDeploymentItems.stream().forEach(asDeploymentItem -> { - logger.info("Current status {} of terminating asDeploymentItem: {}", asDeploymentItem.getStatus(), - asDeploymentItem.getName()); - }); + asDeploymentItems.stream() + .forEach(asDeploymentItem -> logger.info("Current status {} of terminating asDeploymentItem: {}", + asDeploymentItem.getStatus(), asDeploymentItem.getName())); } } @@ -196,8 +195,7 @@ public class TerminateAsTask extends AbstractServiceTask { logger.debug("Executing updateGenericVnfStatustoDeActivated"); final String asInstId = (String) execution.getVariable(AS_INSTANCE_ID_PARAM_NAME); - final boolean result = aaiServiceProvider.updateGenericVnfStatus(asInstId, - OrchestrationStatusEnum.DEACTIVATED); + final boolean result = aaiServiceProvider.updateGenericVnfStatus(asInstId, OrchestrationStatusEnum.DEACTIVATED); if (!result) { abortOperation(execution, "Failed to update GenericVnf status to Deactivated as there" + "is no GenericVnf Found in AAI of ID: " + asInstId); diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/resources/DeleteAs.bpmn b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/resources/DeleteAs.bpmn new file mode 100644 index 0000000..4352eba --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/main/resources/DeleteAs.bpmn @@ -0,0 +1,251 @@ + + + + + Flow_09vhzc2 + + + Flow_0ld9hr8 + + + + Flow_09vhzc2 + Flow_1687i7q + + + + Flow_1687i7q + Flow_0qqc31o + + + Flow_172kwvj + Flow_0ld9hr8 + + + + + Flow_0ifwuxp + + + + Flow_1kk2y0h + + + Flow_0ifwuxp + Flow_1kk2y0h + + + + + + + Flow_053knln + + + + Flow_1gu1dnf + + + Flow_053knln + Flow_1gu1dnf + + + + + + Flow_0zhc29v + Flow_0rnc1yr + + + Flow_0qqc31o + Flow_0zhc29v + Flow_0tb94ui + + + Flow_0rnc1yr + Flow_0blv3cy + Flow_11ap3gp + + + Flow_0tb94ui + Flow_0blv3cy + + + + #{asInstanceExists} + + + + #{not nsInstanceExists} + + + #{not isInNotInstantiatedState} + + + + #{isInNotInstantiatedState} + + + Flow_1t4zqk5 + Flow_172kwvj + + + + Flow_11ap3gp + Flow_1t4zqk5 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/test/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTaskTest.java b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/test/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTaskTest.java new file mode 100644 index 0000000..e90e64b --- /dev/null +++ b/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-bpmn-flows/src/test/java/org/onap/so/cnfm/lcm/bpmn/flows/tasks/DeleteAsTaskTest.java @@ -0,0 +1,111 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.so.cnfm.lcm.bpmn.flows.tasks; + +import static com.github.tomakehurst.wiremock.client.WireMock.delete; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.ok; +import static com.github.tomakehurst.wiremock.client.WireMock.okJson; +import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.onap.aaiclient.client.aai.AAIVersion.V19; +import java.time.LocalDateTime; +import java.util.Optional; +import java.util.UUID; +import org.camunda.bpm.engine.history.HistoricProcessInstance; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.so.cnfm.lcm.bpmn.flows.BaseTest; +import org.onap.so.cnfm.lcm.bpmn.flows.service.JobExecutorService; +import org.onap.so.cnfm.lcm.database.beans.AsInst; +import org.onap.so.cnfm.lcm.database.beans.Job; +import org.onap.so.cnfm.lcm.database.beans.State; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * + * @author Waqas Ikram (waqas.ikram@est.tech) + * + */ +public class DeleteAsTaskTest extends BaseTest { + + @Autowired + private JobExecutorService objUnderTest; + + @Before + public void before() { + wireMockServer.resetAll(); + } + + @After + public void after() { + wireMockServer.resetAll(); + } + + @Test + public void testRunDeleteNsJob_SuccessfulCase() throws InterruptedException { + final String asInstanceId = UUID.randomUUID().toString(); + addDummyAsToDatabase(asInstanceId); + mockAaiEndpoints(asInstanceId); + + objUnderTest.runDeleteAsJob(asInstanceId); + + final Optional optional = getJobByResourceId(asInstanceId); + assertTrue(optional.isPresent()); + final Job job = optional.get(); + + assertTrue(waitForProcessInstanceToFinish(job.getProcessInstanceId())); + + final HistoricProcessInstance historicProcessInstance = getHistoricProcessInstance(job.getProcessInstanceId()); + assertNotNull(historicProcessInstance); + assertEquals(HistoricProcessInstance.STATE_COMPLETED, historicProcessInstance.getState()); + + final Optional optionalAsInst = databaseServiceProvider.getAsInst(asInstanceId); + assertTrue(optionalAsInst.isEmpty()); + + } + + private void mockAaiEndpoints(final String asInstanceId) { + final String modelEndpoint = "/aai/" + V19 + "/network/generic-vnfs/generic-vnf/" + asInstanceId; + final String resourceVersion = UUID.randomUUID().toString(); + + final String body = + "{\"resource-version\": \"" + resourceVersion + "\",\n\"orchestration-status\": \"Assigned\"}"; + wireMockServer.stubFor(get(urlMatching(modelEndpoint)).willReturn(ok()).willReturn(okJson(body))); + wireMockServer.stubFor( + delete(urlMatching(modelEndpoint + "\\?resource-version=" + resourceVersion)).willReturn(ok())); + } + + private void addDummyAsToDatabase(final String asInstanceId) { + final String asdId = UUID.randomUUID().toString(); + + final AsInst asInst = new AsInst().asInstId(asInstanceId).name("asName").asdId(asdId) + .asdInvariantId(asInstanceId).status(State.NOT_INSTANTIATED).statusUpdatedTime(LocalDateTime.now()) + .asApplicationName("asApplicationName").asApplicationVersion("asApplicationVersion") + .asProvider("asProvider").serviceInstanceId(SERVICE_INSTANCE_ID) + .serviceInstanceName("serviceInstanceName").cloudOwner("cloudOwner").cloudRegion("cloudRegion") + .tenantId("tenantId"); + databaseServiceProvider.saveAsInst(asInst); + } + +} -- cgit 1.2.3-korg