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.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
42 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInputBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlowBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlowBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
68 import org.opendaylight.yangtools.yang.binding.DataObject;
69 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
70 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
71 import org.opendaylight.yangtools.yang.common.RpcResult;
72 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
74 @RunWith(MockitoJUnitRunner.class)
75 public class SalFlowServiceImplTest extends TestCase {
77 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
78 private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
79 private static final String DUMMY_NODE_ID = "dummyNodeID";
80 private static final String DUMMY_FLOW_ID = "dummyFlowID";
81 private static final Short DUMMY_TABLE_ID = (short) 0;
83 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
84 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
86 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
87 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
90 private RequestContextStack mockedRequestContextStack;
92 private DeviceContext mockedDeviceContext;
94 private ConnectionContext mockedPrimConnectionContext;
96 private FeaturesReply mockedFeatures;
98 private ConnectionAdapter mockedConnectionAdapter;
100 private MessageSpy mockedMessagSpy;
102 private RequestContext<Object> requestContext;
104 private OutboundQueue outboundQueue;
107 private SalFlowServiceImpl salFlowService;
110 private DeviceState mockedDeviceState;
112 private DeviceInfo mockedDeviceInfo;
114 private DeviceFlowRegistry deviceFlowRegistry;
116 private GetFeaturesOutput mockedFeaturesOutput;
119 public void initialization() {
120 when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
121 when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
122 when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
123 when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
125 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
126 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
127 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
129 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
131 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
132 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
133 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
135 when(requestContext.getXid()).thenReturn(new Xid(84L));
136 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
138 when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
139 when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
140 when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
141 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
142 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
144 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
145 salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext, convertorManager);
149 public void testAddFlow() throws Exception {
154 public void testAddFlowFailCallback() throws Exception {
155 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
157 .setTableId((short)1)
160 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
161 .when(requestContext).getFuture();
163 mockingFlowRegistryLookup();
164 final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
166 assertNotNull(rpcResultFuture);
167 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
168 assertNotNull(addFlowOutputRpcResult);
169 assertFalse(addFlowOutputRpcResult.isSuccessful());
173 public void testRemoveFlowFailCallback() throws Exception {
174 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
178 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
179 .when(requestContext).getFuture();
181 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
183 assertNotNull(rpcResultFuture);
184 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
185 assertNotNull(removeFlowOutputRpcResult);
186 assertFalse(removeFlowOutputRpcResult.isSuccessful());
190 public void testAddFlowWithItemLifecycle() throws Exception {
191 addFlow(mock(ItemLifecycleListener.class));
194 private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
195 AddFlowInput mockedAddFlowInput = new AddFlowInputBuilder()
197 .setTableId((short)1)
200 salFlowService.setItemLifecycleListener(itemLifecycleListener);
202 mockingFlowRegistryLookup();
203 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
204 if (itemLifecycleListener != null) {
205 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
210 public void testRemoveFlow() throws Exception {
215 public void testRemoveFlowWithItemLifecycle() throws Exception {
216 removeFlow(mock(ItemLifecycleListener.class));
219 private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
220 RemoveFlowInput mockedRemoveFlowInput = new RemoveFlowInputBuilder()
222 .setTableId((short)1)
225 if (itemLifecycleListener != null) {
226 salFlowService.setItemLifecycleListener(itemLifecycleListener);
227 mockingFlowRegistryLookup();
231 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
232 if (itemLifecycleListener != null) {
233 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
239 public void testUpdateFlow() throws Exception {
244 public void testUpdateFlowWithItemLifecycle() throws Exception {
245 updateFlow(mock(ItemLifecycleListener.class));
248 private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
249 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
251 UpdatedFlow mockedUpdateFlow = new UpdatedFlowBuilder()
253 .setTableId((short)1)
256 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
258 FlowRef mockedFlowRef = mock(FlowRef.class);
259 Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
260 when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
262 OriginalFlow mockedOriginalFlow = new OriginalFlowBuilder()
264 .setTableId((short)1)
267 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
269 if (itemLifecycleListener != null) {
270 salFlowService.setItemLifecycleListener(itemLifecycleListener);
271 mockingFlowRegistryLookup();
274 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
276 if (itemLifecycleListener != null) {
277 Mockito.verify(itemLifecycleListener).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
282 private void mockingFlowRegistryLookup() {
283 FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
284 FlowId flowId = new FlowId(DUMMY_FLOW_ID);
285 when(mockedFlowDescriptor.getFlowId()).thenReturn(flowId);
286 when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
288 when(deviceFlowRegistry.storeIfNecessary(Matchers.any(FlowRegistryKey.class))).thenReturn(flowId);
289 when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
292 private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
293 assertNotNull(rpcResultFuture);
294 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
295 assertNotNull(addFlowOutputRpcResult);
296 assertTrue(addFlowOutputRpcResult.isSuccessful());