4c207fbe1d557f6a93011162e89c772657abf982
[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 org.slf4j.LoggerFactory
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.fasterxml.jackson.databind.node.NullNode
23 import org.junit.Test
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
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 kotlin.test.assertEquals
31 import kotlin.test.assertNotNull
32
33 /**
34  *
35  *
36  * @author Brinda Santh
37  */
38 class BluePrintRuntimeServiceTest {
39     private val log= LoggerFactory.getLogger(this::class.toString())
40
41     @Test
42     fun `test Resolve NodeTemplate Properties`() {
43         log.info("************************ testResolveNodeTemplateProperties **********************")
44
45         val bluePrintRuntimeService = getBluePrintRuntimeService()
46
47         val inputDataPath = "src/test/resources/data/default-context.json"
48
49         val inputNode: JsonNode = JacksonUtils.jsonNodeFromFile(inputDataPath)
50         bluePrintRuntimeService.assignInputs(inputNode)
51
52         val propContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
53                 .resolveNodeTemplateProperties("activate-process")
54
55         assertNotNull(propContext, "Failed to populate interface property values")
56     }
57
58     @Test
59     fun `test resolve NodeTemplate Capability Properties`() {
60         log.info("************************ testResolveNodeTemplateRequirementProperties **********************")
61         val bluePrintRuntimeService = getBluePrintRuntimeService()
62
63         val executionContext = bluePrintRuntimeService.getExecutionContext()
64
65         BluePrintRuntimeUtils.assignInputsFromClassPathFile(bluePrintRuntimeService.bluePrintContext(),
66                 "data/default-context.json", executionContext)
67
68         val assignmentParams = "{\n" +
69                 "            \"ipAddress\": \"127.0.0.1\",\n" +
70                 "            \"hostName\": \"vnf-host\"\n" +
71                 "          }"
72
73         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params",
74                 JacksonUtils.jsonNode(assignmentParams))
75
76         val capProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties("sample-netconf-device",
77                 "netconf")
78         assertNotNull(capProperties, "Failed to populate capability property values")
79         assertEquals(capProperties["target-ip-address"], "127.0.0.1".asJsonPrimitive(), "Failed to populate parameter target-ip-address")
80         assertEquals(capProperties["port-number"], JacksonUtils.jsonNodeFromObject(830), "Failed to populate parameter port-number")
81     }
82
83     @Test
84     fun `test Resolve NodeTemplate Interface Operation Inputs`() {
85         log.info("************************ testResolveNodeTemplateInterfaceOperationInputs **********************")
86
87         val bluePrintRuntimeService = getBluePrintRuntimeService()
88
89         val executionContext = bluePrintRuntimeService.getExecutionContext()
90
91         BluePrintRuntimeUtils.assignInputsFromClassPathFile(bluePrintRuntimeService.bluePrintContext(),
92                 "data/default-context.json", executionContext)
93
94         val inContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
95                 .resolveNodeTemplateInterfaceOperationInputs("resource-assignment",
96                         "ResourceResolutionComponent", "process")
97
98         assertNotNull(inContext, "Failed to populate interface input property values")
99         assertEquals(inContext["action-name"], JacksonUtils.jsonNodeFromObject("sample-action"), "Failed to populate parameter action-name")
100         assertEquals(inContext["request-id"], JacksonUtils.jsonNodeFromObject("12345"), "Failed to populate parameter action-name")
101     }
102
103     @Test
104     fun `test Resolve NodeTemplate Interface Operation Outputs`() {
105         log.info("************************ testResolveNodeTemplateInterfaceOperationOutputs **********************")
106
107         val bluePrintRuntimeService = getBluePrintRuntimeService()
108
109         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params", NullNode.getInstance())
110
111         bluePrintRuntimeService.resolveNodeTemplateInterfaceOperationOutputs("resource-assignment",
112                 "ResourceResolutionComponent", "process")
113
114         val outputStatus = bluePrintRuntimeService.getNodeTemplateOperationOutputValue("resource-assignment",
115                 "ResourceResolutionComponent", "process", "status")
116         assertEquals("success".asJsonPrimitive(), outputStatus, "Failed to get operation property status")
117
118         val outputParams = bluePrintRuntimeService.getNodeTemplateOperationOutputValue("resource-assignment",
119                 "ResourceResolutionComponent", "process", "resource-assignment-params")
120         assertEquals(NullNode.getInstance(), outputParams, "Failed to get operation property resource-assignment-params")
121
122     }
123
124     @Test
125     fun `test NodeTemplate Context Property`() {
126         log.info("************************ testNodeTemplateContextProperty **********************")
127         val bluePrintRuntimeService = getBluePrintRuntimeService()
128
129         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment-ra-component", "context1",
130                 JacksonUtils.jsonNodeFromObject("context1-value"))
131         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment-ra-component", "context2",
132                 JacksonUtils.jsonNodeFromObject("context2-value"))
133
134         val keys = listOf("context1", "context2")
135
136         val jsonValueNode = bluePrintRuntimeService.getJsonForNodeTemplateAttributeProperties("resource-assignment-ra-component", keys)
137         assertNotNull(jsonValueNode, "Failed to get Json for Node Template Context Properties")
138         log.info("JSON Prepared Value Context {}", jsonValueNode)
139
140     }
141
142     @Test
143     fun `test Resolve DSL Properties`() {
144         log.info("************************ resolveDSLExpression **********************")
145
146         val bluePrintRuntimeService = getBluePrintRuntimeService()
147
148         bluePrintRuntimeService.setInputValue("rest-user-name", PropertyDefinition(), "sample-username"
149                 .asJsonPrimitive())
150
151         val resolvedJsonNode: JsonNode = bluePrintRuntimeService.resolveDSLExpression("dynamic-rest-source")
152         assertNotNull(resolvedJsonNode, "Failed to populate dsl property values")
153     }
154
155     @Test
156     fun `test Resolve Workflow Outputs`() {
157         log.info("************************ resolvePropertyAssignments **********************")
158         val bluePrintRuntimeService = getBluePrintRuntimeService()
159
160         val assignmentParams = "{\"ipAddress\": \"127.0.0.1\", \"hostName\": \"vnf-host\"}"
161
162         bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment", "assignment-params",
163                 JacksonUtils.jsonNode(assignmentParams))
164
165         val resolvedJsonNode = bluePrintRuntimeService.resolveWorkflowOutputs("resource-assignment")
166         assertNotNull(resolvedJsonNode, "Failed to populate workflow output property values")
167     }
168
169     private fun getBluePrintRuntimeService(): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
170         val blueprintBasePath: String = ("./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
171         val blueprintRuntime = BluePrintMetadataUtils.getBluePrintRuntime("1234", blueprintBasePath)
172         val checkBasePath = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)
173
174         assertEquals(blueprintBasePath.asJsonPrimitive(), checkBasePath, "Failed to get base path after runtime creation")
175
176         return blueprintRuntime
177     }
178
179 }