1 package org.opendaylight.openflowplugin.impl.services;
4 import static org.mockito.Mockito.mock;
5 import static org.mockito.Mockito.when;
7 import java.math.BigInteger;
8 import java.util.concurrent.ExecutionException;
9 import java.util.concurrent.Future;
10 import junit.framework.TestCase;
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.junit.runner.RunWith;
14 import org.mockito.Matchers;
15 import org.mockito.Mock;
16 import org.mockito.Mockito;
17 import org.mockito.runners.MockitoJUnitRunner;
18 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
19 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
20 import org.opendaylight.openflowplugin.api.OFConstants;
21 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
24 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
26 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
27 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
28 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
29 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
30 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
31 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowRef;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
56 @RunWith(MockitoJUnitRunner.class)
57 public class SalFlowServiceImplTest extends TestCase {
59 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
60 private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
61 private static final String DUMMY_NODE_ID = "dummyNodeID";
62 private static final String DUMMY_FLOW_ID = "dummyFlowID";
63 private static final Short DUMMY_TABLE_ID = (short) 0;
65 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
66 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
68 private static final KeyedInstanceIdentifier<Table, TableKey> TABLE_II
69 = NODE_II.augmentation(FlowCapableNode.class).child(Table.class, new TableKey(DUMMY_TABLE_ID));
72 private RequestContextStack mockedRequestContextStack;
74 private DeviceContext mockedDeviceContext;
76 private ConnectionContext mockedPrimConnectionContext;
78 private FeaturesReply mockedFeatures;
80 private ConnectionAdapter mockedConnectionAdapter;
82 private MessageSpy mockedMessagSpy;
84 private RequestContext<Object> requestContext;
86 private OutboundQueue outboundQueue;
89 private SalFlowServiceImpl salFlowService;
92 DeviceState mockedDeviceState;
94 private DeviceFlowRegistry deviceFlowRegistry;
97 public void initialization() {
98 when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
99 when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
101 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
102 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
103 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
105 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
107 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
108 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(deviceFlowRegistry);
109 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
111 when(requestContext.getXid()).thenReturn(new Xid(84L));
112 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
114 salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
117 when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(NODE_II);
118 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
122 public void testAddFlow() throws Exception {
127 public void testAddFlowWithItemLifecycle() throws Exception {
128 addFlow(mock(ItemLifecycleListener.class));
131 private void addFlow(final ItemLifecycleListener itemLifecycleListener) throws ExecutionException, InterruptedException {
132 AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
133 salFlowService.setItemLifecycleListener(itemLifecycleListener);
135 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
136 if (itemLifecycleListener != null) {
137 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
142 public void testRemoveFlow() throws Exception {
147 public void testRemoveFlowWithItemLifecycle() throws Exception {
148 removeFlow(mock(ItemLifecycleListener.class));
151 private void removeFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
152 RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
154 if (itemLifecycleListener != null) {
155 salFlowService.setItemLifecycleListener(itemLifecycleListener);
156 mockingFlowRegistryLookup();
160 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
161 if (itemLifecycleListener != null) {
162 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
168 public void testUpdateFlow() throws Exception {
173 public void testUpdateFlowWithItemLifecycle() throws Exception {
174 updateFlow(mock(ItemLifecycleListener.class));
177 private void updateFlow(final ItemLifecycleListener itemLifecycleListener) throws Exception {
178 UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
180 UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
181 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
183 FlowRef mockedFlowRef = mock(FlowRef.class);
184 Mockito.doReturn(TABLE_II.child(Flow.class, new FlowKey(new FlowId(DUMMY_FLOW_ID)))).when(mockedFlowRef).getValue();
185 when(mockedUpdateFlowInput.getFlowRef()).thenReturn(mockedFlowRef);
187 OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
188 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
190 if (itemLifecycleListener != null) {
191 salFlowService.setItemLifecycleListener(itemLifecycleListener);
192 mockingFlowRegistryLookup();
195 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
197 if (itemLifecycleListener != null) {
198 Mockito.verify(itemLifecycleListener).onAdded(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any(), Matchers.<Flow>any());
199 Mockito.verify(itemLifecycleListener).onRemoved(Matchers.<KeyedInstanceIdentifier<Flow, FlowKey>>any());
204 private void mockingFlowRegistryLookup() {
205 FlowDescriptor mockedFlowDescriptor = mock(FlowDescriptor.class);
206 when(mockedFlowDescriptor.getFlowId()).thenReturn(new FlowId(DUMMY_FLOW_ID));
207 when(mockedFlowDescriptor.getTableKey()).thenReturn(new TableKey(DUMMY_TABLE_ID));
209 when(deviceFlowRegistry.retrieveIdForFlow(Matchers.any(FlowRegistryKey.class))).thenReturn(mockedFlowDescriptor);
212 private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
213 assertNotNull(rpcResultFuture);
214 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
215 assertNotNull(addFlowOutputRpcResult);
216 assertTrue(addFlowOutputRpcResult.isSuccessful());
219 private <T extends org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow> T createFlowMock(Class<T> flowClazz) {
220 T mockedFlow = mock(flowClazz);
221 when(mockedFlow.getMatch()).thenReturn(match);