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.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;
72 @RunWith(MockitoJUnitRunner.class)
73 public class SalFlowServiceImplTest extends TestCase {
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;
81 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
82 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
84 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
85 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
88 private RequestContextStack mockedRequestContextStack;
90 private DeviceContext mockedDeviceContext;
92 private ConnectionContext mockedPrimConnectionContext;
94 private FeaturesReply mockedFeatures;
96 private ConnectionAdapter mockedConnectionAdapter;
98 private MessageSpy mockedMessagSpy;
100 private RequestContext<Object> requestContext;
102 private OutboundQueue outboundQueue;
105 private SalFlowServiceImpl salFlowService;
108 private DeviceState mockedDeviceState;
110 private DeviceInfo mockedDeviceInfo;
112 private DeviceFlowRegistry deviceFlowRegistry;
114 private GetFeaturesOutput mockedFeaturesOutput;
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);
123 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
124 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
125 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
127 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
129 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
130 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
131 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
133 when(requestContext.getXid()).thenReturn(new Xid(84L));
134 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
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);
142 salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
146 public void testAddFlow() throws Exception {
151 public void testAddFlowFailCallback() throws Exception {
152 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
154 .setTableId((short)1)
157 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
158 .when(requestContext).getFuture();
160 mockingFlowRegistryLookup();
161 final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
163 assertNotNull(rpcResultFuture);
164 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
165 assertNotNull(addFlowOutputRpcResult);
166 assertFalse(addFlowOutputRpcResult.isSuccessful());
170 public void testRemoveFlowFailCallback() throws Exception {
171 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
175 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
176 .when(requestContext).getFuture();
178 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
180 assertNotNull(rpcResultFuture);
181 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
182 assertNotNull(removeFlowOutputRpcResult);
183 assertFalse(removeFlowOutputRpcResult.isSuccessful());
187 public void testAddFlowWithItemLifecycle() throws Exception {
188 addFlow(mock(ItemLifecycleListener.class));
191 private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
192 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
194 .setTableId((short)1)
197 salFlowService.setItemLifecycleListener(itemLifecycleListener);
199 mockingFlowRegistryLookup();
200 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
201 if (itemLifecycleListener != null) {
202 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
207 public void testRemoveFlow() throws Exception {
212 public void testRemoveFlowWithItemLifecycle() throws Exception {
213 removeFlow(mock(ItemLifecycleListener.class));
216 private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
217 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
219 .setTableId((short)1)
222 if (itemLifecycleListener != null) {
223 salFlowService.setItemLifecycleListener(itemLifecycleListener);
224 mockingFlowRegistryLookup();
228 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
229 if (itemLifecycleListener != null) {
230 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
236 public void testUpdateFlow() throws Exception {
241 public void testUpdateFlowWithItemLifecycle() throws Exception {
242 updateFlow(mock(ItemLifecycleListener.class));
245 private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
246 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
248 UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
250 .setTableId((short)1)
253 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
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);
259 OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
261 .setTableId((short)1)
264 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
266 if (itemLifecycleListener != null) {
267 salFlowService.setItemLifecycleListener(itemLifecycleListener);
268 mockingFlowRegistryLookup();
271 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
273 if (itemLifecycleListener != null) {
274 Mockito.verify(itemLifecycleListener).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
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));
285 when(deviceFlowRegistry.storeIfNecessary(Matchers.any(FlowRegistryKey.class))).thenReturn(flowId);
286 when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
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());