871f8af086490ad366599a65bd193d8d6b65f001
[ccsdk/cds.git] /
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 Relationship Properties`() {
62         log.info("************************ testResolveRelationshipTemplateProperties **********************")
63
64         val bluePrintRuntimeService = getBluePrintRuntimeService()
65
66         val inputDataPath = "src/test/resources/data/default-context.json"
67
68         val inputNode: JsonNode = JacksonUtils.jsonNodeFromFile(inputDataPath)
69         bluePrintRuntimeService.assignInputs(inputNode)
70
71         val propContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
72             .resolveRelationshipTemplateProperties("cli-device-properties")
73
74         assertNotNull(propContext, "Failed to populate relationship property values")
75         assertEquals(
76             "localhost".asJsonPrimitive(),
77             propContext["connection-config"]!!.get("host"),
78             "failed to resolve expression"
79         )
80     }
81
82     @Test
83     fun `test resolve NodeTemplate Capability Properties`() {
84         log.info("************************ testResolveNodeTemplateRequirementProperties **********************")
85         val bluePrintRuntimeService = getBluePrintRuntimeService()
86
87         val executionContext = bluePrintRuntimeService.getExecutionContext()
88
89         BluePrintRuntimeUtils.assignInputsFromClassPathFile(
90             bluePrintRuntimeService.bluePrintContext(),
91             "data/default-context.json", executionContext
92         )
93
94         val assignmentParams = "{\n" +
95             "            \"ipAddress\": \"127.0.0.1\",\n" +
96             "            \"hostName\": \"vnf-host\"\n" +
97             "          }"
98
99         bluePrintRuntimeService.setNodeTemplateAttributeValue(
100             "resource-assignment", "assignment-params",
101             JacksonUtils.jsonNode(assignmentParams)
102         )
103
104         val capProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(
105             "sample-netconf-device",
106             "netconf"
107         )
108         assertNotNull(capProperties, "Failed to populate capability property values")
109         assertEquals(
110             capProperties["target-ip-address"],
111             "127.0.0.1".asJsonPrimitive(),
112             "Failed to populate parameter target-ip-address"
113         )
114         assertEquals(
115             capProperties["port-number"],
116             JacksonUtils.jsonNodeFromObject(830),
117             "Failed to populate parameter port-number"
118         )
119     }
120
121     @Test
122     fun `test Resolve NodeTemplate Interface Operation Inputs`() {
123         log.info("************************ testResolveNodeTemplateInterfaceOperationInputs **********************")
124
125         val bluePrintRuntimeService = getBluePrintRuntimeService()
126
127         val executionContext = bluePrintRuntimeService.getExecutionContext()
128
129         BluePrintRuntimeUtils.assignInputsFromClassPathFile(
130             bluePrintRuntimeService.bluePrintContext(),
131             "data/default-context.json", executionContext
132         )
133
134         val inContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
135             .resolveNodeTemplateInterfaceOperationInputs(
136                 "resource-assignment",
137                 "ResourceResolutionComponent", "process"
138             )
139
140         assertNotNull(inContext, "Failed to populate interface input property values")
141         assertEquals(
142             inContext["action-name"],
143             JacksonUtils.jsonNodeFromObject("sample-action"),
144             "Failed to populate parameter action-name"
145         )
146         assertEquals(
147             inContext["request-id"],
148             JacksonUtils.jsonNodeFromObject("12345"),
149             "Failed to populate parameter action-name"
150         )
151     }
152
153     @Test
154     fun `test Resolve NodeTemplate Interface Operation Outputs`() {
155         log.info("************************ testResolveNodeTemplateInterfaceOperationOutputs **********************")
156
157         val bluePrintRuntimeService = getBluePrintRuntimeService()
158
159         bluePrintRuntimeService.setNodeTemplateAttributeValue(
160             "resource-assignment",
161             "assignment-params",
162             NullNode.getInstance()
163         )
164
165         bluePrintRuntimeService.resolveNodeTemplateInterfaceOperationOutputs(
166             "resource-assignment",
167             "ResourceResolutionComponent", "process"
168         )
169
170         val outputStatus = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
171             "resource-assignment",
172             "ResourceResolutionComponent", "process", "status"
173         )
174         assertEquals("success".asJsonPrimitive(), outputStatus, "Failed to get operation property status")
175
176         val outputParams = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
177             "resource-assignment",
178             "ResourceResolutionComponent", "process", "resource-assignment-params"
179         )
180         assertEquals(
181             NullNode.getInstance(),
182             outputParams,
183             "Failed to get operation property resource-assignment-params"
184         )
185     }
186
187     @Test
188     fun `test NodeTemplate Context Property`() {
189         log.info("************************ testNodeTemplateContextProperty **********************")
190         val bluePrintRuntimeService = getBluePrintRuntimeService()
191
192         bluePrintRuntimeService.setNodeTemplateAttributeValue(
193             "resource-assignment-ra-component", "context1",
194             JacksonUtils.jsonNodeFromObject("context1-value")
195         )
196         bluePrintRuntimeService.setNodeTemplateAttributeValue(
197             "resource-assignment-ra-component", "context2",
198             JacksonUtils.jsonNodeFromObject("context2-value")
199         )
200
201         val keys = listOf("context1", "context2")
202
203         val jsonValueNode =
204             bluePrintRuntimeService.getJsonForNodeTemplateAttributeProperties("resource-assignment-ra-component", keys)
205         assertNotNull(jsonValueNode, "Failed to get Json for Node Template Context Properties")
206         log.info("JSON Prepared Value Context {}", jsonValueNode)
207     }
208
209     @Test
210     fun `test Resolve DSL Properties`() {
211         log.info("************************ resolveDSLExpression **********************")
212
213         val bluePrintRuntimeService = getBluePrintRuntimeService()
214
215         bluePrintRuntimeService.setInputValue("rest-user-name", "sample-username".asJsonPrimitive())
216
217         val resolvedJsonNode: JsonNode = bluePrintRuntimeService.resolveDSLExpression("dynamic-rest-source")
218         assertNotNull(resolvedJsonNode, "Failed to populate dsl property values")
219     }
220
221     @Test
222     fun `test Resolve Workflow Outputs`() {
223         log.info("************************ resolvePropertyAssignments **********************")
224         val bluePrintRuntimeService = getBluePrintRuntimeService()
225
226         val assignmentParams = "{\"ipAddress\": \"127.0.0.1\", \"hostName\": \"vnf-host\"}"
227
228         bluePrintRuntimeService.setNodeTemplateAttributeValue(
229             "resource-assignment", "assignment-params",
230             JacksonUtils.jsonNode(assignmentParams)
231         )
232
233         val resolvedJsonNode = bluePrintRuntimeService.resolveWorkflowOutputs("resource-assignment")
234         assertNotNull(resolvedJsonNode, "Failed to populate workflow output property values")
235     }
236
237     private fun getBluePrintRuntimeService(): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
238         val blueprintBasePath = normalizedPathName(TestConstants.PATH_TEST_BLUEPRINTS_BASECONFIG)
239         val blueprintRuntime = BluePrintMetadataUtils.bluePrintRuntime("1234", blueprintBasePath)
240         val checkProcessId = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID)
241         val checkBasePath = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)
242
243         assertEquals(
244             "1234".asJsonPrimitive(),
245             checkProcessId, "Failed to get process id after runtime creation"
246         )
247         assertEquals(
248             blueprintBasePath.asJsonPrimitive(),
249             checkBasePath, "Failed to get base path after runtime creation"
250         )
251
252         return blueprintRuntime
253     }
254 }