GetFlowNodeCache cli
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / sal / SalFlowServiceImplTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.services.sal;
9
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
12
13 import com.google.common.util.concurrent.Futures;
14 import java.time.LocalDateTime;
15 import java.util.HashMap;
16 import java.util.LinkedList;
17 import java.util.Map;
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;
79
80 @RunWith(MockitoJUnitRunner.class)
81 public class SalFlowServiceImplTest extends TestCase {
82
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;
87
88     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
89             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
90
91     private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
92             = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
93
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);
98
99     private static Queue<FlowGroupCache> caches() {
100         Queue<FlowGroupCache> cache = new LinkedList<>();
101         cache.add(flowcache);
102         return cache;
103     }
104
105     private static final Queue<FlowGroupCache> CACHE = caches();
106
107     private static Map<String, Queue<FlowGroupCache>> createMap() {
108         Map<String,Queue<FlowGroupCache>> myMap = new HashMap<>();
109         myMap.put(KEY, CACHE);
110         return myMap;
111     }
112
113     private static final Map<String, Queue<FlowGroupCache>> MYMAP = createMap();
114
115     @Mock
116     private RequestContextStack mockedRequestContextStack;
117     @Mock
118     private DeviceContext mockedDeviceContext;
119     @Mock
120     private ConnectionContext mockedPrimConnectionContext;
121     @Mock
122     private FeaturesReply mockedFeatures;
123     @Mock
124     private ConnectionAdapter mockedConnectionAdapter;
125     @Mock
126     private MessageSpy mockedMessagSpy;
127     @Mock
128     private RequestContext<Object> requestContext;
129     @Mock
130     private OutboundQueue outboundQueue;
131     @Mock
132     private Match match;
133
134     @Mock
135     private DeviceState mockedDeviceState;
136     @Mock
137     private DeviceInfo mockedDeviceInfo;
138     @Mock
139     private DeviceFlowRegistry deviceFlowRegistry;
140     @Mock
141     private GetFeaturesOutput mockedFeaturesOutput;
142     @Mock
143     private FlowGroupCacheManagerImpl flowGroupCacheManager;
144
145     @Before
146     public void initialization() {
147
148         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
149
150         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
151         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
152         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
153
154         when(requestContext.getXid()).thenReturn(new Xid(Uint32.valueOf(84L)));
155         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
156         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
157
158         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
159
160         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
161         when(flowGroupCacheManager.getAllNodesFlowGroupCache()).thenReturn(MYMAP);
162     }
163
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);
169
170         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
171         return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager,
172                 flowGroupCacheManager);
173     }
174
175     @Test
176     public void testAddFlow() throws Exception {
177         addFlow(OFConstants.OFP_VERSION_1_0);
178         addFlow(OFConstants.OFP_VERSION_1_3);
179     }
180
181     @Test
182     public void testAddFlowFailCallback() throws Exception {
183         addFlowFailCallback(OFConstants.OFP_VERSION_1_0);
184     }
185
186     @Test
187     public void testAddFlowFailCallback1() throws Exception {
188         addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
189     }
190
191     private void addFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
192         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
193                 .setMatch(match)
194                 .setTableId((short)1)
195                 .setNode(noderef)
196                 .build();
197
198         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
199                 .when(requestContext).getFuture();
200
201         final Future<RpcResult<AddFlowOutput>> rpcResultFuture =
202                 mockSalFlowService(version).addFlow(mockedAddFlowInput);
203
204         assertNotNull(rpcResultFuture);
205         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
206         assertNotNull(addFlowOutputRpcResult);
207         assertFalse(addFlowOutputRpcResult.isSuccessful());
208     }
209
210     @Test
211     public void testRemoveFlowFailCallback() throws Exception {
212         removeFlowFailCallback(OFConstants.OFP_VERSION_1_0);
213     }
214
215     @Test
216     public void testRemoveFlowFailCallback1() throws Exception {
217         removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
218     }
219
220     private void removeFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
221         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
222                 .setTableId((short)1)
223                 .setMatch(match)
224                 .setNode(noderef)
225                 .build();
226
227         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
228                 .when(requestContext).getFuture();
229
230         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture =
231                 mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
232
233         assertNotNull(rpcResultFuture);
234         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
235         assertNotNull(removeFlowOutputRpcResult);
236         assertFalse(removeFlowOutputRpcResult.isSuccessful());
237     }
238
239     @Test
240     public void testAddFlowWithItemLifecycle() throws Exception {
241         addFlow(OFConstants.OFP_VERSION_1_0);
242         addFlow(OFConstants.OFP_VERSION_1_3);
243     }
244
245     private void addFlow(final short version) throws ExecutionException, InterruptedException {
246         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
247                 .setMatch(match)
248                 .setTableId((short)1)
249                 .setNode(noderef)
250                 .build();
251         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
252
253         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
254     }
255
256     @Test
257     public void testRemoveFlow() throws Exception {
258         removeFlow(OFConstants.OFP_VERSION_1_0);
259         removeFlow(OFConstants.OFP_VERSION_1_3);
260     }
261
262     @Test
263     public void testRemoveFlowWithItemLifecycle() throws Exception {
264         removeFlow(OFConstants.OFP_VERSION_1_0);
265         removeFlow(OFConstants.OFP_VERSION_1_3);
266     }
267
268     private void removeFlow(final short version) throws Exception {
269         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
270                 .setMatch(match)
271                 .setTableId((short)1)
272                 .setNode(noderef)
273                 .build();
274
275         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
276         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
277     }
278
279     @Test
280     public void testUpdateFlow() throws Exception {
281         updateFlow(OFConstants.OFP_VERSION_1_0);
282         updateFlow(OFConstants.OFP_VERSION_1_3);
283     }
284
285     @Test
286     public void testUpdateFlowWithItemLifecycle() throws Exception {
287         updateFlow(OFConstants.OFP_VERSION_1_0);
288         updateFlow(OFConstants.OFP_VERSION_1_3);
289     }
290
291     private void updateFlow(final short version) throws Exception {
292         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
293         UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
294
295         UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
296                 .setMatch(match)
297                 .setTableId((short)1)
298                 .build();
299
300         UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
301                 .setMatch(match)
302                 .setTableId((short)1)
303                 .setPriority(1)
304                 .build();
305
306         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
307         when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
308
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);
314
315         OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
316                 .setMatch(match)
317                 .setTableId((short)1)
318                 .build();
319
320         OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
321                 .setMatch(match)
322                 .setTableId((short)1)
323                 .setPriority(2)
324                 .build();
325
326         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
327         when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
328
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));
334     }
335
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());
342     }
343 }