diff options
Diffstat (limited to 'ms/blueprintsprocessor/modules')
15 files changed, 572 insertions, 58 deletions
diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/db/BluePrintDBLibConfiguration.kt b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/db/BluePrintDBLibConfiguration.kt index 19e482aab..efefe0290 100644 --- a/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/db/BluePrintDBLibConfiguration.kt +++ b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/db/BluePrintDBLibConfiguration.kt @@ -16,7 +16,9 @@ package org.onap.ccsdk.cds.blueprintsprocessor.db +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties +import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.BluePrintDBLibPropertySevice import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean import org.springframework.context.annotation.ComponentScan @@ -34,8 +36,18 @@ open class BluePrintDBLibConfiguration(private var bluePrintProperties: BluePrin } } +/** + * Exposed Dependency Service by this SSH Lib Module + */ +//TODO("right now not wired with name ") +fun BluePrintDependencyService.dbLibPropertyService(): BluePrintDBLibPropertySevice = + instance(DBLibConstants.SERVICE_BLUEPRINT_DB_LIB_PROPERTY) + + class DBLibConstants { companion object { + //TODO("right now not wired with name ") + const val SERVICE_BLUEPRINT_DB_LIB_PROPERTY = "blueprint-db-lib-property-service" const val PREFIX_DB_PRIMARY: String = "blueprintsprocessor.db.primary" //list of database diff --git a/ms/blueprintsprocessor/modules/commons/processor-core/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/core/BluePrintCoreConfiguration.kt b/ms/blueprintsprocessor/modules/commons/processor-core/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/core/BluePrintCoreConfiguration.kt index 5f1ae7d37..1cb66b8ab 100644 --- a/ms/blueprintsprocessor/modules/commons/processor-core/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/core/BluePrintCoreConfiguration.kt +++ b/ms/blueprintsprocessor/modules/commons/processor-core/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/core/BluePrintCoreConfiguration.kt @@ -17,16 +17,19 @@ package org.onap.ccsdk.cds.blueprintsprocessor.core import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintPathConfiguration +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService +import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.context.properties.bind.Bindable import org.springframework.boot.context.properties.bind.Binder import org.springframework.boot.context.properties.source.ConfigurationPropertySources +import org.springframework.context.ApplicationContext +import org.springframework.context.ApplicationContextAware import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.core.env.Environment import org.springframework.stereotype.Service - @Configuration open class BluePrintCoreConfiguration(private val bluePrintProperties: BlueprintProcessorProperties) { @@ -59,4 +62,17 @@ open class BlueprintProcessorProperties(private var bluePrintPropertyBinder: Bin fun <T> propertyBeanType(prefix: String, type: Class<T>): T { return bluePrintPropertyBinder.bind(prefix, Bindable.of(type)).get() } +} + +@Configuration +// Add Conditional property , If we try to manage on Application level +open class BlueprintDependencyConfiguration : ApplicationContextAware { + + private val log = LoggerFactory.getLogger(BlueprintDependencyConfiguration::class.java)!! + + override fun setApplicationContext(applicationContext: ApplicationContext) { + BluePrintDependencyService.inject(applicationContext) + log.info("Dependency Management module created...") + } + }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/commons/processor-core/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/core/api/data/BlueprintRemoteProcessorData.kt b/ms/blueprintsprocessor/modules/commons/processor-core/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/core/api/data/BlueprintRemoteProcessorData.kt index 83254cecc..2f9ea4a25 100644 --- a/ms/blueprintsprocessor/modules/commons/processor-core/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/core/api/data/BlueprintRemoteProcessorData.kt +++ b/ms/blueprintsprocessor/modules/commons/processor-core/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/core/api/data/BlueprintRemoteProcessorData.kt @@ -37,7 +37,7 @@ data class RemoteScriptExecutionInput(var requestId: String, data class RemoteScriptExecutionOutput(var requestId: String, - var response: String, + var response: List<String>, var status: StatusType = StatusType.SUCCESS, var timestamp: Date = Date()) @@ -46,4 +46,4 @@ data class PrepareRemoteEnvInput(var requestId: String, var remoteIdentifier: RemoteIdentifier? = null, var packages: JsonNode, var timeOut: Long = 120, - var properties: MutableMap<String, JsonNode> = hashMapOf())
\ No newline at end of file + var properties: MutableMap<String, JsonNode> = hashMapOf()) diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt index fa4ff231d..25d1de881 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/BluePrintRestLibConfiguration.kt @@ -18,6 +18,10 @@ package org.onap.ccsdk.cds.blueprintsprocessor.rest +import com.fasterxml.jackson.databind.JsonNode +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.ComponentScan import org.springframework.context.annotation.Configuration @@ -27,6 +31,22 @@ import org.springframework.context.annotation.Configuration @EnableConfigurationProperties open class BluePrintRestLibConfiguration +/** + * Exposed Dependency Service by this Rest Lib Module + */ +fun BluePrintDependencyService.restLibPropertyService(): BluePrintRestLibPropertyService = + instance(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY) + + +fun BluePrintDependencyService.restClientService(selector: String): BlueprintWebClientService { + return restLibPropertyService().blueprintWebClientService(selector) +} + + +fun BluePrintDependencyService.restClientService(jsonNode: JsonNode): BlueprintWebClientService { + return restLibPropertyService().blueprintWebClientService(jsonNode) +} + class RestLibConstants { companion object { diff --git a/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/BluePrintSshLibConfiguration.kt b/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/BluePrintSshLibConfiguration.kt index 48e451f03..3c625a6d3 100644 --- a/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/BluePrintSshLibConfiguration.kt +++ b/ms/blueprintsprocessor/modules/commons/ssh-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ssh/BluePrintSshLibConfiguration.kt @@ -16,6 +16,10 @@ package org.onap.ccsdk.cds.blueprintsprocessor.ssh +import com.fasterxml.jackson.databind.JsonNode +import org.onap.ccsdk.cds.blueprintsprocessor.ssh.service.BluePrintSshLibPropertyService +import org.onap.ccsdk.cds.blueprintsprocessor.ssh.service.BlueprintSshClientService +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.ComponentScan import org.springframework.context.annotation.Configuration @@ -25,6 +29,21 @@ import org.springframework.context.annotation.Configuration @EnableConfigurationProperties open class BluePrintSshLibConfiguration +/** + * Exposed Dependency Service by this SSH Lib Module + */ +fun BluePrintDependencyService.sshLibPropertyService(): BluePrintSshLibPropertyService = + instance(SshLibConstants.SERVICE_BLUEPRINT_SSH_LIB_PROPERTY) + + +fun BluePrintDependencyService.sshClientService(selector: String): BlueprintSshClientService = + sshLibPropertyService().blueprintSshClientService(selector) + + +fun BluePrintDependencyService.sshClientService(jsonNode: JsonNode): BlueprintSshClientService = + sshLibPropertyService().blueprintSshClientService(jsonNode) + + class SshLibConstants { companion object { const val SERVICE_BLUEPRINT_SSH_LIB_PROPERTY = "blueprint-ssh-lib-property-service" 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<String> 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<ErrorMessage> { - 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<ErrorMessage> { - 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<ErrorMessage> { - 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<ErrorMessage> { - 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<ErrorMessage> { + 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<List<ResourceResolution>> = 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<ResourceResolution> = 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<ResourceResolution> = 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<ResourceResolution> = 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 diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractComponentFunction.kt b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractComponentFunction.kt index 23588d25a..408bb58ed 100644 --- a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractComponentFunction.kt +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractComponentFunction.kt @@ -27,6 +27,7 @@ import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType import org.onap.ccsdk.cds.controllerblueprints.core.* import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintVelocityTemplateService import org.slf4j.LoggerFactory /** @@ -146,4 +147,19 @@ abstract class AbstractComponentFunction : BlueprintFunctionNode<ExecutionServic bluePrintRuntimeService.getBluePrintError().addError(error) } + fun artifactContent(artifactName: String): String { + return bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName) + } + + suspend fun mashTemplateNData(artifactName: String, json: String): String { + val content = artifactContent(artifactName) + return BluePrintVelocityTemplateService.generateContent(content, json) + } + + suspend fun readLinesFromArtifact(artifactName: String): List<String> { + val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName) + val file = normalizedFile(bluePrintRuntimeService.bluePrintContext().rootPath, artifactDefinition.file) + return file.readNBLines() + } + }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractScriptComponentFunction.kt b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractScriptComponentFunction.kt index 27cde8a49..f17085ef1 100644 --- a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractScriptComponentFunction.kt +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/AbstractScriptComponentFunction.kt @@ -36,11 +36,13 @@ abstract class AbstractScriptComponentFunction : AbstractComponentFunction() { /** * Store Dynamic Script Dependency Instances, Objects present inside won't be persisted or state maintained. */ + @Deprecated("Dependencies will be resolved dynamically") var functionDependencyInstances: MutableMap<String, Any> = hashMapOf() /** * This will be called from the scripts to serve instance from runtime to scripts. */ + @Deprecated("Dependencies will be resolved dynamically") open fun <T> functionDependencyInstanceAsType(name: String): T { return functionDependencyInstances[name] as? T ?: throw BluePrintProcessorException("couldn't get script property instance ($name)") diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentScriptExecutor.kt b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentScriptExecutor.kt new file mode 100644 index 000000000..056f7e96d --- /dev/null +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentScriptExecutor.kt @@ -0,0 +1,108 @@ +/* + * Copyright © 2019 IBM. + * + * 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.services.execution + +import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType +import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType +import org.onap.ccsdk.cds.controllerblueprints.core.getAsString +import org.springframework.beans.factory.config.ConfigurableBeanFactory +import org.springframework.context.annotation.Scope +import org.springframework.stereotype.Component + +/** + * This is generic Script Component Executor function + * @author Brinda Santh + */ +@Component("component-script-executor") +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +open class ComponentScriptExecutor(private var componentFunctionScriptingService: ComponentFunctionScriptingService) + : AbstractComponentFunction() { + + companion object { + const val SCRIPT_TYPE = "script-type" + const val SCRIPT_CLASS_REFERENCE = "script-class-reference" + const val DYNAMIC_PROPERTIES = "dynamic-properties" + const val RESPONSE_DATA = "response-data" + const val STATUS = "status" + } + + lateinit var scriptComponentFunction: AbstractScriptComponentFunction + + override suspend fun processNB(executionRequest: ExecutionServiceInput) { + + val scriptType = operationInputs.getAsString(SCRIPT_TYPE) + val scriptClassReference = operationInputs.getAsString(SCRIPT_CLASS_REFERENCE) + + val scriptDependencies: MutableList<String> = arrayListOf() + populateScriptDependencies(scriptDependencies) + + scriptComponentFunction = componentFunctionScriptingService.scriptInstance(this, scriptType, + scriptClassReference, scriptDependencies) + + // Handles both script processing and error handling + scriptComponentFunction.executeScript(executionServiceInput) + } + + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + bluePrintRuntimeService.getBluePrintError() + .addError("Failed in ComponentCliExecutor : ${runtimeException.message}") + + } + + open fun populateScriptDependencies(scriptDependencies: MutableList<String>) { + /** Place holder for Child to add extra dependencies */ + } +} + +/** Component Extensions **/ + +fun BluePrintTypes.componentScriptExecutor(): NodeType { + return nodeType(id = "component-script-executor", version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT, + description = "Generic Script Component Executor") { + attribute(ComponentScriptExecutor.RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON, false) + attribute(ComponentScriptExecutor.STATUS, BluePrintConstants.DATA_TYPE_STRING, true) + + operation("ComponentScriptExecutor", "ComponentScriptExecutor Operation") { + inputs { + property(ComponentScriptExecutor.SCRIPT_TYPE, BluePrintConstants.DATA_TYPE_STRING, true, + "Script Type") { + defaultValue(BluePrintConstants.SCRIPT_INTERNAL) + constrains { + validValues(listOf(BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive(), + BluePrintConstants.SCRIPT_JYTHON.asJsonPrimitive(), + BluePrintConstants.SCRIPT_KOTLIN.asJsonPrimitive())) + } + } + property(ComponentScriptExecutor.SCRIPT_CLASS_REFERENCE, BluePrintConstants.DATA_TYPE_STRING, + true, "Kotlin Script class name or jython script name.") + property(ComponentScriptExecutor.DYNAMIC_PROPERTIES, BluePrintConstants.DATA_TYPE_JSON, false, + "Dynamic Json Content or DSL Json reference.") + } + outputs { + property(ComponentScriptExecutor.RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON, false, + "Output Response") + property(ComponentScriptExecutor.STATUS, BluePrintConstants.DATA_TYPE_STRING, true, + "Status of the Component Execution ( success or failure )") + } + } + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/RemoteScriptExecutionService.kt b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/RemoteScriptExecutionService.kt index 7aee95e11..3af57a22b 100644 --- a/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/RemoteScriptExecutionService.kt +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/RemoteScriptExecutionService.kt @@ -148,9 +148,9 @@ class GrpcRemoteScriptExecutionService(private val bluePrintGrpcLibPropertyServi fun ExecutionOutput.asJavaData(): RemoteScriptExecutionOutput { return RemoteScriptExecutionOutput( requestId = this.requestId, - response = this.response, + response = this.responseList, status = StatusType.valueOf(this.status.name) ) } -}
\ No newline at end of file +} diff --git a/ms/blueprintsprocessor/modules/services/execution-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/scripts/AbstractComponentFunctionTest.kt b/ms/blueprintsprocessor/modules/services/execution-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/scripts/AbstractComponentFunctionTest.kt index b404fbed6..224319c24 100644 --- a/ms/blueprintsprocessor/modules/services/execution-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/scripts/AbstractComponentFunctionTest.kt +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/scripts/AbstractComponentFunctionTest.kt @@ -34,7 +34,9 @@ import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInpu import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService +import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.componentScriptExecutor import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext @@ -53,7 +55,7 @@ import kotlin.test.assertNotNull */ @RunWith(SpringRunner::class) @ContextConfiguration(classes = [ComponentFunctionScriptingService::class, - BluePrintScriptsServiceImpl::class,PythonExecutorProperty::class, + BluePrintScriptsServiceImpl::class, PythonExecutorProperty::class, BlueprintJythonService::class]) class AbstractComponentFunctionTest { @@ -183,5 +185,10 @@ class AbstractComponentFunctionTest { return executionServiceInput } + @Test + fun testComponentScriptExecutorNodeType() { + val componentScriptExecutor = BluePrintTypes.componentScriptExecutor() + assertNotNull(componentScriptExecutor.interfaces, "failed to get interface operations") + } } |