2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution
23 import kotlinx.coroutines.runBlocking
24 import org.junit.Assert
25 import org.junit.Before
27 import org.junit.runner.RunWith
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.PayloadUtils
30 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.MockCapabilityScriptRA
31 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
32 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
33 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
34 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError
35 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
36 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
37 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
39 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResolutionSummary
40 import org.slf4j.LoggerFactory
41 import org.springframework.beans.factory.annotation.Autowired
42 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
43 import org.springframework.context.ApplicationContext
44 import org.springframework.context.annotation.ComponentScan
45 import org.springframework.test.context.ContextConfiguration
46 import org.springframework.test.context.TestPropertySource
47 import org.springframework.test.context.junit4.SpringRunner
48 import kotlin.test.assertEquals
49 import kotlin.test.assertNotNull
50 import kotlin.test.assertTrue
53 * ResourceResolutionServiceTest
55 * @author Brinda Santh DATE : 8/15/2018
57 @RunWith(SpringRunner::class)
58 @ContextConfiguration(
59 classes = [TestDatabaseConfiguration::class]
61 @TestPropertySource(locations = ["classpath:application-test.properties"])
62 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
63 @EnableAutoConfiguration
64 class ResourceResolutionServiceTest {
66 private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
69 lateinit var resourceResolutionService: ResourceResolutionService
71 private val props = hashMapOf<String, Any>()
72 private val resolutionKey = "resolutionKey"
73 private val resourceId = "1"
74 private val resourceType = "ServiceInstance"
75 private val occurrence = 0
79 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] = true
80 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = resolutionKey
81 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID] = resourceId
82 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE] = resourceType
83 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = occurrence
84 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = false
88 fun testRegisteredSource() {
89 val sources = resourceResolutionService.registeredResourceSources()
90 assertNotNull(sources, "failed to get registered sources")
94 "source-input", "source-default", "source-db",
95 "source-rest", "source-capability"
98 "failed to get registered sources : $sources"
103 @Throws(Exception::class)
104 fun testResolveResource() {
107 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
109 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
111 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
114 val executionServiceInput =
115 JacksonUtils.readValueFromClassPathFile(
116 "payload/requests/sample-resourceresolution-request.json",
117 ExecutionServiceInput::class.java
120 val resourceAssignmentRuntimeService =
121 ResourceAssignmentUtils.transformToRARuntimeService(
122 bluePrintRuntimeService,
123 "testResolveResource"
127 PayloadUtils.prepareInputsFromWorkflowPayload(
128 bluePrintRuntimeService,
129 executionServiceInput.payload,
130 "resource-assignment"
133 resourceResolutionService.resolveResources(
134 resourceAssignmentRuntimeService,
135 "resource-assignment",
139 }.let { (templateMap, assignmentList) ->
140 assertEquals("This is Sample Velocity Template", templateMap)
142 val expectedAssignmentList = mutableListOf(
143 "service-instance-id" to "siid_1234",
144 "vnf-id" to "vnf_1234",
145 "vnf_name" to "temp_vnf"
147 assertEquals(expectedAssignmentList.size, assignmentList.size)
149 val areEqual = expectedAssignmentList.zip(assignmentList).all { (it1, it2) ->
150 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
152 assertEquals(true, areEqual)
157 * Always perform new resolution even if resolution exists in the database.
160 @Throws(Exception::class)
161 fun testResolveResourceAlwaysPerformNewResolution() {
163 // Occurrence <= 0 indicates to perform new resolution even if resolution exists in the database.
164 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = -1
165 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
167 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
169 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
173 val executionServiceInput =
174 JacksonUtils.readValueFromClassPathFile(
175 "payload/requests/sample-resourceresolution-request.json",
176 ExecutionServiceInput::class.java
179 val resourceAssignmentRuntimeService =
180 ResourceAssignmentUtils.transformToRARuntimeService(
181 bluePrintRuntimeService,
182 "testResolveResource"
185 // Prepare inputs from Request#1
186 PayloadUtils.prepareInputsFromWorkflowPayload(
187 bluePrintRuntimeService,
188 executionServiceInput.payload,
189 "resource-assignment"
192 // Resolve resources as per Request#1
193 resourceResolutionService.resolveResources(
194 resourceAssignmentRuntimeService,
195 "resource-assignment",
201 val executionServiceInput2 =
202 JacksonUtils.readValueFromClassPathFile(
203 "payload/requests/sample-resourceresolution-request2.json",
204 ExecutionServiceInput::class.java
207 // Prepare inputs from Request#2
208 PayloadUtils.prepareInputsFromWorkflowPayload(
209 bluePrintRuntimeService,
210 executionServiceInput2.payload,
211 "resource-assignment"
214 // Resolve resources as per Request#2
215 resourceResolutionService.resolveResources(
216 resourceAssignmentRuntimeService,
217 "resource-assignment",
221 }.let { (templateMap, assignmentList) ->
222 assertEquals("This is Sample Velocity Template", templateMap)
224 val assignmentListForRequest1 = mutableListOf(
225 "service-instance-id" to "siid_1234",
226 "vnf-id" to "vnf_1234",
227 "vnf_name" to "temp_vnf"
229 val assignmentListForRequest2 = mutableListOf(
230 "service-instance-id" to "siid_new_resolution",
231 "vnf-id" to "vnf_new_resolution",
232 "vnf_name" to "temp_vnf_new_resolution"
234 assertEquals(assignmentListForRequest1.size, assignmentList.size)
235 assertEquals(assignmentListForRequest2.size, assignmentList.size)
237 // AlwaysPerformNewResolution use case - resolution request #2 should returns the resolution as per
238 // assignmentListForRequest2 since new resolution is performed.
239 var areEqual = assignmentListForRequest1.zip(assignmentList).all { (it1, it2) ->
240 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
242 assertEquals(false, areEqual)
244 areEqual = assignmentListForRequest2.zip(assignmentList).all { (it1, it2) ->
245 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
247 assertEquals(true, areEqual)
252 * Don't perform new resolution in case resolution already exists in the database.
255 @Throws(Exception::class)
256 fun testResolveResourceNoNewResolution() {
258 // Occurrence > 0 indicates to not perform new resolution if resolution exists in the database.
259 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = 1
260 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
262 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
264 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
268 val executionServiceInput =
269 JacksonUtils.readValueFromClassPathFile(
270 "payload/requests/sample-resourceresolution-request.json",
271 ExecutionServiceInput::class.java
274 val resourceAssignmentRuntimeService =
275 ResourceAssignmentUtils.transformToRARuntimeService(
276 bluePrintRuntimeService,
277 "testResolveResource"
280 // Prepare inputs from Request#1
281 PayloadUtils.prepareInputsFromWorkflowPayload(
282 bluePrintRuntimeService,
283 executionServiceInput.payload,
284 "resource-assignment"
286 // Resolve resources as per Request#1
287 resourceResolutionService.resolveResources(
288 resourceAssignmentRuntimeService,
289 "resource-assignment",
295 val executionServiceInput2 =
296 JacksonUtils.readValueFromClassPathFile(
297 "payload/requests/sample-resourceresolution-request2.json",
298 ExecutionServiceInput::class.java
301 // Prepare inputs from Request#2
302 PayloadUtils.prepareInputsFromWorkflowPayload(
303 bluePrintRuntimeService,
304 executionServiceInput2.payload,
305 "resource-assignment"
308 // Resolve resources as per Request#2
309 resourceResolutionService.resolveResources(
310 resourceAssignmentRuntimeService,
311 "resource-assignment",
315 }.let { (templateMap, assignmentList) ->
316 assertEquals("This is Sample Velocity Template", templateMap)
318 val assignmentListForRequest1 = mutableListOf(
319 "service-instance-id" to "siid_1234",
320 "vnf-id" to "vnf_1234",
321 "vnf_name" to "temp_vnf"
323 val assignmentListForRequest2 = mutableListOf(
324 "service-instance-id" to "siid_new_resolution",
325 "vnf-id" to "vnf_new_resolution",
326 "vnf_name" to "temp_vnf_new_resolution"
328 assertEquals(assignmentListForRequest1.size, assignmentList.size)
329 assertEquals(assignmentListForRequest2.size, assignmentList.size)
331 // NoNewResolution use case - resolution for request #2 returns the same resolution as
332 // assignmentListForRequest1 since no new resolution is/was actually performed.
333 var areEqual = assignmentListForRequest1.zip(assignmentList).all { (it1, it2) ->
334 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
336 assertEquals(true, areEqual)
338 areEqual = assignmentListForRequest2.zip(assignmentList).all { (it1, it2) ->
339 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
341 assertEquals(false, areEqual)
346 @Throws(Exception::class)
347 fun testResolveResources() {
348 val artifactNames = listOf("baseconfig", "another")
350 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
352 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
354 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
357 val executionServiceInput =
358 JacksonUtils.readValueFromClassPathFile(
359 "payload/requests/sample-resourceresolution-request.json",
360 ExecutionServiceInput::class.java
364 PayloadUtils.prepareInputsFromWorkflowPayload(
365 bluePrintRuntimeService,
366 executionServiceInput.payload,
367 "resource-assignment"
370 resourceResolutionService.resolveResources(
371 bluePrintRuntimeService,
372 "resource-assignment",
378 assertEquals(artifactNames.toSet(), it.templateMap.keys)
379 assertEquals(artifactNames.toSet(), it.assignmentMap.keys)
381 assertEquals("This is Sample Velocity Template", it.templateMap["another"])
382 assertEquals("vnf_1234", it.assignmentMap["another"]!!["vnf-id"]!!.asText())
387 @Throws(Exception::class)
388 fun testResolveResourcesWithMappingAndTemplate() {
390 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
392 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
394 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
397 val executionServiceInput =
398 JacksonUtils.readValueFromClassPathFile(
399 "payload/requests/sample-resourceresolution-request.json",
400 ExecutionServiceInput::class.java
403 val resourceAssignmentRuntimeService =
404 ResourceAssignmentUtils.transformToRARuntimeService(
405 bluePrintRuntimeService,
406 "testResolveResourcesWithMappingAndTemplate"
409 val artifactPrefix = "another"
412 PayloadUtils.prepareInputsFromWorkflowPayload(
413 bluePrintRuntimeService,
414 executionServiceInput.payload,
415 "resource-assignment"
419 resourceResolutionService.resolveResources(
420 resourceAssignmentRuntimeService,
421 "resource-assignment",
425 "Couldn't Resolve Resources for artifact $artifactPrefix"
431 @Throws(Exception::class)
432 fun testResolveResourcesResolutionSummary() {
434 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
435 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
437 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
439 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
442 val executionServiceInput =
443 JacksonUtils.readValueFromClassPathFile(
444 "payload/requests/sample-resourceresolution-request.json",
445 ExecutionServiceInput::class.java
448 val resourceAssignmentRuntimeService =
449 ResourceAssignmentUtils.transformToRARuntimeService(
450 bluePrintRuntimeService,
451 "testResolveResourcesWithMappingAndTemplate"
454 val artifactPrefix = "notemplate"
457 PayloadUtils.prepareInputsFromWorkflowPayload(
458 bluePrintRuntimeService,
459 executionServiceInput.payload,
460 "resource-assignment"
463 resourceResolutionService.resolveResources(
464 resourceAssignmentRuntimeService,
465 "resource-assignment",
470 val summaries = JacksonUtils.jsonNode(it.first)["resolution-summary"]
471 val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
472 assertEquals(list.size, 3)
477 @Throws(Exception::class)
478 fun testResolveResourcesWithoutTemplate() {
479 val artifactPrefix = "notemplate"
481 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
483 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
485 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
488 val executionServiceInput =
489 JacksonUtils.readValueFromClassPathFile(
490 "payload/requests/sample-resourceresolution-request.json",
491 ExecutionServiceInput::class.java
494 val resourceAssignmentRuntimeService =
495 ResourceAssignmentUtils.transformToRARuntimeService(
496 bluePrintRuntimeService,
497 "testResolveResourcesWithMappingAndTemplate"
501 PayloadUtils.prepareInputsFromWorkflowPayload(
502 bluePrintRuntimeService,
503 executionServiceInput.payload,
504 "resource-assignment"
507 resourceResolutionService.resolveResources(
508 resourceAssignmentRuntimeService,
509 "resource-assignment",
517 "service-instance-id" : "siid_1234",
518 "vnf-id" : "vnf_1234",
519 "vnf_name" : "temp_vnf"
524 val areEqual = it.second.first().name == "service-instance-id" &&
525 "siid_1234" == it.second.first().property?.value?.asText() ?: null
526 assertEquals(true, areEqual)
531 fun testResolveResourcesWithResourceIdAndResourceType() {
532 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
534 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
536 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
538 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
541 val executionServiceInput =
542 JacksonUtils.readValueFromClassPathFile(
543 "payload/requests/sample-resourceresolution-request.json",
544 ExecutionServiceInput::class.java
547 val resourceAssignmentRuntimeService =
548 ResourceAssignmentUtils.transformToRARuntimeService(
549 bluePrintRuntimeService,
550 "testResolveResourcesWithMappingAndTemplate"
553 val artifactPrefix = "another"
556 PayloadUtils.prepareInputsFromWorkflowPayload(
557 bluePrintRuntimeService,
558 executionServiceInput.payload,
559 "resource-assignment"
563 resourceResolutionService.resolveResources(
564 resourceAssignmentRuntimeService,
565 "resource-assignment",
569 "Couldn't Resolve Resources for artifact $artifactPrefix"
575 fun testResourceResolutionForDefinition() {
576 val resourceDefinitions = BluePrintTypes.resourceDefinitions {
577 resourceDefinition(name = "port-speed", description = "Port Speed") {
578 property(type = "string", required = true)
580 sourceCapability(id = "sdno", description = "SDNO Source") {
582 type(BluePrintConstants.SCRIPT_KOTLIN)
583 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
584 keyDependencies(arrayListOf("device-id"))
587 sourceDb(id = "sdnc", description = "SDNC Controller") {
589 endpointSelector("processor-db")
590 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
592 map("device_id", "\$device-id")
594 keyDependencies(arrayListOf("device-id"))
599 resourceDefinition(name = "device-id", description = "Device Id") {
600 property(type = "string", required = true) {
602 sourceInput(id = "input", description = "Dependency Source") {}
608 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
609 every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
610 every { raRuntimeService.getBluePrintError() } returns BluePrintError()
611 every { raRuntimeService.setBluePrintError(any()) } returns Unit
612 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
613 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
615 val applicationContext = mockk<ApplicationContext>()
616 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
617 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
618 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
620 val sources = arrayListOf<String>("sdno", "sdnc")
622 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
623 val resolvedResources = resourceResolutionService.resolveResourceDefinition(
625 resourceDefinitions, "port-speed", sources
627 assertNotNull(resolvedResources, "failed to resolve the resources")