2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.impl.services.sal;
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
14 import com.google.common.util.concurrent.Futures;
15 import java.math.BigInteger;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import junit.framework.TestCase;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
26 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
27 import org.opendaylight.openflowplugin.api.OFConstants;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
32 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
33 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
34 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
35 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
36 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
37 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
38 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
39 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
40 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
66 import org.opendaylight.yangtools.yang.binding.DataObject;
67 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
68 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
69 import org.opendaylight.yangtools.yang.common.RpcResult;
70 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
72 @RunWith(MockitoJUnitRunner.class)
73 public class SalFlowServiceImplTest extends TestCase {
75 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
76 private static final String DUMMY_NODE_ID = "dummyNodeID";
77 private static final String DUMMY_FLOW_ID = "dummyFlowID";
78 private static final Short DUMMY_TABLE_ID = (short) 0;
80 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
81 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
83 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
84 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
87 private RequestContextStack mockedRequestContextStack;
89 private DeviceContext mockedDeviceContext;
91 private ConnectionContext mockedPrimConnectionContext;
93 private FeaturesReply mockedFeatures;
95 private ConnectionAdapter mockedConnectionAdapter;
97 private MessageSpy mockedMessagSpy;
99 private RequestContext<Object> requestContext;
101 private OutboundQueue outboundQueue;
106 private DeviceState mockedDeviceState;
108 private DeviceInfo mockedDeviceInfo;
110 private DeviceFlowRegistry deviceFlowRegistry;
112 private GetFeaturesOutput mockedFeaturesOutput;
115 public void initialization() {
117 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
119 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
120 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
121 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
123 when(requestContext.getXid()).thenReturn(new Xid(84L));
124 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
125 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
127 when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
129 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
132 private SalFlowServiceImpl mockSalFlowService(final short version) {
133 when(mockedFeatures.getVersion()).thenReturn(version);
134 when(mockedFeaturesOutput.getVersion()).thenReturn(version);
135 when(mockedDeviceInfo.getVersion()).thenReturn(version);
137 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
138 return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
142 public void testAddFlow() throws Exception {
143 addFlow(OFConstants.OFP_VERSION_1_0);
144 addFlow(OFConstants.OFP_VERSION_1_3);
148 public void testAddFlowFailCallback() throws Exception {
149 addFlowFailCallback(OFConstants.OFP_VERSION_1_0);
153 public void testAddFlowFailCallback1() throws Exception {
154 addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
157 private void addFlowFailCallback(short version) throws InterruptedException, ExecutionException {
158 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
160 .setTableId((short)1)
163 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
164 .when(requestContext).getFuture();
166 mockingFlowRegistryLookup();
167 final Future<RpcResult<AddFlowOutput>> rpcResultFuture =
168 mockSalFlowService(version).addFlow(mockedAddFlowInput);
170 assertNotNull(rpcResultFuture);
171 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
172 assertNotNull(addFlowOutputRpcResult);
173 assertFalse(addFlowOutputRpcResult.isSuccessful());
177 public void testRemoveFlowFailCallback() throws Exception {
178 removeFlowFailCallback(OFConstants.OFP_VERSION_1_0);
182 public void testRemoveFlowFailCallback1() throws Exception {
183 removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
186 private void removeFlowFailCallback(short version) throws InterruptedException, ExecutionException {
187 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
188 .setTableId((short)1)
192 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
193 .when(requestContext).getFuture();
195 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture =
196 mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
198 assertNotNull(rpcResultFuture);
199 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
200 assertNotNull(removeFlowOutputRpcResult);
201 assertFalse(removeFlowOutputRpcResult.isSuccessful());
205 public void testAddFlowWithItemLifecycle() throws Exception {
206 addFlow(OFConstants.OFP_VERSION_1_0);
207 addFlow(OFConstants.OFP_VERSION_1_3);
210 private void addFlow(short version) throws ExecutionException,
211 InterruptedException {
212 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
214 .setTableId((short)1)
216 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
218 mockingFlowRegistryLookup();
219 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
223 public void testRemoveFlow() throws Exception {
224 removeFlow(OFConstants.OFP_VERSION_1_0);
225 removeFlow(OFConstants.OFP_VERSION_1_3);
229 public void testRemoveFlowWithItemLifecycle() throws Exception {
230 removeFlow(OFConstants.OFP_VERSION_1_0);
231 removeFlow(OFConstants.OFP_VERSION_1_3);
234 private void removeFlow(short version) throws Exception {
235 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
237 .setTableId((short)1)
240 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
241 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
245 public void testUpdateFlow() throws Exception {
246 updateFlow(OFConstants.OFP_VERSION_1_0);
247 updateFlow(OFConstants.OFP_VERSION_1_3);
251 public void testUpdateFlowWithItemLifecycle() throws Exception {
252 updateFlow(OFConstants.OFP_VERSION_1_0);
253 updateFlow(OFConstants.OFP_VERSION_1_3);
256 private void updateFlow(short version) throws Exception {
257 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
258 UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
260 UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
262 .setTableId((short)1)
265 UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
267 .setTableId((short)1)
271 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
272 when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
274 FlowRef mockedFlowRef = mock(FlowRef.class);
275 Mockito.doReturn(TABLE_II.child(Flow.class,
276 new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
277 when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
278 when(mockedUpdateFlowInput1.getFlowRef()).thenReturn(mockedFlowRef);
280 OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
282 .setTableId((short)1)
285 OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
287 .setTableId((short)1)
291 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
292 when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
294 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
296 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
297 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
300 private void mockingFlowRegistryLookup() {
301 FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
302 FlowId flowId = new FlowId(DUMMY_FLOW_ID);
303 when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
305 when(deviceFlowRegistry
306 .retrieveDescriptor(any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
309 private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException,
310 InterruptedException {
311 assertNotNull(rpcResultFuture);
312 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
313 assertNotNull(addFlowOutputRpcResult);
314 assertTrue(addFlowOutputRpcResult.isSuccessful());