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.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
17 import junit.framework.TestCase;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
25 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
26 import org.opendaylight.openflowplugin.api.OFConstants;
27 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
31 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
32 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
33 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
34 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
35 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
36 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
37 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
38 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
39 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
65 import org.opendaylight.yangtools.yang.binding.DataObject;
66 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
67 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
68 import org.opendaylight.yangtools.yang.common.RpcResult;
69 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
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.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 Short DUMMY_TABLE_ID = (short) 0;
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));
89 private RequestContextStack mockedRequestContextStack;
91 private DeviceContext mockedDeviceContext;
93 private ConnectionContext mockedPrimConnectionContext;
95 private FeaturesReply mockedFeatures;
97 private ConnectionAdapter mockedConnectionAdapter;
99 private MessageSpy mockedMessagSpy;
101 private RequestContext<Object> requestContext;
103 private OutboundQueue outboundQueue;
108 private DeviceState mockedDeviceState;
110 private DeviceInfo mockedDeviceInfo;
112 private DeviceFlowRegistry deviceFlowRegistry;
114 private GetFeaturesOutput mockedFeaturesOutput;
117 public void initialization() {
119 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
121 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
122 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
123 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
125 when(requestContext.getXid()).thenReturn(new Xid(Uint32.valueOf(84L)));
126 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
127 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
129 when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
131 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
134 private SalFlowServiceImpl mockSalFlowService(final short version) {
135 Uint8 ver = Uint8.valueOf(version);
136 when(mockedFeatures.getVersion()).thenReturn(ver);
137 when(mockedFeaturesOutput.getVersion()).thenReturn(ver);
138 when(mockedDeviceInfo.getVersion()).thenReturn(version);
140 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
141 return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
145 public void testAddFlow() throws Exception {
146 addFlow(OFConstants.OFP_VERSION_1_0);
147 addFlow(OFConstants.OFP_VERSION_1_3);
151 public void testAddFlowFailCallback() throws Exception {
152 addFlowFailCallback(OFConstants.OFP_VERSION_1_0);
156 public void testAddFlowFailCallback1() throws Exception {
157 addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
160 private void addFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
161 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
163 .setTableId((short)1)
166 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
167 .when(requestContext).getFuture();
169 mockingFlowRegistryLookup();
170 final Future<RpcResult<AddFlowOutput>> rpcResultFuture =
171 mockSalFlowService(version).addFlow(mockedAddFlowInput);
173 assertNotNull(rpcResultFuture);
174 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
175 assertNotNull(addFlowOutputRpcResult);
176 assertFalse(addFlowOutputRpcResult.isSuccessful());
180 public void testRemoveFlowFailCallback() throws Exception {
181 removeFlowFailCallback(OFConstants.OFP_VERSION_1_0);
185 public void testRemoveFlowFailCallback1() throws Exception {
186 removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
189 private void removeFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
190 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
191 .setTableId((short)1)
195 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
196 .when(requestContext).getFuture();
198 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture =
199 mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
201 assertNotNull(rpcResultFuture);
202 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
203 assertNotNull(removeFlowOutputRpcResult);
204 assertFalse(removeFlowOutputRpcResult.isSuccessful());
208 public void testAddFlowWithItemLifecycle() throws Exception {
209 addFlow(OFConstants.OFP_VERSION_1_0);
210 addFlow(OFConstants.OFP_VERSION_1_3);
213 private void addFlow(final short version) throws ExecutionException, InterruptedException {
214 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
216 .setTableId((short)1)
218 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
220 mockingFlowRegistryLookup();
221 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
225 public void testRemoveFlow() throws Exception {
226 removeFlow(OFConstants.OFP_VERSION_1_0);
227 removeFlow(OFConstants.OFP_VERSION_1_3);
231 public void testRemoveFlowWithItemLifecycle() throws Exception {
232 removeFlow(OFConstants.OFP_VERSION_1_0);
233 removeFlow(OFConstants.OFP_VERSION_1_3);
236 private void removeFlow(final short version) throws Exception {
237 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
239 .setTableId((short)1)
242 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
243 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
247 public void testUpdateFlow() throws Exception {
248 updateFlow(OFConstants.OFP_VERSION_1_0);
249 updateFlow(OFConstants.OFP_VERSION_1_3);
253 public void testUpdateFlowWithItemLifecycle() throws Exception {
254 updateFlow(OFConstants.OFP_VERSION_1_0);
255 updateFlow(OFConstants.OFP_VERSION_1_3);
258 private void updateFlow(final short version) throws Exception {
259 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
260 UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
262 UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
264 .setTableId((short)1)
267 UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
269 .setTableId((short)1)
273 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
274 when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
276 FlowRef mockedFlowRef = mock(FlowRef.class);
277 Mockito.doReturn(TABLE_II.child(Flow.class,
278 new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
279 when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
280 when(mockedUpdateFlowInput1.getFlowRef()).thenReturn(mockedFlowRef);
282 OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
284 .setTableId((short)1)
287 OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
289 .setTableId((short)1)
293 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
294 when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
296 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
298 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
299 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
302 private void mockingFlowRegistryLookup() {
303 FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
304 FlowId flowId = new FlowId(DUMMY_FLOW_ID);
305 when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
307 when(deviceFlowRegistry
308 .retrieveDescriptor(any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
311 private static <T extends DataObject> void verifyOutput(final Future<RpcResult<T>> rpcResultFuture)
312 throws ExecutionException, InterruptedException {
313 assertNotNull(rpcResultFuture);
314 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
315 assertNotNull(addFlowOutputRpcResult);
316 assertTrue(addFlowOutputRpcResult.isSuccessful());