diff options
19 files changed, 998 insertions, 0 deletions
diff --git a/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/artifact_types.json b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/artifact_types.json new file mode 100644 index 000000000..6e5cfc3a1 --- /dev/null +++ b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/artifact_types.json @@ -0,0 +1,34 @@ +{ + "artifact_types" : { + "artifact-directed-graph" : { + "description" : "Directed Graph File", + "version" : "1.0.0", + "derived_from" : "tosca.artifacts.Implementation", + "file_ext" : [ "json", "xml" ] + }, + "artifact-mapping-resource" : { + "description" : "Resource Mapping File used along with Configuration template", + "version" : "1.0.0", + "derived_from" : "tosca.artifacts.Implementation", + "file_ext" : [ "json" ] + }, + "artifact-script-ansible" : { + "description" : "Ansible Script file", + "version" : "1.0.0", + "derived_from" : "tosca.artifacts.Implementation", + "file_ext" : [ "yaml" ] + }, + "artifact-script-python" : { + "description" : "Python Script file", + "version" : "1.0.0", + "derived_from" : "tosca.artifacts.Implementation", + "file_ext" : [ "py" ] + }, + "artifact-template-velocity" : { + "description" : " Velocity Template used for Configuration", + "version" : "1.0.0", + "derived_from" : "tosca.artifacts.Implementation", + "file_ext" : [ "vtl" ] + } + } +}
\ No newline at end of file diff --git a/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/data_types.json b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/data_types.json new file mode 100644 index 000000000..8c304c40b --- /dev/null +++ b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/data_types.json @@ -0,0 +1,3 @@ +{ + "data_types" : { } +}
\ No newline at end of file diff --git a/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/node_types.json b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/node_types.json new file mode 100644 index 000000000..5f0deeb98 --- /dev/null +++ b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/node_types.json @@ -0,0 +1,86 @@ +{ + "node_types" : { + "component-remote-ansible-executor" : { + "description" : "Remote Ansible (AWX) Execution Component.", + "version" : "1.0.0", + "attributes" : { + "ansible-command-status" : { + "required" : false, + "type" : "string" + }, + "ansible-command-logs" : { + "required" : false, + "type" : "string" + } + }, + "capabilities" : { + "component-node" : { + "type" : "tosca.capabilities.Node" + } + }, + "interfaces" : { + "ComponentRemoteAnsibleExecutor" : { + "operations" : { + "process" : { + "inputs" : { + "job-template-name" : { + "description" : "Job template to execute in AWX", + "required" : true, + "type" : "string" + }, + "limit" : { + "description" : "Limit to this comma-separated list of hosts.", + "required" : false, + "type" : "string" + }, + "inventory" : { + "description" : "Use this hosts inventory ID for this run.", + "required" : false, + "type" : "string" + }, + "tags" : { + "description" : "Limit this run to comma-separated list of tags.", + "required" : false, + "type" : "string" + }, + "skip-tags" : { + "description" : "Skip this comma-separated list of tags for this run.", + "required" : false, + "type" : "string" + }, + "extra-vars" : { + "description" : "Specify extra args for this run.", + "required" : false, + "type" : "json" + }, + "endpoint-selector" : { + "description" : "Remote AWX Server selector name.", + "required" : false, + "type" : "string", + "default" : "awx-remote-server" + } + } + } + } + } + }, + "derived_from" : "tosca.nodes.Component" + }, + + "tosca.nodes.Component" : { + "description" : "This is default Component Node", + "version" : "1.0.0", + "derived_from" : "tosca.nodes.Root" + }, + "tosca.nodes.ResourceSource" : { + "description" : "TOSCA base type for Resource Sources", + "version" : "1.0.0", + "derived_from" : "tosca.nodes.Root" + }, + "tosca.nodes.Workflow" : { + "description" : "This is Directed Graph Node Type", + "version" : "1.0.0", + "derived_from" : "tosca.nodes.Root" + } + } +}
\ No newline at end of file diff --git a/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/policy_types.json b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/policy_types.json new file mode 100644 index 000000000..1e44cc70a --- /dev/null +++ b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/policy_types.json @@ -0,0 +1,3 @@ +{ + "policy_types" : { } +}
\ No newline at end of file diff --git a/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/relationship_types.json b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/relationship_types.json new file mode 100644 index 000000000..4ddd7a57c --- /dev/null +++ b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/relationship_types.json @@ -0,0 +1,3 @@ +{ + "relationship_types" : { } +}
\ No newline at end of file diff --git a/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/remote_ansible.json b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/remote_ansible.json new file mode 100644 index 000000000..53ca04a5d --- /dev/null +++ b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/remote_ansible.json @@ -0,0 +1,124 @@ +{ + "tosca_definitions_version": "controller_blueprint_1_0_0", + "metadata": { + "template_author": "Serge Simard", + "author-email": "serge@agilitae.com", + "user-groups": "ADMIN, OPERATION", + "template_name": "remote_ansible", + "template_version": "1.0.0", + "template_tags": "tosca" + }, + "imports": [ + { + "file": "Definitions/data_types.json" + }, + { + "file": "Definitions/relationship_types.json" + }, + { + "file": "Definitions/artifact_types.json" + }, + { + "file": "Definitions/node_types.json" + }, + { + "file": "Definitions/policy_types.json" + } + ], + "dsl_definitions": { + "ansible-remote-endpoint": { + "type": "token-auth", + "url": "http://142.44.184.236", + "token": "Bearer J9gEtMDqf7P4YsJ74fioY9VAhLDIs1" + } + }, + "topology_template": { + "workflows": { + "execute-remote-ansible": { + "steps": { + "process": { + "description": "Execute Remote Ansible Script", + "target": "execute-remote-ansible", + "activities": [ + { + "call_operation": "" + } + ] + } + }, + "inputs": { + "endpoint-selector": { + "required": true, + "type": "string" + }, + "job-template-name": { + "required": true, + "type": "string" + }, + "limit": { + "required": false, + "type": "string" + }, + "inventory": { + "required": false, + "type": "string" + }, + "tags": { + "required": false, + "type": "string" + }, + "skip-tags": { + "required": false, + "type": "string" + }, + "extra-vars" : { + "required" : false, + "type" : "json" + } + }, + "outputs": { + "ansible-command-status": { + "type": "string", + "value": { + "get_attribute": [ + "execute-remote-ansible", + "ansible-command-status" + ] + } + }, + "ansible-command-logs": { + "type": "string", + "value": { + "get_attribute": [ + "execute-remote-ansible", + "ansible-command-logs" + ] + } + } + } + } + }, + "node_templates": { + "execute-remote-ansible": { + "type": "component-remote-ansible-executor", + "interfaces": { + "ComponentRemoteAnsibleExecutor": { + "operations": { + "process": { + "inputs": { + "endpoint-selector": { "get_input": "endpoint-selector" }, + "job-template-name": { "get_input": "job-template-name" }, + "limit": { "get_input": "limit" }, + "inventory": { "get_input": "inventory" }, + "extra-vars": { "get_input": "extra-vars" }, + "tags": { "get_input": "tags" }, + "skip-tags": { "get_input": "skip-tags" } + } + } + } + } + } + } + } + } +}
\ No newline at end of file diff --git a/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/resources_definition_types.json b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/resources_definition_types.json new file mode 100644 index 000000000..0e0dcd235 --- /dev/null +++ b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/Definitions/resources_definition_types.json @@ -0,0 +1,3 @@ +{ + +}
\ No newline at end of file diff --git a/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/TOSCA-Metadata/TOSCA.meta b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/TOSCA-Metadata/TOSCA.meta new file mode 100644 index 000000000..17448a835 --- /dev/null +++ b/components/model-catalog/blueprint-model/test-blueprint/remote_ansible/TOSCA-Metadata/TOSCA.meta @@ -0,0 +1,5 @@ +TOSCA-Meta-File-Version: 1.0.0 +CSAR-Version: 1.0 +Created-By: Serge Simard <serge@agilitae.com> +Entry-Definitions: Definitions/remote_ansible.json +Template-Tags: Serge Simard, remote_ansible
\ No newline at end of file diff --git a/components/model-catalog/definition-type/starter-type/node_type/component-remote-ansible-executor.json b/components/model-catalog/definition-type/starter-type/node_type/component-remote-ansible-executor.json new file mode 100644 index 000000000..498db8246 --- /dev/null +++ b/components/model-catalog/definition-type/starter-type/node_type/component-remote-ansible-executor.json @@ -0,0 +1,65 @@ +{ + "description": "This is Remote Ansible Playbook (AWX) Execution Component.", + "version": "1.0.0", + "attributes": { + "ansible-command-status": { + "required": true, + "type": "string" + }, + "ansible-command-logs": { + "required": true, + "type": "string" + } + }, + "capabilities": { + "component-node": { + "type": "tosca.capabilities.Node" + } + }, + "interfaces": { + "ComponentRemoteAnsibleExecutor": { + "operations": { + "process": { + "inputs": { + "job-template-name": { + "description": "Primary key or name of the job template to launch new job.", + "required": true, + "type": "string" + }, + "limit": { + "description": "Specify host limit for job template to run.", + "required": false, + "type": "string" + }, + "inventory": { + "description": "Specify inventory for job template to run.", + "required": false, + "type": "string" + }, + "extra-vars" : { + "required" : false, + "type" : "json", + "description": "json formatted text that contains extra variables to pass on." + }, + "tags": { + "description": "Specify tagged actions in the playbook to run.", + "required": false, + "type": "string" + }, + "skip-tags": { + "description": "Specify tagged actions in the playbook to omit.", + "required": false, + "type": "string" + }, + "endpoint-selector": { + "description": "Remote AWX Server selector name.", + "required": true, + "type": "string" + } + } + } + } + } + }, + "derived_from": "tosca.nodes.Component" +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/application/pom.xml b/ms/blueprintsprocessor/application/pom.xml index a5d45277d..a2bb0907c 100755 --- a/ms/blueprintsprocessor/application/pom.xml +++ b/ms/blueprintsprocessor/application/pom.xml @@ -51,6 +51,10 @@ </dependency> <dependency> <groupId>org.onap.ccsdk.cds.blueprintsprocessor.functions</groupId> + <artifactId>ansible-awx-executor</artifactId> + </dependency> + <dependency> + <groupId>org.onap.ccsdk.cds.blueprintsprocessor.functions</groupId> <artifactId>netconf-executor</artifactId> </dependency> <dependency> diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/pom.xml b/ms/blueprintsprocessor/functions/ansible-awx-executor/pom.xml new file mode 100644 index 000000000..8eafc4abf --- /dev/null +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/pom.xml @@ -0,0 +1,64 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>functions</artifactId> + <groupId>org.onap.ccsdk.cds.blueprintsprocessor</groupId> + <version>0.5.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <groupId>org.onap.ccsdk.cds.blueprintsprocessor.functions</groupId> + <artifactId>ansible-awx-executor</artifactId> + <name>Blueprints Processor Function - Ansible AWX Executor</name> + <description>Blueprints Processor Function - Ansible Executor</description> + + <dependencies> + <dependency> + <groupId>org.python</groupId> + <artifactId>jython-standalone</artifactId> + </dependency> + <dependency> + <groupId>org.onap.ccsdk.cds.blueprintsprocessor</groupId> + <artifactId>rest-lib</artifactId> + </dependency> + <dependency> + <groupId>org.onap.ccsdk.cds.controllerblueprints</groupId> + <artifactId>blueprint-core</artifactId> + </dependency> + <dependency> + <groupId>org.onap.ccsdk.cds.blueprintsprocessor</groupId> + <artifactId>processor-core</artifactId> + </dependency> + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + <version>${apache.httpcomponents.client.version}</version> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-security</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + +</project> 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 new file mode 100644 index 000000000..6e8cdc243 --- /dev/null +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt @@ -0,0 +1,282 @@ +/* + * 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.functions.ansible.executor + +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.databind.node.ObjectNode +import java.net.URLEncoder +import java.util.NoSuchElementException +import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.* +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.* +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.config.ConfigurableBeanFactory +import org.springframework.context.annotation.Scope +import org.springframework.http.HttpMethod +import org.springframework.stereotype.Component + +/** + * ComponentRemoteAnsibleExecutor + * + * Component that launches a run of a job template (INPUT_JOB_TEMPLATE_NAME) representing an Ansible playbook, + * and its parameters, via the AWX server identified by the INPUT_ENDPOINT_SELECTOR parameter. + * + * It supports extra_vars, limit, tags, skip-tags, inventory (by name or Id) Ansible parameters. + * It reports the results of the execution via properties, named execute-command-status and execute-command-logs + * + * @author Serge Simard + */ +@Component("component-remote-ansible-executor") +@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) +open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService) + : AbstractComponentFunction() { + + private val log = LoggerFactory.getLogger(ComponentRemoteAnsibleExecutor::class.java)!! + + // HTTP related constants + private val HTTP_SUCCESS = 200..202 + private val GET = HttpMethod.GET.name + private val POST = HttpMethod.POST.name + + companion object { + // 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_LIMIT_TO_HOST = "limit" + const val INPUT_INVENTORY = "inventory" + const val INPUT_EXTRA_VARS = "extra-vars" + const val INPUT_TAGS = "tags" + const val INPUT_SKIP_TAGS = "skip-tags" + + // output fields names populated by this executor + const val ATTRIBUTE_EXEC_CMD_STATUS = "ansible-command-status" + const val ATTRIBUTE_EXEC_CMD_LOG = "ansible-command-logs" + + const val CHECKDELAY: Long = 10000 + } + + override suspend fun processNB(executionRequest: ExecutionServiceInput) { + + try { + val restClientService = getAWXRestClient() + + val jobTemplateName = getOperationInput(INPUT_JOB_TEMPLATE_NAME).asText() + val jtId = lookupJobTemplateIDByName(restClientService, jobTemplateName) + if (jtId.isNotEmpty()) { + runJobTemplateOnAWX(restClientService, jobTemplateName, jtId) + } else { + val message = "Job template ${jobTemplateName} does not exists" + log.error(message) + setNodeOutputErrors("Failed", message) + } + } catch (e: Exception) { + log.error("Failed to process on remote executor (${e.message})", e) + setNodeOutputErrors("Failed", "Failed to process on remote executor (${e.message})") + } + } + + + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) { + val message = "Error in ComponentRemoteAnsibleExecutor : ${runtimeException.message}" + log.error(message,runtimeException) + setNodeOutputErrors("Failed", message) + } + + /** Creates a TokenAuthRestClientService, since this executor expect type property to be "token-auth" and the + * token to be an OAuth token (access_token response field) generated via the AWX /api/o/token rest endpoint + * The token field is of the form "Bearer access_token_from_response", for example : + * "blueprintsprocessor.restclient.awx.type=token-auth" + * "blueprintsprocessor.restclient.awx.url=http://ipaddress" + * "blueprintsprocessor.restclient.awx.token=Bearer J9gEtMDqf7P4YsJ74fioY9VAhLDIs1" + */ + private fun getAWXRestClient(): BlueprintWebClientService { + + val endpointSelector = getOperationInput(INPUT_ENDPOINT_SELECTOR).asText()// TODO not asText + + try { + return blueprintRestLibPropertyService.blueprintWebClientService(endpointSelector) + } catch (e : NoSuchElementException) { + throw IllegalArgumentException("No value provided for input selector $endpointSelector", e) + } + } + + /** + * 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 { + val mapper = ObjectMapper() + + // Get Job Template details by name + var response = awxClient.exchangeResource(GET, "/api/v2/job_templates/${job_template_name}/", "") + val jtDetails: JsonNode = mapper.readTree(response.body) + return jtDetails.at("/id").asText() + } + + /** + * Performs the job template execution on AWX, ie. prepare arguments as per job template + * requirements (ask fields) and provided overriding values. Then it launches the run, and monitors + * 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) { + val mapper = ObjectMapper() + + setNodeOutputProperties( "preparing".asJsonPrimitive(), "".asJsonPrimitive()) + + // Get Job Template requirements + var response = awxClient.exchangeResource(GET, "/api/v2/job_templates/${jtId}/launch/","") + val jtLaunchReqs: JsonNode = mapper.readTree(response.body) + var payload = prepareLaunchPayload(awxClient, jtLaunchReqs) + log.info("Running job with $payload, for requestId $processId.") + + // Launch the job for the targeted template + var jtLaunched : JsonNode = JacksonUtils.jsonNode("{}") as ObjectNode + response = awxClient.exchangeResource(POST, "/api/v2/job_templates/${jtId}/launch/", payload) + if (response.status in HTTP_SUCCESS) { + jtLaunched = mapper.readTree(response.body) + val fieldsIgnored: JsonNode = jtLaunched.at("/ignored_fields") + if (fieldsIgnored.rootFieldsToMap().isNotEmpty()) { + log.warn("Ignored fields : $fieldsIgnored, for requestId $processId.") + } + } + + if (response.status in HTTP_SUCCESS) { + val jobId: String = jtLaunched.at("/id").asText() + + // Poll current job status while job is not executed + var jobStatus = "unknown" + var jobEndTime = "null" + while (jobEndTime == "null") { + response = awxClient.exchangeResource(GET, "/api/v2/jobs/${jobId}/", "") + val jobLaunched: JsonNode = mapper.readTree(response.body) + jobStatus = jobLaunched.at("/status").asText() + jobEndTime = jobLaunched.at("/finished").asText() + Thread.sleep(CHECKDELAY) + } + + 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) + + setNodeOutputProperties( jobStatus.asJsonPrimitive(), response.body.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, + // or resources_needed_to_start, in order to help user pinpoint the problems with the request. + val message = "Execution of job template $job_template_name could not be started for requestId $processId." + + " (Response: ${response.body}) " + log.error(message) + setNodeOutputErrors( response.status.toString(), message) + } + } + + /** + * 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 { + val payload = JacksonUtils.jsonNode("{}") as ObjectNode + + // Parameter defaults + val limitProp = getOptionalOperationInput(INPUT_LIMIT_TO_HOST)?.asText() + val tagsProp = getOptionalOperationInput(INPUT_TAGS)?.asText() + val skipTagsProp = getOptionalOperationInput(INPUT_SKIP_TAGS)?.asText() + val inventoryProp : String? = getOptionalOperationInput(INPUT_INVENTORY)?.asText() + val extraArgs : JsonNode = getOperationInput(INPUT_EXTRA_VARS) + + val askLimitOnLaunch = jtLaunchReqs.at( "/ask_limit_on_launch").asBoolean() + if (askLimitOnLaunch && limitProp!!.isNotEmpty()) { + payload.put(INPUT_LIMIT_TO_HOST, limitProp) + } + val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean() + if (askTagsOnLaunch && tagsProp!!.isNotEmpty()) { + payload.put(INPUT_TAGS, tagsProp) + } + if (askTagsOnLaunch && skipTagsProp!!.isNotEmpty()) { + payload.put("skip_tags", skipTagsProp) + } + val askInventoryOnLaunch = jtLaunchReqs.at("/ask_inventory_on_launch").asBoolean() + if (askInventoryOnLaunch && inventoryProp != null) { + var inventoryKeyId = inventoryProp.toIntOrNull() + if (inventoryKeyId == null) { + inventoryKeyId = resolveInventoryIdByName(awxClient, inventoryProp) + } + payload.put(INPUT_INVENTORY, inventoryKeyId) + } + val askVariablesOnLaunch = jtLaunchReqs.at("/ask_variables_on_launch").asBoolean() + if (askVariablesOnLaunch && extraArgs != null) { + payload.put("extra_vars", extraArgs) + } + + val strPayload = "$payload" + + return strPayload + } + + private fun resolveInventoryIdByName(awxClient : BlueprintWebClientService, inventoryProp: String): Int? { + var invId : Int? = null + + // Get Inventory by name + val encoded = URLEncoder.encode(inventoryProp) + val response = awxClient.exchangeResource(GET,"/api/v2/inventories/?name=$encoded","") + //, getRequestHeaders("awx")) + if (response.status in HTTP_SUCCESS) { + val mapper = ObjectMapper() + + // Extract the inventory ID from response + val invDetails = mapper.readTree(response.body) + val nbInvFound = invDetails.at("/count").asInt() + if (nbInvFound == 1) { + invId = invDetails["results"][0]["id"].asInt() + } + if (invId == null) { + log.error("Could not resolve inventory $inventoryProp by name...") + } else { + log.info("Resolved inventory $inventoryProp to ID #: $invId") + } + } + return invId + } + + /** + * Utility function to set the output properties of the executor node + */ + private fun setNodeOutputProperties(status: JsonNode, message: JsonNode) { + setAttribute(ATTRIBUTE_EXEC_CMD_STATUS, status) + log.info("Executor status: $status") + setAttribute(ATTRIBUTE_EXEC_CMD_LOG, message) + log.info("Executor message: $message") + } + + /** + * Utility function to set the output properties and errors of the executor node, in cas of errors + */ + private fun setNodeOutputErrors(status: String, message: String) { + setAttribute(ATTRIBUTE_EXEC_CMD_STATUS, status.asJsonPrimitive()) + setAttribute(ATTRIBUTE_EXEC_CMD_LOG, message.asJsonPrimitive()) + + addError("error", ATTRIBUTE_EXEC_CMD_LOG, "$status : $message") + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutorTest.kt b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutorTest.kt new file mode 100644 index 000000000..b7f1ed5f9 --- /dev/null +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutorTest.kt @@ -0,0 +1,243 @@ +/* + * 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.functions.ansible.executor + +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import io.mockk.every +import io.mockk.mockk +import kotlinx.coroutines.runBlocking +import org.junit.Ignore +import org.junit.Test +import org.junit.runner.RunWith +import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintProperties +import org.onap.ccsdk.cds.blueprintsprocessor.core.BlueprintPropertyConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput +import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData +import org.onap.ccsdk.cds.blueprintsprocessor.rest.BluePrintRestLibConfiguration +import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.putJsonElement +import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService +import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.autoconfigure.EnableAutoConfiguration +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.test.context.ContextConfiguration +import org.springframework.test.context.TestPropertySource +import org.springframework.test.context.junit4.SpringRunner + +@RunWith(SpringRunner::class) +@EnableAutoConfiguration(exclude = [DataSourceAutoConfiguration::class]) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) +@ContextConfiguration(classes = [BluePrintRestLibConfiguration::class, + BlueprintPropertyConfiguration::class, + BluePrintProperties::class, + BluePrintProperties::class]) +@TestPropertySource(properties = +[ + "server.port=8443", + "server.ssl.enabled=true", + "server.ssl.key-store=classpath:keystore.p12", + "server.ssl.key-store-password=changeit", + "server.ssl.keyStoreType=PKCS12", + "server.ssl.keyAlias=tomcat", + "blueprintsprocessor.restclient.awx.type=token-auth", + "blueprintsprocessor.restclient.awx.url=http://142.44.184.236", + "blueprintsprocessor.restclient.awx.token=Bearer J9gEtMDqf7P4YsJ74fioY9VAhLDIs1", + "blueprintsprocessor.restclient.future.keyStoreInstance=PKCS12", + "blueprintsprocessor.restclient.future.sslTrust=src/test/resources/keystore.p12", + "blueprintsprocessor.restclient.future.sslTrustPassword=changeit" +]) +class ComponentRemoteAnsibleExecutorTest { + + @Autowired + lateinit var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService + + @Transient + private val log = LoggerFactory.getLogger(ComponentRemoteAnsibleExecutorTest::class.java) + + @Test + @Ignore + fun testComponentRemoteAnsibleExecutor() { + runBlocking { + + val awxRemoteExecutor = ComponentRemoteAnsibleExecutor(bluePrintRestLibPropertyService) + + val executionServiceInput = JacksonUtils.readValueFromClassPathFile( + "payload/requests/sample-remote-ansible-request.json", + ExecutionServiceInput::class.java)!! + + log.info("Request Inputs : " + executionServiceInput.payload) + + val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("123456-1000", + "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_ansible") + awxRemoteExecutor.bluePrintRuntimeService = bluePrintRuntimeService + + val workflowName = executionServiceInput.actionIdentifiers.actionName + + // Assign Workflow inputs + val input = executionServiceInput.payload.get("$workflowName-request") + bluePrintRuntimeService.assignWorkflowInputs(workflowName, input) + + val stepMetaData: MutableMap<String, JsonNode> = hashMapOf() + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-ansible") + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemoteAnsibleExecutor") + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process") + + val stepInputData = StepData().apply { + name = "execute-remote-ansible" + properties = stepMetaData + } + executionServiceInput.stepData = stepInputData + + awxRemoteExecutor.applyNB(executionServiceInput) + } + } + + /** + * Test cases for Ansible executor to work with the process NB of remote + * executor. + */ + @Test + @Ignore + fun testComponentRemoteAnsibleExecutorProcessNB() { + runBlocking { + // val remoteScriptExecutionService = MockRemoteScriptExecutionService(bluePrintRestLibPropertyService) + val componentRemoteAnsibleExecutor = ComponentRemoteAnsibleExecutor(bluePrintRestLibPropertyService) + val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("123456-1000") + val input = getMockedOutput(bluePrintRuntime) + componentRemoteAnsibleExecutor.bluePrintRuntimeService = bluePrintRuntime + componentRemoteAnsibleExecutor.applyNB(input) + } + } + + /** + * Mocked input information for remote Ansible executor. + */ + fun getMockedOutput(svc: DefaultBluePrintRuntimeService): + ExecutionServiceInput { + val stepMetaData: MutableMap<String, JsonNode> = hashMapOf() + + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-ansible") + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemoteAnsibleExecutor") + stepMetaData.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process") + + val mapper = ObjectMapper() + val rootNode = mapper.createObjectNode() + rootNode.put("ip-address", "0.0.0.0") + rootNode.put("type", "rest") + + val operationalInputs: MutableMap<String, JsonNode> = hashMapOf() + operationalInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE, "execute-remote-ansible") + operationalInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_INTERFACE, "ComponentRemoteAnsibleExecutor") + operationalInputs.putJsonElement(BluePrintConstants.PROPERTY_CURRENT_OPERATION, "process") + operationalInputs.putJsonElement("endpoint-selector", "aai") +// operationalInputs.putJsonElement("dynamic-properties", rootNode) +// operationalInputs.putJsonElement("command", "./run.sh") + operationalInputs.putJsonElement("job-template-name", "CDS_job_template2") + + every { + svc.resolveNodeTemplateInterfaceOperationInputs( + "execute-remote-ansible", + "ComponentRemoteAnsibleExecutor", "process") + } returns operationalInputs + + val stepInputData = StepData().apply { + name = "execute-remote-ansible" + properties = stepMetaData + } + + val executionServiceInput = JacksonUtils + .readValueFromClassPathFile( + "payload/requests/sample-remote-ansible-request.json", + ExecutionServiceInput::class.java)!! + executionServiceInput.stepData = stepInputData + + val operationOutputs = hashMapOf<String, JsonNode>() + every { + svc.resolveNodeTemplateInterfaceOperationOutputs( + "execute-remote-ansible", + "ComponentRemoteAnsibleExecutor", "process") + } returns operationOutputs + val bluePrintRuntimeService = BluePrintMetadataUtils + .getBluePrintRuntime("123456-1000", + "./../../../../components/model-" + + "catalog/blueprint-model/test-blueprint/" + + "remote_ansible") +// every { +// svc.resolveNodeTemplateArtifactDefinition("execute-remote-ansible", "component-script") +// } returns bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition("execute-remote-ansible", +// "component-script") + every { + svc.setNodeTemplateAttributeValue( + "execute-remote-ansible", + "execute-command-status", + "successful".asJsonPrimitive()) + } returns Unit + + every { + svc.setNodeTemplateAttributeValue( + "execute-remote-ansible", + "execute-command-logs", "N/A".asJsonPrimitive()) + } returns Unit + + every { + svc.setNodeTemplateAttributeValue( + "execute-remote-ansible", + "execute-command-logs", + "processed successfully".asJsonPrimitive()) + } returns Unit + + every { + svc.bluePrintContext() + } returns bluePrintRuntimeService.bluePrintContext() + return executionServiceInput + } +} + +//class MockRemoteScriptExecutionService : RemoteScriptExecutionService { +// override suspend fun init(selector: String) { +// } +// +// override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput { +// assertEquals(prepareEnvInput.requestId, "123456-1000", "failed to match request id") +// assertNotNull(prepareEnvInput.packages, "failed to get packages") +// +// val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>() +// every { remoteScriptExecutionOutput.response } returns "prepared successfully" +// every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS +// return remoteScriptExecutionOutput +// } +// +// override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput { +// assertEquals(remoteExecutionInput.requestId, "123456-1000", "failed to match request id") +// +// val remoteScriptExecutionOutput = mockk<RemoteScriptExecutionOutput>() +// every { remoteScriptExecutionOutput.response } returns "processed successfully" +// every { remoteScriptExecutionOutput.status } returns StatusType.SUCCESS +// return remoteScriptExecutionOutput +// } +// +// override suspend fun close() { +// +// } +//} diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/keystore.p12 b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/keystore.p12 Binary files differnew file mode 100644 index 000000000..96b0d3ac3 --- /dev/null +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/keystore.p12 diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/logback-test.xml b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/logback-test.xml new file mode 100644 index 000000000..5b36ac046 --- /dev/null +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/logback-test.xml @@ -0,0 +1,35 @@ +<!--
+ ~ 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.
+ -->
+
+<configuration>
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <!-- encoders are assigned the type
+ ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
+ <encoder>
+ <pattern>%d{HH:mm:ss.SSS} %-5level %logger{55} - %msg%n</pattern>
+ </encoder>
+ </appender>
+
+
+ <logger name="org.springframework" level="warn"/>
+ <logger name="org.hibernate" level="info"/>
+ <logger name="org.onap.ccsdk.cds.blueprintsprocessor" level="info"/>
+
+ <root level="warn">
+ <appender-ref ref="STDOUT"/>
+ </root>
+
+</configuration>
diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/payload/requests/sample-remote-ansible-request.json b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/payload/requests/sample-remote-ansible-request.json new file mode 100644 index 000000000..6169da9c7 --- /dev/null +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/test/resources/payload/requests/sample-remote-ansible-request.json @@ -0,0 +1,29 @@ +{ + "actionIdentifiers": { + "blueprintName": "remote_ansible", + "blueprintVersion": "1.0.0", + "actionName": "execute-remote-ansible", + "mode": "sync" + }, + "commonHeader": { + "flags": { + "force": true, + "ttl": 3600 + }, + "originatorId": "mock", + "requestId": "123456-2000", + "subRequestId": "sub-123456-1000", + "timestamp": "2012-04-23T18:25:43.511Z" + }, + "payload": { + "execute-remote-ansible-request": { + "endpoint-selector": "awx", + "job-template-name": "hello_world_job_template", + "inventory": "Demo Inventory", + "extra-vars": { + "site_id": "3 - Belmont", + "tor_group": "vEPC" + } + } + } +} diff --git a/ms/blueprintsprocessor/functions/pom.xml b/ms/blueprintsprocessor/functions/pom.xml index a635c9f4c..9983b01a7 100755 --- a/ms/blueprintsprocessor/functions/pom.xml +++ b/ms/blueprintsprocessor/functions/pom.xml @@ -29,6 +29,7 @@ <description>Blueprints Processor Functions</description> <modules> <module>resource-resolution</module> + <module>ansible-awx-executor</module> <module>python-executor</module> <module>netconf-executor</module> <module>restconf-executor</module> diff --git a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt index a6bbc39d8..da4d9933f 100644 --- a/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt +++ b/ms/blueprintsprocessor/modules/commons/rest-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/rest/service/BluePrintRestLibPropertyService.kt @@ -56,6 +56,9 @@ open class BluePrintRestLibPropertyService(private var bluePrintProperties: RestLibConstants.TYPE_BASIC_AUTH -> { basicAuthRestClientProperties(prefix) } + RestLibConstants.TYPE_TOKEN_AUTH -> { + tokenRestClientProperties(prefix) + } RestLibConstants.TYPE_SSL_BASIC_AUTH -> { sslBasicAuthRestClientProperties(prefix) } @@ -141,6 +144,12 @@ open class BluePrintRestLibPropertyService(private var bluePrintProperties: } } + private fun tokenRestClientProperties(prefix: String): + TokenAuthRestClientProperties { + return bluePrintProperties.propertyBeanType( + prefix, TokenAuthRestClientProperties::class.java) + } + private fun basicAuthRestClientProperties(prefix: String): BasicAuthRestClientProperties { return bluePrintProperties.propertyBeanType( diff --git a/ms/blueprintsprocessor/parent/pom.xml b/ms/blueprintsprocessor/parent/pom.xml index 5f3e926f7..cbc19adc8 100755 --- a/ms/blueprintsprocessor/parent/pom.xml +++ b/ms/blueprintsprocessor/parent/pom.xml @@ -354,6 +354,11 @@ </dependency> <dependency> <groupId>org.onap.ccsdk.cds.blueprintsprocessor.functions</groupId> + <artifactId>ansible-awx-executor</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.onap.ccsdk.cds.blueprintsprocessor.functions</groupId> <artifactId>netconf-executor</artifactId> <version>${project.version}</version> </dependency> |