449845f7fb7a45b270b8f271b72c5d103602afa3
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / capabilities / NamingResolutionCapabilityTest.kt
1 /*
2  * Copyright © 2019 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.capabilities
18
19 import com.fasterxml.jackson.databind.node.NullNode
20 import io.mockk.coEvery
21 import io.mockk.every
22 import io.mockk.mockk
23 import io.mockk.mockkObject
24 import kotlinx.coroutines.runBlocking
25 import org.junit.Before
26 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
27 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.CapabilityResourceResolutionProcessor
28 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
29 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.resourceAssignments
30 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.resourceDefinitions
31 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
32 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
33 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
34 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
35 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
36 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
37 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
38 import org.onap.ccsdk.cds.controllerblueprints.core.logger
39 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
40 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
41 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
42 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResolutionSummary
43 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
44 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
45 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.BulkResourceSequencingUtils
46 import kotlin.test.Test
47 import kotlin.test.assertEquals
48 import kotlin.test.assertNotNull
49 import kotlin.test.assertTrue
50
51 /**
52  * @author brindasanth
53  */
54
55 class NamingResolutionCapabilityTest {
56
57     private val log = logger(NamingResolutionCapabilityTest::class)
58
59     @Before
60     fun setup() {
61
62         mockkObject(BluePrintDependencyService)
63
64         val blueprintWebClientService = mockk<BlueprintWebClientService>()
65         // Create mock Response
66         val mockResponse = BlueprintWebClientService.WebClientResponse<String>(
67             200,
68             """{
69             "vf-module-name" : "dlsst001dbcx-adsf-Base-01",
70             "vnfc-name" : "dlsst001dbcx"
71             }
72         """.trimMargin()
73         )
74         every { blueprintWebClientService.exchangeResource(any(), any(), any()) } returns mockResponse
75
76         val restLibPropertyService = mockk<BluePrintRestLibPropertyService>()
77         every { restLibPropertyService.blueprintWebClientService("naming-ms") } returns blueprintWebClientService
78         every { BluePrintDependencyService.applicationContext.getBean(any()) } returns restLibPropertyService
79     }
80
81     @Test
82     fun testNamingResolutionCapability() {
83         runBlocking {
84             val componentFunctionScriptingService = mockk<ComponentFunctionScriptingService>()
85             coEvery {
86                 componentFunctionScriptingService
87                     .scriptInstance<ResourceAssignmentProcessor>(any(), any(), any())
88             } returns NamingResolutionCapability()
89
90             coEvery {
91                 componentFunctionScriptingService.cleanupInstance(any(), any())
92             } returns mockk()
93
94             val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
95             every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
96             every { raRuntimeService.getInputValue("vf-module-name") } returns NullNode.getInstance()
97             every { raRuntimeService.getInputValue("vnfc-name") } returns NullNode.getInstance()
98
99             every { raRuntimeService.getResolutionStore("policy-instance-name") } returns "SDNC_Policy.Config_MS_1806SRIOV_VNATJson.4.xml".asJsonPrimitive()
100             every { raRuntimeService.getResolutionStore("naming-code") } returns "dbc".asJsonPrimitive()
101             every { raRuntimeService.getResolutionStore("vnf-name") } returns "vnf-123".asJsonPrimitive()
102             every { raRuntimeService.getResolutionStore("vf-module-label") } returns "adsf".asJsonPrimitive()
103             every { raRuntimeService.getResolutionStore("vf-module-type") } returns "base".asJsonPrimitive()
104             every { raRuntimeService.getResolutionStore("cloud-region-id") } returns "region-123".asJsonPrimitive()
105
106             every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
107             every { raRuntimeService.putDictionaryStore(any(), any()) } returns Unit
108
109             val capabilityResourceResolutionProcessor =
110                 CapabilityResourceResolutionProcessor(componentFunctionScriptingService)
111             capabilityResourceResolutionProcessor.raRuntimeService = raRuntimeService
112
113             capabilityResourceResolutionProcessor.resourceDictionaries = resourceDefinitions()
114             // log.info("ResourceAssignments Definitions : ${ capabilityResourceResolutionProcessor.resourceDictionaries.asJsonString(true)} ")
115             val resourceAssignments = resourceAssignments()
116             val resourceAssignmentList = resourceAssignments.values.toMutableList()
117             // log.info("ResourceAssignments Assignments : ${resourceAssignmentList.asJsonString(true)} ")
118             capabilityResourceResolutionProcessor.resourceAssignments = resourceAssignmentList
119
120             val bulkSequenced =
121                 BulkResourceSequencingUtils.process(capabilityResourceResolutionProcessor.resourceAssignments)
122             // log.info("Bulk Sequenced : ${bulkSequenced} ")
123             val resourceAssignment1 = resourceAssignments["vf-module-name"]
124             var status = capabilityResourceResolutionProcessor.applyNB(resourceAssignment1!!)
125             assertTrue(status, "failed to execute capability source")
126             assertEquals(
127                 "dlsst001dbcx-adsf-Base-01".asJsonPrimitive(), resourceAssignment1.property!!.value,
128                 "assigned value miss match"
129             )
130
131             val resourceAssignment2 = resourceAssignments["vnfc-name"]
132             status = capabilityResourceResolutionProcessor.applyNB(resourceAssignment2!!)
133             assertTrue(status, "failed to execute capability source")
134             assertEquals(
135                 "dlsst001dbcx".asJsonPrimitive(), resourceAssignment2.property!!.value,
136                 "assigned value miss match"
137             )
138
139             val resoulutionSummary =
140                 ResourceAssignmentUtils.generateResolutionSummaryData(
141                     resourceAssignments.values.toList(),
142                     capabilityResourceResolutionProcessor.resourceDictionaries
143                 )
144             log.info(resoulutionSummary.asJsonType().toPrettyString())
145             assertNotNull(resoulutionSummary.asJsonType().get("resolution-summary"))
146
147             val summaries = JacksonUtils.jsonNode(resoulutionSummary)["resolution-summary"]
148             val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
149             val vnfModuleName = list.filter { it.name == "vf-module-name" }
150
151             assertEquals(list.size, 9)
152             assertNotNull(vnfModuleName[0].keyIdentifiers)
153             assertNotNull(vnfModuleName[0].requestPayload)
154         }
155     }
156
157     /** Test dictionaries */
158     private fun resourceDefinitions(): MutableMap<String, ResourceDefinition> {
159         return BluePrintTypes.resourceDefinitions {
160             resourceDefinition("naming-code", "naming-code Resource Definition") {
161                 tags("naming-code")
162                 updatedBy("brindasanth@onap.com")
163                 property("string", true)
164                 sources {
165                     sourceInput("input", "") {}
166                 }
167             }
168             resourceDefinition("naming-type", "naming-type Resource Definition") {
169                 tags("naming-type")
170                 updatedBy("brindasanth@onap.com")
171                 property("string", true)
172                 sources {
173                     sourceInput("input", "") {}
174                 }
175             }
176             resourceDefinition("cloud-region-id", "cloud-region-id Resource Definition") {
177                 tags("cloud-region-id")
178                 updatedBy("brindasanth@onap.com")
179                 property("string", true)
180                 sources {
181                     sourceInput("input", "") {}
182                 }
183             }
184             resourceDefinition("policy-instance-name", "policy-instance-name Resource Definition") {
185                 tags("policy-instance-name")
186                 updatedBy("sp694w@att.com")
187                 property("string", true)
188                 sources {
189                     sourceInput("input", "") {}
190                 }
191             }
192             resourceDefinition("vnf-name", "vnf-name Resource Definition") {
193                 tags("vnf-name")
194                 updatedBy("sp694w@att.com")
195                 property("string", true)
196                 sources {
197                     sourceInput("input", "") {}
198                 }
199             }
200             resourceDefinition("vf-module-label", "vf-module-label Resource Definition") {
201                 tags("vf-module-label")
202                 updatedBy("sp694w@att.com")
203                 property("string", true)
204                 sources {
205                     sourceInput("input", "") {}
206                 }
207             }
208             resourceDefinition("vf-module-type", "vf-module-type Resource Definition") {
209                 tags("vf-module-type")
210                 updatedBy("sp694w@att.com")
211                 property("string", true)
212                 sources {
213                     sourceInput("input", "") {}
214                 }
215             }
216             resourceDefinition("vf-module-name", "vf-module-name Resource Definition") {
217                 tags("vf-module-name")
218                 updatedBy("brindasanth@onap.com")
219                 property("string", true) {
220                     metadata("naming-type", "VF-MODULE")
221                 }
222                 sources {
223                     sourceCapability("naming-ms", "") {
224                         definedProperties {
225                             type("internal")
226                             scriptClassReference(NamingResolutionCapability::class)
227                             keyDependencies(
228                                 arrayListOf(
229                                     "policy-instance-name",
230                                     "naming-code",
231                                     "vnf-name",
232                                     "vf-module-label",
233                                     "vf-module-type"
234                                 )
235                             )
236                         }
237                     }
238                 }
239             }
240             resourceDefinition("vnfc-name", "vnfc-name Resource Definition") {
241                 tags("vnfc-name")
242                 updatedBy("brindasanth@onap.com")
243                 property("string", true) {
244                     metadata("naming-type", "VNFC")
245                 }
246
247                 sources {
248                     sourceCapability("naming-ms", "") {
249                         definedProperties {
250                             type("internal")
251                             scriptClassReference(NamingResolutionCapability::class)
252                             keyDependencies(
253                                 arrayListOf("vf-module-name")
254                             )
255                         }
256                     }
257                 }
258             }
259             resourceDefinition("ra-dict-name-3", "ra-dict-name-3 Resource Definition") {
260                 tags("ra-dict-name-3")
261                 updatedBy("brindasanth@onap.com")
262                 property("string", true)
263                 sources {
264                     sourceCapability("naming-ms", "") {
265                         definedProperties {
266                             type("internal")
267                             scriptClassReference(NamingResolutionCapability::class)
268                             keyDependencies(
269                                 arrayListOf("vf-module-name")
270                             )
271                         }
272                     }
273                 }
274             }
275         }
276     }
277
278     private fun resourceAssignments(): MutableMap<String, ResourceAssignment> {
279         return BluePrintTypes.resourceAssignments {
280             resourceAssignment(
281                 name = "naming-code", dictionaryName = "naming-code",
282                 dictionarySource = "input"
283             ) {
284                 property("string", true, "")
285                 dependencies(arrayListOf())
286             }
287             resourceAssignment(
288                 name = "naming-type", dictionaryName = "naming-type",
289                 dictionarySource = "input"
290             ) {
291                 property("string", true, "")
292                 dependencies(arrayListOf())
293             }
294             resourceAssignment(
295                 name = "cloud-region-id", dictionaryName = " cloud-region-id",
296                 dictionarySource = "input"
297             ) {
298                 property("string", true, "")
299                 dependencies(arrayListOf())
300             }
301             resourceAssignment(
302                 name = "policy-instance-name", dictionaryName = " policy-instance-name",
303                 dictionarySource = "input"
304             ) {
305                 property("string", true, "")
306                 dependencies(arrayListOf())
307             }
308             resourceAssignment(
309                 name = "vnf-name", dictionaryName = " vnf-name",
310                 dictionarySource = "input"
311             ) {
312                 property("string", true, "")
313                 dependencies(arrayListOf())
314             }
315             resourceAssignment(
316                 name = "vf-module-label", dictionaryName = " vf-module-label",
317                 dictionarySource = "input"
318             ) {
319                 property("string", true, "")
320                 dependencies(arrayListOf())
321             }
322             resourceAssignment(
323                 name = "vf-module-type", dictionaryName = " vf-module-type",
324                 dictionarySource = "input"
325             ) {
326                 property("string", true, "")
327                 dependencies(arrayListOf())
328             }
329             resourceAssignment(
330                 name = "vf-module-name", dictionaryName = "vf-module-name",
331                 dictionarySource = "naming-ms"
332             ) {
333                 property("string", true, "")
334                 dependencies(
335                     arrayListOf(
336                         "policy-instance-name",
337                         "naming-code",
338                         "vnf-name",
339                         "vf-module-label",
340                         "vf-module-type"
341                     )
342                 )
343             }
344             resourceAssignment(
345                 name = "vnfc-name", dictionaryName = "vnfc-name",
346                 dictionarySource = "naming-ms"
347             ) {
348                 property("string", true, "")
349                 dependencies(
350                     arrayListOf(
351                         "vf-module-name"
352                     )
353                 )
354             }
355         }
356     }
357 }