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.Mock;
15 import org.mockito.runners.MockitoJUnitRunner;
16 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
17 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
18 import org.opendaylight.openflowplugin.api.OFConstants;
19 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
23 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
24 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
25 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 @RunWith(MockitoJUnitRunner.class)
39 public class SalFlowServiceImplTest extends TestCase {
41 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
42 private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
45 private RequestContextStack mockedRequestContextStack;
47 private DeviceContext mockedDeviceContext;
49 private ConnectionContext mockedPrimConnectionContext;
51 private FeaturesReply mockedFeatures;
53 private ConnectionAdapter mockedConnectionAdapter;
55 private MessageSpy mockedMessagSpy;
57 private RequestContext<Object> requestContext;
59 private OutboundQueue outboundQueue;
62 private SalFlowServiceImpl salFlowService;
66 public void initialization() {
67 when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
68 when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
70 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
71 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
72 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
74 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
76 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
77 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl());
78 when(mockedRequestContextStack.createRequestContext()).thenReturn(requestContext);
80 when(requestContext.getXid()).thenReturn(new Xid(84L));
81 when(requestContext.getFuture()).thenReturn(RpcResultBuilder.success().buildFuture());
83 salFlowService = new SalFlowServiceImpl(mockedRequestContextStack, mockedDeviceContext);
87 public void testAddFlow() throws Exception {
88 final AddFlowInput mockedAddFlowInput = createFlowMock(AddFlowInput.class);
90 verifyOutput(salFlowService.addFlow(mockedAddFlowInput));
94 public void testRemoveFlow() throws Exception {
95 final RemoveFlowInput mockedRemoveFlowInput = createFlowMock(RemoveFlowInput.class);
97 verifyOutput(salFlowService.removeFlow(mockedRemoveFlowInput));
101 public void testUpdateFlow() throws Exception {
102 final UpdateFlowInput mockedUpdateFlowInput = mock(UpdateFlowInput.class);
104 final UpdatedFlow mockedUpdateFlow = createFlowMock(UpdatedFlow.class);
105 when(mockedUpdateFlowInput.getUpdatedFlow()).thenReturn(mockedUpdateFlow);
107 final OriginalFlow mockedOriginalFlow = createFlowMock(OriginalFlow.class);
108 when(mockedUpdateFlowInput.getOriginalFlow()).thenReturn(mockedOriginalFlow);
110 verifyOutput(salFlowService.updateFlow(mockedUpdateFlowInput));
113 private <T extends DataObject> void verifyOutput(Future<RpcResult<T>> rpcResultFuture) throws ExecutionException, InterruptedException {
114 assertNotNull(rpcResultFuture);
115 final RpcResult<?> addFlowOutputRpcResult = rpcResultFuture.get();
116 assertNotNull(addFlowOutputRpcResult);
117 assertTrue(addFlowOutputRpcResult.isSuccessful());
120 private <T extends Flow> T createFlowMock(Class<T> flowClazz) {
121 T mockedFlow = mock(flowClazz);
122 when(mockedFlow.getMatch()).thenReturn(match);