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.time.LocalDateTime;
15 import java.util.HashMap;
16 import java.util.LinkedList;
18 import java.util.Queue;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
21 import junit.framework.TestCase;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
29 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
30 import org.opendaylight.openflowplugin.api.OFConstants;
31 import org.opendaylight.openflowplugin.api.openflow.FlowGroupCache;
32 import org.opendaylight.openflowplugin.api.openflow.FlowGroupStatus;
33 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
35 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
36 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
37 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
38 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
39 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
40 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
41 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
42 import org.opendaylight.openflowplugin.impl.services.cache.FlowGroupCacheManagerImpl;
43 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
44 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
71 import org.opendaylight.yangtools.yang.binding.DataObject;
72 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
73 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
74 import org.opendaylight.yangtools.yang.common.RpcResult;
75 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
76 import org.opendaylight.yangtools.yang.common.Uint32;
77 import org.opendaylight.yangtools.yang.common.Uint64;
78 import org.opendaylight.yangtools.yang.common.Uint8;
80 @RunWith(MockitoJUnitRunner.class)
81 public class SalFlowServiceImplTest extends TestCase {
83 private static final Uint64 DUMMY_DATAPATH_ID = Uint64.valueOf(444);
84 private static final String DUMMY_NODE_ID = "dummyNodeID";
85 private static final String DUMMY_FLOW_ID = "dummyFlowID";
86 private static final Short DUMMY_TABLE_ID = (short) 0;
88 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
89 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
91 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
92 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
94 private NodeRef noderef = new NodeRef(NODE_II);
95 private static final String KEY = "0";
96 private static FlowGroupCache flowcache =
97 new FlowGroupCache("0","mock class", FlowGroupStatus.ADDED, LocalDateTime.MAX);
99 private static Queue<FlowGroupCache> caches() {
100 Queue<FlowGroupCache> cache = new LinkedList<>();
101 cache.add(flowcache);
105 private static final Queue<FlowGroupCache> CACHE = caches();
107 private static Map<String, Queue<FlowGroupCache>> createMap() {
108 Map<String,Queue<FlowGroupCache>> myMap = new HashMap<>();
109 myMap.put(KEY, CACHE);
113 private static final Map<String, Queue<FlowGroupCache>> MYMAP = createMap();
116 private RequestContextStack mockedRequestContextStack;
118 private DeviceContext mockedDeviceContext;
120 private ConnectionContext mockedPrimConnectionContext;
122 private FeaturesReply mockedFeatures;
124 private ConnectionAdapter mockedConnectionAdapter;
126 private MessageSpy mockedMessagSpy;
128 private RequestContext<Object> requestContext;
130 private OutboundQueue outboundQueue;
135 private DeviceState mockedDeviceState;
137 private DeviceInfo mockedDeviceInfo;
139 private DeviceFlowRegistry deviceFlowRegistry;
141 private GetFeaturesOutput mockedFeaturesOutput;
143 private FlowGroupCacheManagerImpl flowGroupCacheManager;
146 public void initialization() {
148 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
150 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
151 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
152 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
154 when(requestContext.getXid()).thenReturn(new Xid(Uint32.valueOf(84L)));
155 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
156 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
158 when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
160 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
161 when(flowGroupCacheManager.getAllNodesFlowGroupCache()).thenReturn(MYMAP);
164 private SalFlowServiceImpl mockSalFlowService(final short version) {
165 Uint8 ver = Uint8.valueOf(version);
166 when(mockedFeatures.getVersion()).thenReturn(ver);
167 when(mockedFeaturesOutput.getVersion()).thenReturn(ver);
168 when(mockedDeviceInfo.getVersion()).thenReturn(version);
170 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
171 return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager,
172 flowGroupCacheManager);
176 public void testAddFlow() throws Exception {
177 addFlow(OFConstants.OFP_VERSION_1_0);
178 addFlow(OFConstants.OFP_VERSION_1_3);
182 public void testAddFlowFailCallback() throws Exception {
183 addFlowFailCallback(OFConstants.OFP_VERSION_1_0);
187 public void testAddFlowFailCallback1() throws Exception {
188 addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
191 private void addFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
192 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
194 .setTableId((short)1)
198 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
199 .when(requestContext).getFuture();
201 final Future<RpcResult<AddFlowOutput>> rpcResultFuture =
202 mockSalFlowService(version).addFlow(mockedAddFlowInput);
204 assertNotNull(rpcResultFuture);
205 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
206 assertNotNull(addFlowOutputRpcResult);
207 assertFalse(addFlowOutputRpcResult.isSuccessful());
211 public void testRemoveFlowFailCallback() throws Exception {
212 removeFlowFailCallback(OFConstants.OFP_VERSION_1_0);
216 public void testRemoveFlowFailCallback1() throws Exception {
217 removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
220 private void removeFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
221 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
222 .setTableId((short)1)
227 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
228 .when(requestContext).getFuture();
230 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture =
231 mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
233 assertNotNull(rpcResultFuture);
234 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
235 assertNotNull(removeFlowOutputRpcResult);
236 assertFalse(removeFlowOutputRpcResult.isSuccessful());
240 public void testAddFlowWithItemLifecycle() throws Exception {
241 addFlow(OFConstants.OFP_VERSION_1_0);
242 addFlow(OFConstants.OFP_VERSION_1_3);
245 private void addFlow(final short version) throws ExecutionException, InterruptedException {
246 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
248 .setTableId((short)1)
251 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
253 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
257 public void testRemoveFlow() throws Exception {
258 removeFlow(OFConstants.OFP_VERSION_1_0);
259 removeFlow(OFConstants.OFP_VERSION_1_3);
263 public void testRemoveFlowWithItemLifecycle() throws Exception {
264 removeFlow(OFConstants.OFP_VERSION_1_0);
265 removeFlow(OFConstants.OFP_VERSION_1_3);
268 private void removeFlow(final short version) throws Exception {
269 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
271 .setTableId((short)1)
275 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
276 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
280 public void testUpdateFlow() throws Exception {
281 updateFlow(OFConstants.OFP_VERSION_1_0);
282 updateFlow(OFConstants.OFP_VERSION_1_3);
286 public void testUpdateFlowWithItemLifecycle() throws Exception {
287 updateFlow(OFConstants.OFP_VERSION_1_0);
288 updateFlow(OFConstants.OFP_VERSION_1_3);
291 private void updateFlow(final short version) throws Exception {
292 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
293 UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
295 UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
297 .setTableId((short)1)
300 UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
302 .setTableId((short)1)
306 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
307 when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
309 FlowRef mockedFlowRef = mock(FlowRef.class);
310 Mockito.doReturn(TABLE_II.child(Flow.class,
311 new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
312 when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
313 when(mockedUpdateFlowInput1.getFlowRef()).thenReturn(mockedFlowRef);
315 OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
317 .setTableId((short)1)
320 OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
322 .setTableId((short)1)
326 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
327 when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
329 SalFlowServiceImpl salFlowService = mockSalFlowService(version);
330 when(mockedUpdateFlowInput.getNode()).thenReturn(noderef);
331 when(mockedUpdateFlowInput1.getNode()).thenReturn(noderef);
332 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
333 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
336 private static <T extends DataObject> void verifyOutput(final Future<RpcResult<T>> rpcResultFuture)
337 throws ExecutionException, InterruptedException {
338 assertNotNull(rpcResultFuture);
339 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
340 assertNotNull(addFlowOutputRpcResult);
341 assertTrue(addFlowOutputRpcResult.isSuccessful());