From 35481027b3fdf251a3b520ab5b1ae89c7d2d0e34 Mon Sep 17 00:00:00 2001 From: Kavitha P Date: Wed, 25 Aug 2021 16:12:18 +0530 Subject: CCSDK-3434 CBA workflow status store Change-Id: Iaeac6fa534c569bbc152e6c8a78c2dd23b6c4b1a Signed-off-by: Kavitha P Issue-ID: CCSDK-3434 --- ms/blueprintsprocessor/modules/inbounds/pom.xml | 1 + .../modules/inbounds/selfservice-api/pom.xml | 6 + .../selfservice/api/ExecutionServiceHandler.kt | 13 +- .../api/ExecutionServiceControllerTest.kt | 4 +- .../selfservice/api/ExecutionServiceHandlerTest.kt | 63 ++++- .../selfservice/api/TestDatabaseConfiguration.kt | 4 +- .../src/test/resources/application-test.properties | 4 + .../modules/inbounds/workflow-api/pom.xml | 56 +++++ .../workflow/api/ErrorHandling.kt | 35 +++ .../workflow/api/WorkflowController.kt | 109 +++++++++ .../workflow/api/WorkflowExceptionHandler.kt | 29 +++ .../workflow/api/ErrorCatalogTestConfiguration.kt | 28 +++ .../workflow/api/TestDatabaseConfiguration.kt | 62 +++++ .../workflow/api/WorkflowControllerTest.kt | 268 +++++++++++++++++++++ .../src/test/resources/application-test.properties | 39 +++ 15 files changed, 715 insertions(+), 6 deletions(-) create mode 100644 ms/blueprintsprocessor/modules/inbounds/workflow-api/pom.xml create mode 100644 ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/ErrorHandling.kt create mode 100644 ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowController.kt create mode 100644 ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowExceptionHandler.kt create mode 100644 ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/ErrorCatalogTestConfiguration.kt create mode 100644 ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/TestDatabaseConfiguration.kt create mode 100644 ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowControllerTest.kt create mode 100644 ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/resources/application-test.properties (limited to 'ms/blueprintsprocessor/modules') diff --git a/ms/blueprintsprocessor/modules/inbounds/pom.xml b/ms/blueprintsprocessor/modules/inbounds/pom.xml index 2b474bb3f..b29dc36b5 100644 --- a/ms/blueprintsprocessor/modules/inbounds/pom.xml +++ b/ms/blueprintsprocessor/modules/inbounds/pom.xml @@ -37,6 +37,7 @@ selfservice-api health-api health-api-common + workflow-api diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/pom.xml b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/pom.xml index 94cc382d7..d7d0a0ffb 100755 --- a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/pom.xml +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/pom.xml @@ -52,6 +52,12 @@ org.onap.ccsdk.cds.blueprintsprocessor.modules message-lib + + + org.onap.ccsdk.cds.blueprintsprocessor.functions + blueprint-audit-status + 1.2.0-SNAPSHOT + diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandler.kt b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandler.kt index e604987a7..c2c7a60e9 100644 --- a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandler.kt +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandler.kt @@ -33,6 +33,7 @@ import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.SelfServiceMetricC import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.SelfServiceMetricConstants.TIMER_PROCESS import org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api.utils.cbaMetricTags import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractServiceFunction +import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.StoreAuditService import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration @@ -50,6 +51,7 @@ class ExecutionServiceHandler( private val bluePrintWorkflowExecutionService: BluePrintWorkflowExecutionService, private val publishAuditService: PublishAuditService, + private val storeAuditService: StoreAuditService, private val meterRegistry: MeterRegistry ) { @@ -102,6 +104,10 @@ class ExecutionServiceHandler( // Audit input publishAuditService.publishExecutionInput(executionServiceInput) + // store audit input details + val auditStoreId: Long = storeAuditService.storeExecutionInput(executionServiceInput) + log.info("StoreAuditService ID $auditStoreId") + val sample = Timer.start() try { /** Check Blueprint is needed for this request */ @@ -130,9 +136,14 @@ class ExecutionServiceHandler( // Update process metrics sample.stop(meterRegistry.timer(TIMER_PROCESS, cbaMetricTags(executionServiceInput))) meterRegistry.counter(COUNTER_PROCESS, cbaMetricTags(executionServiceOutput)).increment() - // Audit output publishAuditService.publishExecutionOutput(executionServiceInput.correlationUUID, executionServiceOutput) + + // store audit input details + storeAuditService.storeExecutionOutput( + auditStoreId, executionServiceInput.correlationUUID, + executionServiceOutput + ) return executionServiceOutput } diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceControllerTest.kt b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceControllerTest.kt index d7d7aaa2a..ebaa8bd7e 100644 --- a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceControllerTest.kt +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceControllerTest.kt @@ -23,6 +23,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.DatabaseStoreAuditService import org.onap.ccsdk.cds.controllerblueprints.core.compress import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService @@ -46,7 +47,8 @@ import kotlin.test.assertTrue @ContextConfiguration( classes = [ ExecutionServiceHandler::class, BluePrintCoreConfiguration::class, - BluePrintCatalogService::class, SelfServiceApiTestConfiguration::class, + BluePrintCatalogService::class, DatabaseStoreAuditService::class, + SelfServiceApiTestConfiguration::class, ErrorCatalogTestConfiguration::class, SimpleMeterRegistry::class ] ) diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandlerTest.kt b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandlerTest.kt index 0a89c5782..af3cbd89a 100644 --- a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandlerTest.kt +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/ExecutionServiceHandlerTest.kt @@ -16,11 +16,14 @@ package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.node.ObjectNode import io.micrometer.core.instrument.MeterRegistry import io.mockk.coVerify import io.mockk.Runs import io.mockk.coEvery import io.mockk.coVerify +import io.mockk.every import io.mockk.just import io.mockk.mockk import kotlinx.coroutines.runBlocking @@ -30,9 +33,13 @@ import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceOutput +import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.DatabaseStoreAuditService +import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.db.BlueprintAuditStatusRepository +import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.db.BlueprintWorkflowAuditStatus import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractServiceFunction import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.mock.mockito.MockBean import org.springframework.context.ApplicationContext @@ -60,6 +67,11 @@ class ExecutionServiceHandlerTest { @Autowired lateinit var applicationContext: ApplicationContext + private val blueprintAuditStatusRepository = + mockk() + + private val testDatabaseStoreAuditService = DatabaseStoreAuditService(blueprintAuditStatusRepository) + @Before fun init() { BluePrintDependencyService.inject(applicationContext) @@ -80,7 +92,7 @@ class ExecutionServiceHandlerTest { } } runBlocking { - val executionServiceHandler = ExecutionServiceHandler(mockk(), mockk(), mockk(), mockk(), mockk(relaxed = true)) + val executionServiceHandler = ExecutionServiceHandler(mockk(), mockk(), mockk(), mockk(), testDatabaseStoreAuditService, mockk(relaxed = true)) val isServiceFunction = executionServiceHandler.checkServiceFunction(executionServiceInput) assertTrue(isServiceFunction, "failed to checkServiceFunction") val executionServiceOutput = executionServiceHandler.executeServiceFunction(executionServiceInput) @@ -90,6 +102,10 @@ class ExecutionServiceHandlerTest { @Test fun testPublishAuditFunction() { + + val jsonContent = JacksonUtils.getClassPathFileContent("execution-input/sample-payload.json") + val json: ObjectNode = ObjectMapper().readTree(jsonContent) as ObjectNode + val executionServiceInput = ExecutionServiceInput().apply { commonHeader = CommonHeader().apply { requestId = "1234" @@ -100,20 +116,29 @@ class ExecutionServiceHandlerTest { blueprintName = "default" blueprintVersion = "1.0.0" actionName = "mock-service-action" + mode = "async" } } - + executionServiceInput.payload = json val publishAuditService = mockk(relaxed = true) + val wfAudit = createWorkflowAuditStatusRecord(1000) + val executionServiceHandler = ExecutionServiceHandler( mockk(), mockk(), mockk(), publishAuditService, + testDatabaseStoreAuditService, mockk(relaxed = true) ) - + var testOutput: Long = 1000 coEvery { publishAuditService.publishExecutionInput(ExecutionServiceInput()) } just Runs + runBlocking { + every { blueprintAuditStatusRepository.findById(testOutput) } returns wfAudit + every { blueprintAuditStatusRepository.saveAndFlush(any()) } returns wfAudit + } + var executionServiceOutput: ExecutionServiceOutput? = null runBlocking { executionServiceOutput = executionServiceHandler.doProcess(executionServiceInput) @@ -122,8 +147,40 @@ class ExecutionServiceHandlerTest { coVerify { publishAuditService.publishExecutionInput(executionServiceInput) publishAuditService.publishExecutionOutput(executionServiceInput.correlationUUID, executionServiceOutput!!) + testOutput = testDatabaseStoreAuditService.storeExecutionInput(executionServiceInput) } } + + private fun createWorkflowAuditStatusRecord( + id: Long + ): BlueprintWorkflowAuditStatus { + + var blueprintWorkflowAuditStatus: BlueprintWorkflowAuditStatus = + BlueprintWorkflowAuditStatus() + blueprintWorkflowAuditStatus.id = id + blueprintWorkflowAuditStatus.originatorId = "SDNC_DG" + blueprintWorkflowAuditStatus.requestMode = "sync" + blueprintWorkflowAuditStatus.requestId = "ab543-3asd4" + blueprintWorkflowAuditStatus.subRequestId = "81c9-4910" + blueprintWorkflowAuditStatus.status = "In progress" + blueprintWorkflowAuditStatus.blueprintName = "multi-steps" + blueprintWorkflowAuditStatus.blueprintVersion = "1.0.0" + blueprintWorkflowAuditStatus.workflowName = "multi-steps-workflow" + blueprintWorkflowAuditStatus.updatedBy = "CBA" + blueprintWorkflowAuditStatus.requestMode = "sync" + blueprintWorkflowAuditStatus.workflowTaskContent = "{\n" + + " \"multi-steps-workflow-request\": {\n" + + " \"multi-steps-workflow-properties\": {\n" + + " \"prop1\": \"testing\",\n" + + " \"prop2\": \"testing description\",\n" + + " \"prop3\": \"user name \",\n" + + " \"prop4\" : \"test project\"\n" + + " }\n" + + " }\n" + + " }" + blueprintWorkflowAuditStatus.workflowResponseContent = " " + return blueprintWorkflowAuditStatus + } } @Service("mock-service-action") diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/TestDatabaseConfiguration.kt b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/TestDatabaseConfiguration.kt index bca05f68b..5a2c5e5db 100644 --- a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/TestDatabaseConfiguration.kt +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/selfservice/api/TestDatabaseConfiguration.kt @@ -33,7 +33,8 @@ import javax.sql.DataSource @EnableJpaRepositories( basePackages = [ "org.onap.ccsdk.cds.blueprintsprocessor.db.primary", - "org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution" + "org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution", + "org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit" ], entityManagerFactoryRef = "primaryEntityManager", transactionManagerRef = "primaryTransactionManager" @@ -46,6 +47,7 @@ open class TestDatabaseConfiguration(primaryDataSourceProperties: PrimaryDataSou open fun primaryEntityManager(): LocalContainerEntityManagerFactoryBean { return primaryEntityManager( "org.onap.ccsdk.cds.blueprintsprocessor.db.primary", + "org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit", "org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution" ) } diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/resources/application-test.properties b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/resources/application-test.properties index 77b61a421..8dfaf7896 100644 --- a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/resources/application-test.properties +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/resources/application-test.properties @@ -68,3 +68,7 @@ blueprintsprocessor.messageproducer.self-service-api.audit.response.type=kafka-b blueprintsprocessor.messageproducer.self-service-api.audit.response.bootstrapServers=127.0.0.1:9092 blueprintsprocessor.messageproducer.self-service-api.audit.response.clientId=audit-response-producer-client-id blueprintsprocessor.messageproducer.self-service-api.audit.response.topic=audit-response-producer.t + +#Workflow Audit store enable +blueprintsprocessor.workflow.self-service-api.audit.storeEnable=true + diff --git a/ms/blueprintsprocessor/modules/inbounds/workflow-api/pom.xml b/ms/blueprintsprocessor/modules/inbounds/workflow-api/pom.xml new file mode 100644 index 000000000..7e5ebecb8 --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/workflow-api/pom.xml @@ -0,0 +1,56 @@ + + + + + 4.0.0 + + + org.onap.ccsdk.cds.blueprintsprocessor + modules-inbounds + 1.2.0-SNAPSHOT + + + org.onap.ccsdk.cds.blueprintsprocessor.modules + workflow-api + jar + + MS Blueprints Processor Modules - Inbounds - Workflow API + + + + org.springframework.security + spring-security-core + + + org.onap.ccsdk.cds.blueprintsprocessor.modules + blueprint-core + + + org.onap.ccsdk.cds.error.catalog + error-catalog-services + + + org.onap.ccsdk.cds.blueprintsprocessor.functions + blueprint-audit-status + ${project.version} + + + org.onap.ccsdk.cds.blueprintsprocessor.modules + db-lib + + + diff --git a/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/ErrorHandling.kt b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/ErrorHandling.kt new file mode 100644 index 000000000..5e4289e7b --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/ErrorHandling.kt @@ -0,0 +1,35 @@ +/* + * Copyright © 2021 Aarna Networks, Inc. + * All rights reserved. + * 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. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.workflow.api + +object WorkflowApiDomains { + + // Workflow Api Domains Constants + const val WORKFLOW_API = "org.onap.ccsdk.cds.blueprintsprocessor.workflow.api" +} + +object WorkflowApiHttpErrorCodes { + init { + // Register HttpErrorCodes + } +} + +object WorkflowGrpcErrorCodes { + init { + // Register GrpcErrorCodes + } +} diff --git a/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowController.kt b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowController.kt new file mode 100644 index 000000000..e131bf378 --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowController.kt @@ -0,0 +1,109 @@ +/* + * Copyright © 2021 Aarna Networks, Inc. + * All rights reserved. + * 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. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.workflow.api + +import com.fasterxml.jackson.databind.JsonNode +import io.swagger.annotations.Api +import io.swagger.annotations.ApiOperation +import io.swagger.annotations.ApiParam +import kotlinx.coroutines.runBlocking +import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.db.BlueprintWorkflowAuditStatus +import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.StoreAuditService +import org.onap.ccsdk.cds.controllerblueprints.core.httpProcessorException +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity +import org.springframework.security.access.prepost.PreAuthorize +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RequestMethod +import org.springframework.web.bind.annotation.ResponseBody +import org.springframework.web.bind.annotation.RestController + +/** + * Rest API which handles retrieving the workflow store records + */ +@RestController +@RequestMapping("/api/v1/workflow") +@Api( + value = "/api/v1/workflow", + description = "Interaction with blueprint workflow status resources" +) +class WorkflowController { + private val log = + LoggerFactory.getLogger(WorkflowController::class.toString()) + + @Autowired + lateinit var databaseStoreAuditService: StoreAuditService + + private val JSON_MIME_TYPE = "application/json" + + /** + * workflow healthcheck api + */ + @RequestMapping( + path = ["/health-check"], + method = [RequestMethod.GET], + produces = [MediaType.APPLICATION_JSON_VALUE] + ) + @ResponseBody + @ApiOperation(value = "Health Check", hidden = true) + fun workflowControllerHealthCheck(): JsonNode = runBlocking { + JacksonUtils.getJsonNode("Success") + } + + /** + * API to retreive records based on request and subrequest ID * + */ + @GetMapping( + "/audit-status/{requestId}/{subRequestId}", + produces = [MediaType.APPLICATION_JSON_VALUE] + ) + @ApiOperation( + value = "Get all workflow status records using request and Subrequest", + notes = "Retrieve all stored workflow audit records based on " + + "requestId and SubrequestId", + response = BlueprintWorkflowAuditStatus::class, + responseContainer = "List", + produces = MediaType.APPLICATION_JSON_VALUE + ) + @ResponseBody + @PreAuthorize("hasRole('USER')") + suspend fun getWorkFlowByRequestAndSubRequest( + @ApiParam(value = "request ID", required = true) + @PathVariable(value = "requestId") requestId: String, + @ApiParam(value = "sub request ID", required = true) + @PathVariable(value = "subRequestId") subRequestId: String + ): ResponseEntity> = runBlocking { + var requestIdResults: List + if (requestId.isNotEmpty()) { + requestIdResults = databaseStoreAuditService + .getWorkflowStatusByRequestIdAndSubRequestId(requestId, subRequestId) + } else { + throw httpProcessorException( + ErrorCatalogCodes.REQUEST_NOT_FOUND, WorkflowApiDomains.WORKFLOW_API, + "Missing param." + ) + } + val expectedMediaType: MediaType = MediaType.valueOf(JSON_MIME_TYPE) + ResponseEntity.ok().contentType(expectedMediaType).body(requestIdResults) + } +} diff --git a/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowExceptionHandler.kt b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowExceptionHandler.kt new file mode 100644 index 000000000..18dfe085b --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowExceptionHandler.kt @@ -0,0 +1,29 @@ +/* + * Copyright © 2021 Aarna Networks, Inc. + * All rights reserved. + * 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. + */ +package org.onap.ccsdk.cds.blueprintsprocessor.workflow.api + +import org.onap.ccsdk.cds.error.catalog.services.ErrorCatalogExceptionHandler +import org.onap.ccsdk.cds.error.catalog.services.ErrorCatalogService +import org.springframework.web.bind.annotation.RestControllerAdvice + +/** + * Handle exceptions in Workflow API and provide relevant HTTP status codes and + * messages + * + */ +@RestControllerAdvice("org.onap.ccsdk.cds.blueprintsprocessor.workflow.api") +open class WorkflowExceptionHandler(private val errorCatalogService: ErrorCatalogService) : + ErrorCatalogExceptionHandler(errorCatalogService) diff --git a/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/ErrorCatalogTestConfiguration.kt b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/ErrorCatalogTestConfiguration.kt new file mode 100644 index 000000000..a05f569c0 --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/ErrorCatalogTestConfiguration.kt @@ -0,0 +1,28 @@ +/* + * Copyright © 2021 Aarna Networks, Inc. + * All rights reserved. + * 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. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.workflow.api + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration +import org.springframework.context.annotation.ComponentScan +import org.springframework.context.annotation.Configuration + +@Configuration +@ComponentScan( + basePackages = ["org.onap.ccsdk.cds.error.catalog"] +) +@EnableAutoConfiguration +open class ErrorCatalogTestConfiguration diff --git a/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/TestDatabaseConfiguration.kt b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/TestDatabaseConfiguration.kt new file mode 100644 index 000000000..7ca90e9ec --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/TestDatabaseConfiguration.kt @@ -0,0 +1,62 @@ +/* + * Copyright © 2021 Aarna Networks, Inc. + * All rights reserved. + * 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. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.workflow.api + +import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.db.PrimaryDataSourceProperties +import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.PrimaryDatabaseConfiguration +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.Import +import org.springframework.data.jpa.repository.config.EnableJpaAuditing +import org.springframework.data.jpa.repository.config.EnableJpaRepositories +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean +import org.springframework.transaction.PlatformTransactionManager +import javax.sql.DataSource + +@Configuration +@Import(BluePrintDBLibConfiguration::class) +@EnableJpaRepositories( + basePackages = [ + "org.onap.ccsdk.cds.blueprintsprocessor.db.primary", + "org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit" + ], + entityManagerFactoryRef = "primaryEntityManager", + transactionManagerRef = "primaryTransactionManager" +) +@EnableJpaAuditing +open class TestDatabaseConfiguration(primaryDataSourceProperties: PrimaryDataSourceProperties) : + PrimaryDatabaseConfiguration(primaryDataSourceProperties) { + + @Bean("primaryEntityManager") + open fun primaryEntityManager(): LocalContainerEntityManagerFactoryBean { + return primaryEntityManager( + "org.onap.ccsdk.cds.blueprintsprocessor.db.primary", + "org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.db" + ) + } + + @Bean("primaryDataSource") + override fun primaryDataSource(): DataSource { + return super.primaryDataSource() + } + + @Bean("primaryTransactionManager") + override fun primaryTransactionManager(): PlatformTransactionManager { + return super.primaryTransactionManager() + } +} diff --git a/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowControllerTest.kt b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowControllerTest.kt new file mode 100644 index 000000000..bf6960f02 --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/workflow/api/WorkflowControllerTest.kt @@ -0,0 +1,268 @@ +/* + * Copyright © 2021 Aarna Networks, Inc. + * All rights reserved. + * 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. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.workflow.api + +import kotlinx.coroutines.runBlocking +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.DatabaseStoreAuditService +import org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit.db.BlueprintWorkflowAuditStatus +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest +import org.springframework.context.annotation.ComponentScan +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource +import org.springframework.test.context.junit4.SpringRunner +import org.springframework.test.web.reactive.server.WebTestClient +import java.text.SimpleDateFormat +import java.util.List + +@RunWith(SpringRunner::class) +@WebFluxTest +@ContextConfiguration( + classes = [ + TestDatabaseConfiguration::class, ErrorCatalogTestConfiguration::class, + WorkflowController::class, DatabaseStoreAuditService::class + ] +) +@ComponentScan( + basePackages = [ + "org.onap.ccsdk.cds.controllerblueprints.core.service", + "org.onap.ccsdk.cds.blueprintsprocessor.functions.workflow.audit", + "org.onap.ccsdk.cds.blueprintsprocessor.workflow.api" + ] +) +@TestPropertySource(locations = ["classpath:application-test.properties"]) +class WorkflowControllerTest { + + @Autowired + lateinit var databaseStoreAuditService: DatabaseStoreAuditService + @Autowired + lateinit var testWebClient: WebTestClient + + @Test + fun testWorkflowControllerHealthCheck() { + runBlocking { + testWebClient.get().uri("/api/v1/workflow/health-check") + .exchange() + .expectStatus().isOk + .expectBody() + .equals("Success") + } + } + + @Test + fun testGetWorkFlowByRequestAndSubRequest() { + + val wfAudit1 = createWorkflowAuditStatusRecord(1000) + val wfAudit2 = createWorkflowAuditStatusRecord(1001) + runBlocking { + + storeToDB(wfAudit1) + storeToDB(wfAudit2) + val testRequestId: String = "ab543-3asd4" + val testSubRequestId: String = "81c9-4910" + testWebClient + .get() + .uri("/api/v1/workflow/audit-status/$testRequestId/$testSubRequestId") + .exchange() + .expectStatus().isOk + .expectBody() + .consumeWith { + val json = String(it.responseBody!!) + val typeFactory = JacksonUtils.objectMapper.typeFactory + val list: List = JacksonUtils.objectMapper.readValue( + json, + typeFactory.constructCollectionType(List::class.java, BlueprintWorkflowAuditStatus::class.java) + ) + Assert.assertEquals(2, list.size) + assertEqual(list[0]) + assertEqual(list[1]) + } + } + } + + @Test + fun testEmptyRequestAndSubRequest() { + + val wfAudit1 = createWorkflowAuditStatusRecordSetTwo(1002) + val wfAudit2 = createWorkflowAuditStatusRecordSetTwo(1003) + runBlocking { + + storeToDB(wfAudit1) + storeToDB(wfAudit2) + val testRequestId: String = "" + val testSubRequestId: String = "" + testWebClient + .get() + .uri("/api/v1/workflow/audit-status/$testRequestId/$testSubRequestId") + .exchange() + .expectStatus().is4xxClientError + .expectBody() + } + } + + @Test + @Throws(BluePrintException::class) + fun testErrorRequestAndSubRequest() { + + val wfAudit1 = createWorkflowAuditStatusRecordSetTwo(1002) + val wfAudit2 = createWorkflowAuditStatusRecordSetTwo(1003) + runBlocking { + + storeToDB(wfAudit1) + storeToDB(wfAudit2) + val testRequestId: String = "" + val testSubRequestId: String = "" + testWebClient + .get() + .uri("/api/v1/workflow/audit-status/$testRequestId/$testSubRequestId") + .exchange() + .expectStatus().is4xxClientError + .expectBody() + .consumeWith { + Assert.assertTrue( + "Cause: request Id and requesy sub Id is empty. \n " + + "Action : Please verify your request.", + it.status.is4xxClientError + ) + } + } + } + + private fun assertEqual(blueprintWorkflowAuditStatus: BlueprintWorkflowAuditStatus) { + Assert.assertEquals(blueprintWorkflowAuditStatus.status, "In progress") + Assert.assertEquals(blueprintWorkflowAuditStatus.requestId, "ab543-3asd4") + Assert.assertEquals(blueprintWorkflowAuditStatus.subRequestId, "81c9-4910") + Assert.assertEquals(blueprintWorkflowAuditStatus.blueprintName, "multi-steps") + Assert.assertEquals(blueprintWorkflowAuditStatus.blueprintVersion, "1.0.0") + Assert.assertEquals(blueprintWorkflowAuditStatus.requestMode, "sync") + Assert.assertEquals(blueprintWorkflowAuditStatus.workflowName, "multi-steps-workflow") + Assert.assertEquals(blueprintWorkflowAuditStatus.originatorId, "SDNC_DG") + } + + private fun createWorkflowAuditStatusRecord( + id: Long + ): BlueprintWorkflowAuditStatus { + + var blueprintWorkflowAuditStatus: BlueprintWorkflowAuditStatus = + BlueprintWorkflowAuditStatus() + blueprintWorkflowAuditStatus.id = id + blueprintWorkflowAuditStatus.originatorId = "SDNC_DG" + blueprintWorkflowAuditStatus.requestMode = "sync" + blueprintWorkflowAuditStatus.requestId = "ab543-3asd4" + blueprintWorkflowAuditStatus.subRequestId = "81c9-4910" + blueprintWorkflowAuditStatus.status = "In progress" + blueprintWorkflowAuditStatus.blueprintName = "multi-steps" + blueprintWorkflowAuditStatus.blueprintVersion = "1.0.0" + blueprintWorkflowAuditStatus.workflowName = "multi-steps-workflow" + blueprintWorkflowAuditStatus.updatedBy = "CBA" + blueprintWorkflowAuditStatus.requestMode = "sync" + blueprintWorkflowAuditStatus.workflowTaskContent = "{\n" + + " \"multi-steps-workflow-request\": {\n" + + " \"multi-steps-workflow-properties\": {\n" + + " \"prop1\": \"testing\",\n" + + " \"prop2\": \"testing description\",\n" + + " \"prop3\": \"user name \",\n" + + " \"prop4\" : \"test project\"\n" + + " }\n" + + " }\n" + + " }" + blueprintWorkflowAuditStatus.workflowResponseContent = " " + return blueprintWorkflowAuditStatus + } + + private fun createWorkflowAuditStatusRecordSetTwo( + id: Long + ): BlueprintWorkflowAuditStatus { + + var blueprintWorkflowAuditStatus: BlueprintWorkflowAuditStatus = + BlueprintWorkflowAuditStatus() + blueprintWorkflowAuditStatus.id = id + blueprintWorkflowAuditStatus.originatorId = "SDNC_DG" + blueprintWorkflowAuditStatus.requestMode = "sync" + blueprintWorkflowAuditStatus.requestId = "ab543-3asd5" + blueprintWorkflowAuditStatus.subRequestId = "81c9-4911" + blueprintWorkflowAuditStatus.status = "In progress" + blueprintWorkflowAuditStatus.blueprintName = "multi-steps" + blueprintWorkflowAuditStatus.blueprintVersion = "1.0.0" + blueprintWorkflowAuditStatus.workflowName = "multi-steps-workflow" + blueprintWorkflowAuditStatus.updatedBy = "CBA" + blueprintWorkflowAuditStatus.requestMode = "sync" + blueprintWorkflowAuditStatus.workflowTaskContent = "{\n" + + " \"multi-steps-workflow-request\": {\n" + + " \"multi-steps-workflow-properties\": {\n" + + " \"prop1\": \"testing\",\n" + + " \"prop2\": \"testing description\",\n" + + " \"prop3\": \"user name \",\n" + + " \"prop4\" : \"test project\"\n" + + " }\n" + + " }\n" + + " }" + blueprintWorkflowAuditStatus.workflowResponseContent = " " + return blueprintWorkflowAuditStatus + } + + private suspend fun store( + id: Long + ) { + val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") + val dateString = "2021-08-08T18:25:43.511Z" + val dateForTest = formatter.parse(dateString) + databaseStoreAuditService.write( + id, "SDNC_DG", "ab543-3asd4", + "81c9-4910", + "multi-steps-workflow", "multi-steps", "1.0.0", + "{\n" + + " \"multi-steps-workflow-request\": {\n" + + " \"multi-steps-workflow-properties\": {\n" + + " \"prop1\": \"testing\",\n" + + " \"prop2\": \"testing description\",\n" + + " \"prop3\": \"user name \",\n" + + " \"prop4\" : \"test project\"\n" + + " }\n" + + " }\n" + + " }", + "In progress", dateForTest, dateForTest, dateForTest, "CBA", "sync", " " + ) + } + + private suspend fun storeToDB( + blueprintWorkflowAuditStatus: BlueprintWorkflowAuditStatus + ) { + databaseStoreAuditService.write( + blueprintWorkflowAuditStatus.id, + blueprintWorkflowAuditStatus.originatorId, + blueprintWorkflowAuditStatus.requestId, + blueprintWorkflowAuditStatus.subRequestId, + blueprintWorkflowAuditStatus.workflowName, + blueprintWorkflowAuditStatus.blueprintName, + blueprintWorkflowAuditStatus.blueprintVersion, + blueprintWorkflowAuditStatus.workflowTaskContent, + blueprintWorkflowAuditStatus.status, + blueprintWorkflowAuditStatus.startDate, + blueprintWorkflowAuditStatus.endDate, + blueprintWorkflowAuditStatus.updatedDate, + blueprintWorkflowAuditStatus.updatedBy, + blueprintWorkflowAuditStatus.requestMode, + blueprintWorkflowAuditStatus.workflowResponseContent + ) + } +} diff --git a/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/resources/application-test.properties b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/resources/application-test.properties new file mode 100644 index 000000000..b970c53ef --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/workflow-api/src/test/resources/application-test.properties @@ -0,0 +1,39 @@ +# +# Copyright © 2021 Aarna Networks, Inc. +# All rights reserved. +# 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. +# +blueprintsprocessor.db.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1 +blueprintsprocessor.db.username=sa +blueprintsprocessor.db.password= +blueprintsprocessor.db.driverClassName=org.h2.Driver +blueprintsprocessor.db.hibernateHbm2ddlAuto=create-drop +blueprintsprocessor.db.hibernateDDLAuto=update +blueprintsprocessor.db.hibernateNamingStrategy=org.hibernate.cfg.ImprovedNamingStrategy +blueprintsprocessor.db.hibernateDialect=org.hibernate.dialect.H2Dialect +# Controller Blueprints Core Configuration +blueprintsprocessor.blueprintDeployPath=./target/blueprints/deploy +blueprintsprocessor.blueprintWorkingPath=./target/blueprints/work +blueprintsprocessor.blueprintArchivePath=./target/blueprints/archive + +# Error Managements +error.catalog.applicationId=cds +error.catalog.type=properties +error.catalog.errorDefinitionDir=./../../../application/src/test/resources/ + +# Python executor +blueprints.processor.functions.python.executor.executionPath=./../../../../components/scripts/python/ccsdk_blueprints +blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_blueprints + +#Workflow Audit store enable +blueprintsprocessor.workflow.self-service-api.audit.storeEnable=true -- cgit 1.2.3-korg