af67ef1b5882c11f5e081b55c30f1d52244ed787
[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.math.BigInteger;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import junit.framework.TestCase;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
26 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
27 import org.opendaylight.openflowplugin.api.OFConstants;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
32 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
33 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
34 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
35 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
36 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
37 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
38 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
39 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
40 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
66 import org.opendaylight.yangtools.yang.binding.DataObject;
67 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
68 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
69 import org.opendaylight.yangtools.yang.common.RpcResult;
70 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
71
72 @RunWith(MockitoJUnitRunner.class)
73 public class SalFlowServiceImplTest extends TestCase {
74
75     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
76     private static final String DUMMY_NODE_ID = "dummyNodeID";
77     private static final String DUMMY_FLOW_ID = "dummyFlowID";
78     private static final Short DUMMY_TABLE_ID = (short) 0;
79
80     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
81             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
82
83     private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
84             = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
85
86     @Mock
87     private RequestContextStack mockedRequestContextStack;
88     @Mock
89     private DeviceContext mockedDeviceContext;
90     @Mock
91     private ConnectionContext mockedPrimConnectionContext;
92     @Mock
93     private FeaturesReply mockedFeatures;
94     @Mock
95     private ConnectionAdapter mockedConnectionAdapter;
96     @Mock
97     private MessageSpy mockedMessagSpy;
98     @Mock
99     private RequestContext<Object> requestContext;
100     @Mock
101     private OutboundQueue outboundQueue;
102     @Mock
103     private Match match;
104
105     @Mock
106     private DeviceState mockedDeviceState;
107     @Mock
108     private DeviceInfo mockedDeviceInfo;
109     @Mock
110     private DeviceFlowRegistry deviceFlowRegistry;
111     @Mock
112     private GetFeaturesOutput mockedFeaturesOutput;
113
114     @Before
115     public void initialization() {
116
117         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
118
119         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
120         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
121         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
122
123         when(requestContext.getXid()).thenReturn(new Xid(84L));
124         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
125         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
126
127         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
128
129         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
130     }
131
132     private SalFlowServiceImpl mockSalFlowService(final short version) {
133         when(mockedFeatures.getVersion()).thenReturn(version);
134         when(mockedFeaturesOutput.getVersion()).thenReturn(version);
135         when(mockedDeviceInfo.getVersion()).thenReturn(version);
136
137         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
138         return new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
139     }
140
141     @Test
142     public void testAddFlow() throws Exception {
143         addFlow(OFConstants.OFP_VERSION_1_0);
144         addFlow(OFConstants.OFP_VERSION_1_3);
145     }
146
147     @Test
148     public void testAddFlowFailCallback() throws Exception {
149         addFlowFailCallback(OFConstants.OFP_VERSION_1_0);
150     }
151
152     @Test
153     public void testAddFlowFailCallback1() throws Exception {
154         addFlowFailCallback(OFConstants.OFP_VERSION_1_3);
155     }
156
157     private void addFlowFailCallback(short version) throws InterruptedException, ExecutionException {
158         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
159                 .setMatch(match)
160                 .setTableId((short)1)
161                 .build();
162
163         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
164                 .when(requestContext).getFuture();
165
166         mockingFlowRegistryLookup();
167         final Future<RpcResult<AddFlowOutput>> rpcResultFuture =
168                 mockSalFlowService(version).addFlow(mockedAddFlowInput);
169
170         assertNotNull(rpcResultFuture);
171         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
172         assertNotNull(addFlowOutputRpcResult);
173         assertFalse(addFlowOutputRpcResult.isSuccessful());
174     }
175
176     @Test
177     public void testRemoveFlowFailCallback() throws Exception {
178         removeFlowFailCallback(OFConstants.OFP_VERSION_1_0);
179     }
180
181     @Test
182     public void testRemoveFlowFailCallback1() throws Exception {
183         removeFlowFailCallback(OFConstants.OFP_VERSION_1_3);
184     }
185
186     private void removeFlowFailCallback(short version) throws InterruptedException, ExecutionException {
187         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
188                 .setTableId((short)1)
189                 .setMatch(match)
190                 .build();
191
192         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
193                 .when(requestContext).getFuture();
194
195         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture =
196                 mockSalFlowService(version).removeFlow(mockedRemoveFlowInput);
197
198         assertNotNull(rpcResultFuture);
199         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
200         assertNotNull(removeFlowOutputRpcResult);
201         assertFalse(removeFlowOutputRpcResult.isSuccessful());
202     }
203
204     @Test
205     public void testAddFlowWithItemLifecycle() throws Exception {
206         addFlow(OFConstants.OFP_VERSION_1_0);
207         addFlow(OFConstants.OFP_VERSION_1_3);
208     }
209
210     private void addFlow(short version) throws ExecutionException,
211                                                                                                   InterruptedException {
212         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
213                 .setMatch(match)
214                 .setTableId((short)1)
215                 .build();
216         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
217
218         mockingFlowRegistryLookup();
219         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
220     }
221
222     @Test
223     public void testRemoveFlow() throws Exception {
224         removeFlow(OFConstants.OFP_VERSION_1_0);
225         removeFlow(OFConstants.OFP_VERSION_1_3);
226     }
227
228     @Test
229     public void testRemoveFlowWithItemLifecycle() throws Exception {
230         removeFlow(OFConstants.OFP_VERSION_1_0);
231         removeFlow(OFConstants.OFP_VERSION_1_3);
232     }
233
234     private void removeFlow(short version) throws Exception {
235         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
236                 .setMatch(match)
237                 .setTableId((short)1)
238                 .build();
239
240         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
241         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
242     }
243
244     @Test
245     public void testUpdateFlow() throws Exception {
246         updateFlow(OFConstants.OFP_VERSION_1_0);
247         updateFlow(OFConstants.OFP_VERSION_1_3);
248     }
249
250     @Test
251     public void testUpdateFlowWithItemLifecycle() throws Exception {
252         updateFlow(OFConstants.OFP_VERSION_1_0);
253         updateFlow(OFConstants.OFP_VERSION_1_3);
254     }
255
256     private void updateFlow(short version) throws Exception {
257         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
258         UpdateFlowInput mockedUpdateFlowInput1 = mock(UpdateFlowInput.class);
259
260         UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
261                 .setMatch(match)
262                 .setTableId((short)1)
263                 .build();
264
265         UpdatedFlow mockedUpdateFlow1 = new UpdatedFlowBuilder()
266                 .setMatch(match)
267                 .setTableId((short)1)
268                 .setPriority(1)
269                 .build();
270
271         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
272         when(mockedUpdateFlowInput1.getUpdatedFlow()).thenReturn(mockedUpdateFlow1);
273
274         FlowRef mockedFlowRef = mock(FlowRef.class);
275         Mockito.doReturn(TABLE_II.child(Flow.class,
276                          new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
277         when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
278         when(mockedUpdateFlowInput1.getFlowRef()).thenReturn(mockedFlowRef);
279
280         OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
281                 .setMatch(match)
282                 .setTableId((short)1)
283                 .build();
284
285         OriginalFlow mockedOriginalFlow1 = new OriginalFlowBuilder()
286                 .setMatch(match)
287                 .setTableId((short)1)
288                 .setPriority(2)
289                 .build();
290
291         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
292         when(mockedUpdateFlowInput1.getOriginalFlow()).thenReturn(mockedOriginalFlow1);
293
294         SalFlowServiceImpl salFlowService = mockSalFlowService(version);
295
296         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
297         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput1));
298     }
299
300     private void mockingFlowRegistryLookup() {
301         FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
302         FlowId flowId = new FlowId(DUMMY_FLOW_ID);
303         when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
304
305         when(deviceFlowRegistry
306                 .retrieveDescriptor(any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
307     }
308
309     private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException,
310                                                                                                   InterruptedException {
311         assertNotNull(rpcResultFuture);
312         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
313         assertNotNull(addFlowOutputRpcResult);
314         assertTrue(addFlowOutputRpcResult.isSuccessful());
315     }
316 }