Add missing license headers
[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         final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
153
154         assertNotNull(rpcResultFuture);
155         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
156         assertNotNull(addFlowOutputRpcResult);
157         assertFalse(addFlowOutputRpcResult.isSuccessful());
158     }
159
160     @Test
161     public void testRemoveFlowFailCallback() throws Exception {
162         RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
163         Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
164                 .when(requestContext).getFuture();
165
166         final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
167
168         assertNotNull(rpcResultFuture);
169         final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
170         assertNotNull(removeFlowOutputRpcResult);
171         assertFalse(removeFlowOutputRpcResult.isSuccessful());
172     }
173
174     @Test
175     public void testAddFlowWithItemLifecycle() throws Exception {
176         addFlow(mock(ItemLifecycleListener.class));
177     }
178
179     private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
180         AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
181         salFlowService.setItemLifecycleListener(itemLifecycleListener);
182
183         verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
184         if (itemLifecycleListener != null) {
185             Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
186         }
187     }
188
189     @Test
190     public void testRemoveFlow() throws Exception {
191         removeFlow(null);
192     }
193
194     @Test
195     public void testRemoveFlowWithItemLifecycle() throws Exception {
196         removeFlow(mock(ItemLifecycleListener.class));
197     }
198
199     private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
200         RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
201
202         if (itemLifecycleListener != null) {
203             salFlowService.setItemLifecycleListener(itemLifecycleListener);
204             mockingFlowRegistryLookup();
205
206         }
207
208         verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
209         if (itemLifecycleListener != null) {
210             Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
211         }
212
213     }
214
215     @Test
216     public void testUpdateFlow() throws Exception {
217         updateFlow(null);
218     }
219
220     @Test
221     public void testUpdateFlowWithItemLifecycle() throws Exception {
222         updateFlow(mock(ItemLifecycleListener.class));
223     }
224
225     private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
226         UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
227
228         UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
229         when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
230
231         FlowRef mockedFlowRef = mock(FlowRef.class);
232         Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
233         when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
234
235         OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
236         when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
237
238         if (itemLifecycleListener != null) {
239             salFlowService.setItemLifecycleListener(itemLifecycleListener);
240             mockingFlowRegistryLookup();
241         }
242
243         verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
244
245         if (itemLifecycleListener != null) {
246             Mockito.verify(itemLifecycleListener).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
247         }
248
249     }
250
251     private void mockingFlowRegistryLookup() {
252         FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
253         when(mockedFlowDescriptor.getFlowId()).thenReturn(new FlowId(DUMMY_FLOW_ID));
254         when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
255
256         when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
257     }
258
259     private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
260         assertNotNull(rpcResultFuture);
261         final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
262         assertNotNull(addFlowOutputRpcResult);
263         assertTrue(addFlowOutputRpcResult.isSuccessful());
264     }
265
266     private <T extends org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow> T createFlowMock(Class<T> flowClazz) {
267         T mockedFlow = mock(flowClazz);
268         when(mockedFlow.getMatch()).thenReturn(match);
269         return mockedFlow;
270     }
271 }