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 * Always perform new resolution even if resolution exists in the database.
255 @Throws(Exception::class)
256 fun testResolveResourcesForMaxOccurrence() {
258 // Occurrence <= 0 indicates to perform new resolution even if resolution exists in the database.
259 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = -1
260 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
262 // Run time for Request#1
263 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
265 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
269 val executionServiceInput =
270 JacksonUtils.readValueFromClassPathFile(
271 "payload/requests/sample-resourceresolution-request.json",
272 ExecutionServiceInput::class.java
275 // Prepare inputs from Request#1
276 PayloadUtils.prepareInputsFromWorkflowPayload(
277 bluePrintRuntimeService,
278 executionServiceInput.payload,
279 "resource-assignment"
282 val resourceAssignmentRuntimeService =
283 ResourceAssignmentUtils.transformToRARuntimeService(
284 bluePrintRuntimeService,
285 "testResolveResource"
288 // Resolve resources as per Request#1
289 resourceResolutionService.resolveResources(
290 resourceAssignmentRuntimeService,
291 "resource-assignment",
296 // Run time for Request#2
297 val bluePrintRuntimeService2 = BluePrintMetadataUtils.getBluePrintRuntime(
299 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
303 val executionServiceInput2 =
304 JacksonUtils.readValueFromClassPathFile(
305 "payload/requests/sample-resourceresolution-request2.json",
306 ExecutionServiceInput::class.java
309 val resourceAssignmentRuntimeService2 =
310 ResourceAssignmentUtils.transformToRARuntimeService(
311 bluePrintRuntimeService2,
312 "testResolveResource"
315 // Prepare inputs from Request#2
316 PayloadUtils.prepareInputsFromWorkflowPayload(
317 bluePrintRuntimeService2,
318 executionServiceInput2.payload,
319 "resource-assignment"
322 // Resolve resources as per Request#2
323 resourceResolutionService.resolveResources(
324 resourceAssignmentRuntimeService2,
325 "resource-assignment",
329 }.let { (template, assignmentList) ->
330 assertEquals("This is maxoccurrence Velocity Template", template)
332 val assignmentListForRequest1 = mutableListOf(
333 "firmware-version" to "firmware-version-0",
334 "ip-address" to "192.0.0.1"
336 val assignmentListForRequest2 = mutableListOf(
337 "firmware-version" to "firmware-version-1",
338 "ip-address" to "192.0.0.1"
340 assertEquals(assignmentListForRequest1.size, assignmentList.size)
341 assertEquals(assignmentListForRequest2.size, assignmentList.size)
343 // firmware-version has max-occurrence = 0 means perform new resolution all the time.
344 // ip-address has max-occurrence = 1 so its resolution should only be done once.
346 // AlwaysPerformNewResolution + max-occurrence feature use case - resolution request #2 should returns
347 // the resolution as per assignmentListForRequest2 since new resolution is only performed for
348 // firmware-version and not for ip-address.
349 var areEqual = assignmentListForRequest1.zip(assignmentList).all { (it1, it2) ->
350 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
352 assertEquals(false, areEqual)
354 areEqual = assignmentListForRequest2.zip(assignmentList).all { (it1, it2) ->
355 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
357 assertEquals(true, areEqual)
362 * Don't perform new resolution in case resolution already exists in the database.
365 @Throws(Exception::class)
366 fun testResolveResourceNoNewResolution() {
368 // Occurrence > 0 indicates to not perform new resolution if resolution exists in the database.
369 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE] = 1
370 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
372 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
374 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
378 val executionServiceInput =
379 JacksonUtils.readValueFromClassPathFile(
380 "payload/requests/sample-resourceresolution-request.json",
381 ExecutionServiceInput::class.java
384 val resourceAssignmentRuntimeService =
385 ResourceAssignmentUtils.transformToRARuntimeService(
386 bluePrintRuntimeService,
387 "testResolveResource"
390 // Prepare inputs from Request#1
391 PayloadUtils.prepareInputsFromWorkflowPayload(
392 bluePrintRuntimeService,
393 executionServiceInput.payload,
394 "resource-assignment"
396 // Resolve resources as per Request#1
397 resourceResolutionService.resolveResources(
398 resourceAssignmentRuntimeService,
399 "resource-assignment",
405 val executionServiceInput2 =
406 JacksonUtils.readValueFromClassPathFile(
407 "payload/requests/sample-resourceresolution-request2.json",
408 ExecutionServiceInput::class.java
411 // Prepare inputs from Request#2
412 PayloadUtils.prepareInputsFromWorkflowPayload(
413 bluePrintRuntimeService,
414 executionServiceInput2.payload,
415 "resource-assignment"
418 // Resolve resources as per Request#2
419 resourceResolutionService.resolveResources(
420 resourceAssignmentRuntimeService,
421 "resource-assignment",
425 }.let { (templateMap, assignmentList) ->
426 assertEquals("This is Sample Velocity Template", templateMap)
428 val assignmentListForRequest1 = mutableListOf(
429 "service-instance-id" to "siid_1234",
430 "vnf-id" to "vnf_1234",
431 "vnf_name" to "temp_vnf"
433 val assignmentListForRequest2 = mutableListOf(
434 "service-instance-id" to "siid_new_resolution",
435 "vnf-id" to "vnf_new_resolution",
436 "vnf_name" to "temp_vnf_new_resolution"
438 assertEquals(assignmentListForRequest1.size, assignmentList.size)
439 assertEquals(assignmentListForRequest2.size, assignmentList.size)
441 // NoNewResolution use case - resolution for request #2 returns the same resolution as
442 // assignmentListForRequest1 since no new resolution is/was actually performed.
443 var areEqual = assignmentListForRequest1.zip(assignmentList).all { (it1, it2) ->
444 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
446 assertEquals(true, areEqual)
448 areEqual = assignmentListForRequest2.zip(assignmentList).all { (it1, it2) ->
449 it1.first == it2.name && it1.second == it2.property?.value?.asText() ?: null
451 assertEquals(false, areEqual)
456 @Throws(Exception::class)
457 fun testResolveResources() {
458 val artifactNames = listOf("baseconfig", "another")
460 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
462 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
464 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
467 val executionServiceInput =
468 JacksonUtils.readValueFromClassPathFile(
469 "payload/requests/sample-resourceresolution-request.json",
470 ExecutionServiceInput::class.java
474 PayloadUtils.prepareInputsFromWorkflowPayload(
475 bluePrintRuntimeService,
476 executionServiceInput.payload,
477 "resource-assignment"
480 resourceResolutionService.resolveResources(
481 bluePrintRuntimeService,
482 "resource-assignment",
488 assertEquals(artifactNames.toSet(), it.templateMap.keys)
489 assertEquals(artifactNames.toSet(), it.assignmentMap.keys)
491 assertEquals("This is Sample Velocity Template", it.templateMap["another"])
492 assertEquals("vnf_1234", it.assignmentMap["another"]!!["vnf-id"]!!.asText())
497 @Throws(Exception::class)
498 fun testResolveResourcesWithMappingAndTemplate() {
500 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
502 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
504 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
507 val executionServiceInput =
508 JacksonUtils.readValueFromClassPathFile(
509 "payload/requests/sample-resourceresolution-request.json",
510 ExecutionServiceInput::class.java
513 val resourceAssignmentRuntimeService =
514 ResourceAssignmentUtils.transformToRARuntimeService(
515 bluePrintRuntimeService,
516 "testResolveResourcesWithMappingAndTemplate"
519 val artifactPrefix = "another"
522 PayloadUtils.prepareInputsFromWorkflowPayload(
523 bluePrintRuntimeService,
524 executionServiceInput.payload,
525 "resource-assignment"
529 resourceResolutionService.resolveResources(
530 resourceAssignmentRuntimeService,
531 "resource-assignment",
535 "Couldn't Resolve Resources for artifact $artifactPrefix"
541 @Throws(Exception::class)
542 fun testResolveResourcesResolutionSummary() {
544 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY] = true
545 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
547 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
549 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
552 val executionServiceInput =
553 JacksonUtils.readValueFromClassPathFile(
554 "payload/requests/sample-resourceresolution-request.json",
555 ExecutionServiceInput::class.java
558 val resourceAssignmentRuntimeService =
559 ResourceAssignmentUtils.transformToRARuntimeService(
560 bluePrintRuntimeService,
561 "testResolveResourcesWithMappingAndTemplate"
564 val artifactPrefix = "notemplate"
567 PayloadUtils.prepareInputsFromWorkflowPayload(
568 bluePrintRuntimeService,
569 executionServiceInput.payload,
570 "resource-assignment"
573 resourceResolutionService.resolveResources(
574 resourceAssignmentRuntimeService,
575 "resource-assignment",
580 val summaries = JacksonUtils.jsonNode(it.first)["resolution-summary"]
581 val list = JacksonUtils.getListFromJsonNode(summaries, ResolutionSummary::class.java)
582 assertEquals(list.size, 3)
587 @Throws(Exception::class)
588 fun testResolveResourcesWithoutTemplate() {
589 val artifactPrefix = "notemplate"
591 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
593 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
595 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
598 val executionServiceInput =
599 JacksonUtils.readValueFromClassPathFile(
600 "payload/requests/sample-resourceresolution-request.json",
601 ExecutionServiceInput::class.java
604 val resourceAssignmentRuntimeService =
605 ResourceAssignmentUtils.transformToRARuntimeService(
606 bluePrintRuntimeService,
607 "testResolveResourcesWithMappingAndTemplate"
611 PayloadUtils.prepareInputsFromWorkflowPayload(
612 bluePrintRuntimeService,
613 executionServiceInput.payload,
614 "resource-assignment"
617 resourceResolutionService.resolveResources(
618 resourceAssignmentRuntimeService,
619 "resource-assignment",
627 "service-instance-id" : "siid_1234",
628 "vnf-id" : "vnf_1234",
629 "vnf_name" : "temp_vnf"
634 val areEqual = it.second.first().name == "service-instance-id" &&
635 "siid_1234" == it.second.first().property?.value?.asText() ?: null
636 assertEquals(true, areEqual)
641 fun testResolveResourcesWithResourceIdAndResourceType() {
642 props[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY] = ""
644 Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService)
646 val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
648 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
651 val executionServiceInput =
652 JacksonUtils.readValueFromClassPathFile(
653 "payload/requests/sample-resourceresolution-request.json",
654 ExecutionServiceInput::class.java
657 val resourceAssignmentRuntimeService =
658 ResourceAssignmentUtils.transformToRARuntimeService(
659 bluePrintRuntimeService,
660 "testResolveResourcesWithMappingAndTemplate"
663 val artifactPrefix = "another"
666 PayloadUtils.prepareInputsFromWorkflowPayload(
667 bluePrintRuntimeService,
668 executionServiceInput.payload,
669 "resource-assignment"
673 resourceResolutionService.resolveResources(
674 resourceAssignmentRuntimeService,
675 "resource-assignment",
679 "Couldn't Resolve Resources for artifact $artifactPrefix"
685 fun testResourceResolutionForDefinition() {
686 val resourceDefinitions = BluePrintTypes.resourceDefinitions {
687 resourceDefinition(name = "port-speed", description = "Port Speed") {
688 property(type = "string", required = true)
690 sourceCapability(id = "sdno", description = "SDNO Source") {
692 type(BluePrintConstants.SCRIPT_KOTLIN)
693 scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!)
694 keyDependencies(arrayListOf("device-id"))
697 sourceDb(id = "sdnc", description = "SDNC Controller") {
699 endpointSelector("processor-db")
700 query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id")
702 map("device_id", "\$device-id")
704 keyDependencies(arrayListOf("device-id"))
709 resourceDefinition(name = "device-id", description = "Device Id") {
710 property(type = "string", required = true) {
712 sourceInput(id = "input", description = "Dependency Source") {}
718 val raRuntimeService = mockk<ResourceAssignmentRuntimeService>()
719 every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>()
720 every { raRuntimeService.getBluePrintError() } returns BluePrintError()
721 every { raRuntimeService.setBluePrintError(any()) } returns Unit
722 every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive()
723 every { raRuntimeService.putResolutionStore(any(), any()) } returns Unit
725 val applicationContext = mockk<ApplicationContext>()
726 every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA()
727 every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA()
728 every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA()
730 val sources = arrayListOf<String>("sdno", "sdnc")
732 val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk())
733 val resolvedResources = resourceResolutionService.resolveResourceDefinition(
735 resourceDefinitions, "port-speed", sources
737 assertNotNull(resolvedResources, "failed to resolve the resources")