Bug 5540 - ConvertorManager DataContainer source, one Convertor interface
[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.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.Matchers;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.runners.MockitoJUnitRunner;
26 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
27 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
28 import org.opendaylight.openflowplugin.api.OFConstants;
29 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
30 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
32 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
33 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
35 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
36 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
37 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
38 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
39 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
40 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
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 Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
77     private static final String DUMMY_NODE_ID = "dummyNodeID";
78     private static final String DUMMY_FLOW_ID = "dummyFlowID";
79     private static final Short DUMMY_TABLE_ID = (short) 0;
80
81     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
82             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
83
84     private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
85             = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
86
87     @Mock
88     private RequestContextStack mockedRequestContextStack;
89     @Mock
90     private DeviceContext mockedDeviceContext;
91     @Mock
92     private ConnectionContext mockedPrimConnectionContext;
93     @Mock
94     private FeaturesReply mockedFeatures;
95     @Mock
96     private ConnectionAdapter mockedConnectionAdapter;
97     @Mock
98     private MessageSpy mockedMessagSpy;
99     @Mock
100     private RequestContext<Object> requestContext;
101     @Mock
102     private OutboundQueue outboundQueue;
103     @Mock
104     private Match match;
105     private SalFlowServiceImpl salFlowService;
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         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
119         when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
120         when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
121         when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
122
123         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
124         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
125         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
126
127         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
128
129         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
130         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
131         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
132
133         when(requestContext.getXid()).thenReturn(new Xid(84L));
134         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
135
136         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
137         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
138         when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
139         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
140         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
141
142         salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
143     }
144
145     @Test
146     public void testAddFlow() throws Exception {
147         addFlow(null);
148     }
149
150     @Test
151     public void testAddFlowFailCallback() throws Exception {
152         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
153                 .setMatch(match)
154                 .setTableId((short)1)
155                 .build();
156
157         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
158                 .when(requestContext).getFuture();
159
160         mockingFlowRegistryLookup();
161         final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
162
163         assertNotNull(rpcResultFuture);
164         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
165         assertNotNull(addFlowOutputRpcResult);
166         assertFalse(addFlowOutputRpcResult.isSuccessful());
167     }
168
169     @Test
170     public void testRemoveFlowFailCallback() throws Exception {
171         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
172                 .setMatch(match)
173                 .build();
174
175         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
176                 .when(requestContext).getFuture();
177
178         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
179
180         assertNotNull(rpcResultFuture);
181         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
182         assertNotNull(removeFlowOutputRpcResult);
183         assertFalse(removeFlowOutputRpcResult.isSuccessful());
184     }
185
186     @Test
187     public void testAddFlowWithItemLifecycle() throws Exception {
188         addFlow(mock(ItemLifecycleListener.class));
189     }
190
191     private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
192         AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
193                 .setMatch(match)
194                 .setTableId((short)1)
195                 .build();
196
197         salFlowService.setItemLifecycleListener(itemLifecycleListener);
198
199         mockingFlowRegistryLookup();
200         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
201         if (itemLifecycleListener != null) {
202             Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
203         }
204     }
205
206     @Test
207     public void testRemoveFlow() throws Exception {
208         removeFlow(null);
209     }
210
211     @Test
212     public void testRemoveFlowWithItemLifecycle() throws Exception {
213         removeFlow(mock(ItemLifecycleListener.class));
214     }
215
216     private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
217         RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
218                 .setMatch(match)
219                 .setTableId((short)1)
220                 .build();
221
222         if (itemLifecycleListener != null) {
223             salFlowService.setItemLifecycleListener(itemLifecycleListener);
224             mockingFlowRegistryLookup();
225
226         }
227
228         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
229         if (itemLifecycleListener != null) {
230             Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
231         }
232
233     }
234
235     @Test
236     public void testUpdateFlow() throws Exception {
237         updateFlow(null);
238     }
239
240     @Test
241     public void testUpdateFlowWithItemLifecycle() throws Exception {
242         updateFlow(mock(ItemLifecycleListener.class));
243     }
244
245     private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
246         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
247
248         UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
249                 .setMatch(match)
250                 .setTableId((short)1)
251                 .build();
252
253         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
254
255         FlowRef mockedFlowRef = mock(FlowRef.class);
256         Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
257         when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
258
259         OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
260                 .setMatch(match)
261                 .setTableId((short)1)
262                 .build();
263
264         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
265
266         if (itemLifecycleListener != null) {
267             salFlowService.setItemLifecycleListener(itemLifecycleListener);
268             mockingFlowRegistryLookup();
269         }
270
271         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
272
273         if (itemLifecycleListener != null) {
274             Mockito.verify(itemLifecycleListener).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
275         }
276
277     }
278
279     private void mockingFlowRegistryLookup() {
280         FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
281         FlowId flowId = new FlowId(DUMMY_FLOW_ID);
282         when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
283         when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
284
285         when(deviceFlowRegistry.storeIfNecessary(Matchers.any(FlowRegistryKey.class))).thenReturn(flowId);
286         when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
287     }
288
289     private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
290         assertNotNull(rpcResultFuture);
291         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
292         assertNotNull(addFlowOutputRpcResult);
293         assertTrue(addFlowOutputRpcResult.isSuccessful());
294     }
295 }