1 package org.opendaylight.openflowplugin.impl.services;
4 import static org.mockito.Mockito.mock;
5 import static org.mockito.Mockito.when;
7 import com.google.common.util.concurrent.Futures;
8 import java.math.BigInteger;
9 import java.util.concurrent.ExecutionException;
10 import java.util.concurrent.Future;
11 import junit.framework.TestCase;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
15 import org.mockito.Matchers;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.runners.MockitoJUnitRunner;
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
20 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
25 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
27 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
28 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
29 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
30 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
31 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
54 import org.opendaylight.yangtools.yang.binding.DataObject;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
57 import org.opendaylight.yangtools.yang.common.RpcResult;
58 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
60 @RunWith(MockitoJUnitRunner.class)
61 public class SalFlowServiceImplTest extends TestCase {
63 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
64 private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
65 private static final String DUMMY_NODE_ID = "dummyNodeID";
66 private static final String DUMMY_FLOW_ID = "dummyFlowID";
67 private static final Short DUMMY_TABLE_ID = (short) 0;
69 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
70 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
72 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
73 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
76 private RequestContextStack mockedRequestContextStack;
78 private DeviceContext mockedDeviceContext;
80 private ConnectionContext mockedPrimConnectionContext;
82 private FeaturesReply mockedFeatures;
84 private ConnectionAdapter mockedConnectionAdapter;
86 private MessageSpy mockedMessagSpy;
88 private RequestContext<Object> requestContext;
90 private OutboundQueue outboundQueue;
93 private SalFlowServiceImpl salFlowService;
96 private DeviceState mockedDeviceState;
98 private DeviceFlowRegistry deviceFlowRegistry;
100 private GetFeaturesOutput mockedFeaturesOutput;
103 public void initialization() {
104 when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
105 when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
106 when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
107 when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
109 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
110 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
111 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
113 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
115 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
116 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
117 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
119 when(requestContext.getXid()).thenReturn(new Xid(84L));
120 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
122 when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(NODE_II);
123 when(mockedDeviceState.getFeatures()).thenReturn(mockedFeaturesOutput);
124 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
126 salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
130 public void testAddFlow() throws Exception {
135 public void testAddFlowFailCallback() throws Exception {
136 AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
137 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
138 .when(requestContext).getFuture();
140 final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
142 assertNotNull(rpcResultFuture);
143 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
144 assertNotNull(addFlowOutputRpcResult);
145 assertFalse(addFlowOutputRpcResult.isSuccessful());
149 public void testRemoveFlowFailCallback() throws Exception {
150 RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
151 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
152 .when(requestContext).getFuture();
154 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
156 assertNotNull(rpcResultFuture);
157 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
158 assertNotNull(removeFlowOutputRpcResult);
159 assertFalse(removeFlowOutputRpcResult.isSuccessful());
163 public void testAddFlowWithItemLifecycle() throws Exception {
164 addFlow(mock(ItemLifecycleListener.class));
167 private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
168 AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
169 salFlowService.setItemLifecycleListener(itemLifecycleListener);
171 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
172 if (itemLifecycleListener != null) {
173 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
178 public void testRemoveFlow() throws Exception {
183 public void testRemoveFlowWithItemLifecycle() throws Exception {
184 removeFlow(mock(ItemLifecycleListener.class));
187 private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
188 RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
190 if (itemLifecycleListener != null) {
191 salFlowService.setItemLifecycleListener(itemLifecycleListener);
192 mockingFlowRegistryLookup();
196 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
197 if (itemLifecycleListener != null) {
198 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
204 public void testUpdateFlow() throws Exception {
209 public void testUpdateFlowWithItemLifecycle() throws Exception {
210 updateFlow(mock(ItemLifecycleListener.class));
213 private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
214 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
216 UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
217 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
219 FlowRef mockedFlowRef = mock(FlowRef.class);
220 Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
221 when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
223 OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
224 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
226 if (itemLifecycleListener != null) {
227 salFlowService.setItemLifecycleListener(itemLifecycleListener);
228 mockingFlowRegistryLookup();
231 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
233 if (itemLifecycleListener != null) {
234 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
235 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
240 private void mockingFlowRegistryLookup() {
241 FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
242 when(mockedFlowDescriptor.getFlowId()).thenReturn(new FlowId(DUMMY_FLOW_ID));
243 when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
245 when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
248 private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
249 assertNotNull(rpcResultFuture);
250 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
251 assertNotNull(addFlowOutputRpcResult);
252 assertTrue(addFlowOutputRpcResult.isSuccessful());
255 private <T extends org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow> T createFlowMock(Class<T> flowClazz) {
256 T mockedFlow = mock(flowClazz);
257 when(mockedFlow.getMatch()).thenReturn(match);