2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2023 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * ================================================================================
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.impl.operations
24 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_OPERATIONAL
25 import static org.onap.cps.ncmp.api.impl.operations.DatastoreType.PASSTHROUGH_RUNNING
26 import static org.onap.cps.ncmp.api.impl.operations.OperationType.CREATE
27 import static org.onap.cps.ncmp.api.impl.operations.OperationType.READ
28 import static org.onap.cps.ncmp.api.impl.operations.OperationType.UPDATE
29 import static org.onap.cps.ncmp.api.impl.events.mapper.CloudEventMapper.toTargetEvent
30 import static org.onap.cps.ncmp.api.NcmpResponseStatus.UNABLE_TO_READ_RESOURCE_DATA
31 import static org.onap.cps.ncmp.api.NcmpResponseStatus.DMI_SERVICE_NOT_RESPONDING
33 import com.fasterxml.jackson.databind.ObjectMapper
34 import org.onap.cps.ncmp.api.impl.config.NcmpConfiguration
35 import org.onap.cps.ncmp.api.impl.events.EventsPublisher
36 import org.onap.cps.ncmp.api.impl.exception.HttpClientRequestException
37 import org.onap.cps.ncmp.api.impl.utils.DmiServiceUrlBuilder
38 import org.onap.cps.ncmp.api.impl.utils.context.CpsApplicationContext
39 import org.onap.cps.ncmp.api.models.DataOperationRequest
40 import org.onap.cps.ncmp.events.async1_0_0.DataOperationEvent
41 import org.onap.cps.ncmp.utils.TestUtils
42 import org.onap.cps.utils.JsonObjectMapper
43 import org.spockframework.spring.SpringBean
44 import org.springframework.beans.factory.annotation.Autowired
45 import org.springframework.boot.test.context.SpringBootTest
46 import org.springframework.http.ResponseEntity
47 import org.springframework.test.context.ContextConfiguration
48 import org.springframework.http.HttpStatus
49 import spock.lang.Shared
50 import java.util.concurrent.TimeoutException
53 @ContextConfiguration(classes = [EventsPublisher, CpsApplicationContext, NcmpConfiguration.DmiProperties, DmiDataOperations])
54 class DmiDataOperationsSpec extends DmiOperationsBaseSpec {
57 DmiServiceUrlBuilder dmiServiceUrlBuilder = Mock()
58 def dmiServiceBaseUrl = "${dmiServiceName}/dmi/v1/ch/${cmHandleId}/data/ds/ncmp-datastore:"
60 def NO_REQUEST_ID = null
62 def OPTIONS_PARAM = '(a=1,b=2)'
65 JsonObjectMapper spiedJsonObjectMapper = Spy(new JsonObjectMapper(new ObjectMapper()))
68 DmiDataOperations objectUnderTest
71 EventsPublisher eventsPublisher = Stub()
73 def 'call get resource data for #expectedDatastoreInUrl from DMI without topic #scenario.'() {
74 given: 'a cm handle for #cmHandleId'
75 mockYangModelCmHandleRetrieval(dmiProperties)
76 and: 'a positive response from DMI service when it is called with the expected parameters'
77 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
78 def expectedUrl = dmiServiceBaseUrl + "${expectedDatastoreInUrl}?resourceIdentifier=${resourceIdentifier}${expectedOptionsInUrl}"
79 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, READ) >> responseFromDmi
80 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
81 when: 'get resource data is invoked'
82 def result = objectUnderTest.getResourceDataFromDmi(dataStore.datastoreName, cmHandleId, resourceIdentifier,
83 options, NO_TOPIC, NO_REQUEST_ID)
84 then: 'the result is the response from the DMI service'
85 assert result == responseFromDmi
86 where: 'the following parameters are used'
87 scenario | dmiProperties | dataStore | options || expectedJson | expectedDatastoreInUrl | expectedOptionsInUrl
88 'without properties' | [] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
89 'with properties' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | '&options=(a=1,b=2)'
90 'null options' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | null || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
91 'empty options' | [yangModelCmHandleProperty] | PASSTHROUGH_OPERATIONAL | '' || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-operational' | ''
92 'datastore running without properties' | [] | PASSTHROUGH_RUNNING | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{}}' | 'passthrough-running' | '&options=(a=1,b=2)'
93 'datastore running with properties' | [yangModelCmHandleProperty] | PASSTHROUGH_RUNNING | OPTIONS_PARAM || '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}' | 'passthrough-running' | '&options=(a=1,b=2)'
96 def 'Execute (async) data operation from DMI service.'() {
97 given: 'collection of yang model cm Handles and data operation request'
98 mockYangModelCmHandleCollectionRetrieval([yangModelCmHandleProperty])
99 def dataOperationBatchRequestJsonData = TestUtils.getResourceFileContent('dataOperationRequest.json')
100 def dataOperationRequest = spiedJsonObjectMapper.convertJsonString(dataOperationBatchRequestJsonData, DataOperationRequest.class)
101 dataOperationRequest.dataOperationDefinitions[0].cmHandleIds = [cmHandleId]
102 def requestBodyAsJsonStringArg = null
103 and: 'a positive response from DMI service when it is called with valid request parameters'
104 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.ACCEPTED)
105 def expectedDmiBatchResourceDataUrl = "ncmp/v1/data/topic=my-topic-name"
106 def expectedBatchRequestAsJson = '{"operations":[{"operation":"read","operationId":"operational-14","datastore":"ncmp-datastore:passthrough-operational","options":"some option","resourceIdentifier":"some resource identifier","cmHandles":[{"id":"some-cm-handle","cmHandleProperties":{"prop1":"val1"}}]}]}'
107 mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, _, READ.operationName) >> responseFromDmi
108 dmiServiceUrlBuilder.getDataOperationRequestUrl(_, _) >> expectedDmiBatchResourceDataUrl
109 when: 'get resource data for group of cm handles are invoked'
110 objectUnderTest.requestResourceDataFromDmi('my-topic-name', dataOperationRequest, 'requestId')
111 then: 'wait a little to allow execution of service method by task executor (on separate thread)'
113 then: 'validate ncmp generated dmi request body json args'
114 1 * mockDmiRestClient.postOperationWithJsonData(expectedDmiBatchResourceDataUrl, _, READ) >> { args -> requestBodyAsJsonStringArg = args[1] }
115 assert requestBodyAsJsonStringArg == expectedBatchRequestAsJson
118 def 'Execute (async) data operation from DMI service for #scenario.'() {
119 given: 'data operation request body and dmi resource url'
120 def dmiDataOperation = DmiDataOperation.builder().operationId('some-operation-id').build()
121 dmiDataOperation.getCmHandles().add(CmHandle.builder().id('some-cm-handle-id').build())
122 def dmiDataOperationResourceDataUrl = "http://dmi-service-name:dmi-port/dmi/v1/data?topic=my-topic-name&requestId=some-request-id"
123 def actualDataOperationCloudEvent = null
124 when: 'exception occurs after sending request to dmi service'
125 objectUnderTest.handleTaskCompletionException(new Throwable(exception), dmiDataOperationResourceDataUrl, List.of(dmiDataOperation))
126 then: 'a cloud event is published'
127 eventsPublisher.publishCloudEvent('my-topic-name', 'some-request-id', _) >> { args -> actualDataOperationCloudEvent = args[2] }
128 and: 'the event contains the expected error details'
129 def eventDataValue = extractDataValue(actualDataOperationCloudEvent)
130 assert eventDataValue.operationId == dmiDataOperation.operationId
131 assert eventDataValue.ids == dmiDataOperation.cmHandles.id
132 assert eventDataValue.statusCode == responseCode.code
133 assert eventDataValue.statusMessage == responseCode.message
134 where: 'the following exceptions are occurred'
135 scenario | exception || responseCode
136 'http client request exception' | new HttpClientRequestException('error-message', 'error-details', HttpStatus.SERVICE_UNAVAILABLE.value()) || UNABLE_TO_READ_RESOURCE_DATA
137 'timeout exception' | new TimeoutException() || DMI_SERVICE_NOT_RESPONDING
140 def 'call get all resource data.'() {
141 given: 'the system returns a cm handle with a sample property'
142 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
143 and: 'a positive response from DMI service when it is called with the expected parameters'
144 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
145 def expectedUrl = dmiServiceBaseUrl + "passthrough-operational?resourceIdentifier=/"
146 mockDmiRestClient.postOperationWithJsonData(expectedUrl, '{"operation":"read","cmHandleProperties":{"prop1":"val1"}}', READ) >> responseFromDmi
147 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
148 when: 'get resource data is invoked'
149 def result = objectUnderTest.getResourceDataFromDmi( PASSTHROUGH_OPERATIONAL.datastoreName, cmHandleId, NO_REQUEST_ID)
150 then: 'the result is the response from the DMI service'
151 assert result == responseFromDmi
154 def 'Write data for pass-through:running datastore in DMI.'() {
155 given: 'a cm handle for #cmHandleId'
156 mockYangModelCmHandleRetrieval([yangModelCmHandleProperty])
157 and: 'a positive response from DMI service when it is called with the expected parameters'
158 def expectedUrl = dmiServiceBaseUrl + "passthrough-running?resourceIdentifier=${resourceIdentifier}"
159 def expectedJson = '{"operation":"' + expectedOperationInUrl + '","dataType":"some data type","data":"requestData","cmHandleProperties":{"prop1":"val1"}}'
160 def responseFromDmi = new ResponseEntity<Object>(HttpStatus.OK)
161 dmiServiceUrlBuilder.getDmiDatastoreUrl(_, _) >> expectedUrl
162 mockDmiRestClient.postOperationWithJsonData(expectedUrl, expectedJson, operation) >> responseFromDmi
163 when: 'write resource method is invoked'
164 def result = objectUnderTest.writeResourceDataPassThroughRunningFromDmi(cmHandleId, 'parent/child', operation, 'requestData', 'some data type')
165 then: 'the result is the response from the DMI service'
166 assert result == responseFromDmi
167 where: 'the following operation is performed'
168 operation || expectedOperationInUrl
173 def extractDataValue(actualDataOperationCloudEvent) {
174 return toTargetEvent(actualDataOperationCloudEvent, DataOperationEvent.class).data.responses[0]