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.Mockito.mock;
11 import static org.mockito.Mockito.when;
13 import com.google.common.util.concurrent.Futures;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.Future;
16 import junit.framework.TestCase;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
24 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
30 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
32 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
33 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
34 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
35 import org.opendaylight.openflowplugin.impl.services.cache.FlowGroupCacheManagerImpl;
36 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
37 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
64 import org.opendaylight.yangtools.yang.binding.DataObject;
65 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
66 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
67 import org.opendaylight.yangtools.yang.common.RpcResult;
68 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
69 import org.opendaylight.yangtools.yang.common.Uint16;
70 import org.opendaylight.yangtools.yang.common.Uint32;
71 import org.opendaylight.yangtools.yang.common.Uint64;
72 import org.opendaylight.yangtools.yang.common.Uint8;
74 @RunWith(MockitoJUnitRunner.StrictStubs.class)
75 public class SalFlowServiceImplTest extends TestCase {
77 private static final Uint64 DUMMY_DATAPATH_ID = Uint64.valueOf(444);
78 private static final String DUMMY_NODE_ID = "dummyNodeID";
79 private static final String DUMMY_FLOW_ID = "dummyFlowID";
80 private static final Uint8 DUMMY_TABLE_ID = Uint8.ZERO;
82 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
83 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
85 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
86 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
88 private final NodeRef noderef = new NodeRef(NODE_II);
91 private RequestContextStack mockedRequestContextStack;
93 private DeviceContext mockedDeviceContext;
95 private ConnectionContext mockedPrimConnectionContext;
97 private FeaturesReply mockedFeatures;
99 private ConnectionAdapter mockedConnectionAdapter;
101 private MessageSpy mockedMessagSpy;
103 private RequestContext<Object> requestContext;
105 private OutboundQueue outboundQueue;
110 private DeviceState mockedDeviceState;
112 private DeviceInfo mockedDeviceInfo;
114 private DeviceFlowRegistry deviceFlowRegistry;
116 private GetFeaturesOutput mockedFeaturesOutput;
118 private FlowGroupCacheManagerImpl flowGroupCacheManager;
121 public void initialization() {
123 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
125 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
126 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
127 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
129 when(requestContext.getXid()).thenReturn(new Xid(Uint32.valueOf(84L)));
130 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
131 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
133 when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
135 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
138 private SalFlowServiceImpl mockSalFlowService(final short version) {
139 Uint8 ver = Uint8.valueOf(version);
140 when(mockedFeatures.getVersion()).thenReturn(ver);
141 when(mockedFeaturesOutput.getVersion()).thenReturn(ver);
142 when(mockedDeviceInfo.getVersion()).thenReturn(version);
144 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
145 return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager,
146 flowGroupCacheManager);
150 public void testAddFlow() throws Exception {
151 addFlow(OFConstants.OFP_VERSION_1_0);
152 addFlow(OFConstants.OFP_VERSION_1_3);
156 public void testAddFlowFailCallback() throws Exception {
157 addFlowFailCallback(OFConstants.OFP_VERSION_1_0);
161 public void testAddFlowFailCallback1() throws Exception {
162 addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
165 private void addFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
166 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
168 .setTableId(Uint8.ONE)
172 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
173 .when(requestContext).getFuture();
175 final Future<RpcResult<AddFlowOutput>> rpcResultFuture =
176 mockSalFlowService(version).addFlow(mockedAddFlowInput);
178 assertNotNull(rpcResultFuture);
179 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
180 assertNotNull(addFlowOutputRpcResult);
181 assertFalse(addFlowOutputRpcResult.isSuccessful());
185 public void testRemoveFlowFailCallback() throws Exception {
186 removeFlowFailCallback(OFConstants.OFP_VERSION_1_0);
190 public void testRemoveFlowFailCallback1() throws Exception {
191 removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
194 private void removeFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
195 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
196 .setTableId(Uint8.ONE)
201 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
202 .when(requestContext).getFuture();
204 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture =
205 mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
207 assertNotNull(rpcResultFuture);
208 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
209 assertNotNull(removeFlowOutputRpcResult);
210 assertFalse(removeFlowOutputRpcResult.isSuccessful());
214 public void testAddFlowWithItemLifecycle() throws Exception {
215 addFlow(OFConstants.OFP_VERSION_1_0);
216 addFlow(OFConstants.OFP_VERSION_1_3);
219 private void addFlow(final short version) throws ExecutionException, InterruptedException {
220 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
222 .setTableId(Uint8.ONE)
225 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
227 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
231 public void testRemoveFlow() throws Exception {
232 removeFlow(OFConstants.OFP_VERSION_1_0);
233 removeFlow(OFConstants.OFP_VERSION_1_3);
237 public void testRemoveFlowWithItemLifecycle() throws Exception {
238 removeFlow(OFConstants.OFP_VERSION_1_0);
239 removeFlow(OFConstants.OFP_VERSION_1_3);
242 private void removeFlow(final short version) throws Exception {
243 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
245 .setTableId(Uint8.ONE)
249 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
250 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
254 public void testUpdateFlow() throws Exception {
255 updateFlow(OFConstants.OFP_VERSION_1_0);
256 updateFlow(OFConstants.OFP_VERSION_1_3);
260 public void testUpdateFlowWithItemLifecycle() throws Exception {
261 updateFlow(OFConstants.OFP_VERSION_1_0);
262 updateFlow(OFConstants.OFP_VERSION_1_3);
265 private void updateFlow(final short version) throws Exception {
266 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
267 UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
269 UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
271 .setTableId(Uint8.ONE)
274 UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
276 .setTableId(Uint8.ONE)
277 .setPriority(Uint16.ONE)
280 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
281 when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
283 FlowRef mockedFlowRef = mock(FlowRef.class);
284 Mockito.doReturn(TABLE_II.child(Flow.class,
285 new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
286 when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
287 when(mockedUpdateFlowInput1.getFlowRef()).thenReturn(mockedFlowRef);
289 OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
291 .setTableId(Uint8.ONE)
294 OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
296 .setTableId(Uint8.ONE)
297 .setPriority(Uint16.TWO)
300 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
301 when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
303 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
304 when(mockedUpdateFlowInput.getNode()).thenReturn(noderef);
305 when(mockedUpdateFlowInput1.getNode()).thenReturn(noderef);
306 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
307 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
310 private static <T extends DataObject> void verifyOutput(final Future<RpcResult<T>> rpcResultFuture)
311 throws ExecutionException, InterruptedException {
312 assertNotNull(rpcResultFuture);
313 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
314 assertNotNull(addFlowOutputRpcResult);
315 assertTrue(addFlowOutputRpcResult.isSuccessful());