2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.services;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
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;
68 @RunWith(MockitoJUnitRunner.class)
69 public class SalFlowServiceImplTest extends TestCase {
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;
77 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
78 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
80 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
81 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
84 private RequestContextStack mockedRequestContextStack;
86 private DeviceContext mockedDeviceContext;
88 private ConnectionContext mockedPrimConnectionContext;
90 private FeaturesReply mockedFeatures;
92 private ConnectionAdapter mockedConnectionAdapter;
94 private MessageSpy mockedMessagSpy;
96 private RequestContext<Object> requestContext;
98 private OutboundQueue outboundQueue;
101 private SalFlowServiceImpl salFlowService;
104 private DeviceState mockedDeviceState;
106 private DeviceInfo mockedDeviceInfo;
108 private DeviceFlowRegistry deviceFlowRegistry;
110 private GetFeaturesOutput mockedFeaturesOutput;
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);
119 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
120 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
121 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
123 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
125 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
126 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
127 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
129 when(requestContext.getXid()).thenReturn(new Xid(84L));
130 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
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);
138 salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
142 public void testAddFlow() throws Exception {
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();
152 final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
154 assertNotNull(rpcResultFuture);
155 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
156 assertNotNull(addFlowOutputRpcResult);
157 assertFalse(addFlowOutputRpcResult.isSuccessful());
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();
166 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
168 assertNotNull(rpcResultFuture);
169 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
170 assertNotNull(removeFlowOutputRpcResult);
171 assertFalse(removeFlowOutputRpcResult.isSuccessful());
175 public void testAddFlowWithItemLifecycle() throws Exception {
176 addFlow(mock(ItemLifecycleListener.class));
179 private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
180 AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
181 salFlowService.setItemLifecycleListener(itemLifecycleListener);
183 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
184 if (itemLifecycleListener != null) {
185 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
190 public void testRemoveFlow() throws Exception {
195 public void testRemoveFlowWithItemLifecycle() throws Exception {
196 removeFlow(mock(ItemLifecycleListener.class));
199 private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
200 RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
202 if (itemLifecycleListener != null) {
203 salFlowService.setItemLifecycleListener(itemLifecycleListener);
204 mockingFlowRegistryLookup();
208 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
209 if (itemLifecycleListener != null) {
210 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
216 public void testUpdateFlow() throws Exception {
221 public void testUpdateFlowWithItemLifecycle() throws Exception {
222 updateFlow(mock(ItemLifecycleListener.class));
225 private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
226 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
228 UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
229 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
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);
235 OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
236 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
238 if (itemLifecycleListener != null) {
239 salFlowService.setItemLifecycleListener(itemLifecycleListener);
240 mockingFlowRegistryLookup();
243 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
245 if (itemLifecycleListener != null) {
246 Mockito.verify(itemLifecycleListener).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
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));
256 when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
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());
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);