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.INVALID_INPUT_ERROR_MESSAGE;
29 import static org.onap.sdnc.northbound.GenericResourceApiProvider.NO_SERVICE_LOGIC_ACTIVE;
30 import static org.onap.sdnc.northbound.GenericResourceApiProvider.NULL_OR_EMPTY_ERROR_PARAM;
31 import static org.onap.sdnc.northbound.util.MDSALUtil.build;
32 import static org.onap.sdnc.northbound.util.MDSALUtil.exec;
33 import static org.onap.sdnc.northbound.util.MDSALUtil.networkInformation;
34 import static org.onap.sdnc.northbound.util.MDSALUtil.networkResponseInformation;
35 import static org.onap.sdnc.northbound.util.MDSALUtil.networkTopologyOperationInput;
36 import static org.onap.sdnc.northbound.util.MDSALUtil.networkTopologyOperationOutput;
37 import static org.onap.sdnc.northbound.util.MDSALUtil.requestInformation;
38 import static org.onap.sdnc.northbound.util.MDSALUtil.sdncRequestHeader;
39 import static org.onap.sdnc.northbound.util.MDSALUtil.service;
40 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceData;
41 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceInformationBuilder;
42 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceLevelOperStatus;
43 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceResponseInformation;
44 import static org.onap.sdnc.northbound.util.MDSALUtil.serviceStatus;
46 import java.time.Instant;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.Mockito;
52 import org.mockito.runners.MockitoJUnitRunner;
53 import org.onap.sdnc.northbound.util.PropBuilder;
54 import org.opendaylight.mdsal.binding.api.DataBroker;
55 import org.opendaylight.mdsal.binding.api.TransactionChainClosedException;
56 import org.opendaylight.mdsal.binding.api.WriteTransaction;
57 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
58 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.NetworkTopologyOperationInput;
59 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.NetworkTopologyOperationOutput;
60 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.OperStatusData.LastAction;
61 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.OperStatusData.LastOrderStatus;
62 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.OperStatusData.LastRpcAction;
63 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.OperStatusData.OrderStatus;
64 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.request.information.RequestInformation;
65 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader;
66 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.sdnc.request.header.SdncRequestHeader.SvcAction;
67 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.data.ServiceData;
68 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.information.ServiceInformation;
69 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.model.infrastructure.Service;
70 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.generic.resource.rev170824.service.status.ServiceStatus;
71 import org.opendaylight.yangtools.yang.common.RpcResult;
75 * This class test the NetworkTopologyOperation mdsal RPC.
77 @RunWith(MockitoJUnitRunner.class)
78 public class NetworkTopologyOperationRPCTest extends GenericResourceApiProviderTest {
81 final String SVC_OPERATION = "network-topology-operation";
85 public void setUp() throws Exception {
87 svcClient.setScvOperation(SVC_OPERATION);
91 public void should_fail_when_service_instance_id_not_present() throws Exception {
93 NetworkTopologyOperationInput input = build(networkTopologyOperationInput());
95 NetworkTopologyOperationOutput output =
96 exec(genericResourceApiProvider::networkTopologyOperation, input, RpcResult::getResult);
98 assertEquals("404", output.getResponseCode());
99 assertEquals(NULL_OR_EMPTY_ERROR_PARAM, output.getResponseMessage());
100 assertEquals("Y", output.getAckFinalIndicator());
105 public void should_fail_when_invalid_service_data() throws Exception {
107 NetworkTopologyOperationInput input = build(networkTopologyOperationInput()
108 .setServiceInformation(build(serviceInformationBuilder()
109 .setServiceInstanceId("test-service-instance-id")
113 NetworkTopologyOperationOutput output =
114 exec(genericResourceApiProvider::networkTopologyOperation, input, RpcResult::getResult);
116 assertEquals("404", output.getResponseCode());
117 assertEquals(INVALID_INPUT_ERROR_MESSAGE, output.getResponseMessage());
118 assertEquals("Y", output.getAckFinalIndicator());
123 public void should_fail_when_client_execution_failed() throws Exception {
125 svcClient.mockHasGraph(true);
126 svcClient.mockExecute(new RuntimeException("test exception"));
128 NetworkTopologyOperationInput input = build(networkTopologyOperationInput()
129 .setServiceInformation(build(serviceInformationBuilder()
130 .setServiceInstanceId("test-service-instance-id")
134 persistServiceInDataBroker(input);
136 NetworkTopologyOperationOutput output =
137 exec(genericResourceApiProvider::networkTopologyOperation, input, RpcResult::getResult);
139 assertEquals("500", output.getResponseCode());
140 assertEquals("test exception", output.getResponseMessage());
141 assertEquals("Y", output.getAckFinalIndicator());
145 public void should_fail_when_client_has_no_graph() throws Exception {
147 svcClient.mockHasGraph(false);
149 NetworkTopologyOperationInput input = build(networkTopologyOperationInput()
150 .setServiceInformation(build(serviceInformationBuilder()
151 .setServiceInstanceId("test-service-instance-id")
155 persistServiceInDataBroker(input);
157 NetworkTopologyOperationOutput output =
158 exec(genericResourceApiProvider::networkTopologyOperation, input, RpcResult::getResult);
160 assertEquals("503", output.getResponseCode());
161 assertEquals(NO_SERVICE_LOGIC_ACTIVE + APP_NAME + ": '" + SVC_OPERATION + "'", output.getResponseMessage());
162 assertEquals("Y", output.getAckFinalIndicator());
166 public void should_fail_when_failed_to_update_mdsal() throws Exception {
168 PropBuilder svcResultProp = svcClient.createExecuteOKResult();
169 svcClient.mockExecute(svcResultProp);
170 svcClient.mockHasGraph(true);
171 WriteTransaction mockWriteTransaction = mock(WriteTransaction.class);
172 when(mockWriteTransaction.commit()).thenThrow(new TransactionChainClosedException("test exception"));
174 DataBroker spyDataBroker = Mockito.spy(dataBroker);
175 when(spyDataBroker.newWriteOnlyTransaction()).thenReturn(mockWriteTransaction);
176 genericResourceApiProvider.setDataBroker(spyDataBroker);
178 NetworkTopologyOperationInput input = build(networkTopologyOperationInput()
179 .setServiceInformation(build(serviceInformationBuilder()
180 .setServiceInstanceId("test-service-instance-id")
184 persistServiceInDataBroker(input);
185 NetworkTopologyOperationOutput output =
186 exec(genericResourceApiProvider::networkTopologyOperation, input, RpcResult::getResult);
188 assertEquals("500", output.getResponseCode());
189 assertEquals("test exception", output.getResponseMessage());
190 assertEquals("Y", output.getAckFinalIndicator());
194 * Verify ServiceTopologyOperation RPC executes a DG then produces the expected {@link
195 * NetworkTopologyOperationOutput} and persisted the expected {@link Service} in the {@link DataBroker}
198 public void should_success_when_no_errors_encountered() throws Exception {
200 //mock svcClient to perform a successful execution with the expected parameters
201 svcClient.mockHasGraph(true);
202 PropBuilder svcResultProp = svcClient.createExecuteOKResult();
203 svcClient.mockExecute(svcResultProp);
205 //construct the input parameter for the NetworkTopologyOperation
206 NetworkTopologyOperationInput input = createNTOI();
208 //pre-populate the DataBroke with the required ServiceData.
209 Service service = persistServiceInDataBroker(input);
211 //execute the mdsal exec
212 NetworkTopologyOperationOutput output = exec(
213 genericResourceApiProvider::networkTopologyOperation
215 , RpcResult::getResult
218 assertEquals("200", output.getResponseCode());
219 assertEquals("OK", output.getResponseMessage());
220 assertEquals("Y", output.getAckFinalIndicator());
222 //verify the returned NetworkTopologyOperationOutput
223 NetworkTopologyOperationOutput expectedNetworkTopologyOperationOutput
224 = createExpectedNTOO(svcResultProp, input);
225 assertEquals(expectedNetworkTopologyOperationOutput, output);
227 //verify the persisted Service
228 Service actualService = db.read(
229 input.getServiceInformation().getServiceInstanceId(),
230 LogicalDatastoreType.CONFIGURATION
232 Service expectedService = createExpectedService(
233 expectedNetworkTopologyOperationOutput,
235 service.getServiceData(),
237 assertEquals(expectedService, actualService);
242 private NetworkTopologyOperationInput createNTOI() {
245 networkTopologyOperationInput()
246 .setSdncRequestHeader(build(sdncRequestHeader()
247 .setSvcRequestId("svc-request-id: xyz")
248 .setSvcAction(SvcAction.Assign)
250 .setRequestInformation(build(requestInformation()
251 .setRequestId("request-id: xyz")
252 .setRequestAction(RequestInformation.RequestAction.CreateServiceInstance)
254 .setServiceInformation(build(serviceInformationBuilder()
255 .setServiceInstanceId("service-instance-id: xyz")
257 .setNetworkInformation(build(
263 private Service persistServiceInDataBroker(
264 NetworkTopologyOperationInput networkTopologyOperationInput
266 Service service = build(
268 .setServiceInstanceId(
269 networkTopologyOperationInput.getServiceInformation().getServiceInstanceId()
271 .setServiceData(build(
273 .setServiceLevelOperStatus(build(
274 serviceLevelOperStatus()
275 .setOrderStatus(OrderStatus.Created)
276 .setModifyTimestamp(Instant.now().toString())
277 .setLastSvcRequestId("svc-request-id: abc")
278 .setLastRpcAction(LastRpcAction.Activate)
279 .setLastOrderStatus(LastOrderStatus.PendingAssignment)
280 .setLastAction(LastAction.ActivateNetworkInstance)
281 .setCreateTimestamp(Instant.now().toString())
286 db.write(true, service, LogicalDatastoreType.CONFIGURATION);
291 private NetworkTopologyOperationOutput createExpectedNTOO(
292 PropBuilder expectedSvcResultProp,
293 NetworkTopologyOperationInput expectedNetworkTopologyOperationInput) {
295 networkTopologyOperationOutput()
296 .setSvcRequestId(expectedNetworkTopologyOperationInput.getSdncRequestHeader().getSvcRequestId())
297 .setResponseCode(expectedSvcResultProp.get(svcClient.errorCode))
298 .setAckFinalIndicator(expectedSvcResultProp.get(svcClient.ackFinal))
299 .setResponseMessage(expectedSvcResultProp.get(svcClient.errorMessage))
300 .setServiceResponseInformation(build(serviceResponseInformation()
301 .setInstanceId(expectedNetworkTopologyOperationInput.getServiceInformation().getServiceInstanceId())
302 .setObjectPath(expectedSvcResultProp.get(svcClient.serviceObjectPath))
304 .setNetworkResponseInformation(build(
305 networkResponseInformation()
306 .setInstanceId(expectedSvcResultProp.get(svcClient.networkId))
307 .setObjectPath(expectedSvcResultProp.get(svcClient.networkObjectPath))
312 private Service createExpectedService(
313 NetworkTopologyOperationOutput expectedNetworkTopologyOperationOutput,
314 NetworkTopologyOperationInput expectedNetworkTopologyOperationInput,
315 ServiceData expectedServiceData,
316 Service actualService
319 //We cannot predict the timeStamp value so just steal it from the actual
320 //we need this to prevent the equals method from returning false as a result of the timestamp
321 String responseTimeStamp = actualService == null || actualService.getServiceStatus() == null ?
322 null : actualService.getServiceStatus().getResponseTimestamp();
324 SdncRequestHeader expectedSdncRequestHeader = expectedNetworkTopologyOperationInput.getSdncRequestHeader();
325 ServiceInformation expectedServiceInformation = expectedNetworkTopologyOperationInput.getServiceInformation();
326 RequestInformation expectedRequestInformation = expectedNetworkTopologyOperationInput.getRequestInformation();
330 .setServiceInstanceId(expectedServiceInformation.getServiceInstanceId())
331 .setServiceData(build(serviceData()))
332 .setServiceData(expectedServiceData)
342 public ServiceStatus.RpcAction toRpcAction(SvcAction fromEnum) {
343 return fromEnum == null ? null : ServiceStatus.RpcAction.valueOf(fromEnum.name());