aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/capabilities/IpAssignResolutionCapabilityTest.kt
blob: 4ce5df18f99f6898d85c2ab7c4a73ec2d9c00b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright © 2019 AT&T.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.capabilities

import com.fasterxml.jackson.databind.node.NullNode
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkObject
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.CapabilityResourceResolutionProcessor
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.resourceAssignments
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.resourceDefinitions
import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
import org.onap.ccsdk.cds.controllerblueprints.core.logger
import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintDependencyService
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResolutionSummary
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

/**
 * @author saurav.paira
 */

class IpAssignResolutionCapabilityTest {

    val log = logger(IpAssignResolutionCapabilityTest::class)

    @Before
    fun setup() {

        mockkObject(BluePrintDependencyService)

        val blueprintWebClientService = mockk<BlueprintWebClientService>()
        // Create mock Response
        val mockResponse = BlueprintWebClientService.WebClientResponse(
            200,
            """{
            "fixed_ipv4_Address_01" : "10.10.10.11",
            "fixed_ipv4_Address_02" : "10.10.10.12",
            "fixed_ipv4_Address_03" : "10.10.10.13"
            }
        """.trimMargin()
        )
        every { blueprintWebClientService.exchangeResource(any(), any(), any()) } returns mockResponse

        val restLibPropertyService = mockk<BluePrintRestLibPropertyService>()
        every { restLibPropertyService.blueprintWebClientService("ipassign-ms") } returns blueprintWebClientService
        every { BluePrintDependencyService.applicationContext.getBean(any()) } returns restLibPropertyService
    }

    @Test
    fun testIpAssignResolutionCapability() {
        runBlocking {
            val componentFunctionScriptingService = mockk<ComponentFunctionScriptingService>()
            coEvery {
                componentFunctionScriptingService
                    .scriptInstance<ResourceAssignmentProcessor>(any(), any(), any())
            } returns IpAssignResolutionCapability()

            val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
            every { raRuntimeService.bluePrintContext() } returns mockk()
            every { raRuntimeService.getInputValue("fixed_ipv4_Address_01") } returns NullNode.getInstance()
            every { raRuntimeService.getInputValue("fixed_ipv4_Address_02") } returns NullNode.getInstance()
            every { raRuntimeService.getInputValue("fixed_ipv4_Address_03") } returns NullNode.getInstance()

            every { raRuntimeService.getResolutionStore("CloudRegionId") } returns "cloud-123".asJsonPrimitive()
            every { raRuntimeService.getResolutionStore("IpServiceName") } returns "MobilityPlan".asJsonPrimitive()

            every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
            every { raRuntimeService.putDictionaryStore(any(), any()) } returns Unit

            val capabilityResourceResolutionProcessor =
                CapabilityResourceResolutionProcessor(componentFunctionScriptingService)
            capabilityResourceResolutionProcessor.raRuntimeService = raRuntimeService

            capabilityResourceResolutionProcessor.resourceDictionaries = resourceDefinitions()
            // log.info("ResourceAssignments Definitions : ${ capabilityResourceResolutionProcessor.resourceDictionaries.asJsonString(true)} ")
            val resourceAssignments = resourceAssignments()
            val resourceAssignmentList = resourceAssignments.values.toMutableList()
            // log.info("ResourceAssignments Assignments : ${resourceAssignmentList.asJsonString(true)} ")
            capabilityResourceResolutionProcessor.resourceAssignments = resourceAssignmentList

            var status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_01"]!!)
            assertTrue(status, "failed to execute capability source")
            assertEquals(
                "10.10.10.11".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_01"]!!.property!!.value,
                "assigned value miss match"
            )

            status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_02"]!!)
            assertTrue(status, "failed to execute capability source")
            assertEquals(
                "10.10.10.12".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_02"]!!.property!!.value,
                "assigned value miss match"
            )

            status = capabilityResourceResolutionProcessor.applyNB(resourceAssignments["fixed_ipv4_Address_03"]!!)
            assertTrue(status, "failed to execute capability source")
            assertEquals(
                "10.10.10.13".asJsonPrimitive(), resourceAssignments["fixed_ipv4_Address_03"]!!.property!!.value,
                "assigned value miss match"
            )

            val resoulutionSummary =
                ResourceAssignmentUtils.generateResolutionSummaryData(
                    resourceAssignments.values.toList(),
                    capabilityResourceResolutionProcessor.resourceDictionaries
                )
            log.info(resoulutionSummary.asJsonType().toPrettyString())
            assertNotNull(resoulutionSummary.asJsonType().get("resolution-summary"))

            val summaries = JacksonUtils.jsonNode(resoulutionSummary)["resolution-summary"]
            val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
            val ipAddress = list.filter { it.name == "fixed_ipv4_Address_01" }

            assertEquals(list.size, 5)
            assertNotNull(ipAddress[0].keyIdentifiers)
            assertNotNull(ipAddress[0].requestPayload)
        }
    }

