Properly check for FlowRef before touching caches
[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.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;
73
74 @RunWith(MockitoJUnitRunner.StrictStubs.class)
75 public class SalFlowServiceImplTest extends TestCase {
76
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;
81
82     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
83             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
84
85     private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
86             = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
87
88     private final NodeRef noderef = new NodeRef(NODE_II);
89
90     @Mock
91     private RequestContextStack mockedRequestContextStack;
92     @Mock
93     private DeviceContext mockedDeviceContext;
94     @Mock
95     private ConnectionContext mockedPrimConnectionContext;
96     @Mock
97     private FeaturesReply mockedFeatures;
98     @Mock
99     private ConnectionAdapter mockedConnectionAdapter;
100     @Mock
101     private MessageSpy mockedMessagSpy;
102     @Mock
103     private RequestContext<Object> requestContext;
104     @Mock
105     private OutboundQueue outboundQueue;
106     @Mock
107     private Match match;
108
109     @Mock
110     private DeviceState mockedDeviceState;
111     @Mock
112     private DeviceInfo mockedDeviceInfo;
113     @Mock
114     private DeviceFlowRegistry deviceFlowRegistry;
115     @Mock
116     private GetFeaturesOutput mockedFeaturesOutput;
117     @Mock
118     private FlowGroupCacheManagerImpl flowGroupCacheManager;
119
120     @Before
121     public void initialization() {
122
123         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
124
125         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
126         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
127         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
128
129         when(requestContext.getXid()).thenReturn(new Xid(Uint32.valueOf(84L)));
130         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
131         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
132
133         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
134
135         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
136     }
137
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);
143
144         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
145         return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager,
146                 flowGroupCacheManager);
147     }
148
149     @Test
150     public void testAddFlow() throws Exception {
151         addFlow(OFConstants.OFP_VERSION_1_0);
152         addFlow(OFConstants.OFP_VERSION_1_3);
153     }
154
155     @Test
156     public void testAddFlowFailCallback() throws Exception {
157         addFlowFailCallback(OFConstants.OFP_VERSION_1_0);
158     }
159
160     @Test
161     public void testAddFlowFailCallback1() throws Exception {
162         addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
163     }
164
165     private void addFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
166         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
167                 .setMatch(match)
168                 .setTableId(Uint8.ONE)
169                 .setNode(noderef)
170                 .build();
171
172         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
173                 .when(requestContext).getFuture();
174
175         final Future<RpcResult<AddFlowOutput>> rpcResultFuture =
176                 mockSalFlowService(version).addFlow(mockedAddFlowInput);
177
178         assertNotNull(rpcResultFuture);
179         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
180         assertNotNull(addFlowOutputRpcResult);
181         assertFalse(addFlowOutputRpcResult.isSuccessful());
182     }
183
184     @Test
185     public void testRemoveFlowFailCallback() throws Exception {
186         removeFlowFailCallback(OFConstants.OFP_VERSION_1_0);
187     }
188
189     @Test
190     public void testRemoveFlowFailCallback1() throws Exception {
191         removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
192     }
193
194     private void removeFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
195         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
196                 .setTableId(Uint8.ONE)
197                 .setMatch(match)
198                 .setNode(noderef)
199                 .build();
200
201         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
202                 .when(requestContext).getFuture();
203
204         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture =
205                 mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
206
207         assertNotNull(rpcResultFuture);
208         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
209         assertNotNull(removeFlowOutputRpcResult);
210         assertFalse(removeFlowOutputRpcResult.isSuccessful());
211     }
212
213     @Test
214     public void testAddFlowWithItemLifecycle() throws Exception {
215         addFlow(OFConstants.OFP_VERSION_1_0);
216         addFlow(OFConstants.OFP_VERSION_1_3);
217     }
218
219     private void addFlow(final short version) throws ExecutionException, InterruptedException {
220         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
221                 .setMatch(match)
222                 .setTableId(Uint8.ONE)
223                 .setNode(noderef)
224                 .build();
225         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
226
227         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
228     }
229
230     @Test
231     public void testRemoveFlow() throws Exception {
232         removeFlow(OFConstants.OFP_VERSION_1_0);
233         removeFlow(OFConstants.OFP_VERSION_1_3);
234     }
235
236     @Test
237     public void testRemoveFlowWithItemLifecycle() throws Exception {
238         removeFlow(OFConstants.OFP_VERSION_1_0);
239         removeFlow(OFConstants.OFP_VERSION_1_3);
240     }
241
242     private void removeFlow(final short version) throws Exception {
243         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
244                 .setMatch(match)
245                 .setTableId(Uint8.ONE)
246                 .setNode(noderef)
247                 .build();
248
249         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
250         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
251     }
252
253     @Test
254     public void testUpdateFlow() throws Exception {
255         updateFlow(OFConstants.OFP_VERSION_1_0);
256         updateFlow(OFConstants.OFP_VERSION_1_3);
257     }
258
259     @Test
260     public void testUpdateFlowWithItemLifecycle() throws Exception {
261         updateFlow(OFConstants.OFP_VERSION_1_0);
262         updateFlow(OFConstants.OFP_VERSION_1_3);
263     }
264
265     private void updateFlow(final short version) throws Exception {
266         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
267         UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
268
269         UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
270                 .setMatch(match)
271                 .setTableId(Uint8.ONE)
272                 .build();
273
274         UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
275                 .setMatch(match)
276                 .setTableId(Uint8.ONE)
277                 .setPriority(Uint16.ONE)
278                 .build();
279
280         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
281         when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
282
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);
288
289         OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
290                 .setMatch(match)
291                 .setTableId(Uint8.ONE)
292                 .build();
293
294         OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
295                 .setMatch(match)
296                 .setTableId(Uint8.ONE)
297                 .setPriority(Uint16.TWO)
298                 .build();
299
300         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
301         when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
302
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));
308     }
309
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());
316     }
317 }