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 one data operation request with #serviceName and Module Set Tag set.'() {
94 given: 'data operation request'
95 def dataOperationRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
96 def dataOperationRequest = jsonObjectMapper.convertJsonString(dataOperationRequestJsonData, DataOperationRequest.class)
97 and: '1 known cm handles: ch1-dmi1'
98 def yangModelCmHandles = getYangModelCmHandlesForOneCmHandle()
99 when: 'data operation request is processed'
100 def operationsOutPerDmiServiceName = ResourceDataOperationRequestUtils.processPerDefinitionInDataOperationsRequest(clientTopic,'request-id', dataOperationRequest, yangModelCmHandles)
101 and: 'converted to a json node'
102 def dmiDataOperationRequestBody = operationsOutPerDmiServiceName['dmi1']
103 def cmHandlesInRequestBody = dmiDataOperationRequestBody[0].cmHandles
104 then: 'it contains the correct operation details'
105 assert cmHandlesInRequestBody.size() == 1
106 assert cmHandlesInRequestBody[0].id == 'ch1-dmi1'
107 assert cmHandlesInRequestBody[0].moduleSetTag == 'module-set-tag1'
110 def 'Process per data operation request with non-ready, non-existing cm handle and publish event to client specified topic'() {
111 given: 'consumer subscribing to client topic'
112 def cloudEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties('test-1', CloudEventDeserializer))
113 cloudEventKafkaConsumer.subscribe([clientTopic])
114 and: 'data operation request having non-ready and non-existing cm handle ids'
115 def dataOperationRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
116 def dataOperationRequest = jsonObjectMapper.convertJsonString(dataOperationRequestJsonData, DataOperationRequest.class)
117 when: 'data operation request is processed'
118 ResourceDataOperationRequestUtils.processPerDefinitionInDataOperationsRequest(clientTopic, 'request-id', dataOperationRequest, yangModelCmHandles)
119 and: 'subscribed client specified topic is polled and first record is selected'
120 def consumerRecordOut = cloudEventKafkaConsumer.poll(Duration.ofMillis(1500)).last()
121 then: 'verify cloud compliant headers'
122 def consumerRecordOutHeaders = consumerRecordOut.headers()
123 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_id') != null
124 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_type') == dataOperationType
125 and: 'verify that extension is included into header'
126 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_correlationid') == 'request-id'
127 assert KafkaHeaders.getParsedKafkaHeader(consumerRecordOutHeaders, 'ce_destination') == clientTopic
128 and: 'map consumer record to expected event type'
129 def dataOperationResponseEvent =
130 toTargetEvent(consumerRecordOut.value(), DataOperationEvent.class)
131 and: 'data operation response event response size is 3'
132 dataOperationResponseEvent.data.responses.size() == 3
133 and: 'verify published data operation response as json string'
134 def dataOperationResponseEventJson = TestUtils.getResourceFileContent('dataOperationResponseEvent.json')
135 jsonObjectMapper.asJsonString(dataOperationResponseEvent.data.responses) == dataOperationResponseEventJson
138 def 'Publish error response for entire data operations request when async task fails'() {
139 given: 'consumer subscribing to client topic'
140 def cloudEventKafkaConsumer = new KafkaConsumer<>(eventConsumerConfigProperties(consumerGroupId, CloudEventDeserializer))
141 cloudEventKafkaConsumer.subscribe([clientTopic])
142 and: 'data operation request having non-ready and non-existing cm handle ids'
143 def dataOperationRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
144 def dataOperationRequest = jsonObjectMapper.convertJsonString(dataOperationRequestJsonData, DataOperationRequest.class)
145 when: 'an error occurs for the entire data operations request'
146 ResourceDataOperationRequestUtils.handleAsyncTaskCompletionForDataOperationsRequest(clientTopic, 'request-id', dataOperationRequest, exceptionThrown)
147 and: 'subscribed client specified topic is polled and first record is selected'
148 def consumerRecordOut = cloudEventKafkaConsumer.poll(Duration.ofMillis(1500)).last()
149 def dataOperationResponseEvent = toTargetEvent(consumerRecordOut.value(), DataOperationEvent.class)
150 then: 'data operation response event response size is 3'
151 dataOperationResponseEvent.data.responses.size() == 3
152 and: 'all 3 have the expected error code'
153 dataOperationResponseEvent.data.responses.each {
154 assert it.statusCode == errorReportedToClientTopic.code
157 scenario | exceptionThrown | consumerGroupId || errorReportedToClientTopic
158 'task timed out' | new TimeoutException() | 'test-2' || NcmpResponseStatus.DMI_SERVICE_NOT_RESPONDING
159 'unspecified error' | new RuntimeException() | 'test-3' || NcmpResponseStatus.UNKNOWN_ERROR
162 static def getYangModelCmHandles() {
163 def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
164 def readyState = new CompositeStateBuilder().withCmHandleState(CmHandleState.READY).withLastUpdatedTimeNow().build()
165 def advisedState = new CompositeStateBuilder().withCmHandleState(CmHandleState.ADVISED).withLastUpdatedTimeNow().build()
166 return [new YangModelCmHandle(id: 'ch1-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
167 new YangModelCmHandle(id: 'ch2-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
168 new YangModelCmHandle(id: 'ch6-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
169 new YangModelCmHandle(id: 'ch8-dmi1', dmiServiceName: 'dmi1', dmiProperties: dmiProperties, compositeState: readyState),
170 new YangModelCmHandle(id: 'ch3-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
171 new YangModelCmHandle(id: 'ch4-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
172 new YangModelCmHandle(id: 'ch7-dmi2', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: readyState),
173 new YangModelCmHandle(id: 'non-ready-cm-handle', dmiServiceName: 'dmi2', dmiProperties: dmiProperties, compositeState: advisedState)
177 static def getYangModelCmHandlesForOneCmHandle() {
178 def dmiProperties = [new YangModelCmHandle.Property('prop', 'some DMI property')]
179 def readyState = new CompositeStateBuilder().withCmHandleState(CmHandleState.READY).withLastUpdatedTimeNow().build()
180 return [new YangModelCmHandle(id: 'ch1-dmi1', dmiServiceName: 'dmi1', moduleSetTag: 'module-set-tag1', dmiProperties: dmiProperties, compositeState: readyState)]