2 * Copyright (c) 2015 Cisco Systems, Inc. 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;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.when;
15 import com.google.common.util.concurrent.Futures;
16 import java.math.BigInteger;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Future;
19 import junit.framework.TestCase;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Matchers;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
28 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
29 import org.opendaylight.openflowplugin.api.OFConstants;
30 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
33 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
34 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
35 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
36 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
37 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
38 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
39 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
40 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
41 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
42 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
43 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
69 import org.opendaylight.yangtools.yang.binding.DataObject;
70 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
71 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
72 import org.opendaylight.yangtools.yang.common.RpcResult;
73 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
75 @RunWith(MockitoJUnitRunner.class)
76 public class SalFlowServiceImplTest extends TestCase {
78 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
79 private static final String DUMMY_NODE_ID = "dummyNodeID";
80 private static final String DUMMY_FLOW_ID = "dummyFlowID";
81 private static final Short DUMMY_TABLE_ID = (short) 0;
83 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
84 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
86 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
87 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
90 private RequestContextStack mockedRequestContextStack;
92 private DeviceContext mockedDeviceContext;
94 private ConnectionContext mockedPrimConnectionContext;
96 private FeaturesReply mockedFeatures;
98 private ConnectionAdapter mockedConnectionAdapter;
100 private MessageSpy mockedMessagSpy;
102 private RequestContext<Object> requestContext;
104 private OutboundQueue outboundQueue;
109 private DeviceState mockedDeviceState;
111 private DeviceInfo mockedDeviceInfo;
113 private DeviceFlowRegistry deviceFlowRegistry;
115 private GetFeaturesOutput mockedFeaturesOutput;
118 public void initialization() {
119 when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
120 when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
122 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
123 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
124 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
126 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
127 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
128 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
130 when(requestContext.getXid()).thenReturn(new Xid(84L));
131 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
132 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
134 when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
135 when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
137 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
138 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
141 private SalFlowServiceImpl mockSalFlowService(final short version) {
142 when(mockedFeatures.getVersion()).thenReturn(version);
143 when(mockedFeaturesOutput.getVersion()).thenReturn(version);
144 when(mockedDeviceInfo.getVersion()).thenReturn(version);
146 if (OFConstants.OFP_VERSION_1_3 >= version) {
147 when(mockedDeviceContext.isUseSingleLayerSerialization()).thenReturn(true);
150 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
151 return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
155 public void testAddFlow() throws Exception {
156 addFlow(null, OFConstants.OFP_VERSION_1_0);
157 addFlow(null, OFConstants.OFP_VERSION_1_3);
161 public void testAddFlowFailCallback() throws Exception {
162 addFlowFailCallback(OFConstants.OFP_VERSION_1_0);
166 public void testAddFlowFailCallback1() throws Exception {
167 addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
170 private void addFlowFailCallback(short version) throws InterruptedException, ExecutionException {
171 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
173 .setTableId((short)1)
176 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
177 .when(requestContext).getFuture();
179 mockingFlowRegistryLookup();
180 final Future<RpcResult<AddFlowOutput>> rpcResultFuture = mockSalFlowService(version).addFlow(mockedAddFlowInput);
182 assertNotNull(rpcResultFuture);
183 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
184 assertNotNull(addFlowOutputRpcResult);
185 assertFalse(addFlowOutputRpcResult.isSuccessful());
189 public void testRemoveFlowFailCallback() throws Exception {
190 removeFlowFailCallback(OFConstants.OFP_VERSION_1_0);
194 public void testRemoveFlowFailCallback1() throws Exception {
195 removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
198 private void removeFlowFailCallback(short version) throws InterruptedException, ExecutionException {
199 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
203 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
204 .when(requestContext).getFuture();
206 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
208 assertNotNull(rpcResultFuture);
209 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
210 assertNotNull(removeFlowOutputRpcResult);
211 assertFalse(removeFlowOutputRpcResult.isSuccessful());
215 public void testAddFlowWithItemLifecycle() throws Exception {
216 addFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_0);
217 addFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_3);
220 private void addFlow(final ItemLifecycleListener itemLifecycleListener, short version) throws ExecutionException, InterruptedException {
221 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
223 .setTableId((short)1)
225 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
226 salFlowService.setItemLifecycleListener(itemLifecycleListener);
228 mockingFlowRegistryLookup();
229 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
230 if (itemLifecycleListener != null) {
231 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
236 public void testRemoveFlow() throws Exception {
237 removeFlow(null, OFConstants.OFP_VERSION_1_0);
238 removeFlow(null, OFConstants.OFP_VERSION_1_3);
242 public void testRemoveFlowWithItemLifecycle() throws Exception {
243 removeFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_0);
244 removeFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_3);
247 private void removeFlow(final ItemLifecycleListener itemLifecycleListener, short version) throws Exception {
248 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
250 .setTableId((short)1)
253 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
254 if (itemLifecycleListener != null) {
255 salFlowService.setItemLifecycleListener(itemLifecycleListener);
256 mockingFlowRegistryLookup();
260 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
261 if (itemLifecycleListener != null) {
262 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
268 public void testUpdateFlow() throws Exception {
269 updateFlow(null, OFConstants.OFP_VERSION_1_0);
270 updateFlow(null, OFConstants.OFP_VERSION_1_3);
274 public void testUpdateFlowWithItemLifecycle() throws Exception {
275 updateFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_0);
276 updateFlow(mock(ItemLifecycleListener.class), OFConstants.OFP_VERSION_1_3);
279 private void updateFlow(final ItemLifecycleListener itemLifecycleListener, short version) throws Exception {
280 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
281 UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
283 UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
285 .setTableId((short)1)
288 UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
290 .setTableId((short)1)
291 .setPriority(Integer.valueOf(1))
294 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
295 when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
297 FlowRef mockedFlowRef = mock(FlowRef.class);
298 Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
299 when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
300 when(mockedUpdateFlowInput1.getFlowRef()).thenReturn(mockedFlowRef);
302 OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
304 .setTableId((short)1)
307 OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
309 .setTableId((short)1)
310 .setPriority(Integer.valueOf(2))
313 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
314 when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
316 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
317 if (itemLifecycleListener != null) {
318 salFlowService.setItemLifecycleListener(itemLifecycleListener);
319 mockingFlowRegistryLookup();
322 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
323 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
325 if (itemLifecycleListener != null) {
326 Mockito.verify(itemLifecycleListener, times(2)).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
331 private void mockingFlowRegistryLookup() {
332 FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
333 FlowId flowId = new FlowId(DUMMY_FLOW_ID);
334 when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
335 when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
337 when(deviceFlowRegistry.storeIfNecessary(Matchers.any(FlowRegistryKey.class))).thenReturn(flowId);
338 when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
341 private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
342 assertNotNull(rpcResultFuture);
343 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
344 assertNotNull(addFlowOutputRpcResult);
345 assertTrue(addFlowOutputRpcResult.isSuccessful());