2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2024 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.utils.data.operation
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import io.cloudevents.CloudEvent
25 import io.cloudevents.kafka.CloudEventDeserializer
26 import io.cloudevents.kafka.impl.KafkaHeaders
27 import org.apache.kafka.clients.consumer.KafkaConsumer
28 import org.onap.cps.events.EventsPublisher
29 import org.onap.cps.ncmp.api.NcmpResponseStatus
30 import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext
31 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
32 import org.onap.cps.ncmp.api.impl.inventory.CmHandleState
33 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder
34 import org.onap.cps.ncmp.api.kafka.MessagingBaseSpec
35 import org.onap.cps.ncmp.api.models.DataOperationRequest
36 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent
37 import org.onap.cps.ncmp.utils.TestUtils
38 import org.onap.cps.utils.JsonObjectMapper
39 import org.spockframework.spring.SpringBean
40 import org.springframework.beans.factory.annotation.Autowired
41 import org.springframework.test.context.ContextConfiguration
43 import java.time.Duration
44 import java.util.concurrent.TimeoutException
46 import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent
48 @ContextConfiguration(classes = [EventsPublisher, CpsApplicationContext, ObjectMapper])
49 class ResourceDataOperationRequestUtilsSpec extends MessagingBaseSpec {
51 def static clientTopic = 'my-topic-name'
52 def static dataOperationType = 'org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent'
55 JsonObjectMapper jsonObjectMapper = new JsonObjectMapper(new ObjectMapper())
58 EventsPublisher eventPublisher = new EventsPublisher<CloudEvent>(legacyEventKafkaTemplate, cloudEventKafkaTemplate)
61 ObjectMapper objectMapper
63 def 'Process per data operation request with #serviceName.'() {
64 given: 'data operation request with 3 operations'
65 def dataOperationRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
66 def dataOperationRequest = jsonObjectMapper.convertJsonString(dataOperationRequestJsonData, DataOperationRequest.class)
67 and: '4 known cm handles: ch1-dmi1, ch2-dmi1, ch3-dmi2, ch4-dmi2'
68 def yangModelCmHandles = getYangModelCmHandles()
69 when: 'data operation request is processed'
70 def operationsOutPerDmiServiceName = ResourceDataOperationRequestUtils.processPerDefinitionInDataOperationsRequest(clientTopic,'request-id', dataOperationRequest, yangModelCmHandles)
71 and: 'converted to a json node'
72 def dmiDataOperationRequestBody = jsonObjectMapper.asJsonString(operationsOutPerDmiServiceName.get(serviceName))
73 def dmiDataOperationRequestBodyAsJsonNode = jsonObjectMapper.convertToJsonNode(dmiDataOperationRequestBody).get(operationIndex)
74 then: 'it contains the correct operation details'
75 assert dmiDataOperationRequestBodyAsJsonNode.get('operation').asText() == 'read'
76 assert dmiDataOperationRequestBodyAsJsonNode.get('operationId').asText() == expectedOperationId
77 assert dmiDataOperationRequestBodyAsJsonNode.get('datastore').asText() == expectedDatastore
78 and: 'the correct cm handles (just for #serviceName)'
79 assert dmiDataOperationRequestBodyAsJsonNode.get('cmHandles').size() == expectedCmHandleIds.size()
80 expectedCmHandleIds.each {
81 dmiDataOperationRequestBodyAsJsonNode.get('cmHandles').toString().contains(it)
83 where: 'the following dmi service and operations are checked'
84 serviceName | operationIndex || expectedOperationId | expectedDatastore | expectedCmHandleIds
85 'dmi1' | 0 || 'operational-14' | 'ncmp-datastore:passthrough-operational' | ['ch6-dmi1']
86 'dmi1' | 1 || 'running-12' | 'ncmp-datastore:passthrough-running' | ['ch1-dmi1', 'ch2-dmi1']
87 'dmi1' | 2 || 'operational-15' | 'ncmp-datastore:passthrough-operational' | ['ch6-dmi1']
88 'dmi2' | 0 || 'operational-14' | 'ncmp-datastore:passthrough-operational' | ['ch3-dmi2']
89 'dmi2' | 1 || 'running-12' | 'ncmp-datastore:passthrough-running' | ['ch7-dmi2']
90 'dmi2' | 2 || 'operational-15' | 'ncmp-datastore:passthrough-operational' | ['ch4-dmi2']
93 def 'Process per data operation request with non-ready, non-existing cm handle and publish event to client specified topic'() {
94 given: 'consumer subscribing to client topic'
95 def cloudEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('test-1', CloudEventDeserializer))
96 cloudEventKafkaConsumer.subscribe([clientTopic])
97 and: 'data operation request having non-ready and non-existing cm handle ids'
98 def dataOperationRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
99 def dataOperationRequest = jsonObjectMapper.convertJsonString(dataOperationRequestJsonData, DataOperationRequest.class)
100 when: 'data operation request is processed'
101 ResourceDataOperationRequestUtils.processPerDefinitionInDataOperationsRequest(clientTopic, 'request-id', dataOperationRequest, yangModelCmHandles)
102 and: 'subscribed client specified topic is polled and first record is selected'
103 def consumerRecordOut = cloudEventKafkaConsumer.poll(Duration.ofMillis(1500)).last()
104 then: 'verify cloud compliant headers'
105 def consumerRecordOutHeaders = consumerRecordOut.headers()
106 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_id') != null
107 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_type') == dataOperationType
108 and: 'verify that extension is included into header'
109 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_correlationid') == 'request-id'
110 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_destination') == clientTopic
111 and: 'map consumer record to expected event type'
112 def dataOperationResponseEvent =
113 toTargetEvent(consumerRecordOut.value(), DataOperationEvent.class)
114 and: 'data operation response event response size is 3'
115 dataOperationResponseEvent.data.responses.size() == 3
116 and: 'verify published data operation response as json string'
117 def dataOperationResponseEventJson = TestUtils.getResourceFileContent('dataOperationResponseEvent.json')
118 jsonObjectMapper.asJsonString(dataOperationResponseEvent.data.responses) == dataOperationResponseEventJson
121 def 'Publish error response for entire data operations request when async task fails'() {
122 given: 'consumer subscribing to client topic'
123 def cloudEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties(consumerGroupId, CloudEventDeserializer))
124 cloudEventKafkaConsumer.subscribe([clientTopic])
125 and: 'data operation request having non-ready and non-existing cm handle ids'
126 def dataOperationRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
127 def dataOperationRequest = jsonObjectMapper.convertJsonString(dataOperationRequestJsonData, DataOperationRequest.class)
128 when: 'an error occurs for the entire data operations request'
129 ResourceDataOperationRequestUtils.handleAsyncTaskCompletionForDataOperationsRequest(clientTopic, 'request-id', dataOperationRequest, exceptionThrown)
130 and: 'subscribed client specified topic is polled and first record is selected'
131 def consumerRecordOut = cloudEventKafkaConsumer.poll(Duration.ofMillis(1500)).last()
132 def dataOperationResponseEvent = toTargetEvent(consumerRecordOut.value(), DataOperationEvent.class)
133 then: 'data operation response event response size is 3'
134 dataOperationResponseEvent.data.responses.size() == 3
135 and: 'all 3 have the expected error code'
136 dataOperationResponseEvent.data.responses.each {
137 assert it.statusCode == errorReportedToClientTopic.code
140 scenario | exceptionThrown | consumerGroupId || errorReportedToClientTopic
141 'task timed out' | new TimeoutException() | 'test-2' || NcmpResponseStatus.DMI_SERVICE_NOT_RESPONDING
142 'unspecified error' | new RuntimeException() | 'test-3' || NcmpResponseStatus.UNKNOWN_ERROR
145 static def getYangModelCmHandles() {
146 def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
147 def readyState = new CompositeStateBuilder().withCmHandleState(CmHandleState.READY).withLastUpdatedTimeNow().build()
148 def advisedState = new CompositeStateBuilder().withCmHandleState(CmHandleState.ADVISED).withLastUpdatedTimeNow().build()
149 return [new YangModelCmHandle(id: 'ch1-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
150 new YangModelCmHandle(id: 'ch2-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
151 new YangModelCmHandle(id: 'ch6-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
152 new YangModelCmHandle(id: 'ch8-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
153 new YangModelCmHandle(id: 'ch3-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
154 new YangModelCmHandle(id: 'ch4-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
155 new YangModelCmHandle(id: 'ch7-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
156 new YangModelCmHandle(id: 'non-ready-cm-handle', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: advisedState)