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.yangtools.yang.binding.DataObject;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
56 import org.opendaylight.yangtools.yang.common.RpcResult;
57 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
59 @RunWith(MockitoJUnitRunner.class)
60 public class SalFlowServiceImplTest extends TestCase {
62 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
63 private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
64 private static final String DUMMY_NODE_ID = "dummyNodeID";
65 private static final String DUMMY_FLOW_ID = "dummyFlowID";
66 private static final Short DUMMY_TABLE_ID = (short) 0;
68 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
69 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
71 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
72 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
75 private RequestContextStack mockedRequestContextStack;
77 private DeviceContext mockedDeviceContext;
79 private ConnectionContext mockedPrimConnectionContext;
81 private FeaturesReply mockedFeatures;
83 private ConnectionAdapter mockedConnectionAdapter;
85 private MessageSpy mockedMessagSpy;
87 private RequestContext<Object> requestContext;
89 private OutboundQueue outboundQueue;
92 private SalFlowServiceImpl salFlowService;
95 DeviceState mockedDeviceState;
97 private DeviceFlowRegistry deviceFlowRegistry;
100 public void initialization() {
101 when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
102 when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
104 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
105 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
106 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
108 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
110 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
111 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
112 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
114 when(requestContext.getXid()).thenReturn(new Xid(84L));
115 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
117 salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
120 when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(NODE_II);
121 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
125 public void testAddFlow() throws Exception {
130 public void testAddFlowFailCallback() throws Exception {
131 AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
132 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
133 .when(requestContext).getFuture();
135 final Future<RpcResult<AddFlowOutput>> rpcResultFuture = salFlowService.addFlow(mockedAddFlowInput);
137 assertNotNull(rpcResultFuture);
138 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
139 assertNotNull(addFlowOutputRpcResult);
140 assertFalse(addFlowOutputRpcResult.isSuccessful());
144 public void testRemoveFlowFailCallback() throws Exception {
145 RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
146 Mockito.doReturn(Futures.<RequestContext<Object>>immediateFailedFuture(new Exception("ut-failed-response")))
147 .when(requestContext).getFuture();
149 final Future<RpcResult<RemoveFlowOutput>> rpcResultFuture = salFlowService.removeFlow(mockedRemoveFlowInput);
151 assertNotNull(rpcResultFuture);
152 final RpcResult<?> removeFlowOutputRpcResult = rpcResultFuture.get();
153 assertNotNull(removeFlowOutputRpcResult);
154 assertFalse(removeFlowOutputRpcResult.isSuccessful());
158 public void testAddFlowWithItemLifecycle() throws Exception {
159 addFlow(mock(ItemLifecycleListener.class));
162 private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
163 AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
164 salFlowService.setItemLifecycleListener(itemLifecycleListener);
166 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
167 if (itemLifecycleListener != null) {
168 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
173 public void testRemoveFlow() throws Exception {
178 public void testRemoveFlowWithItemLifecycle() throws Exception {
179 removeFlow(mock(ItemLifecycleListener.class));
182 private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
183 RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
185 if (itemLifecycleListener != null) {
186 salFlowService.setItemLifecycleListener(itemLifecycleListener);
187 mockingFlowRegistryLookup();
191 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
192 if (itemLifecycleListener != null) {
193 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
199 public void testUpdateFlow() throws Exception {
204 public void testUpdateFlowWithItemLifecycle() throws Exception {
205 updateFlow(mock(ItemLifecycleListener.class));
208 private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
209 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
211 UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
212 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
214 FlowRef mockedFlowRef = mock(FlowRef.class);
215 Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
216 when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
218 OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
219 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
221 if (itemLifecycleListener != null) {
222 salFlowService.setItemLifecycleListener(itemLifecycleListener);
223 mockingFlowRegistryLookup();
226 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
228 if (itemLifecycleListener != null) {
229 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
230 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
235 private void mockingFlowRegistryLookup() {
236 FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
237 when(mockedFlowDescriptor.getFlowId()).thenReturn(new FlowId(DUMMY_FLOW_ID));
238 when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
240 when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
243 private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
244 assertNotNull(rpcResultFuture);
245 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
246 assertNotNull(addFlowOutputRpcResult);
247 assertTrue(addFlowOutputRpcResult.isSuccessful());
250 private <T extends org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow> T createFlowMock(Class<T> flowClazz) {
251 T mockedFlow = mock(flowClazz);
252 when(mockedFlow.getMatch()).thenReturn(match);