46af7563d9db6a8348419718f671a65fff722247
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / SalFlowServiceImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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;
9
10
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.when;
14
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;
74
75 @RunWith(MockitoJUnitRunner.class)
76 public class SalFlowServiceImplTest extends TestCase {
77
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;
82
83     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
84             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
85
86     private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
87             = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
88
89     @Mock
90     private RequestContextStack mockedRequestContextStack;
91     @Mock
92     private DeviceContext mockedDeviceContext;
93     @Mock
94     private ConnectionContext mockedPrimConnectionContext;
95     @Mock
96     private FeaturesReply mockedFeatures;
97     @Mock
98     private ConnectionAdapter mockedConnectionAdapter;
99     @Mock
100     private MessageSpy mockedMessagSpy;
101     @Mock
102     private RequestContext<Object> requestContext;
103     @Mock
104     private OutboundQueue outboundQueue;
105     @Mock
106     private Match match;
107
108     @Mock
109     private DeviceState mockedDeviceState;
110     @Mock
111     private DeviceInfo mockedDeviceInfo;
112     @Mock
113     private DeviceFlowRegistry deviceFlowRegistry;
114     @Mock
115     private GetFeaturesOutput mockedFeaturesOutput;
116
117     @Before
118     public void initialization() {
119         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
120         when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
121
122         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
123         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
124         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
125
126         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
127         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
128         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
129
130         when(requestContext.getXid()).thenReturn(new Xid(84L));
131         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
132         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
133
134         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
135         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
136
137         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
138         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
139     }
140
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);
145
146         if (OFConstants.OFP_VERSION_1_3 >= version) {
147             when(mockedDeviceContext.isUseSingleLayerSerialization()).thenReturn(true);
148         }
149
150         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
151         return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
152     }
153
154     @Test
155     public void testAddFlow() throws Exception {
156         addFlow(null, OFConstants.OFP_VERSION_1_0);
157         addFlow(null, OFConstants.OFP_VERSION_1_3);
158     }
159
160     @Test
161     public void testAddFlowFailCallback() throws Exception {
162         addFlowFailCallback(OFConstants.OFP_VERSION_1_0);
163     }
164
165     @Test
166     public void testAddFlowFailCallback1() throws Exception {
167         addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
168     }
169
170     private void addFlowFailCallback(short version) throws InterruptedException, ExecutionException {
171         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
172                 .setMatch(match)
173                 .setTableId((short)1)
174                 .build();
175
176         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
177                 .when(requestContext).getFuture();
178
179         mockingFlowRegistryLookup();
180         final Future<RpcResult<AddFlowOutput>> rpcResultFuture = mockSalFlowService(version).addFlow(mockedAddFlowInput);
181
182         assertNotNull(rpcResultFuture);
183         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
184         assertNotNull(addFlowOutputRpcResult);
185         assertFalse(addFlowOutputRpcResult.isSuccessful());
186     }
187
188     @Test
189     public void testRemoveFlowFailCallback() throws Exception {
190         removeFlowFailCallback(OFConstants.OFP_VERSION_1_0);
191     }
192
193     @Test
194     public void testRemoveFlowFailCallback1() throws Exception {
195         removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
196     }
197
198     private void removeFlowFailCallback(short version) throws InterruptedException, ExecutionException {
199         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
200                 .setMatch(match)
201                 .build();
202
203         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
204                 .when(requestContext).getFuture();
205
206         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
207
208         assertNotNull(rpcResultFuture);
209         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
210         assertNotNull(removeFlowOutputRpcResult);
211         assertFalse(removeFlowOutputRpcResult.isSuccessful());
212     }
213
214     @Test
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);
218     }
219
220     private void addFlow(final ItemLifecycleListener itemLifecycleListener, short version) throws ExecutionException, InterruptedException {
221         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
222                 .setMatch(match)
223                 .setTableId((short)1)
224                 .build();
225         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
226         salFlowService.setItemLifecycleListener(itemLifecycleListener);
227
228         mockingFlowRegistryLookup();
229         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
230         if (itemLifecycleListener != null) {
231             Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
232         }
233     }
234
235     @Test
236     public void testRemoveFlow() throws Exception {
237         removeFlow(null, OFConstants.OFP_VERSION_1_0);
238         removeFlow(null, OFConstants.OFP_VERSION_1_3);
239     }
240
241     @Test
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);
245     }
246
247     private void removeFlow(final ItemLifecycleListener itemLifecycleListener, short version) throws Exception {
248         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
249                 .setMatch(match)
250                 .setTableId((short)1)
251                 .build();
252
253         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
254         if (itemLifecycleListener != null) {
255             salFlowService.setItemLifecycleListener(itemLifecycleListener);
256             mockingFlowRegistryLookup();
257
258         }
259
260         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
261         if (itemLifecycleListener != null) {
262             Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
263         }
264
265     }
266
267     @Test
268     public void testUpdateFlow() throws Exception {
269         updateFlow(null, OFConstants.OFP_VERSION_1_0);
270         updateFlow(null, OFConstants.OFP_VERSION_1_3);
271     }
272
273     @Test
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);
277     }
278
279     private void updateFlow(final ItemLifecycleListener itemLifecycleListener, short version) throws Exception {
280         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
281         UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
282
283         UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
284                 .setMatch(match)
285                 .setTableId((short)1)
286                 .build();
287
288         UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
289                 .setMatch(match)
290                 .setTableId((short)1)
291                 .setPriority(Integer.valueOf(1))
292                 .build();
293
294         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
295         when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
296
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);
301
302         OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
303                 .setMatch(match)
304                 .setTableId((short)1)
305                 .build();
306
307         OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
308                 .setMatch(match)
309                 .setTableId((short)1)
310                 .setPriority(Integer.valueOf(2))
311                 .build();
312
313         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
314         when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
315
316         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
317         if (itemLifecycleListener != null) {
318             salFlowService.setItemLifecycleListener(itemLifecycleListener);
319             mockingFlowRegistryLookup();
320         }
321
322         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
323         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
324
325         if (itemLifecycleListener != null) {
326             Mockito.verify(itemLifecycleListener, times(2)).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
327         }
328
329     }
330
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));
336
337         when(deviceFlowRegistry.storeIfNecessary(Matchers.any(FlowRegistryKey.class))).thenReturn(flowId);
338         when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
339     }
340
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());
346     }
347 }