2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.sdnc.northbound;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 import static org.onap.sdnc.northbound.GenericResourceApiProvider.APP_NAME;
28 import static org.onap.sdnc.northbound.GenericResourceApiProvider.NO_SERVICE_LOGIC_ACTIVE;
29 import static org.onap.sdnc.northbound.GenericResourceApiProvider.NULL_OR_EMPTY_ERROR_PARAM;
30 import static org.onap.sdnc.northbound.util.MDSALUtil.exec;
31 import static org.onap.sdnc.northbound.util.MDSALUtil.requestInformation;
32 import static org.onap.sdnc.northbound.util.MDSALUtil.sdncRequestHeader;
33 import static org.onap.sdnc.northbound.util.MDSALUtil.service;
34 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceData;
35 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceInformationBuilder;
36 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceResponseInformation;
37 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceStatus;
38 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationInput;
39 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationOutput;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.Mockito;
45 import org.mockito.runners.MockitoJUnitRunner;
46 import org.onap.sdnc.northbound.util.PropBuilder;
47 import org.opendaylight.mdsal.binding.api.DataBroker;
48 import org.opendaylight.mdsal.binding.api.TransactionChainClosedException;
49 import org.opendaylight.mdsal.binding.api.WriteTransaction;
50 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
51 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationInput;
52 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationOutput;
53 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformation;
54 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader;
55 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader.SvcAction;
56 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.information.ServiceInformation;
57 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.Service;
58 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatus;
59 import org.opendaylight.yangtools.yang.common.RpcResult;
63 * This class test the ServiceTopologyOperation mdsal RPC.
65 @RunWith(MockitoJUnitRunner.class)
66 public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderTest {
68 final String SVC_OPERATION = "service-topology-operation";
71 public void setUp() throws Exception {
73 svcClient.setScvOperation(SVC_OPERATION);
77 * Verify ServiceTopologyOperation RPC executes a DG then produces the expected {@link
78 * ServiceTopologyOperationOutput} and persisted the expected {@link Service} in the {@link DataBroker}
81 public void testServiceTopologyOperationRPC_ExecuteDG_Success() throws Exception {
83 //mock svcClient to perform a successful execution with the expected parameters
84 svcClient.mockHasGraph(true);
85 PropBuilder svcResultProp = svcClient.createExecuteOKResult();
86 svcClient.mockExecute(svcResultProp);
88 // create the ServiceTopologyOperationInput from the template
89 ServiceTopologyOperationInput input = createSTOI();
91 //execute the mdsal exec
92 ServiceTopologyOperationOutput output = exec(
93 genericResourceApiProvider::serviceTopologyOperation
95 , RpcResult::getResult
98 assertEquals("200", output.getResponseCode());
99 assertEquals("OK", output.getResponseMessage());
100 assertEquals("Y", output.getAckFinalIndicator());
102 //verify the returned ServiceTopologyOperationOutput
103 ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput = createExpectedSTOO(svcResultProp,
105 assertEquals(expectedServiceTopologyOperationOutput, output);
107 //verify the persisted Service
108 Service actualService = db.read(input.getServiceInformation().getServiceInstanceId(),
109 LogicalDatastoreType.CONFIGURATION);
110 Service expectedService = createExpectedService(
111 expectedServiceTopologyOperationOutput,
114 assertEquals(expectedService, actualService);
120 public void should_fail_when_service_info_not_present() throws Exception {
121 // create the ServiceTopologyOperationInput from the template
122 ServiceTopologyOperationInput input =
123 serviceTopologyOperationInput()
124 .setSdncRequestHeader(sdncRequestHeader()
125 .setSvcRequestId("svc-request-id: xyz")
126 .setSvcAction(SvcAction.Assign).build()
128 .setRequestInformation(requestInformation()
129 .setRequestId("request-id: xyz")
130 .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance).build()
133 //execute the mdsal exec
134 ServiceTopologyOperationOutput output = exec(
135 genericResourceApiProvider::serviceTopologyOperation
137 , RpcResult::getResult
140 assertEquals("404", output.getResponseCode());
141 assertEquals(NULL_OR_EMPTY_ERROR_PARAM, output.getResponseMessage());
142 assertEquals("Y", output.getAckFinalIndicator());
147 public void should_fail_when_client_execution_failed() throws Exception {
149 svcClient.mockHasGraph(true);
150 svcClient.mockExecute(new RuntimeException("test exception"));
152 ServiceTopologyOperationInput input = createSTOI();
154 //execute the mdsal exec
155 ServiceTopologyOperationOutput output = exec(
156 genericResourceApiProvider::serviceTopologyOperation
158 , RpcResult::getResult
161 assertEquals("500", output.getResponseCode());
162 assertEquals("test exception", output.getResponseMessage());
163 assertEquals("Y", output.getAckFinalIndicator());
167 public void delete_fail_when_client_execution_failed() throws Exception {
169 //mock svcClient to perform a successful execution with the expected parameters
170 svcClient.mockHasGraph(true);
171 PropBuilder svcResultProp = svcClient.createExecuteOKResult();
172 svcClient.mockExecute(svcResultProp);
174 ServiceTopologyOperationInput input = deleteSTOI();
176 //execute the mdsal exec
177 ServiceTopologyOperationOutput output = exec(
178 genericResourceApiProvider::serviceTopologyOperation
180 , RpcResult::getResult
183 assertEquals("200", output.getResponseCode());
184 assertEquals("OK", output.getResponseMessage());
185 assertEquals("Y", output.getAckFinalIndicator());
189 public void delete_service_fail_when_client_execution_failed() throws Exception {
191 //mock svcClient to perform a successful execution with the expected parameters
192 svcClient.mockHasGraph(true);
193 PropBuilder svcResultProp = svcClient.createExecuteOKResult();
194 svcClient.mockExecute(svcResultProp);
196 ServiceTopologyOperationInput input = deleteServiceSTOI();
198 //execute the mdsal exec
199 ServiceTopologyOperationOutput output = exec(
200 genericResourceApiProvider::serviceTopologyOperation
202 , RpcResult::getResult
205 assertEquals("200", output.getResponseCode());
206 assertEquals("Y", output.getAckFinalIndicator());
210 public void should_fail_when_client_has_no_graph() throws Exception {
211 svcClient.mockHasGraph(false);
213 ServiceTopologyOperationInput input = createSTOI();
215 //execute the mdsal exec
216 ServiceTopologyOperationOutput output = exec(
217 genericResourceApiProvider::serviceTopologyOperation
219 , RpcResult::getResult
222 assertEquals("503", output.getResponseCode());
223 assertEquals(NO_SERVICE_LOGIC_ACTIVE + APP_NAME + ": '" + SVC_OPERATION + "'", output.getResponseMessage());
224 assertEquals("Y", output.getAckFinalIndicator());
229 public void should_fail_when_failed_to_update_mdsal() throws Exception {
231 svcClient.mockHasGraph(true);
232 WriteTransaction mockWriteTransaction = mock(WriteTransaction.class);
233 when(mockWriteTransaction.commit()).thenThrow(new TransactionChainClosedException("test exception"));
235 DataBroker spyDataBroker = Mockito.spy(dataBroker);
236 when(spyDataBroker.newWriteOnlyTransaction()).thenReturn(mockWriteTransaction);
237 genericResourceApiProvider.setDataBroker(spyDataBroker);
239 ServiceTopologyOperationInput input = createSTOI();
241 //execute the mdsal exec
242 ServiceTopologyOperationOutput output = exec(
243 genericResourceApiProvider::serviceTopologyOperation
245 , RpcResult::getResult
248 assertEquals("500", output.getResponseCode());
249 assertEquals("test exception", output.getResponseMessage());
250 assertEquals("Y", output.getAckFinalIndicator());
253 private ServiceTopologyOperationInput createSTOI() {
256 serviceTopologyOperationInput()
257 .setSdncRequestHeader(sdncRequestHeader()
258 .setSvcRequestId("svc-request-id: xyz")
259 .setSvcAction(SvcAction.Assign).build()
261 .setRequestInformation(requestInformation()
262 .setRequestId("request-id: xyz")
263 .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance).build()
265 .setServiceInformation(serviceInformationBuilder()
266 .setServiceInstanceId("service-instance-id: xyz").build()
270 private ServiceTopologyOperationInput deleteSTOI() {
273 serviceTopologyOperationInput()
274 .setSdncRequestHeader(sdncRequestHeader()
275 .setSvcRequestId("svc-request-id: xyz")
276 .setSvcAction(SvcAction.Unassign).build()
278 .setRequestInformation(requestInformation()
279 .setRequestId("request-id: xyz")
280 .setRequestAction(RequestInformation.RequestAction.DeleteServiceInstance).build()
282 .setServiceInformation(serviceInformationBuilder()
283 .setServiceInstanceId("service-instance-id: xyz").build()
287 private ServiceTopologyOperationInput deleteServiceSTOI() {
290 serviceTopologyOperationInput()
291 .setSdncRequestHeader(sdncRequestHeader()
292 .setSvcRequestId("svc-request-id: xyz")
293 .setSvcAction(SvcAction.Delete).build()
295 .setRequestInformation(requestInformation()
296 .setRequestId("request-id: xyz")
297 .setRequestAction(RequestInformation.RequestAction.DeleteServiceInstance).build()
299 .setServiceInformation(serviceInformationBuilder()
300 .setServiceInstanceId("service-instance-id: xyz").build()
305 private ServiceTopologyOperationOutput createExpectedSTOO(PropBuilder expectedSvcResultProp,
306 ServiceTopologyOperationInput expectedServiceTopologyOperationInput) {
308 serviceTopologyOperationOutput()
309 .setSvcRequestId(expectedServiceTopologyOperationInput.getSdncRequestHeader().getSvcRequestId())
310 .setResponseCode(expectedSvcResultProp.get(svcClient.errorCode))
311 .setAckFinalIndicator(expectedSvcResultProp.get(svcClient.ackFinal))
312 .setResponseMessage(expectedSvcResultProp.get(svcClient.errorMessage))
313 .setServiceResponseInformation(serviceResponseInformation()
314 .setInstanceId(expectedServiceTopologyOperationInput.getServiceInformation().getServiceInstanceId())
315 .setObjectPath(expectedSvcResultProp.get(svcClient.serviceObjectPath)).build()
319 private Service createExpectedService(
320 ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput,
321 ServiceTopologyOperationInput expectedServiceTopologyOperationInput,
322 Service actualService
325 //We cannot predict the timeStamp value so just steal it from the actual
326 //we need this to prevent the equals method from returning false as a result of the timestamp
327 String responseTimeStamp = actualService == null || actualService.getServiceStatus() == null ?
328 null : actualService.getServiceStatus().getResponseTimestamp();
330 SdncRequestHeader expectedSdncRequestHeader = expectedServiceTopologyOperationInput.getSdncRequestHeader();
331 ServiceInformation expectedServiceInformation = expectedServiceTopologyOperationInput.getServiceInformation();
332 RequestInformation expectedRequestInformation = expectedServiceTopologyOperationInput.getRequestInformation();
336 .setServiceInstanceId(expectedServiceInformation.getServiceInstanceId())
337 .setServiceData(serviceData().build())
340 .setAction(expectedRequestInformation.getRequestAction().name())
341 .setFinalIndicator(expectedServiceTopologyOperationOutput.getAckFinalIndicator())
342 .setResponseCode(expectedServiceTopologyOperationOutput.getResponseCode())
343 .setResponseMessage(expectedServiceTopologyOperationOutput.getResponseMessage())
344 .setRpcAction(toRpcAction(expectedSdncRequestHeader.getSvcAction()))
345 .setRpcName(SVC_OPERATION)
346 .setRequestStatus(ServiceStatus.RequestStatus.Synccomplete)
347 .setResponseTimestamp(responseTimeStamp).build()
352 public ServiceStatus.RpcAction toRpcAction(SvcAction fromEnum) {
353 return fromEnum == null ? null : ServiceStatus.RpcAction.valueOf(fromEnum.name());