From 307341793d257fd1a673699d97855c9b10f69fcc Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Tue, 30 Jul 2019 13:45:07 -0400 Subject: Add missing implementation and Operation Type. Change-Id: I739ad054fafc0c302fb6ad03999561f2b3cf9652 Issue-ID: CCSDK-1380 Signed-off-by: Brinda Santh --- .../kotlin/internal/scripts/InternalSimpleCli.kt | 2 +- .../cli/executor/ComponentCliExecutorTest.kt | 6 +- .../executor/ComponentRestconfExecutorTest.kt | 4 +- .../services/execution/ComponentScriptExecutor.kt | 160 ++------------------- .../execution/ComponentScriptExecutorDSL.kt | 123 ++++++++++++++++ .../scripts/AbstractComponentFunctionTest.kt | 6 +- .../core/dsl/AbstractNodeTemplateImplBuilder.kt | 26 ++++ .../core/dsl/BluePrintTemplateDSLBuilder.kt | 101 +++++++++---- .../core/dsl/BluePrintDSLTest.kt | 57 +++++++- 9 files changed, 298 insertions(+), 187 deletions(-) create mode 100644 ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentScriptExecutorDSL.kt create mode 100644 ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/AbstractNodeTemplateImplBuilder.kt (limited to 'ms') diff --git a/ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/internal/scripts/InternalSimpleCli.kt b/ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/internal/scripts/InternalSimpleCli.kt index 4c1d92ae6..cf27cc2de 100644 --- a/ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/internal/scripts/InternalSimpleCli.kt +++ b/ms/blueprintsprocessor/functions/cli-executor/src/main/kotlin/internal/scripts/InternalSimpleCli.kt @@ -71,7 +71,7 @@ open class Check : AbstractScriptComponentFunction() { sshClientService.closeSessionNB() // Set the Response Data - setAttribute(ComponentScriptExecutor.RESPONSE_DATA, responseLog.asJsonPrimitive()) + setAttribute(ComponentScriptExecutor.ATTRIBUTE_RESPONSE_DATA, responseLog.asJsonPrimitive()) log.info("Executing process") } diff --git a/ms/blueprintsprocessor/functions/cli-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/ComponentCliExecutorTest.kt b/ms/blueprintsprocessor/functions/cli-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/ComponentCliExecutorTest.kt index 30e7c8906..9c0258db0 100644 --- a/ms/blueprintsprocessor/functions/cli-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/ComponentCliExecutorTest.kt +++ b/ms/blueprintsprocessor/functions/cli-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/cli/executor/ComponentCliExecutorTest.kt @@ -34,11 +34,11 @@ import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ExecutionServic import org.onap.ccsdk.cds.blueprintsprocessor.ssh.BluePrintSshLibConfiguration import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils -import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl import org.springframework.beans.factory.annotation.Autowired import org.springframework.test.annotation.DirtiesContext import org.springframework.test.context.ContextConfiguration @@ -79,8 +79,8 @@ class ComponentCliExecutorTest { operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "activate-cli".asJsonPrimitive() operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = "interfaceName".asJsonPrimitive() operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "operationName".asJsonPrimitive() - operationInputs[ComponentScriptExecutor.SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive() - operationInputs[ComponentScriptExecutor.SCRIPT_CLASS_REFERENCE] = + operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive() + operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE] = "internal.scripts.TestCliScriptFunction".asJsonPrimitive() val stepInputData = StepData().apply { diff --git a/ms/blueprintsprocessor/functions/restconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/ComponentRestconfExecutorTest.kt b/ms/blueprintsprocessor/functions/restconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/ComponentRestconfExecutorTest.kt index fb47b6ad1..2b2b83e4c 100644 --- a/ms/blueprintsprocessor/functions/restconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/ComponentRestconfExecutorTest.kt +++ b/ms/blueprintsprocessor/functions/restconf-executor/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/restconf/executor/ComponentRestconfExecutorTest.kt @@ -79,8 +79,8 @@ class ComponentRestconfExecutorTest { operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "activate-restconf".asJsonPrimitive() operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] = "interfaceName".asJsonPrimitive() operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "operationName".asJsonPrimitive() - operationInputs[ComponentScriptExecutor.SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive() - operationInputs[ComponentScriptExecutor.SCRIPT_CLASS_REFERENCE] = + operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_TYPE] = BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive() + operationInputs[ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE] = "internal.scripts.TestRestconfConfigure".asJsonPrimitive() val stepInputData = StepData().apply { 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 index 95b2afc4c..c66c3e913 100644 --- 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 @@ -16,16 +16,8 @@ package org.onap.ccsdk.cds.blueprintsprocessor.services.execution -import com.fasterxml.jackson.databind.JsonNode import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput -import org.onap.ccsdk.cds.controllerblueprints.core.* -import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition -import org.onap.ccsdk.cds.controllerblueprints.core.data.Implementation -import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate -import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType -import org.onap.ccsdk.cds.controllerblueprints.core.dsl.ArtifactDefinitionBuilder -import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeTemplate -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 @@ -40,19 +32,23 @@ open class ComponentScriptExecutor(private var 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" + const val INPUT_SCRIPT_TYPE = "script-type" + const val INPUT_SCRIPT_CLASS_REFERENCE = "script-class-reference" + const val INPUT_DYNAMIC_PROPERTIES = "dynamic-properties" + + const val ATTRIBUTE_RESPONSE_DATA = "response-data" + const val ATTRIBUTE_STATUS = "status" + + const val OUTPUT_RESPONSE_DATA = "response-data" + const val OUTPUT_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 scriptType = operationInputs.getAsString(INPUT_SCRIPT_TYPE) + val scriptClassReference = operationInputs.getAsString(INPUT_SCRIPT_CLASS_REFERENCE) val scriptDependencies: MutableList = arrayListOf() populateScriptDependencies(scriptDependencies) @@ -73,136 +69,4 @@ open class ComponentScriptExecutor(private var componentFunctionScriptingService open fun populateScriptDependencies(scriptDependencies: MutableList) { /** 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_NODE_COMPONENT, - 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) - constrain { - 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 )") - } - } - } -} - -/** Component Builder */ - -fun componentScriptExecutor(id: String, description: String, - block: ComponentScriptExecutorBuilder.() -> Unit): NodeTemplate { - return ComponentScriptExecutorBuilder(id, description).apply(block).build() -} - -class ComponentScriptExecutorBuilder(private val id: String, private val description: String) { - private var implementation: Implementation? = null - private var inputs: MutableMap? = null - private var outputs: MutableMap? = null - private var artifacts: MutableMap? = null - - fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) { - val implementation = Implementation().apply { - this.operationHost = operationHost!! - this.timeout = timeout - } - this.implementation = implementation - } - - fun inputs(block: InputAssignmentBuilder.() -> Unit) { - this.inputs = InputAssignmentBuilder().apply(block).build() - } - - fun outputs(block: OutputAssignmentBuilder.() -> Unit) { - this.outputs = OutputAssignmentBuilder().apply(block).build() - } - - fun artifact(id: String, type: String, file: String) { - if (artifacts == null) - artifacts = hashMapOf() - artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build() - } - - fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) { - if (artifacts == null) - artifacts = hashMapOf() - artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build() - } - - fun build(): NodeTemplate { - return nodeTemplate(id, "component-script-executor", description) { - operation("ComponentScriptExecutor") { - implementation(implementation) - inputs(inputs) - outputs(outputs) - } - artifacts(artifacts) - } - } - - class InputAssignmentBuilder { - val properties: MutableMap = hashMapOf() - - fun type(type: String) { - properties[ComponentScriptExecutor.SCRIPT_TYPE] = type.asJsonPrimitive() - } - - fun scriptClassReference(scriptClassReference: String) { - properties[ComponentScriptExecutor.SCRIPT_CLASS_REFERENCE] = scriptClassReference.asJsonPrimitive() - } - - fun dynamicProperty(dynamicProperty: Any) { - dynamicProperty(dynamicProperty.asJsonType()) - } - - fun dynamicProperty(dynamicProperty: JsonNode) { - properties[ComponentScriptExecutor.DYNAMIC_PROPERTIES] = dynamicProperty - } - - fun build(): MutableMap { - return properties - } - } - - class OutputAssignmentBuilder { - val properties: MutableMap = hashMapOf() - - fun status(status: String) { - properties[ComponentScriptExecutor.STATUS] = status.asJsonPrimitive() - } - - fun responseData(responseData: Any) { - responseData(responseData.asJsonType()) - } - - fun responseData(responseData: JsonNode) { - properties[ComponentScriptExecutor.RESPONSE_DATA] = responseData - } - - fun build(): MutableMap { - return properties - } - } } \ 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/ComponentScriptExecutorDSL.kt b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentScriptExecutorDSL.kt new file mode 100644 index 000000000..1b905fa60 --- /dev/null +++ b/ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentScriptExecutorDSL.kt @@ -0,0 +1,123 @@ +/* + * 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 com.fasterxml.jackson.databind.JsonNode +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.asJsonType +import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate +import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType +import org.onap.ccsdk.cds.controllerblueprints.core.dsl.AbstractNodeTemplateImplBuilder +import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder +import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType + +/** Component Extensions **/ + +fun BluePrintTypes.nodeTypeComponentScriptExecutor(): NodeType { + return nodeType(id = "component-script-executor", version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, + description = "Generic Script Component Executor") { + attribute(ComponentScriptExecutor.ATTRIBUTE_RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON, false) + attribute(ComponentScriptExecutor.ATTRIBUTE_STATUS, BluePrintConstants.DATA_TYPE_STRING, true) + + operation("ComponentScriptExecutor", "ComponentScriptExecutor Operation") { + inputs { + property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, BluePrintConstants.DATA_TYPE_STRING, + true, "Script Type") { + defaultValue(BluePrintConstants.SCRIPT_INTERNAL) + constrain { + validValues(listOf(BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive(), + BluePrintConstants.SCRIPT_JYTHON.asJsonPrimitive(), + BluePrintConstants.SCRIPT_KOTLIN.asJsonPrimitive())) + } + } + property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, BluePrintConstants.DATA_TYPE_STRING, + true, "Kotlin Script class name or jython script name.") + property(ComponentScriptExecutor.INPUT_DYNAMIC_PROPERTIES, BluePrintConstants.DATA_TYPE_JSON, + false, "Dynamic Json Content or DSL Json reference.") + } + outputs { + property(ComponentScriptExecutor.OUTPUT_RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON, + false, "Output Response") + property(ComponentScriptExecutor.OUTPUT_STATUS, BluePrintConstants.DATA_TYPE_STRING, + true, "Status of the Component Execution ( success or failure )") + } + } + } +} + +/** Component Builder */ +fun BluePrintTypes.nodeTemplateComponentScriptExecutor(id: String, + description: String, + block: ComponentScriptExecutorNodeTemplateImplBuilder.() -> Unit) + : NodeTemplate { + return ComponentScriptExecutorNodeTemplateImplBuilder(id, description).apply(block).build() +} + +class ComponentScriptExecutorNodeTemplateImplBuilder(id: String, description: String) : + AbstractNodeTemplateImplBuilder(id, "component-script-executor", + "ComponentScriptExecutor", + description) + +class ComponentScriptExecutorInputAssignmentBuilder : PropertiesAssignmentBuilder() { + + fun type(type: String) { + type(type.asJsonPrimitive()) + } + + fun type(type: JsonNode) { + property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, type) + } + + fun scriptClassReference(scriptClassReference: String) { + scriptClassReference(scriptClassReference.asJsonPrimitive()) + } + + fun scriptClassReference(scriptClassReference: JsonNode) { + property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, scriptClassReference) + } + + fun dynamicProperty(dynamicProperty: String) { + dynamicProperty(dynamicProperty.asJsonType()) + } + + fun dynamicProperty(dynamicProperty: JsonNode) { + property(ComponentScriptExecutor.INPUT_DYNAMIC_PROPERTIES, dynamicProperty) + } +} + +class ComponentScriptExecutorOutputAssignmentBuilder : PropertiesAssignmentBuilder() { + + fun status(status: String) { + status(status.asJsonPrimitive()) + } + + fun status(status: JsonNode) { + property(ComponentScriptExecutor.OUTPUT_STATUS, status) + } + + fun responseData(responseData: String) { + responseData(responseData.asJsonType()) + } + + fun responseData(responseData: JsonNode) { + property(ComponentScriptExecutor.OUTPUT_RESPONSE_DATA, responseData) + } +} \ 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 309db9df4..07be8c809 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,15 +34,15 @@ 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.blueprintsprocessor.services.execution.nodeTypeComponentScriptExecutor 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.scripts.BluePrintScriptsServiceImpl import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils -import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl import org.springframework.beans.factory.annotation.Autowired import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.junit4.SpringRunner @@ -187,7 +187,7 @@ class AbstractComponentFunctionTest { @Test fun testComponentScriptExecutorNodeType() { - val componentScriptExecutor = BluePrintTypes.componentScriptExecutor() + val componentScriptExecutor = BluePrintTypes.nodeTypeComponentScriptExecutor() assertNotNull(componentScriptExecutor.interfaces, "failed to get interface operations") } } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/AbstractNodeTemplateImplBuilder.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/AbstractNodeTemplateImplBuilder.kt new file mode 100644 index 000000000..7ab139070 --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/AbstractNodeTemplateImplBuilder.kt @@ -0,0 +1,26 @@ +/* + * 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.controllerblueprints.core.dsl + +abstract class AbstractNodeTemplateImplBuilder( + id: String, type: String, private val interfaceName: String, description: String +) : NodeTemplateBuilder(id, type, description) { + + open fun operation(description: String, block: OperationAssignmentBuilder.() -> Unit) { + typedOperation(interfaceName, description, block) + } +} diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTemplateDSLBuilder.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTemplateDSLBuilder.kt index 93b6f4e4e..b3aaabe38 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTemplateDSLBuilder.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTemplateDSLBuilder.kt @@ -20,6 +20,9 @@ import com.fasterxml.jackson.databind.JsonNode import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType import org.onap.ccsdk.cds.controllerblueprints.core.data.* +import kotlin.reflect.KClass +import kotlin.reflect.full.createInstance +import kotlin.reflect.jvm.reflect class TopologyTemplateBuilder { private var topologyTemplate = TopologyTemplate() @@ -39,7 +42,8 @@ class TopologyTemplateBuilder { } fun nodeTemplateOperation(nodeTemplateName: String, type: String, interfaceName: String, description: String, - operationBlock: OperationAssignmentBuilder.() -> Unit) { + operationBlock: OperationAssignmentBuilder.() -> Unit) { if (nodeTemplates == null) nodeTemplates = hashMapOf() @@ -84,9 +88,9 @@ class TopologyTemplateBuilder { } } -class NodeTemplateBuilder(private val id: String, - private val type: String, - private val description: String? = "") { +open class NodeTemplateBuilder(private val id: String, + private val type: String, + private val description: String? = "") { private var nodeTemplate: NodeTemplate = NodeTemplate() private var properties: MutableMap? = null private var interfaces: MutableMap? = null @@ -94,14 +98,19 @@ class NodeTemplateBuilder(private val id: String, private var capabilities: MutableMap? = null private var requirements: MutableMap? = null + fun properties(properties: MutableMap?) { + this.properties = properties + } + fun properties(block: PropertiesAssignmentBuilder.() -> Unit) { if (properties == null) properties = hashMapOf() properties = PropertiesAssignmentBuilder().apply(block).build() } - fun operation(interfaceName: String, description: String? = "", - block: OperationAssignmentBuilder.() -> Unit) { + open fun typedOperation( + interfaceName: String, description: String = "", + block: OperationAssignmentBuilder.() -> Unit) { if (interfaces == null) interfaces = hashMapOf() @@ -109,10 +118,15 @@ class NodeTemplateBuilder(private val id: String, val defaultOperationName = BluePrintConstants.DEFAULT_STEP_OPERATION interfaceAssignment.operations = hashMapOf() interfaceAssignment.operations!![defaultOperationName] = - OperationAssignmentBuilder(defaultOperationName, description).apply(block).build() + OperationAssignmentBuilder(defaultOperationName, description).apply(block).build() interfaces!![interfaceName] = interfaceAssignment } + fun operation(interfaceName: String, description: String, + block: OperationAssignmentBuilder.() -> Unit) { + typedOperation(interfaceName, description, block) + } + fun artifact(id: String, type: String, file: String) { if (artifacts == null) artifacts = hashMapOf() @@ -190,10 +204,10 @@ class ArtifactDefinitionBuilder(private val id: String, private val type: String } } -class CapabilityAssignmentBuilder(private val id: String) { - private var capabilityAssignment: CapabilityAssignment = CapabilityAssignment() - private var attributes: MutableMap? = null - private var properties: MutableMap? = null +open class CapabilityAssignmentBuilder(private val id: String) { + var capabilityAssignment: CapabilityAssignment = CapabilityAssignment() + var attributes: MutableMap? = null + var properties: MutableMap? = null fun attributes(block: AttributesAssignmentBuilder.() -> Unit) { if (attributes == null) @@ -214,9 +228,9 @@ class CapabilityAssignmentBuilder(private val id: String) { } } -class RequirementAssignmentBuilder(private val id: String, private val capability: String, - private val node: String, - private val relationship: String) { +open class RequirementAssignmentBuilder(private val id: String, private val capability: String, + private val node: String, + private val relationship: String) { private var requirementAssignment: RequirementAssignment = RequirementAssignment() fun build(): RequirementAssignment { @@ -233,10 +247,12 @@ class InterfaceAssignmentBuilder(private val id: String) { private var interfaceAssignment: InterfaceAssignment = InterfaceAssignment() private var operations: MutableMap? = null - fun operation(id: String, description: String? = "", block: OperationAssignmentBuilder.() -> Unit) { + fun operation(id: String, description: String? = "", + block: OperationAssignmentBuilder.() -> Unit) { if (operations == null) operations = hashMapOf() - operations!![id] = OperationAssignmentBuilder(id, description).apply(block).build() + operations!![id] = OperationAssignmentBuilder( + id, description).apply(block).build() } fun build(): InterfaceAssignment { @@ -246,8 +262,9 @@ class InterfaceAssignmentBuilder(private val id: String) { } } -class OperationAssignmentBuilder(private val id: String, - private val description: String? = "") { +class OperationAssignmentBuilder( + private val id: String, + private val description: String? = "") { private var operationAssignment: OperationAssignment = OperationAssignment() @@ -256,27 +273,33 @@ class OperationAssignmentBuilder(private val id: String, } fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) { - val implementation = Implementation().apply { + operationAssignment.implementation = Implementation().apply { this.operationHost = operationHost!! this.timeout = timeout } - operationAssignment.implementation = implementation + } + + fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF, + block: ImplementationBuilder.() -> Unit) { + operationAssignment.implementation = ImplementationBuilder(timeout, operationHost!!).apply(block).build() } fun inputs(inputs: MutableMap?) { operationAssignment.inputs = inputs } - fun inputs(block: PropertiesAssignmentBuilder.() -> Unit) { - operationAssignment.inputs = PropertiesAssignmentBuilder().apply(block).build() + fun inputs(block: In.() -> Unit) { + val instance: In = (block.reflect()?.parameters?.get(0)?.type?.classifier as KClass).createInstance() + operationAssignment.inputs = instance.apply(block).build() } fun outputs(outputs: MutableMap?) { operationAssignment.outputs = outputs } - fun outputs(block: PropertiesAssignmentBuilder.() -> Unit) { - operationAssignment.outputs = PropertiesAssignmentBuilder().apply(block).build() + fun outputs(block: Out.() -> Unit) { + val instance: Out = (block.reflect()?.parameters?.get(0)?.type?.classifier as KClass).createInstance() + operationAssignment.outputs = instance.apply(block).build() } fun build(): OperationAssignment { @@ -286,8 +309,30 @@ class OperationAssignmentBuilder(private val id: String, } } -class PropertiesAssignmentBuilder { - private var properties: MutableMap = hashMapOf() +class ImplementationBuilder(private val timeout: Int, private val operationHost: String) { + private val implementation = Implementation() + + fun primary(primary: String) { + implementation.primary = primary + } + + fun dependencies(vararg dependencies: String) { + if (implementation.dependencies == null) + implementation.dependencies = arrayListOf() + dependencies.forEach { + implementation.dependencies!!.add(it) + } + } + + fun build(): Implementation { + implementation.timeout = timeout + implementation.operationHost = operationHost + return implementation + } +} + +open class PropertiesAssignmentBuilder { + var properties: MutableMap = hashMapOf() fun property(id: String, value: Any) { property(id, value.asJsonType()) @@ -302,8 +347,8 @@ class PropertiesAssignmentBuilder { } } -class AttributesAssignmentBuilder { - private var attributes: MutableMap = hashMapOf() +open class AttributesAssignmentBuilder { + var attributes: MutableMap = hashMapOf() fun attribute(id: String, value: String) { attribute(id, value.asJsonType()) diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLTest.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLTest.kt index c0641be69..e5f1cbf5d 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLTest.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLTest.kt @@ -16,9 +16,12 @@ package org.onap.ccsdk.cds.controllerblueprints.core.dsl +import com.fasterxml.jackson.databind.JsonNode import org.junit.Test import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString +import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType import kotlin.test.assertNotNull @@ -94,6 +97,10 @@ class BluePrintDSLTest { topologyTemplate { nodeTemplateOperation(nodeTemplateName = "activate", type = "sample-node-type", interfaceName = "RestconfExecutor", description = "sample activation") { + implementation(360, "SELF") { + primary("Scripts/sample.py") + dependencies("one", "two") + } inputs { property("json-content", """{ "name" : "cds"}""") property("array-content", """["controller", "blueprints"]""") @@ -150,7 +157,7 @@ class BluePrintDSLTest { assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template") assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates, "failed to get nodeTypes") assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates!!["activate"], "failed to get nodeTypes(activate)") - //println(serviceTemplate.asJsonString(true)) + println(serviceTemplate.asJsonString(true)) } @Test @@ -185,7 +192,7 @@ class BluePrintDSLTest { } } assertNotNull(nodeType, "failed to get nodeType") - // println(nodeType.asJsonString(true)) + // println(nodeType.asJsonString(true)) } @Test @@ -207,4 +214,50 @@ class BluePrintDSLTest { //println(serviceTemplate.asJsonString(true)) } + @Test + fun testNodeTemplateOperationTypes() { + + val testNodeTemplateInstance = BluePrintTypes.nodeTemplateComponentTestExecutor(id = "test-node-template", + description = "") { + operation("") { + implementation(360) + inputs { + request("i am request") + } + outputs { + response(getAttribute("attribute1")) + } + } + } + assertNotNull(testNodeTemplateInstance, "failed to get test node template") + //println(testNodeTemplateInstance.asJsonString(true)) + } +} + +fun BluePrintTypes.nodeTemplateComponentTestExecutor(id: String, + description: String, + block: TestNodeTemplateImplBuilder.() -> Unit) + : NodeTemplate { + return TestNodeTemplateImplBuilder(id, description).apply(block).build() +} + +class TestNodeTemplateImplBuilder(id: String, description: String) : + AbstractNodeTemplateImplBuilder(id, "component-test-executor", + "ComponentTestExecutor", + description) + +class TestInput : PropertiesAssignmentBuilder() { + fun request(request: String) { + property("request", request.asJsonPrimitive()) + } +} + +class TestOutput : PropertiesAssignmentBuilder() { + fun response(response: String) { + response(response.asJsonPrimitive()) + } + + fun response(response: JsonNode) { + property("response", response) + } } -- cgit 1.2.3-korg