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