From 396b49a03e418ce7f68587c3e4444ed262325e0a Mon Sep 17 00:00:00 2001 From: Alexis de Talhouët Date: Thu, 20 Jun 2019 22:12:08 -0400 Subject: Store resolution and expose with REST API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ie1fe9c02542ccd5fbfa18f1e2d6ddb633f55c214 Issue-ID: CCSDK-1423 Signed-off-by: Alexis de Talhouët --- .../resource/api/ResourceResolutionController.java | 48 ---- .../ResolutionResultsServiceExceptionHandler.kt | 14 +- .../resolutionresults/api/ResourceController.kt | 84 +++++++ .../resolutionresults/api/ResourceException.kt | 21 ++ .../api/ResourceControllerTest.kt | 251 +++++++++++++++++++++ 5 files changed, 366 insertions(+), 52 deletions(-) delete mode 100644 ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/java/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceResolutionController.java create mode 100644 ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceController.kt create mode 100644 ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceException.kt create mode 100644 ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceControllerTest.kt (limited to 'ms/blueprintsprocessor/modules') diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/java/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceResolutionController.java b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/java/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceResolutionController.java deleted file mode 100644 index 3e94d79ee..000000000 --- a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/java/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceResolutionController.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * - * 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.resource.api; - -import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionService; -import org.springframework.http.MediaType; -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; -import reactor.core.publisher.Mono; - -/** - * ResourceResolutionController - * - * @author Brinda Santh Date : 8/13/2018 - */ - -@RestController -@RequestMapping("/api/v1/resource") -public class ResourceResolutionController { - - private ResourceResolutionService resourceResolutionService; - - public ResourceResolutionController(ResourceResolutionService resourceResolutionService) { - this.resourceResolutionService = resourceResolutionService; - } - - @RequestMapping(path = "/ping", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) - public @ResponseBody - Mono ping() { - return Mono.just("Success"); - } -} diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceExceptionHandler.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceExceptionHandler.kt index 69641c628..7f8f7da79 100644 --- a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceExceptionHandler.kt +++ b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResolutionResultsServiceExceptionHandler.kt @@ -48,7 +48,7 @@ open class ResolutionResultsServiceExceptionHandler { @ExceptionHandler fun ResolutionResultsServiceExceptionHandler(e: BluePrintProcessorException): ResponseEntity { - log.error(e.message) + log.error(e.message, e) val errorCode = ErrorCode.BLUEPRINT_PATH_MISSING val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg) return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)) @@ -56,7 +56,7 @@ open class ResolutionResultsServiceExceptionHandler { @ExceptionHandler fun ResolutionResultsServiceExceptionHandler(e: ServerWebInputException): ResponseEntity { - log.error(e.message) + log.error(e.message, e) val errorCode = ErrorCode.INVALID_REQUEST_FORMAT val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg) return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)) @@ -64,7 +64,7 @@ open class ResolutionResultsServiceExceptionHandler { @ExceptionHandler fun ResolutionResultsServiceExceptionHandler(e: EmptyResultDataAccessException): ResponseEntity { - log.error(e.message) + log.error(e.message, e) var errorCode = ErrorCode.RESOURCE_NOT_FOUND val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg) return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)) @@ -72,7 +72,7 @@ open class ResolutionResultsServiceExceptionHandler { @ExceptionHandler fun ResolutionResultsServiceExceptionHandler(e: JpaObjectRetrievalFailureException): ResponseEntity { - log.error(e.message) + log.error(e.message, e) var errorCode = ErrorCode.RESOURCE_NOT_FOUND val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg) @@ -86,6 +86,12 @@ open class ResolutionResultsServiceExceptionHandler { val errorMessage = ErrorMessage(errorCode.message(e.message!!), errorCode.value, debugMsg) return ResponseEntity(errorMessage, HttpStatus.resolve(errorCode.httpCode)) } + + @ExceptionHandler + fun ResolutionResultsServiceExceptionHandler(e: ResourceException): ResponseEntity { + log.error(e.message, e) + return ResponseEntity(ErrorMessage(e.message, e.code, debugMsg), HttpStatus.resolve(e.code)) + } } @JsonInclude(JsonInclude.Include.NON_NULL) diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceController.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceController.kt new file mode 100644 index 000000000..40aa1a3e6 --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceController.kt @@ -0,0 +1,84 @@ +/* + * Copyright © 2019 Bell Canada + * + * 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.resolutionresults.api + +import io.swagger.annotations.ApiOperation +import kotlinx.coroutines.runBlocking +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService +import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity +import org.springframework.security.access.prepost.PreAuthorize +import org.springframework.web.bind.annotation.* + +@RestController +@RequestMapping("/api/v1/resources") +open class ResourceController(private var resourceResolutionDBService: ResourceResolutionDBService) { + + @RequestMapping(path = ["/ping"], method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE]) + @ResponseBody + fun ping(): String = runBlocking { + "Success" + } + + @RequestMapping(path = [""], + method = [RequestMethod.GET], produces = [MediaType.APPLICATION_JSON_VALUE]) + @ApiOperation(value = "Fetch all resource values associated to a resolution key. ", + notes = "Retrieve a stored resource value using the blueprint metadata, artifact name and the resolution-key.", + produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + @PreAuthorize("hasRole('USER')") + fun getAllFromResolutionKeyOrFromResourceTypeAndId(@RequestParam(value = "bpName", required = true) bpName: String, + @RequestParam(value = "bpVersion", required = true) bpVersion: String, + @RequestParam(value = "artifactName", required = false, defaultValue = "") artifactName: String, + @RequestParam(value = "resolutionKey", required = false, defaultValue = "") resolutionKey: String, + @RequestParam(value = "resourceType", required = false, defaultValue = "") resourceType: String, + @RequestParam(value = "resourceId", required = false, defaultValue = "") resourceId: String) + : ResponseEntity> = runBlocking { + + if ((resolutionKey.isNotEmpty() || artifactName.isNotEmpty()) && (resourceId.isNotEmpty() || resourceType.isNotEmpty())) { + throw ResourceException("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.") + } else if (resolutionKey.isNotEmpty() && artifactName.isNotEmpty()) { + ResponseEntity.ok() + .body(resourceResolutionDBService.readWithResolutionKey(bpName, bpVersion, artifactName, resolutionKey)) + } else if (resourceType.isNotEmpty() && resourceId.isNotEmpty()){ + ResponseEntity.ok() + .body(resourceResolutionDBService.readWithResourceIdAndResourceType(bpName, bpVersion, resourceId, resourceType)) + } else { + throw ResourceException("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.") + } + } + + @RequestMapping(path = ["/resource"], + method = [RequestMethod.GET], + produces = [MediaType.APPLICATION_JSON_VALUE]) + @ApiOperation(value = "Fetch a resource value using resolution key.", + notes = "Retrieve a stored resource value using the blueprint metadata, artifact name, resolution-key along with the name of the resource value to retrieve.", + produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + @PreAuthorize("hasRole('USER')") + fun getOneFromResolutionKey(@RequestParam(value = "bpName", required = true) bpName: String, + @RequestParam(value = "bpVersion", required = true) bpVersion: String, + @RequestParam(value = "artifactName", required = true) artifactName: String, + @RequestParam(value = "resolutionKey", required = true) resolutionKey: String, + @RequestParam(value = "name", required = true) name: String) + : ResponseEntity = runBlocking { + + ResponseEntity.ok() + .body(resourceResolutionDBService.readValue(bpName, bpVersion, artifactName, resolutionKey, name)) + } +} \ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceException.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceException.kt new file mode 100644 index 000000000..6815c05ef --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceException.kt @@ -0,0 +1,21 @@ +/* + * Copyright (C) 2019 Bell Canada. + * + * 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.resolutionresults.api + +class ResourceException(message: String) : RuntimeException(message) { + var code: Int = 404 +} + diff --git a/ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceControllerTest.kt b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceControllerTest.kt new file mode 100644 index 000000000..fa8bf4459 --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/resource-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resolutionresults/api/ResourceControllerTest.kt @@ -0,0 +1,251 @@ +/* + * Copyright © 2019 Bell Canada. + * + * 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.resolutionresults.api + +import com.fasterxml.jackson.core.type.TypeReference +import com.fasterxml.jackson.module.kotlin.readValue +import kotlinx.coroutines.runBlocking +import org.junit.Assert +import org.junit.Test +import org.junit.runner.RunWith +import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolution +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.ResourceResolutionDBService +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition +import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintCatalogService +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment +import org.python.jline.console.internal.ConsoleRunner.property +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.autoconfigure.security.SecurityProperties +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest +import org.springframework.context.annotation.ComponentScan +import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatus +import org.springframework.http.MediaType +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 org.springframework.web.reactive.function.BodyInserters +import java.util.function.Consumer +import kotlin.test.BeforeTest +import org.h2.value.DataType.readValue +import java.util.* +import org.h2.value.DataType.readValue +import org.python.bouncycastle.asn1.x500.style.RFC4519Style.l +import org.h2.value.DataType.readValue +import java.lang.reflect.Array + + +@RunWith(SpringRunner::class) +@WebFluxTest +@ContextConfiguration(classes = [ResourceController::class, ResourceResolutionDBService::class, SecurityProperties::class]) +@ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"]) +@TestPropertySource(locations = ["classpath:application-test.properties"]) +class ResourceControllerTest { + + private val log = LoggerFactory.getLogger(ResourceControllerTest::class.toString()) + + @Autowired + lateinit var resourceResolutionDBService: ResourceResolutionDBService + @Autowired + lateinit var webTestClient: WebTestClient + + val blueprintName = "baseconfiguration" + val blueprintVersion = "1.0.0" + val templatePrefix = "activate" + + @Test + fun `ping return Success`() { + runBlocking { + webTestClient.get().uri("/api/v1/resources/ping") + .exchange() + .expectStatus().isOk + .expectBody() + .equals("Success") + } + } + + @Test + fun getAllFromResolutionKeyTest() { + + val resolutionKey = "1" + val ra1 = createRA("bob") + val ra2 = createRA("dylan") + + runBlocking { + + store(ra1, resKey = resolutionKey) + store(ra2, resKey = resolutionKey) + + webTestClient + .get() + .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey") + .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, ResourceResolution::class.java)) + Assert.assertEquals(2, list.size) + assertEqual(ra1, list[0]) + assertEqual(ra1, list[0]) + } + } + } + + @Test + fun getAllFromFromResourceTypeAndIdTest() { + + val resourceId = "1" + val resourceType = "ServiceInstance" + val ra1 = createRA("bob") + val ra2 = createRA("dylan") + + runBlocking { + + store(ra1, resId = resourceId, resType = resourceType) + store(ra2, resId = resourceId, resType = resourceType) + + webTestClient + .get() + .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&resourceType=$resourceType&resourceId=$resourceId") + .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, ResourceResolution::class.java)) + Assert.assertEquals(2, list.size) + assertEqual(ra1, list[0]) + assertEqual(ra1, list[0]) + } + } + } + + + @Test + fun getAllFromMissingParamTest() { + runBlocking { + webTestClient + .get() + .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion") + .exchange() + .expectStatus().is4xxClientError + .expectBody() + .consumeWith { + val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java) + Assert.assertEquals("Missing param. Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.", + r.message) + } + } + } + + @Test + fun getAllFromWrongInputTest() { + runBlocking { + webTestClient + .get() + .uri("/api/v1/resources?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=test&resourceId=1") + .exchange() + .expectStatus().is4xxClientError + .expectBody() + .consumeWith { + val r = JacksonUtils.objectMapper.readValue(it.responseBody, ErrorMessage::class.java) + Assert.assertEquals("Either retrieve resolved value using artifact name and resolution-key OR using resource-id and resource-type.", + r.message) + } + } + } + + @Test + fun getOneFromResolutionKeyTest() { + val resolutionKey = "3" + val ra = createRA("joe") + runBlocking { + store(ra, resKey = resolutionKey) + } + runBlocking { + webTestClient.get() + .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=joe") + .exchange() + .expectStatus().isOk + .expectBody() + .consumeWith { + val r = JacksonUtils.objectMapper.readValue(it.responseBody, ResourceResolution::class.java) + assertEqual(ra, r) + } + } + } + + @Test + fun getOneFromResolutionKey404Test() { + val resolutionKey = "3" + runBlocking { + webTestClient.get() + .uri("/api/v1/resources/resource?bpName=$blueprintName&bpVersion=$blueprintVersion&artifactName=$templatePrefix&resolutionKey=$resolutionKey&name=doesntexist") + .exchange() + .expectStatus().is4xxClientError + .expectBody() + } + } + + private suspend fun store(resourceAssignment: ResourceAssignment, resKey: String = "", resId: String = "", + resType: String = "") { + resourceResolutionDBService.write(blueprintName, + blueprintVersion, + resKey, + resId, + resType, + templatePrefix, + resourceAssignment) + } + + private fun createRA(prefix: String): ResourceAssignment { + val property = PropertyDefinition() + property.value = "value$prefix".asJsonPrimitive() + + val resourceAssignment = ResourceAssignment() + resourceAssignment.name = prefix + resourceAssignment.dictionaryName = "dd$prefix" + resourceAssignment.dictionarySource = "source$prefix" + resourceAssignment.version = 2 + resourceAssignment.status = "SUCCESS" + resourceAssignment.property = property + return resourceAssignment + } + + private fun assertEqual(resourceAssignment: ResourceAssignment, resourceResolution: ResourceResolution) { + Assert.assertEquals(JacksonUtils.getValue(resourceAssignment.property?.value!!).toString(), + resourceResolution.value) + Assert.assertEquals(resourceAssignment.status, resourceResolution.status) + Assert.assertEquals(resourceAssignment.dictionarySource, resourceResolution.dictionarySource) + Assert.assertEquals(resourceAssignment.dictionaryName, resourceResolution.dictionaryName) + Assert.assertEquals(resourceAssignment.version, resourceResolution.dictionaryVersion) + Assert.assertEquals(resourceAssignment.name, resourceResolution.name) + Assert.assertEquals(blueprintVersion, resourceResolution.blueprintVersion) + Assert.assertEquals(blueprintName, resourceResolution.blueprintName) + + } +} \ No newline at end of file -- cgit 1.2.3-korg