diff options
Diffstat (limited to 'ms')
9 files changed, 390 insertions, 161 deletions
diff --git a/ms/blueprintsprocessor/application/pom.xml b/ms/blueprintsprocessor/application/pom.xml index 314b09c42..120b948be 100755 --- a/ms/blueprintsprocessor/application/pom.xml +++ b/ms/blueprintsprocessor/application/pom.xml @@ -132,6 +132,12 @@ <scope>test</scope> </dependency> <dependency> + <groupId>com.schibsted.spt.data</groupId> + <artifactId>jslt</artifactId> + <version>0.1.8</version> + <scope>test</scope> + </dependency> + <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> </dependency> diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTests.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTest.kt index 0a57277ea..ad4173c9e 100644 --- a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTests.kt +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/BlueprintsAcceptanceTest.kt @@ -19,7 +19,9 @@ */ package org.onap.ccsdk.cds.blueprintsprocessor +import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.node.MissingNode import com.nhaarman.mockitokotlin2.any import com.nhaarman.mockitokotlin2.argThat import com.nhaarman.mockitokotlin2.atLeast @@ -53,15 +55,17 @@ import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.TestPropertySource import org.springframework.test.context.junit4.rules.SpringClassRule import org.springframework.test.context.junit4.rules.SpringMethodRule +import org.springframework.test.web.reactive.server.EntityExchangeResult import org.springframework.test.web.reactive.server.WebTestClient -import org.yaml.snakeyaml.Yaml import reactor.core.publisher.Mono import java.io.File -import java.nio.file.Path +import java.nio.charset.StandardCharsets import java.nio.file.Paths import kotlin.test.BeforeTest import kotlin.test.Test +// Only one runner can be configured with jUnit 4. We had to replace the SpringRunner by equivalent jUnit rules. +// See more on https://docs.spring.io/autorepo/docs/spring-framework/current/spring-framework-reference/testing.html#testcontext-junit4-rules @RunWith(Parameterized::class) // Set blueprintsprocessor.httpPort=0 to trigger a random port selection @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) @@ -71,8 +75,7 @@ import kotlin.test.Test TestSecuritySettings.ServerContextInitializer::class ]) @TestPropertySource(locations = ["classpath:application-test.properties"]) -@Suppress("UNCHECKED_CAST") -class BlueprintsAcceptanceTests(private val blueprintName: String, private val filename: String) { +class BlueprintsAcceptanceTest(private val blueprintName: String, private val filename: String) { companion object { const val UAT_BLUEPRINTS_BASE_DIR = "../../../components/model-catalog/blueprint-model/uat-blueprints" @@ -82,11 +85,15 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f @JvmField val springClassRule = SpringClassRule() - val log: Logger = LoggerFactory.getLogger(BlueprintsAcceptanceTests::class.java) + val log: Logger = LoggerFactory.getLogger(BlueprintsAcceptanceTest::class.java) + /** + * Generates the parameters to create a test instance for every blueprint found under UAT_BLUEPRINTS_BASE_DIR + * that contains the proper UAT definition file. + */ @Parameterized.Parameters(name = "{index} {0}") @JvmStatic - fun filenames(): List<Array<String>> { + fun testParameters(): List<Array<String>> { return File(UAT_BLUEPRINTS_BASE_DIR) .listFiles { file -> file.isDirectory && File(file, EMBEDDED_UAT_FILE).isFile } ?.map { file -> arrayOf(file.nameWithoutExtension, file.canonicalPath) } @@ -119,38 +126,31 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f @Test fun testBlueprint() { - val yaml: Map<String, *> = loadYaml(Paths.get(filename, EMBEDDED_UAT_FILE)) + val uat = UatDefinition.load(mapper, Paths.get(filename, EMBEDDED_UAT_FILE)) uploadBlueprint(blueprintName) // Configure mocked external services - val services = yaml["external-services"] as List<Map<String, *>>? ?: emptyList() - val expectationPerClient = services.map { service -> - val selector = service["selector"] as String - val expectations = (service["expectations"] as List<Map<String, *>>).map { - parseExpectation(it) - } - val mockClient = createRestClientMock(selector, expectations) - mockClient to expectations - }.toMap() + val expectationPerClient = uat.externalServices.associateBy( + { service -> createRestClientMock(service.selector, service.expectations) }, + { service -> service.expectations } + ) // Run processes - for (process in (yaml["processes"] as List<Map<String, *>>)) { - val processName = process["name"] - log.info("Executing process '$processName'") - val request = mapper.writeValueAsString(process["request"]) - val expectedResponse = mapper.writeValueAsString(process["expectedResponse"]) - processBlueprint(request, expectedResponse) + for (process in uat.processes) { + log.info("Executing process '${process.name}'") + processBlueprint(process.request, process.expectedResponse, + JsonNormalizer.getNormalizer(mapper, process.responseNormalizerSpec)) } - // Validate request payloads + // Validate request payloads to external services for ((mockClient, expectations) in expectationPerClient) { expectations.forEach { expectation -> verify(mockClient, atLeastOnce()).exchangeResource( - eq(expectation.method), - eq(expectation.path), - argThat { assertJsonEqual(expectation.expectedRequestBody, this) }, - expectation.requestHeadersMatcher()) + eq(expectation.request.method), + eq(expectation.request.path), + argThat { assertJsonEqual(expectation.request.body, this) }, + expectation.request.requestHeadersMatcher()) } // Don't mind the invocations to the overloaded exchangeResource(String, String, String) verify(mockClient, atLeast(0)).exchangeResource(any(), any(), any()) @@ -158,7 +158,8 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f } } - private fun createRestClientMock(selector: String, restExpectations: List<RestExpectation>): BlueprintWebClientService { + private fun createRestClientMock(selector: String, restExpectations: List<ExpectationDefinition>) + : BlueprintWebClientService { val restClient = mock<BlueprintWebClientService>(verboseLogging = true) // Delegates to overloaded exchangeResource(String, String, String, Map<String, String>) @@ -171,11 +172,11 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f } for (expectation in restExpectations) { whenever(restClient.exchangeResource( - eq(expectation.method), - eq(expectation.path), + eq(expectation.request.method), + eq(expectation.request.path), any(), any())) - .thenReturn(WebClientResponse(expectation.statusCode, expectation.responseBody)) + .thenReturn(WebClientResponse(expectation.response.status, expectation.response.body.toString())) } whenever(restClientFactory.blueprintWebClientService(selector)) @@ -194,17 +195,20 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f .expectStatus().isOk } - private fun processBlueprint(request: String, expectedResponse: String) { + private fun processBlueprint(request: JsonNode, expectedResponse: JsonNode, + responseNormalizer: (String) -> String) { webTestClient .post() .uri("/api/v1/execution-service/process") .header("Authorization", TestSecuritySettings.clientAuthToken()) .contentType(MediaType.APPLICATION_JSON_UTF8) - .body(Mono.just(request), String::class.java) + .body(Mono.just(request.toString()), String::class.java) .exchange() .expectStatus().isOk .expectBody() - .json(expectedResponse) + .consumeWith { response -> + assertJsonEqual(expectedResponse, responseNormalizer(getBodyAsString(response))) + } } private fun getBlueprintAsResource(blueprintName: String): Resource { @@ -216,65 +220,21 @@ class BlueprintsAcceptanceTests(private val blueprintName: String, private val f } } - private fun loadYaml(path: Path): Map<String, Any> { - return path.toFile().reader().use { reader -> - Yaml().load(reader) - } - } - - private fun assertJsonEqual(expected: Any, actual: String): Boolean { - if (actual != expected) { - // assertEquals throws an exception whenever match fails - JSONAssert.assertEquals(mapper.writeValueAsString(expected), actual, JSONCompareMode.LENIENT) + private fun assertJsonEqual(expected: JsonNode, actual: String): Boolean { + if ((actual == "") && (expected is MissingNode)) { + return true } + JSONAssert.assertEquals(expected.toString(), actual, JSONCompareMode.LENIENT) + // assertEquals throws an exception whenever match fails return true } - private fun parseExpectation(expectation: Map<String, *>): RestExpectation { - val request = expectation["request"] as Map<String, Any> - val method = request["method"] as String - val path = joinPath(request.getValue("path")) - val contentType = request["content-type"] as String? - val requestBody = request.getOrDefault("body", "") - - val response = expectation["response"] as Map<String, Any>? ?: emptyMap() - val status = response["status"] as Int? ?: 200 - val responseBody = when (val body = response["body"] ?: "") { - is String -> body - else -> mapper.writeValueAsString(body) - } - - return RestExpectation(method, path, contentType, requestBody, status, responseBody) - } - - /** - * Join a multilevel lists of strings. - * Example: joinPath(listOf("a", listOf("b", "c"), "d")) will result in "a/b/c/d". - */ - private fun joinPath(any: Any): String { - fun recursiveJoin(any: Any, sb: StringBuilder): StringBuilder { - when (any) { - is List<*> -> any.filterNotNull().forEach { recursiveJoin(it, sb) } - is String -> { - if (sb.isNotEmpty()) { - sb.append('/') - } - sb.append(any) - } - else -> throw IllegalArgumentException("Unsupported type: ${any.javaClass}") - } - return sb - } - - return recursiveJoin(any, StringBuilder()).toString() - } - - data class RestExpectation(val method: String, val path: String, val contentType: String?, - val expectedRequestBody: Any, - val statusCode: Int, val responseBody: String) { - - fun requestHeadersMatcher(): Map<String, String> { - return if (contentType != null) eq(mapOf("Content-Type" to contentType)) else any() + private fun getBodyAsString(result: EntityExchangeResult<ByteArray>): String { + val body = result.responseBody + if ((body == null) || body.isEmpty()) { + return "" } + val charset = result.responseHeaders.contentType?.charset ?: StandardCharsets.UTF_8 + return String(body, charset) } }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt index 4576f2761..3c517e6ac 100644 --- a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/ExtendedTemporaryFolder.kt @@ -1,3 +1,22 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ package org.onap.ccsdk.cds.blueprintsprocessor import org.junit.rules.TemporaryFolder diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/JsonNormalizer.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/JsonNormalizer.kt new file mode 100644 index 000000000..69673f931 --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/JsonNormalizer.kt @@ -0,0 +1,79 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.cds.blueprintsprocessor + +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.node.ContainerNode +import com.fasterxml.jackson.databind.node.MissingNode +import com.fasterxml.jackson.databind.node.ObjectNode +import com.schibsted.spt.data.jslt.Parser + +class JsonNormalizer { + + companion object { + + fun getNormalizer(mapper: ObjectMapper, jsltSpec: JsonNode): (String) -> String { + if (jsltSpec is MissingNode) { + return { it } + } + return { s: String -> + val input = mapper.readTree(s) + val expandedJstlSpec = expandJstlSpec(jsltSpec) + val jslt = Parser.compileString(expandedJstlSpec) + val output = jslt.apply(input) + output.toString() + } + } + + /** + * Creates an extended JSTL spec by appending the "*: ." wildcard pattern to every inner JSON object, and + * removing the extra quotes added by the standard YAML/JSON converters on fields prefixed by "?". + * + * @param jstlSpec the JSTL spec as a structured JSON object. + * @return the string representation of the extended JSTL spec. + */ + private fun expandJstlSpec(jstlSpec: JsonNode): String { + val extendedJstlSpec = updateObjectNodes(jstlSpec, "*", ".") + return extendedJstlSpec.toString() + // Handle the "?" as a prefix to literal/non-quoted values + .replace("\"\\?([^\"]+)\"".toRegex(), "$1") + // Also, remove the quotes added by Jackson for key and value of the wildcard matcher + .replace("\"([.*])\"".toRegex(), "$1") + } + + /** + * Expands a structured JSON object, by adding the given key and value to every nested ObjectNode. + * + * @param jsonNode the root node. + * @param fieldName the fixed field name. + * @param fieldValue the fixed field value. + */ + private fun updateObjectNodes(jsonNode: JsonNode, fieldName: String, fieldValue: String): JsonNode { + if (jsonNode is ContainerNode<*>) { + (jsonNode as? ObjectNode)?.put(fieldName, fieldValue) + jsonNode.forEach { child -> + updateObjectNodes(child, fieldName, fieldValue) + } + } + return jsonNode + } + } +} diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/PathDeserializer.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/PathDeserializer.kt new file mode 100644 index 000000000..1a232f2d3 --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/PathDeserializer.kt @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.cds.blueprintsprocessor + +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.deser.std.StdDeserializer + +class PathDeserializer : StdDeserializer<String>(String::class.java) { + override fun deserialize(jp: JsonParser, ctxt: DeserializationContext?): String { + val path = jp.codec.readValue(jp, Any::class.java) + return flatJoin(path) + } + + /** + * Join a multilevel lists of strings. + * Example: flatJoin(listOf("a", listOf("b", "c"), "d")) will result in "a/b/c/d". + */ + private fun flatJoin(path: Any): String { + fun flatJoinTo(sb: StringBuilder, path: Any): StringBuilder { + when (path) { + is List<*> -> path.filterNotNull().forEach { flatJoinTo(sb, it) } + is String -> { + if (sb.isNotEmpty()) { + sb.append('/') + } + sb.append(path) + } + else -> throw IllegalArgumentException("Unsupported type: ${path.javaClass}") + } + return sb + } + return flatJoinTo(StringBuilder(), path).toString() + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/UatDefinition.kt b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/UatDefinition.kt new file mode 100644 index 000000000..ce2061168 --- /dev/null +++ b/ms/blueprintsprocessor/application/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/UatDefinition.kt @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ +package org.onap.ccsdk.cds.blueprintsprocessor + +import com.fasterxml.jackson.annotation.JsonAlias +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.annotation.JsonDeserialize +import com.fasterxml.jackson.databind.node.MissingNode +import com.nhaarman.mockitokotlin2.any +import com.nhaarman.mockitokotlin2.eq +import org.yaml.snakeyaml.Yaml +import java.nio.file.Path + +data class ProcessDefinition(val name: String, val request: JsonNode, val expectedResponse: JsonNode, + val responseNormalizerSpec: JsonNode = MissingNode.getInstance()) + +data class RequestDefinition(val method: String, + @JsonDeserialize(using = PathDeserializer::class) + val path: String, + @JsonAlias("content-type") + val contentType: String? = null, + val body: JsonNode = MissingNode.getInstance()) { + fun requestHeadersMatcher(): Map<String, String> { + return if (contentType != null) eq(mapOf("Content-Type" to contentType)) else any() + } +} + +data class ResponseDefinition(val status: Int = 200, val body: JsonNode = MissingNode.getInstance()) { + companion object { + val DEFAULT_RESPONSE = ResponseDefinition() + } +} + +data class ExpectationDefinition(val request: RequestDefinition, + val response: ResponseDefinition = ResponseDefinition.DEFAULT_RESPONSE) + +data class ServiceDefinition(val selector: String, val expectations: List<ExpectationDefinition>) + +data class UatDefinition(val processes: List<ProcessDefinition>, + @JsonAlias("external-services") + val externalServices: List<ServiceDefinition> = emptyList()) { + + companion object { + fun load(mapper: ObjectMapper, path: Path): UatDefinition { + return path.toFile().reader().use { reader -> + mapper.convertValue(Yaml().load(reader), UatDefinition::class.java) + } + } + } +} diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt index 947a9630d..743aa714b 100644 --- a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt @@ -24,10 +24,7 @@ import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInpu import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction -import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive -import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString -import org.onap.ccsdk.cds.controllerblueprints.core.isNotNull -import org.onap.ccsdk.cds.controllerblueprints.core.rootFieldsToMap +import org.onap.ccsdk.cds.controllerblueprints.core.* import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.slf4j.LoggerFactory import org.springframework.beans.factory.config.ConfigurableBeanFactory @@ -68,6 +65,7 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe // input fields names accepted by this executor const val INPUT_ENDPOINT_SELECTOR = "endpoint-selector" const val INPUT_JOB_TEMPLATE_NAME = "job-template-name" + const val INPUT_WORKFLOW_JOB_TEMPLATE_NAME = "workflow-job-template-id" const val INPUT_LIMIT_TO_HOST = "limit" const val INPUT_INVENTORY = "inventory" const val INPUT_EXTRA_VARS = "extra-vars" @@ -85,12 +83,20 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe try { val restClientService = getAWXRestClient() - val jobTemplateName = getOperationInput(INPUT_JOB_TEMPLATE_NAME).asText() - val jtId = lookupJobTemplateIDByName(restClientService, jobTemplateName) + // Get either a job template name or a workflow template name property + var workflowURIPrefix = "" + var jobTemplateName = getOperationInput(INPUT_JOB_TEMPLATE_NAME).returnNullIfMissing()?.textValue() ?: "" + val isWorkflowJT = jobTemplateName.isBlank() + if (isWorkflowJT) { + jobTemplateName = getOperationInput(INPUT_WORKFLOW_JOB_TEMPLATE_NAME).asText() + workflowURIPrefix = "workflow_" + } + + val jtId = lookupJobTemplateIDByName(restClientService, jobTemplateName, workflowURIPrefix) if (jtId.isNotEmpty()) { - runJobTemplateOnAWX(restClientService, jobTemplateName, jtId) + runJobTemplateOnAWX(restClientService, jobTemplateName, jtId, workflowURIPrefix) } else { - val message = "Job template ${jobTemplateName} does not exists" + val message = "Workflow/Job template ${jobTemplateName} does not exists" log.error(message) setNodeOutputErrors(ATTRIBUTE_EXEC_CMD_STATUS_ERROR, message) } @@ -135,9 +141,10 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe /** * Finds the job template ID based on the job template name provided in the request */ - private fun lookupJobTemplateIDByName(awxClient: BlueprintWebClientService, job_template_name: String?): String { + private fun lookupJobTemplateIDByName(awxClient: BlueprintWebClientService, job_template_name: String?, + workflowPrefix : String) : String { val encodedJTName = URI(null, null, - "/api/v2/job_templates/${job_template_name}/", + "/api/v2/${workflowPrefix}job_templates/${job_template_name}/", null, null).rawPath // Get Job Template details by name @@ -152,19 +159,20 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe * its execution. Finally, it retrieves the job results via the stdout api. * The status and output attributes are populated in the process. */ - private fun runJobTemplateOnAWX(awxClient: BlueprintWebClientService, job_template_name: String?, jtId: String) { + private fun runJobTemplateOnAWX(awxClient: BlueprintWebClientService, job_template_name: String?, jtId: String, + workflowPrefix : String) { setNodeOutputProperties("preparing".asJsonPrimitive(), "".asJsonPrimitive()) // Get Job Template requirements - var response = awxClient.exchangeResource(GET, "/api/v2/job_templates/${jtId}/launch/", "") + var response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}job_templates/${jtId}/launch/", "") // FIXME: handle non-successful SC val jtLaunchReqs: JsonNode = mapper.readTree(response.body) - val payload = prepareLaunchPayload(awxClient, jtLaunchReqs) + val payload = prepareLaunchPayload(awxClient, jtLaunchReqs, workflowPrefix.isBlank()) log.info("Running job with $payload, for requestId $processId.") // Launch the job for the targeted template var jtLaunched: JsonNode = JacksonUtils.objectMapper.createObjectNode() - response = awxClient.exchangeResource(POST, "/api/v2/job_templates/${jtId}/launch/", payload) + response = awxClient.exchangeResource(POST, "/api/v2/${workflowPrefix}job_templates/${jtId}/launch/", payload) if (response.status in HTTP_SUCCESS) { jtLaunched = mapper.readTree(response.body) val fieldsIgnored: JsonNode = jtLaunched.at("/ignored_fields") @@ -180,7 +188,7 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe var jobStatus = "unknown" var jobEndTime = "null" while (jobEndTime == "null") { - response = awxClient.exchangeResource(GET, "/api/v2/jobs/${jobId}/", "") + response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}jobs/${jobId}/", "") val jobLaunched: JsonNode = mapper.readTree(response.body) jobStatus = jobLaunched.at("/status").asText() jobEndTime = jobLaunched.at("/finished").asText() @@ -189,12 +197,10 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe log.info("Execution of job template $job_template_name in job #$jobId finished with status ($jobStatus) for requestId $processId") - // Get job execution results (stdout) - val plainTextHeaders = mutableMapOf<String, String>() - plainTextHeaders["Content-Type"] = "text/plain ;utf-8" - response = awxClient.exchangeResource(GET, "/api/v2/jobs/${jobId}/stdout/?format=txt", "", plainTextHeaders) + // Get workflow/job execution results + val collectedOutput = extractJobRunResponse(awxClient, jobId, workflowPrefix) - setNodeOutputProperties(jobStatus.asJsonPrimitive(), response.body.asJsonPrimitive()) + setNodeOutputProperties(jobStatus.asJsonPrimitive(), collectedOutput.asJsonPrimitive()) } else { // The job template requirements were not fulfilled with the values passed in. The message below will // provide more information via the response, like the ignored_fields, or variables_needed_to_start, @@ -207,42 +213,77 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe } /** + * Extracts the response from either a job stdout call OR collects the workflow run output + */ + private fun extractJobRunResponse(awxClient: BlueprintWebClientService, jobId: String, workflowPrefix: String): String { + + // First, collect all job ID from either the job template run or the workflow nodes that ran + var jobIds : Array<String> + var collectedResponses = StringBuilder() + if (workflowPrefix.isNotEmpty()) { + var response = awxClient.exchangeResource(GET, "/api/v2/${workflowPrefix}jobs/${jobId}/workflow_nodes/", "") + val jobDetails = mapper.readTree(response.body).at("/results") + jobIds = emptyArray() + for (jobDetail in jobDetails.elements()) { + jobIds = jobIds.plus( jobDetail.at("/summary_fields/job/id").asText() ) + } + } else { + jobIds = arrayOf(jobId) + } + + // Then collect the response text from the corresponding jobIds + val plainTextHeaders = mutableMapOf<String, String>() + plainTextHeaders["Content-Type"] = "text/plain ;utf-8" + for (aJobId in jobIds) { + var response = awxClient.exchangeResource(GET, "/api/v2/jobs/${aJobId}/stdout/?format=txt", "", plainTextHeaders) + collectedResponses.append("Output for job ${aJobId}:") + collectedResponses.append(response.body) + } + return collectedResponses.toString() + } + + /** * Prepares the JSON payload expected by the job template api, * by applying the overrides that were provided * and allowed by the template definition flags in jtLaunchReqs */ - private fun prepareLaunchPayload(awxClient: BlueprintWebClientService, jtLaunchReqs: JsonNode): String { + private fun prepareLaunchPayload(awxClient: BlueprintWebClientService, jtLaunchReqs: JsonNode, + isWorkflow : Boolean): String { val payload = JacksonUtils.objectMapper.createObjectNode() // Parameter defaults - val limitProp = getOptionalOperationInput(INPUT_LIMIT_TO_HOST) - val tagsProp = getOptionalOperationInput(INPUT_TAGS) - val skipTagsProp = getOptionalOperationInput(INPUT_SKIP_TAGS) val inventoryProp = getOptionalOperationInput(INPUT_INVENTORY) val extraArgs = getOperationInput(INPUT_EXTRA_VARS) - val askLimitOnLaunch = jtLaunchReqs.at("/ask_limit_on_launch").asBoolean() - if (askLimitOnLaunch && limitProp.isNotNull()) { - payload.set(INPUT_LIMIT_TO_HOST, limitProp) - } - val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean() - if (askTagsOnLaunch && tagsProp.isNotNull()) { - payload.set(INPUT_TAGS, tagsProp) - } - if (askTagsOnLaunch && skipTagsProp.isNotNull()) { - payload.set("skip_tags", skipTagsProp) + if (!isWorkflow) { + val limitProp = getOptionalOperationInput(INPUT_LIMIT_TO_HOST) + val tagsProp = getOptionalOperationInput(INPUT_TAGS) + val skipTagsProp = getOptionalOperationInput(INPUT_SKIP_TAGS) + + val askLimitOnLaunch = jtLaunchReqs.at("/ask_limit_on_launch").asBoolean() + if (askLimitOnLaunch && limitProp.isNotNull()) { + payload.set(INPUT_LIMIT_TO_HOST, limitProp) + } + val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean() + if (askTagsOnLaunch && tagsProp.isNotNull()) { + payload.set(INPUT_TAGS, tagsProp) + } + if (askTagsOnLaunch && skipTagsProp.isNotNull()) { + payload.set("skip_tags", skipTagsProp) + } } + val askInventoryOnLaunch = jtLaunchReqs.at("/ask_inventory_on_launch").asBoolean() if (askInventoryOnLaunch && inventoryProp.isNotNull()) { var inventoryKeyId = if (inventoryProp is TextNode) { - resolveInventoryIdByName(awxClient, inventoryProp!!.textValue())?.asJsonPrimitive() + resolveInventoryIdByName(awxClient, inventoryProp.textValue())?.asJsonPrimitive() } else { inventoryProp } payload.set(INPUT_INVENTORY, inventoryKeyId) } val askVariablesOnLaunch = jtLaunchReqs.at("/ask_variables_on_launch").asBoolean() - if (askVariablesOnLaunch && extraArgs != null) { + if (askVariablesOnLaunch) { payload.set("extra_vars", extraArgs) } return payload.asJsonString(false) diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt index c11377012..08bc6c3fd 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt @@ -24,7 +24,6 @@ import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils import org.slf4j.LoggerFactory import org.slf4j.helpers.MessageFormatter -import java.lang.Float import kotlin.reflect.KClass /** @@ -98,10 +97,10 @@ fun <T : Any?> T.asJsonPrimitive(): JsonNode { fun String.asJsonType(bpDataType: String): JsonNode { return when (bpDataType.toLowerCase()) { BluePrintConstants.DATA_TYPE_STRING -> this.asJsonPrimitive() - BluePrintConstants.DATA_TYPE_BOOLEAN -> java.lang.Boolean.valueOf(this).asJsonPrimitive() - BluePrintConstants.DATA_TYPE_INTEGER -> Integer.valueOf(this).asJsonPrimitive() - BluePrintConstants.DATA_TYPE_FLOAT -> Float.valueOf(this).asJsonPrimitive() - BluePrintConstants.DATA_TYPE_DOUBLE -> java.lang.Double.valueOf(this).asJsonPrimitive() + BluePrintConstants.DATA_TYPE_BOOLEAN -> this.toBoolean().asJsonPrimitive() + BluePrintConstants.DATA_TYPE_INTEGER -> this.toInt().asJsonPrimitive() + BluePrintConstants.DATA_TYPE_FLOAT -> this.toFloat().asJsonPrimitive() + BluePrintConstants.DATA_TYPE_DOUBLE -> this.toDouble().asJsonPrimitive() // For List, Map and Complex Types. else -> this.jsonAsJsonType() } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt index 768f8753f..73dff9379 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt @@ -236,51 +236,55 @@ class JacksonUtils { } } - fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) { + fun populatePrimitiveValues(key: String, value: JsonNode, primitiveType: String, objectNode: ObjectNode) { when (primitiveType.toLowerCase()) { - BluePrintConstants.DATA_TYPE_STRING, BluePrintConstants.DATA_TYPE_BOOLEAN, BluePrintConstants.DATA_TYPE_INTEGER, BluePrintConstants.DATA_TYPE_FLOAT, BluePrintConstants.DATA_TYPE_DOUBLE, - BluePrintConstants.DATA_TYPE_TIMESTAMP -> - objectNode.set(key, value.asJsonPrimitive()) - else -> objectNode.set(key, value.asJsonType()) + BluePrintConstants.DATA_TYPE_TIMESTAMP, + BluePrintConstants.DATA_TYPE_STRING -> + objectNode.set(key, value) + else -> throw BluePrintException("populatePrimitiveValues expected only primitive values! Received: ($value)") } } - fun populatePrimitiveValues(value: Any, primitiveType: String, arrayNode: ArrayNode) { + fun populatePrimitiveValues(value: JsonNode, primitiveType: String, arrayNode: ArrayNode) { when (primitiveType.toLowerCase()) { - BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(value as Boolean) - BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(value as Int) - BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(value as Float) - BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(value as Double) - BluePrintConstants.DATA_TYPE_TIMESTAMP -> arrayNode.add(value as String) - else -> arrayNode.add(value as String) + BluePrintConstants.DATA_TYPE_BOOLEAN, + BluePrintConstants.DATA_TYPE_INTEGER, + BluePrintConstants.DATA_TYPE_FLOAT, + BluePrintConstants.DATA_TYPE_DOUBLE, + BluePrintConstants.DATA_TYPE_TIMESTAMP, + BluePrintConstants.DATA_TYPE_STRING -> arrayNode.add(value) + else -> throw BluePrintException("populatePrimitiveValues expected only primitive values! Received: ($value)") } } fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) { - when (primitiveType.toLowerCase()) { - BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, false) - BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, 0) - BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, 0.0) - BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, 0.0) - else -> objectNode.put(key, "") - } + val defaultValue = getDefaultValueOfPrimitiveAsJsonNode(primitiveType) ?: + throw BluePrintException("populatePrimitiveDefaultValues expected only primitive values! Received type ($primitiveType)") + objectNode.set(key, defaultValue) } fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) { - when (primitiveType.toLowerCase()) { - BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(false) - BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(0) - BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(0.0) - BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(0.0) - else -> arrayNode.add("") + val defaultValue = getDefaultValueOfPrimitiveAsJsonNode(primitiveType) ?: + throw BluePrintException("populatePrimitiveDefaultValuesForArrayNode expected only primitive values! Received type ($primitiveType)") + arrayNode.add(defaultValue) + } + + private fun getDefaultValueOfPrimitiveAsJsonNode(primitiveType: String): JsonNode? { + return when (primitiveType.toLowerCase()) { + BluePrintConstants.DATA_TYPE_BOOLEAN -> BooleanNode.valueOf(false) + BluePrintConstants.DATA_TYPE_INTEGER -> IntNode.valueOf(0) + BluePrintConstants.DATA_TYPE_FLOAT -> FloatNode.valueOf(0.0f) + BluePrintConstants.DATA_TYPE_DOUBLE -> DoubleNode.valueOf(0.0) + BluePrintConstants.DATA_TYPE_STRING -> MissingNode.getInstance() + else -> null } } - fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) { + fun populateJsonNodeValues(key: String, nodeValue: JsonNode, type: String, objectNode: ObjectNode) { if (nodeValue == null || nodeValue is NullNode) { objectNode.set(key, nodeValue) } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) { @@ -292,12 +296,13 @@ class JacksonUtils { fun convertPrimitiveResourceValue(type: String, value: String): JsonNode { return when (type.toLowerCase()) { - BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNodeFromObject(java.lang.Boolean.valueOf(value)) - BluePrintConstants.DATA_TYPE_INTEGER -> jsonNodeFromObject(Integer.valueOf(value)) - BluePrintConstants.DATA_TYPE_FLOAT -> jsonNodeFromObject(java.lang.Float.valueOf(value)) - BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNodeFromObject(java.lang.Double.valueOf(value)) + BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNodeFromObject(value.toBoolean()) + BluePrintConstants.DATA_TYPE_INTEGER -> jsonNodeFromObject(value.toInt()) + BluePrintConstants.DATA_TYPE_FLOAT -> jsonNodeFromObject(value.toFloat()) + BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNodeFromObject(value.toDouble()) + BluePrintConstants.DATA_TYPE_STRING -> jsonNodeFromObject(value) else -> getJsonNode(value) } } } -}
\ No newline at end of file +} |