    /** Test dictionaries */

    /** Test dictionaries */
    private fun resourceDefinitions(): MutableMap<String, ResourceDefinition> {
        return BluePrintTypes.resourceDefinitions {
            resourceDefinition("CloudRegionId", "Cloud Region Id Resource Definition") {
                tags("CloudRegionId")
                updatedBy("saurav.paira@att.com")
                property("string", true)
                sources {
                    sourceInput("input", "") {}
                }
            }
            resourceDefinition("IpServiceName", "Ip Service Name Resource Definition") {
                tags("IpServiceName")
                updatedBy("saurav.paira@att.com")
                property("string", true)
                sources {
                    sourceInput("input", "") {}
                }
            }
            resourceDefinition("fixed_ipv4_Address_01", "fixed_ipv4_Address_01 Resource Definition") {
                tags("fixed_ipv4_Address_01")
                updatedBy("saurav.paira@att.com")
                property("string", true)
                sources {
                    sourceCapability("ipassign-ms", "") {
                        definedProperties {
                            type("internal")
                            scriptClassReference(IpAssignResolutionCapability::class)
                            keyDependencies(
                                arrayListOf(
                                    "CloudRegionId",
                                    "IpServiceName"
                                )
                            )
                        }
                    }
                }
            }
            resourceDefinition("fixed_ipv4_Address_02", "fixed_ipv4_Address_02 Resource Definition") {
                tags("fixed_ipv4_Address_01")
                updatedBy("saurav.paira@att.com")
                property("string", true)
                sources {
                    sourceCapability("ipassign-ms", "") {
                        definedProperties {
                            type("internal")
                            scriptClassReference(IpAssignResolutionCapability::class)
                            keyDependencies(
                                arrayListOf(
                                    "CloudRegionId",
                                    "IpServiceName"
                                )
                            )
                        }
                    }
                }
            }
            resourceDefinition("fixed_ipv4_Address_03", "fixed_ipv4_Address_03 Resource Definition") {
                tags("fixed_ipv4_Address_03")
                updatedBy("saurav.paira@att.com")
                property("string", true)
                sources {
                    sourceCapability("ipassign-ms", "") {
                        definedProperties {
                            type("internal")
                            scriptClassReference(IpAssignResolutionCapability::class)
                            keyDependencies(
                                arrayListOf(
                                    "CloudRegionId",
                                    "IpServiceName"
                                )
                            )
                        }
                    }
                }
            }
        }
    }

    private fun resourceAssignments(): MutableMap<String, ResourceAssignment> {
        return BluePrintTypes.resourceAssignments {
            resourceAssignment(
                name = "CloudRegionId", dictionaryName = "CloudRegionId",
                dictionarySource = "input"
            ) {
                property("string", true, "")
                dependencies(arrayListOf())
            }
            resourceAssignment(
                name = "IpServiceName", dictionaryName = "IpServiceName",
                dictionarySource = "input"
            ) {
                property("string", true, "")
                dependencies(arrayListOf())
            }
            resourceAssignment(
                name = "fixed_ipv4_Address_01", dictionaryName = "fixed_ipv4_Address_01",
                dictionarySource = "ipassign-ms"
            ) {
                property("string", true, "")
                dependencies(
                    arrayListOf(
                        "CloudRegionId",
                        "IpServiceName"
                    )
                )
            }
            resourceAssignment(
                name = "fixed_ipv4_Address_02", dictionaryName = "fixed_ipv4_Address_02",
                dictionarySource = "ipassign-ms"
            ) {
                property("string", true, "")
                dependencies(
                    arrayListOf(
                        "fixed_ipv4_Address_01"
                    )
                )
            }
            resourceAssignment(
                name = "fixed_ipv4_Address_03", dictionaryName = "fixed_ipv4_Address_03",
                dictionarySource = "ipassign-ms"
            ) {
                property("string", true, "")
                dependencies(
                    arrayListOf(
                        "fixed_ipv4_Address_02"
                    )
                )
            }
        }
    }
}