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.build;
31 import static org.onap.sdnc.northbound.util.MDSALUtil.exec;
32 import static org.onap.sdnc.northbound.util.MDSALUtil.requestInformation;
33 import static org.onap.sdnc.northbound.util.MDSALUtil.sdncRequestHeader;
34 import static org.onap.sdnc.northbound.util.MDSALUtil.service;
35 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceData;
36 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceInformationBuilder;
37 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceResponseInformation;
38 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceStatus;
39 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationInput;
40 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceTopologyOperationOutput;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mockito;
46 import org.mockito.runners.MockitoJUnitRunner;
47 import org.onap.sdnc.northbound.util.PropBuilder;
48 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
49 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
50 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
51 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainClosedException;
52 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationInput;
53 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.ServiceTopologyOperationOutput;
54 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformation;
55 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader;
56 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader.SvcAction;
57 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.information.ServiceInformation;
58 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.Service;
59 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatus;
60 import org.opendaylight.yangtools.yang.common.RpcResult;
64 * This class test the ServiceTopologyOperation mdsal RPC.
66 @RunWith(MockitoJUnitRunner.class)
67 public class ServiceTopologyOperationRPCTest extends GenericResourceApiProviderTest {
69 final String SVC_OPERATION = "service-topology-operation";
72 public void setUp() throws Exception {
74 svcClient.setScvOperation(SVC_OPERATION);
78 * Verify ServiceTopologyOperation RPC executes a DG then produces the expected {@link
79 * ServiceTopologyOperationOutput} and persisted the expected {@link Service} in the {@link DataBroker}
82 public void testServiceTopologyOperationRPC_ExecuteDG_Success() throws Exception {
84 //mock svcClient to perform a successful execution with the expected parameters
85 svcClient.mockHasGraph(true);
86 PropBuilder svcResultProp = svcClient.createExecuteOKResult();
87 svcClient.mockExecute(svcResultProp);
89 // create the ServiceTopologyOperationInput from the template
90 ServiceTopologyOperationInput input = createSTOI();
92 //execute the mdsal exec
93 ServiceTopologyOperationOutput output = exec(
94 genericResourceApiProvider::serviceTopologyOperation
96 , RpcResult::getResult
99 assertEquals("200", output.getResponseCode());
100 assertEquals("OK", output.getResponseMessage());
101 assertEquals("Y", output.getAckFinalIndicator());
103 //verify the returned ServiceTopologyOperationOutput
104 ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput = createExpectedSTOO(svcResultProp,
106 assertEquals(expectedServiceTopologyOperationOutput, output);
108 //verify the persisted Service
109 Service actualService = db.read(input.getServiceInformation().getServiceInstanceId(),
110 LogicalDatastoreType.CONFIGURATION);
111 Service expectedService = createExpectedService(
112 expectedServiceTopologyOperationOutput,
115 assertEquals(expectedService, actualService);
121 public void should_fail_when_service_info_not_present() throws Exception {
122 // create the ServiceTopologyOperationInput from the template
123 ServiceTopologyOperationInput input = build(
124 serviceTopologyOperationInput()
125 .setSdncRequestHeader(build(sdncRequestHeader()
126 .setSvcRequestId("svc-request-id: xyz")
127 .setSvcAction(SvcAction.Assign)
129 .setRequestInformation(build(requestInformation()
130 .setRequestId("request-id: xyz")
131 .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
134 //execute the mdsal exec
135 ServiceTopologyOperationOutput output = exec(
136 genericResourceApiProvider::serviceTopologyOperation
138 , RpcResult::getResult
141 assertEquals("404", output.getResponseCode());
142 assertEquals(NULL_OR_EMPTY_ERROR_PARAM, output.getResponseMessage());
143 assertEquals("Y", output.getAckFinalIndicator());
148 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("500", 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.submit()).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(build(sdncRequestHeader()
258 .setSvcRequestId("svc-request-id: xyz")
259 .setSvcAction(SvcAction.Assign)
261 .setRequestInformation(build(requestInformation()
262 .setRequestId("request-id: xyz")
263 .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
265 .setServiceInformation(build(serviceInformationBuilder()
266 .setServiceInstanceId("service-instance-id: xyz")
271 private ServiceTopologyOperationInput deleteSTOI() {
274 serviceTopologyOperationInput()
275 .setSdncRequestHeader(build(sdncRequestHeader()
276 .setSvcRequestId("svc-request-id: xyz")
277 .setSvcAction(SvcAction.Unassign)
279 .setRequestInformation(build(requestInformation()
280 .setRequestId("request-id: xyz")
281 .setRequestAction(RequestInformation.RequestAction.DeleteServiceInstance)
283 .setServiceInformation(build(serviceInformationBuilder()
284 .setServiceInstanceId("service-instance-id: xyz")
289 private ServiceTopologyOperationInput deleteServiceSTOI() {
292 serviceTopologyOperationInput()
293 .setSdncRequestHeader(build(sdncRequestHeader()
294 .setSvcRequestId("svc-request-id: xyz")
295 .setSvcAction(SvcAction.Delete)
297 .setRequestInformation(build(requestInformation()
298 .setRequestId("request-id: xyz")
299 .setRequestAction(RequestInformation.RequestAction.DeleteServiceInstance)
301 .setServiceInformation(build(serviceInformationBuilder()
302 .setServiceInstanceId("service-instance-id: xyz")
308 private ServiceTopologyOperationOutput createExpectedSTOO(PropBuilder expectedSvcResultProp,
309 ServiceTopologyOperationInput expectedServiceTopologyOperationInput) {
311 serviceTopologyOperationOutput()
312 .setSvcRequestId(expectedServiceTopologyOperationInput.getSdncRequestHeader().getSvcRequestId())
313 .setResponseCode(expectedSvcResultProp.get(svcClient.errorCode))
314 .setAckFinalIndicator(expectedSvcResultProp.get(svcClient.ackFinal))
315 .setResponseMessage(expectedSvcResultProp.get(svcClient.errorMessage))
316 .setServiceResponseInformation(build(serviceResponseInformation()
317 .setInstanceId(expectedServiceTopologyOperationInput.getServiceInformation().getServiceInstanceId())
318 .setObjectPath(expectedSvcResultProp.get(svcClient.serviceObjectPath))
323 private Service createExpectedService(
324 ServiceTopologyOperationOutput expectedServiceTopologyOperationOutput,
325 ServiceTopologyOperationInput expectedServiceTopologyOperationInput,
326 Service actualService
329 //We cannot predict the timeStamp value so just steal it from the actual
330 //we need this to prevent the equals method from returning false as a result of the timestamp
331 String responseTimeStamp = actualService == null || actualService.getServiceStatus() == null ?
332 null : actualService.getServiceStatus().getResponseTimestamp();
334 SdncRequestHeader expectedSdncRequestHeader = expectedServiceTopologyOperationInput.getSdncRequestHeader();
335 ServiceInformation expectedServiceInformation = expectedServiceTopologyOperationInput.getServiceInformation();
336 RequestInformation expectedRequestInformation = expectedServiceTopologyOperationInput.getRequestInformation();
340 .setServiceInstanceId(expectedServiceInformation.getServiceInstanceId())
341 .setServiceData(build(serviceData()))
345 .setAction(expectedRequestInformation.getRequestAction().name())
346 .setFinalIndicator(expectedServiceTopologyOperationOutput.getAckFinalIndicator())
347 .setResponseCode(expectedServiceTopologyOperationOutput.getResponseCode())
348 .setResponseMessage(expectedServiceTopologyOperationOutput.getResponseMessage())
349 .setRpcAction(toRpcAction(expectedSdncRequestHeader.getSvcAction()))
350 .setRpcName(SVC_OPERATION)
351 .setRequestStatus(ServiceStatus.RequestStatus.Synccomplete)
352 .setResponseTimestamp(responseTimeStamp)
359 public ServiceStatus.RpcAction toRpcAction(SvcAction fromEnum) {
360 return fromEnum == null ? null : ServiceStatus.RpcAction.valueOf(fromEnum.name());