2 * Copyright © 2019 Bell Canada.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots
19 import com.fasterxml.jackson.databind.JsonNode
20 import kotlinx.coroutines.runBlocking
21 import org.junit.Assert.assertEquals
22 import org.junit.Assert.assertTrue
23 import org.junit.Before
25 import org.junit.runner.RunWith
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertyConfiguration
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
29 import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration
30 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.DIFF_JSON
31 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.DIFF_XML
32 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.OPERATION_DIFF
33 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.OPERATION_FETCH
34 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.ComponentConfigSnapshotsExecutor.Companion.OPERATION_STORE
35 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db.ResourceConfigSnapshot
36 import org.onap.ccsdk.cds.blueprintsprocessor.functions.config.snapshots.db.ResourceConfigSnapshotService
37 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
38 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
39 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
40 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
41 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
42 import org.springframework.beans.factory.annotation.Autowired
43 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
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
49 @RunWith(SpringRunner::class)
50 @ContextConfiguration(
51 classes = [ResourceConfigSnapshotService::class,
52 BluePrintPropertyConfiguration::class, BluePrintPropertiesService::class,
53 BluePrintDBLibConfiguration::class, BluePrintLoadConfiguration::class]
55 @TestPropertySource(locations = ["classpath:application-test.properties"])
56 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor", "org.onap.ccsdk.cds.controllerblueprints"])
57 @EnableAutoConfiguration
58 @Suppress("SameParameterValue")
59 class ComponentConfigSnapshotsExecutorTest {
62 lateinit var cfgSnapshotService: ResourceConfigSnapshotService
63 lateinit var cfgSnapshotComponent: ComponentConfigSnapshotsExecutor
64 private var bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
66 "./../../../../components/model-catalog/blueprint-model/test-blueprint/remote_scripts"
69 private val resourceId = "1"
70 private val resourceType = "ServiceInstance"
71 private val props = mutableMapOf<String, JsonNode>()
72 private val nodeTemplateName = "nodeTemplateName"
74 private val executionRequest = ExecutionServiceInput()
78 cfgSnapshotComponent = ComponentConfigSnapshotsExecutor(cfgSnapshotService)
79 props[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_ID] = resourceId.asJsonPrimitive()
80 props[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_TYPE] = resourceType.asJsonPrimitive()
82 cfgSnapshotComponent.operationInputs = props
83 cfgSnapshotComponent.bluePrintRuntimeService = bluePrintRuntimeService
84 cfgSnapshotComponent.nodeTemplateName = nodeTemplateName
86 cfgSnapshotComponent.executionServiceInput = executionRequest
87 cfgSnapshotComponent.processId = "12"
88 cfgSnapshotComponent.workflowName = "workflow"
89 cfgSnapshotComponent.stepName = "step"
90 cfgSnapshotComponent.interfaceName = "interfaceName"
91 cfgSnapshotComponent.operationName = "operationName"
95 fun processNBFetchWithResourceIdAndResourceTypeSingleFind() {
96 val snapshot = ResourceConfigSnapshot()
97 val snapshotConfig = "TEST1"
98 snapshot.config_snapshot = snapshotConfig
104 cfgSnapshotService.write(snapshotConfig, resId, resType)
105 prepareRequestProperties(OPERATION_FETCH, resId, resType, ResourceConfigSnapshot.Status.RUNNING.name)
107 cfgSnapshotComponent.processNB(executionRequest)
108 } catch (e: BluePrintProcessorException) {
109 kotlin.test.assertEquals(
110 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
115 // then; we should get success and the TEST1 payload in our output properties
117 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
118 bluePrintRuntimeService.getNodeTemplateAttributeValue(
120 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
124 snapshotConfig.asJsonPrimitive(),
125 bluePrintRuntimeService.getNodeTemplateAttributeValue(
127 ComponentConfigSnapshotsExecutor.OUTPUT_SNAPSHOT
134 fun processNBFetchCandidateWithResourceIdAndResourceTypeSingleFind() {
135 val snapshot = ResourceConfigSnapshot()
136 val snapshotConfig = "TEST"
137 snapshot.config_snapshot = snapshotConfig
143 cfgSnapshotService.write(snapshotConfig, resId, resType, ResourceConfigSnapshot.Status.CANDIDATE)
144 prepareRequestProperties(OPERATION_FETCH, resId, resType, ResourceConfigSnapshot.Status.CANDIDATE.name)
146 cfgSnapshotComponent.processNB(executionRequest)
147 } catch (e: BluePrintProcessorException) {
148 kotlin.test.assertEquals(
149 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
154 // then; we should get success and the TEST payload in our output properties
156 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
157 bluePrintRuntimeService.getNodeTemplateAttributeValue(
159 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
163 snapshotConfig.asJsonPrimitive(),
164 bluePrintRuntimeService.getNodeTemplateAttributeValue(
166 ComponentConfigSnapshotsExecutor.OUTPUT_SNAPSHOT
173 fun processNBStoreWithResourceIdAndResourceType() {
174 val snapshot = ResourceConfigSnapshot()
175 val snapshotConfig = "PAYLOAD"
176 snapshot.config_snapshot = snapshotConfig
182 prepareRequestProperties(OPERATION_STORE, resId, resType, snapshotConfig)
184 cfgSnapshotComponent.processNB(executionRequest)
185 } catch (e: BluePrintProcessorException) {
186 kotlin.test.assertEquals(
187 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
193 // then; we should get success and the PAYLOAD payload in our output properties
195 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
196 bluePrintRuntimeService.getNodeTemplateAttributeValue(
198 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
202 snapshotConfig.asJsonPrimitive(),
203 bluePrintRuntimeService.getNodeTemplateAttributeValue(
205 ComponentConfigSnapshotsExecutor.OUTPUT_SNAPSHOT
212 fun processNBFetchNoneFound() {
215 // when; asking for unknown resource Id/ resource Type combo; should get an error response
217 prepareRequestProperties(OPERATION_FETCH, "asdasd", "PNF", ResourceConfigSnapshot.Status.RUNNING.name)
219 cfgSnapshotComponent.processNB(executionRequest)
220 } catch (e: BluePrintProcessorException) {
221 kotlin.test.assertEquals(
222 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
229 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
230 bluePrintRuntimeService.getNodeTemplateAttributeValue(
232 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
239 fun processNBErrorOperationUnknown() {
242 // when; asking for unknown operation update; should get an error response
244 prepareRequestProperties("update", "asdasd", "PNF", ResourceConfigSnapshot.Status.RUNNING.name)
246 cfgSnapshotComponent.processNB(executionRequest)
247 } catch (e: BluePrintProcessorException) {
248 kotlin.test.assertEquals(
249 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
255 // then; we should get error in our output properties
256 assertTrue(bluePrintRuntimeService.getBluePrintError().errors.size == 1)
258 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_ERROR.asJsonPrimitive(),
259 bluePrintRuntimeService.getNodeTemplateAttributeValue(
261 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
264 val msg = "Operation parameter must be fetch, store or diff"
266 msg.asJsonPrimitive(),
267 bluePrintRuntimeService.getNodeTemplateAttributeValue(
269 ComponentConfigSnapshotsExecutor.OUTPUT_MESSAGE
276 fun processNBErrorDiffContentTypeUnknown() {
279 // when; asking for unknown content type diff operation; should get an error response
283 cfgSnapshotService.write("snapshotConfig", resId, resType, ResourceConfigSnapshot.Status.CANDIDATE)
284 prepareRequestProperties(OPERATION_DIFF, resId, resType, "YANG")
286 cfgSnapshotComponent.processNB(executionRequest)
287 } catch (e: BluePrintProcessorException) {
288 kotlin.test.assertEquals(
289 "Can't proceed with the cfg snapshot lookup: provide resource-id and resource-type.",
295 // then; we should get error in our output properties
297 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_ERROR.asJsonPrimitive(),
298 bluePrintRuntimeService.getNodeTemplateAttributeValue(
300 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
303 val message = "Could not compare config snapshots for type YANG"
305 message.asJsonPrimitive(),
306 bluePrintRuntimeService.getNodeTemplateAttributeValue(
308 ComponentConfigSnapshotsExecutor.OUTPUT_MESSAGE
315 fun processNBCompareTwoJsonConfigSnapshots() {
319 // when; comparing RUNNING vs CANDIDATE json configs; should get an success response; with differences
323 preparePayload("config-payload-running.json", resId, resType, ResourceConfigSnapshot.Status.RUNNING)
324 preparePayload("config-payload-candidate.json", resId, resType, ResourceConfigSnapshot.Status.CANDIDATE)
326 prepareRequestProperties(OPERATION_DIFF, resId, resType, DIFF_JSON)
327 cfgSnapshotComponent.processNB(executionRequest)
328 } catch (e: BluePrintProcessorException) {
329 kotlin.test.assertEquals(
330 "Can't proceed with the cfg snapshot diff: provide resource-id and resource-type.",
336 // then; we should get success
337 assertTrue(bluePrintRuntimeService.getBluePrintError().errors.size == 0)
339 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
340 bluePrintRuntimeService.getNodeTemplateAttributeValue(
342 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
346 // then; we should get JSON-patches differences in our response property
348 "[{\"op\":\"add\",\"path\":\"/system-uptime-information/last-configured-time/new-child-object\",\"value\":{\"property\":\"value\"}}," +
349 "{\"op\":\"replace\",\"path\":\"/system-uptime-information/system-booted-time/time-length\",\"value\":\"14:52:54\"}," +
350 "{\"op\":\"replace\",\"path\":\"/system-uptime-information/time-source\",\"value\":\" DNS CLOCK \"}," +
351 "{\"op\":\"add\",\"path\":\"/system-uptime-information/uptime-information/load-average-10\",\"value\":\"0.05\"}]"
353 diffJson.asJsonPrimitive(),
354 bluePrintRuntimeService.getNodeTemplateAttributeValue(
356 ComponentConfigSnapshotsExecutor.OUTPUT_SNAPSHOT
363 fun processNBCompareTwoXmlConfigSnapshots() {
367 // when; comparing RUNNING vs CANDIDATE xml configs; should get an success response; with differences
371 preparePayload("interface-running.xml", resId, resType, ResourceConfigSnapshot.Status.RUNNING)
372 preparePayload("interface-candidate.xml", resId, resType, ResourceConfigSnapshot.Status.CANDIDATE)
374 prepareRequestProperties(OPERATION_DIFF, resId, resType, DIFF_XML)
376 cfgSnapshotComponent.processNB(executionRequest)
377 } catch (e: BluePrintProcessorException) {
378 kotlin.test.assertEquals(
379 "Can't proceed with the cfg snapshot diff: provide resource-id and resource-type.",
385 // then; we should get success
386 assertTrue(bluePrintRuntimeService.getBluePrintError().errors.size == 0)
388 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS_SUCCESS.asJsonPrimitive(),
389 bluePrintRuntimeService.getNodeTemplateAttributeValue(
391 ComponentConfigSnapshotsExecutor.OUTPUT_STATUS
395 // then; we should get XML-patches differences in our response property
396 val diffXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
398 "<replace sel=\"/output[1]/interface-information[1]/interface-flapped[1]/@seconds\">2343</replace>" +
399 "<replace sel=\"/output[1]/interface-information[1]/interface-flapped[1]/text()[1]\">34</replace>" +
400 "<replace sel=\"/output[1]/interface-information[1]/traffic-statistics[1]/input-packets[1]/text()[1]\">09098789</replace>" +
401 "<replace sel=\"/output[1]/interface-information[1]/traffic-statistics[1]/output-packets[1]/text()[1]\">2828828</replace>" +
402 "<add sel=\"/output[1]/interface-information[1]/physical-interface[1]\"><interface-name>TEGig400-int01</interface-name></add>" +
405 diffXml.asJsonPrimitive(),
406 bluePrintRuntimeService.getNodeTemplateAttributeValue(
408 ComponentConfigSnapshotsExecutor.OUTPUT_SNAPSHOT
414 private fun preparePayload(filename: String, resId: String, resType: String, status: ResourceConfigSnapshot.Status) {
416 cfgSnapshotService.write(JacksonUtils.getClassPathFileContent("payload/requests/$filename"), resId, resType, status)
420 private fun prepareRequestProperties(oper: String, resId: String, resType: String, optional: String = "") {
421 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_OPERATION] = oper.asJsonPrimitive()
422 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_ID] = resId.asJsonPrimitive()
423 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_TYPE] = resType.asJsonPrimitive()
426 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_DIFF_CONTENT_TYPE] = "".asJsonPrimitive()
427 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_SNAPSHOT] = "".asJsonPrimitive()
428 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_STATUS] =
429 ResourceConfigSnapshot.Status.RUNNING.name.asJsonPrimitive()
433 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_DIFF_CONTENT_TYPE] = optional.asJsonPrimitive()
435 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_SNAPSHOT] = optional.asJsonPrimitive()
437 cfgSnapshotComponent.operationInputs[ComponentConfigSnapshotsExecutor.INPUT_RESOURCE_STATUS] = optional.asJsonPrimitive()