6600c9e8a5ec0ab102e997dad54ed54643cd6335
[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.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.RemoveFlowOutput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
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.types.rev131026.FlowRef;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
62 import org.opendaylight.yangtools.yang.binding.DataObject;
63 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
65 import org.opendaylight.yangtools.yang.common.RpcResult;
66 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
67
68 @RunWith(MockitoJUnitRunner.class)
69 public class SalFlowServiceImplTest extends TestCase {
70
71     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
72     private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
73     private static final String DUMMY_NODE_ID = "dummyNodeID";
74     private static final String DUMMY_FLOW_ID = "dummyFlowID";
75     private static final Short DUMMY_TABLE_ID = (short) 0;
76
77     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
78             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
79
80     private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
81             = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
82
83     @Mock
84     private RequestContextStack mockedRequestContextStack;
85     @Mock
86     private DeviceContext mockedDeviceContext;
87     @Mock
88     private ConnectionContext mockedPrimConnectionContext;
89     @Mock
90     private FeaturesReply mockedFeatures;
91     @Mock
92     private ConnectionAdapter mockedConnectionAdapter;
93     @Mock
94     private MessageSpy mockedMessagSpy;
95     @Mock
96     private RequestContext<Object> requestContext;
97     @Mock
98     private OutboundQueue outboundQueue;
99     @Mock
100     private Match match;
101     private SalFlowServiceImpl salFlowService;
102
103     @Mock
104     private DeviceState mockedDeviceState;
105     @Mock
106     private DeviceInfo mockedDeviceInfo;
107     @Mock
108     private DeviceFlowRegistry deviceFlowRegistry;
109     @Mock
110     private GetFeaturesOutput mockedFeaturesOutput;
111
112     @Before
113     public void initialization() {
114         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
115         when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
116         when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
117         when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
118
119         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
120         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
121         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
122
123         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
124
125         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
126         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
127         when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
128
129         when(requestContext.getXid()).thenReturn(new Xid(84L));
130         when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
131
132         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
133         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
134         when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
135         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
136         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
137
138         salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
139     }
140
141     @Test
142     public void testAddFlow() throws Exception {
143         addFlow(null);
144     }
145
146     @Test
147     public void testAddFlowFailCallback() throws Exception {
148         AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
149         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
150                 .when(requestContext).getFuture();
151
152         mockingFlowRegistryLookup();
153         final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
154
155         assertNotNull(rpcResultFuture);
156         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
157         assertNotNull(addFlowOutputRpcResult);
158         assertFalse(addFlowOutputRpcResult.isSuccessful());
159     }
160
161     @Test
162     public void testRemoveFlowFailCallback() throws Exception {
163         RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
164         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
165                 .when(requestContext).getFuture();
166
167         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
168
169         assertNotNull(rpcResultFuture);
170         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
171         assertNotNull(removeFlowOutputRpcResult);
172         assertFalse(removeFlowOutputRpcResult.isSuccessful());
173     }
174
175     @Test
176     public void testAddFlowWithItemLifecycle() throws Exception {
177         addFlow(mock(ItemLifecycleListener.class));
178     }
179
180     private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
181         AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
182         salFlowService.setItemLifecycleListener(itemLifecycleListener);
183
184         mockingFlowRegistryLookup();
185         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
186         if (itemLifecycleListener != null) {
187             Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
188         }
189     }
190
191     @Test
192     public void testRemoveFlow() throws Exception {
193         removeFlow(null);
194     }
195
196     @Test
197     public void testRemoveFlowWithItemLifecycle() throws Exception {
198         removeFlow(mock(ItemLifecycleListener.class));
199     }
200
201     private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
202         RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
203
204         if (itemLifecycleListener != null) {
205             salFlowService.setItemLifecycleListener(itemLifecycleListener);
206             mockingFlowRegistryLookup();
207
208         }
209
210         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
211         if (itemLifecycleListener != null) {
212             Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
213         }
214
215     }
216
217     @Test
218     public void testUpdateFlow() throws Exception {
219         updateFlow(null);
220     }
221
222     @Test
223     public void testUpdateFlowWithItemLifecycle() throws Exception {
224         updateFlow(mock(ItemLifecycleListener.class));
225     }
226
227     private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
228         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
229
230         UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
231         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
232
233         FlowRef mockedFlowRef = mock(FlowRef.class);
234         Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
235         when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
236
237         OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
238         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
239
240         if (itemLifecycleListener != null) {
241             salFlowService.setItemLifecycleListener(itemLifecycleListener);
242             mockingFlowRegistryLookup();
243         }
244
245         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
246
247         if (itemLifecycleListener != null) {
248             Mockito.verify(itemLifecycleListener).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
249         }
250
251     }
252
253     private void mockingFlowRegistryLookup() {
254         FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
255         FlowId flowId = new FlowId(DUMMY_FLOW_ID);
256         when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
257         when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
258
259         when(deviceFlowRegistry.storeIfNecessary(Matchers.any(FlowRegistryKey.class))).thenReturn(flowId);
260         when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
261     }
262
263     private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
264         assertNotNull(rpcResultFuture);
265         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
266         assertNotNull(addFlowOutputRpcResult);
267         assertTrue(addFlowOutputRpcResult.isSuccessful());
268     }
269
270     private <T extends org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow> T createFlowMock(Class<T> flowClazz) {
271         T mockedFlow = mock(flowClazz);
272         when(mockedFlow.getMatch()).thenReturn(match);
273         return mockedFlow;
274     }
275 }