Bump mdsal to 5.0.2
[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.ArgumentMatchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
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;
73
74 @RunWith(MockitoJUnitRunner.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 Short DUMMY_TABLE_ID = (short) 0;
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     @Mock
89     private RequestContextStack mockedRequestContextStack;
90     @Mock
91     private DeviceContext mockedDeviceContext;
92     @Mock
93     private ConnectionContext mockedPrimConnectionContext;
94     @Mock
95     private FeaturesReply mockedFeatures;
96     @Mock
97     private ConnectionAdapter mockedConnectionAdapter;
98     @Mock
99     private MessageSpy mockedMessagSpy;
100     @Mock
101     private RequestContext<Object> requestContext;
102     @Mock
103     private OutboundQueue outboundQueue;
104     @Mock
105     private Match match;
106
107     @Mock
108     private DeviceState mockedDeviceState;
109     @Mock
110     private DeviceInfo mockedDeviceInfo;
111     @Mock
112     private DeviceFlowRegistry deviceFlowRegistry;
113     @Mock
114     private GetFeaturesOutput mockedFeaturesOutput;
115
116     @Before
117     public void initialization() {
118
119         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
120
121         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
122         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
123         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
124
125         when(requestContext.getXid()).thenReturn(new Xid(Uint32.valueOf(84L)));
126         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
127         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
128
129         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
130
131         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
132     }
133
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);
139
140         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
141         return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
142     }
143
144     @Test
145     public void testAddFlow() throws Exception {
146         addFlow(OFConstants.OFP_VERSION_1_0);
147         addFlow(OFConstants.OFP_VERSION_1_3);
148     }
149
150     @Test
151     public void testAddFlowFailCallback() throws Exception {
152         addFlowFailCallback(OFConstants.OFP_VERSION_1_0);
153     }
154
155     @Test
156     public void testAddFlowFailCallback1() throws Exception {
157         addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
158     }
159
160     private void addFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
161         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
162                 .setMatch(match)
163                 .setTableId((short)1)
164                 .build();
165
166         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
167                 .when(requestContext).getFuture();
168
169         mockingFlowRegistryLookup();
170         final Future<RpcResult<AddFlowOutput>> rpcResultFuture =
171                 mockSalFlowService(version).addFlow(mockedAddFlowInput);
172
173         assertNotNull(rpcResultFuture);
174         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
175         assertNotNull(addFlowOutputRpcResult);
176         assertFalse(addFlowOutputRpcResult.isSuccessful());
177     }
178
179     @Test
180     public void testRemoveFlowFailCallback() throws Exception {
181         removeFlowFailCallback(OFConstants.OFP_VERSION_1_0);
182     }
183
184     @Test
185     public void testRemoveFlowFailCallback1() throws Exception {
186         removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
187     }
188
189     private void removeFlowFailCallback(final short version) throws InterruptedException, ExecutionException {
190         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
191                 .setTableId((short)1)
192                 .setMatch(match)
193                 .build();
194
195         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
196                 .when(requestContext).getFuture();
197
198         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture =
199                 mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
200
201         assertNotNull(rpcResultFuture);
202         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
203         assertNotNull(removeFlowOutputRpcResult);
204         assertFalse(removeFlowOutputRpcResult.isSuccessful());
205     }
206
207     @Test
208     public void testAddFlowWithItemLifecycle() throws Exception {
209         addFlow(OFConstants.OFP_VERSION_1_0);
210         addFlow(OFConstants.OFP_VERSION_1_3);
211     }
212
213     private void addFlow(final short version) throws ExecutionException, InterruptedException {
214         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
215                 .setMatch(match)
216                 .setTableId((short)1)
217                 .build();
218         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
219
220         mockingFlowRegistryLookup();
221         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
222     }
223
224     @Test
225     public void testRemoveFlow() throws Exception {
226         removeFlow(OFConstants.OFP_VERSION_1_0);
227         removeFlow(OFConstants.OFP_VERSION_1_3);
228     }
229
230     @Test
231     public void testRemoveFlowWithItemLifecycle() throws Exception {
232         removeFlow(OFConstants.OFP_VERSION_1_0);
233         removeFlow(OFConstants.OFP_VERSION_1_3);
234     }
235
236     private void removeFlow(final short version) throws Exception {
237         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
238                 .setMatch(match)
239                 .setTableId((short)1)
240                 .build();
241
242         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
243         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
244     }
245
246     @Test
247     public void testUpdateFlow() throws Exception {
248         updateFlow(OFConstants.OFP_VERSION_1_0);
249         updateFlow(OFConstants.OFP_VERSION_1_3);
250     }
251
252     @Test
253     public void testUpdateFlowWithItemLifecycle() throws Exception {
254         updateFlow(OFConstants.OFP_VERSION_1_0);
255         updateFlow(OFConstants.OFP_VERSION_1_3);
256     }
257
258     private void updateFlow(final short version) throws Exception {
259         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
260         UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
261
262         UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
263                 .setMatch(match)
264                 .setTableId((short)1)
265                 .build();
266
267         UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
268                 .setMatch(match)
269                 .setTableId((short)1)
270                 .setPriority(1)
271                 .build();
272
273         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
274         when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
275
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);
281
282         OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
283                 .setMatch(match)
284                 .setTableId((short)1)
285                 .build();
286
287         OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
288                 .setMatch(match)
289                 .setTableId((short)1)
290                 .setPriority(2)
291                 .build();
292
293         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
294         when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
295
296         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
297
298         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
299         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
300     }
301
302     private void mockingFlowRegistryLookup() {
303         FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
304         FlowId flowId = new FlowId(DUMMY_FLOW_ID);
305         when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
306
307         when(deviceFlowRegistry
308                 .retrieveDescriptor(any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
309     }
310
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());
317     }
318 }