2 * Copyright © 2018-2019 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
21 import com.fasterxml.jackson.databind.node.ObjectNode
22 import io.mockk.coEvery
25 import kotlinx.coroutines.runBlocking
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ACTION_MODE_SYNC
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader
30 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
31 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StepData
32 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.createExecutionServiceOutputProto
33 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.createStatus
34 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.toProto
35 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
36 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
37 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
38 import org.onap.ccsdk.cds.controllerblueprints.core.data.Implementation
39 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.serviceTemplate
40 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
41 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
42 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
43 import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRuntimeService
44 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput
45 import kotlin.test.assertNotNull
47 class ComponentRemoteScriptExecutorTest {
50 fun testNodeComponentRemoteScriptExecutorType() {
51 val nodeType = BluePrintTypes.nodeTypeComponentRemoteScriptExecutor()
52 assertNotNull(nodeType, "failed to generate nodeType Component Remote Script Executor")
56 fun testNodeTemplateComponentRemoteScriptExecutor() {
58 val serviceTemplate = serviceTemplate("remote-script-dsl", "1.0.0", "xx@xx.com", "remote-script-ds") {
60 nodeTemplateComponentRemoteScriptExecutor(
62 "This is sample node template"
64 definedOperation(" Sample Operation") {
65 implementation(180, "SELF")
67 selector("remote-script-executor")
68 blueprintName("sample")
69 blueprintVersion("1.0.0")
70 blueprintAction("sample-action")
72 requestData("""{"key" :"value"}""")
80 nodeTypeComponentRemoteScriptExecutor()
82 // println(serviceTemplate.asJsonString(true))
83 assertNotNull(serviceTemplate, "failed to service template")
84 assertNotNull(serviceTemplate.nodeTypes, "failed to service template node Types")
86 serviceTemplate.nodeTypes!!["component-remote-script-executor"],
87 "failed to service template nodeType(component-remote-script-executor)"
90 serviceTemplate.topologyTemplate?.nodeTemplates?.get("remote-sample"),
91 "failed to nodeTemplate(remote-sample)"
96 fun testComponentRemoteScriptExecutor() {
98 /** Mock blueprint context */
99 val blueprintContext = mockk<BluePrintContext>()
100 every { blueprintContext.rootPath } returns normalizedPathName("target")
102 blueprintContext.nodeTemplateOperationImplementation(
103 "remote-execute", "ComponentRemoteScriptExecutor", "process"
105 } returns Implementation()
107 val bluePrintRuntime = mockk<DefaultBluePrintRuntimeService>("1234")
108 every { bluePrintRuntime.bluePrintContext() } returns blueprintContext
110 val mockExecutionServiceInput = mockExecutionServiceInput(bluePrintRuntime)
112 val mockStreamingRemoteExecutionService = mockk<StreamingRemoteExecutionService<
113 org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput,
114 ExecutionServiceOutput>>()
117 mockStreamingRemoteExecutionService.sendNonInteractive(
123 } returns mockExecutionServiceOutput(mockExecutionServiceInput)
125 val componentRemoteScriptExecutor = ComponentRemoteScriptExecutor(mockStreamingRemoteExecutionService)
126 componentRemoteScriptExecutor.bluePrintRuntimeService = bluePrintRuntime
127 componentRemoteScriptExecutor.implementation = Implementation()
128 val componentRemoteScriptExecutorOutput = componentRemoteScriptExecutor.applyNB(mockExecutionServiceInput)
129 assertNotNull(componentRemoteScriptExecutorOutput, "failed to get executor output")
133 private fun mockExecutionServiceInput(bluePrintRuntime: DefaultBluePrintRuntimeService): ExecutionServiceInput {
135 val mapper = ObjectMapper()
136 val requestNode = mapper.createObjectNode()
137 requestNode.put("ip-address", "0.0.0.0")
138 requestNode.put("type", "grpc")
140 val operationInputs = hashMapOf<String, JsonNode>()
141 operationInputs[BluePrintConstants.PROPERTY_CURRENT_NODE_TEMPLATE] = "remote-execute".asJsonPrimitive()
142 operationInputs[BluePrintConstants.PROPERTY_CURRENT_INTERFACE] =
143 "ComponentRemoteScriptExecutor".asJsonPrimitive()
144 operationInputs[BluePrintConstants.PROPERTY_CURRENT_OPERATION] = "process".asJsonPrimitive()
146 operationInputs[ComponentRemoteScriptExecutor.INPUT_SELECTOR] = "remote-script-executor".asJsonPrimitive()
147 operationInputs[ComponentRemoteScriptExecutor.INPUT_BLUEPRINT_NAME] = "sample-blueprint".asJsonPrimitive()
148 operationInputs[ComponentRemoteScriptExecutor.INPUT_BLUEPRINT_VERSION] = "1.0.0".asJsonPrimitive()
149 operationInputs[ComponentRemoteScriptExecutor.INPUT_BLUEPRINT_ACTION] = "remote-activate".asJsonPrimitive()
150 operationInputs[ComponentRemoteScriptExecutor.INPUT_TIMEOUT] = 120.asJsonPrimitive()
151 operationInputs[ComponentRemoteScriptExecutor.INPUT_REQUEST_DATA] = requestNode
153 val stepInputData = StepData().apply {
154 name = "remote-execute"
155 properties = operationInputs
157 val executionServiceInput = ExecutionServiceInput().apply {
158 commonHeader = CommonHeader().apply {
160 subRequestId = "1234-123"
161 originatorId = "test-client"
163 actionIdentifiers = ActionIdentifiers().apply {
164 blueprintName = "sample-blueprint"
165 blueprintVersion = "1.0.0"
166 actionName = "remote-activate"
167 mode = ACTION_MODE_SYNC
169 payload = """{}""".jsonAsJsonType() as ObjectNode
171 executionServiceInput.stepData = stepInputData
174 bluePrintRuntime.resolveNodeTemplateInterfaceOperationInputs(
175 "remote-execute", "ComponentRemoteScriptExecutor", "process"
177 } returns operationInputs
179 /** Mock Set Attributes */
181 bluePrintRuntime.setNodeTemplateAttributeValue(
182 "remote-execute", any(), any()
186 val operationOutputs = hashMapOf<String, JsonNode>()
188 bluePrintRuntime.resolveNodeTemplateInterfaceOperationOutputs(
189 "remote-execute", "ComponentRemoteScriptExecutor", "process"
191 } returns operationOutputs
193 return executionServiceInput
196 private fun mockExecutionServiceOutput(executionServiceInput: ExecutionServiceInput): ExecutionServiceOutput {
197 val actionName = executionServiceInput.actionIdentifiers.actionName
198 val responsePayload = """
200 "$actionName-response" :{
205 return createExecutionServiceOutputProto(
206 executionServiceInput.commonHeader.toProto(),
207 executionServiceInput.actionIdentifiers.toProto(),
208 createStatus(BluePrintConstants.STATUS_SUCCESS, 200),