Refractor controller blueprint modules
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / BluePrintRuntimeServiceTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018-2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core.service
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.node.NullNode
22 import org.junit.Test
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.TestConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
26 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintRuntimeUtils
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.slf4j.LoggerFactory
31 import kotlin.test.assertEquals
32 import kotlin.test.assertNotNull
33
34 /**
35  *
36  *
37  * @author Brinda Santh
38  */
39 class BluePrintRuntimeServiceTest {
40
41     private val log = LoggerFactory.getLogger(this::class.toString())
42
43     @Test
44     fun `test Resolve NodeTemplate Properties`() {
45         log.info("************************ testResolveNodeTemplateProperties **********************")
46
47         val bluePrintRuntimeService = getBluePrintRuntimeService()
48
49         val inputDataPath = "src/test/resources/data/default-context.json"
50
51         val inputNode: JsonNode = JacksonUtils.jsonNodeFromFile(inputDataPath)
52         bluePrintRuntimeService.assignInputs(inputNode)
53
54         val propContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
55             .resolveNodeTemplateProperties("activate-process")
56
57         assertNotNull(propContext, "Failed to populate interface property values")
58     }
59
60     @Test
61     fun `test resolve NodeTemplate Capability Properties`() {
62         log.info("************************ testResolveNodeTemplateRequirementProperties **********************")
63         val bluePrintRuntimeService = getBluePrintRuntimeService()
64
65         val executionContext = bluePrintRuntimeService.getExecutionContext()
66
67         BluePrintRuntimeUtils.assignInputsFromClassPathFile(
68             bluePrintRuntimeService.bluePrintContext(),
69             "data/default-context.json", executionContext
70         )
71
72         val assignmentParams = "{\n" +
73                 "            \"ipAddress\": \"127.0.0.1\",\n" +
74                 "            \"hostName\": \"vnf-host\"\n" +
75                 "          }"
76
77         bluePrintRuntimeService.setNodeTemplateAttributeValue(
78             "resource-assignment", "assignment-params",
79             JacksonUtils.jsonNode(assignmentParams)
80         )
81
82         val capProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(
83             "sample-netconf-device",
84             "netconf"
85         )
86         assertNotNull(capProperties, "Failed to populate capability property values")
87         assertEquals(capProperties["target-ip-address"], "127.0.0.1".asJsonPrimitive(), "Failed to populate parameter target-ip-address")
88         assertEquals(capProperties["port-number"], JacksonUtils.jsonNodeFromObject(830), "Failed to populate parameter port-number")
89     }
90
91     @Test
92     fun `test Resolve NodeTemplate Interface Operation Inputs`() {
93         log.info("************************ testResolveNodeTemplateInterfaceOperationInputs **********************")
94
95         val bluePrintRuntimeService = getBluePrintRuntimeService()
96
97         val executionContext = bluePrintRuntimeService.getExecutionContext()
98
99         BluePrintRuntimeUtils.assignInputsFromClassPathFile(
100             bluePrintRuntimeService.bluePrintContext(),
101             "data/default-context.json", executionContext
102         )
103
104         val inContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
105             .resolveNodeTemplateInterfaceOperationInputs(
106                 "resource-assignment",
107                 "ResourceResolutionComponent", "process"
108             )
109
110         assertNotNull(inContext, "Failed to populate interface input property values")
111         assertEquals(inContext["action-name"], JacksonUtils.jsonNodeFromObject("sample-action"), "Failed to populate parameter action-name")
112         assertEquals(inContext["request-id"], JacksonUtils.jsonNodeFromObject("12345"), "Failed to populate parameter action-name")
113     }
114
115     @Test
116     fun `test Resolve NodeTemplate Interface Operation Outputs`() {
117         log.info("************************ testResolveNodeTemplateInterfaceOperationOutputs **********************")
118
119         val bluePrintRuntimeService = getBluePrintRuntimeService()
120
121         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params", NullNode.getInstance())
122
123         bluePrintRuntimeService.resolveNodeTemplateInterfaceOperationOutputs(
124             "resource-assignment",
125             "ResourceResolutionComponent", "process"
126         )
127
128         val outputStatus = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
129             "resource-assignment",
130             "ResourceResolutionComponent", "process", "status"
131         )
132         assertEquals("success".asJsonPrimitive(), outputStatus, "Failed to get operation property status")
133
134         val outputParams = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
135             "resource-assignment",
136             "ResourceResolutionComponent", "process", "resource-assignment-params"
137         )
138         assertEquals(NullNode.getInstance(), outputParams, "Failed to get operation property resource-assignment-params")
139     }
140
141     @Test
142     fun `test NodeTemplate Context Property`() {
143         log.info("************************ testNodeTemplateContextProperty **********************")
144         val bluePrintRuntimeService = getBluePrintRuntimeService()
145
146         bluePrintRuntimeService.setNodeTemplateAttributeValue(
147             "resource-assignment-ra-component", "context1",
148             JacksonUtils.jsonNodeFromObject("context1-value")
149         )
150         bluePrintRuntimeService.setNodeTemplateAttributeValue(
151             "resource-assignment-ra-component", "context2",
152             JacksonUtils.jsonNodeFromObject("context2-value")
153         )
154
155         val keys = listOf("context1", "context2")
156
157         val jsonValueNode = bluePrintRuntimeService.getJsonForNodeTemplateAttributeProperties("resource-assignment-ra-component", keys)
158         assertNotNull(jsonValueNode, "Failed to get Json for Node Template Context Properties")
159         log.info("JSON Prepared Value Context {}", jsonValueNode)
160     }
161
162     @Test
163     fun `test Resolve DSL Properties`() {
164         log.info("************************ resolveDSLExpression **********************")
165
166         val bluePrintRuntimeService = getBluePrintRuntimeService()
167
168         bluePrintRuntimeService.setInputValue("rest-user-name", "sample-username".asJsonPrimitive())
169
170         val resolvedJsonNode: JsonNode = bluePrintRuntimeService.resolveDSLExpression("dynamic-rest-source")
171         assertNotNull(resolvedJsonNode, "Failed to populate dsl property values")
172     }
173
174     @Test
175     fun `test Resolve Workflow Outputs`() {
176         log.info("************************ resolvePropertyAssignments **********************")
177         val bluePrintRuntimeService = getBluePrintRuntimeService()
178
179         val assignmentParams = "{\"ipAddress\": \"127.0.0.1\", \"hostName\": \"vnf-host\"}"
180
181         bluePrintRuntimeService.setNodeTemplateAttributeValue(
182             "resource-assignment", "assignment-params",
183             JacksonUtils.jsonNode(assignmentParams)
184         )
185
186         val resolvedJsonNode = bluePrintRuntimeService.resolveWorkflowOutputs("resource-assignment")
187         assertNotNull(resolvedJsonNode, "Failed to populate workflow output property values")
188     }
189
190     private fun getBluePrintRuntimeService(): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
191         val blueprintBasePath = normalizedPathName(TestConstants.PATH_TEST_BLUEPRINTS_BASECONFIG)
192         val blueprintRuntime = BluePrintMetadataUtils.getBluePrintRuntime("1234", blueprintBasePath)
193         val checkProcessId = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID)
194         val checkBasePath = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)
195
196         assertEquals(
197             "1234".asJsonPrimitive(),
198             checkProcessId, "Failed to get process id after runtime creation"
199         )
200         assertEquals(
201             blueprintBasePath.asJsonPrimitive(),
202             checkBasePath, "Failed to get base path after runtime creation"
203         )
204
205         return blueprintRuntime
206     }
207 }