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.DeviceInfo;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
26 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
27 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
28 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
29 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
30 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
31 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
32 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
33 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
55 import org.opendaylight.yangtools.yang.binding.DataObject;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
58 import org.opendaylight.yangtools.yang.common.RpcResult;
59 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
61 @RunWith(MockitoJUnitRunner.class)
62 public class SalFlowServiceImplTest extends TestCase {
64 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
65 private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
66 private static final String DUMMY_NODE_ID = "dummyNodeID";
67 private static final String DUMMY_FLOW_ID = "dummyFlowID";
68 private static final Short DUMMY_TABLE_ID = (short) 0;
70 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
71 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
73 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
74 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
77 private RequestContextStack mockedRequestContextStack;
79 private DeviceContext mockedDeviceContext;
81 private ConnectionContext mockedPrimConnectionContext;
83 private FeaturesReply mockedFeatures;
85 private ConnectionAdapter mockedConnectionAdapter;
87 private MessageSpy mockedMessagSpy;
89 private RequestContext<Object> requestContext;
91 private OutboundQueue outboundQueue;
94 private SalFlowServiceImpl salFlowService;
97 private DeviceState mockedDeviceState;
99 private DeviceInfo mockedDeviceInfo;
101 private DeviceFlowRegistry deviceFlowRegistry;
103 private GetFeaturesOutput mockedFeaturesOutput;
106 public void initialization() {
107 when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
108 when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
109 when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
110 when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
112 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
113 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
114 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
116 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
118 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
119 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
120 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
122 when(requestContext.getXid()).thenReturn(new Xid(84L));
123 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
125 when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
126 when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
127 when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
128 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
129 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
131 salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
135 public void testAddFlow() throws Exception {
140 public void testAddFlowFailCallback() throws Exception {
141 AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
142 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
143 .when(requestContext).getFuture();
145 final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
147 assertNotNull(rpcResultFuture);
148 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
149 assertNotNull(addFlowOutputRpcResult);
150 assertFalse(addFlowOutputRpcResult.isSuccessful());
154 public void testRemoveFlowFailCallback() throws Exception {
155 RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
156 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
157 .when(requestContext).getFuture();
159 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
161 assertNotNull(rpcResultFuture);
162 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
163 assertNotNull(removeFlowOutputRpcResult);
164 assertFalse(removeFlowOutputRpcResult.isSuccessful());
168 public void testAddFlowWithItemLifecycle() throws Exception {
169 addFlow(mock(ItemLifecycleListener.class));
172 private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
173 AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
174 salFlowService.setItemLifecycleListener(itemLifecycleListener);
176 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
177 if (itemLifecycleListener != null) {
178 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
183 public void testRemoveFlow() throws Exception {
188 public void testRemoveFlowWithItemLifecycle() throws Exception {
189 removeFlow(mock(ItemLifecycleListener.class));
192 private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
193 RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
195 if (itemLifecycleListener != null) {
196 salFlowService.setItemLifecycleListener(itemLifecycleListener);
197 mockingFlowRegistryLookup();
201 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
202 if (itemLifecycleListener != null) {
203 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
209 public void testUpdateFlow() throws Exception {
214 public void testUpdateFlowWithItemLifecycle() throws Exception {
215 updateFlow(mock(ItemLifecycleListener.class));
218 private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
219 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
221 UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
222 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
224 FlowRef mockedFlowRef = mock(FlowRef.class);
225 Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
226 when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
228 OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
229 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
231 if (itemLifecycleListener != null) {
232 salFlowService.setItemLifecycleListener(itemLifecycleListener);
233 mockingFlowRegistryLookup();
236 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
238 if (itemLifecycleListener != null) {
239 Mockito.verify(itemLifecycleListener).onUpdated(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
244 private void mockingFlowRegistryLookup() {
245 FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
246 when(mockedFlowDescriptor.getFlowId()).thenReturn(new FlowId(DUMMY_FLOW_ID));
247 when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
249 when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
252 private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
253 assertNotNull(rpcResultFuture);
254 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
255 assertNotNull(addFlowOutputRpcResult);
256 assertTrue(addFlowOutputRpcResult.isSuccessful());
259 private <T extends org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow> T createFlowMock(Class<T> flowClazz) {
260 T mockedFlow = mock(flowClazz);
261 when(mockedFlow.getMatch()).thenReturn(match);