1 package org.opendaylight.openflowplugin.impl.services;
3 import com.google.common.util.concurrent.Futures;
4 import com.google.common.util.concurrent.ListenableFuture;
5 import io.netty.util.HashedWheelTimer;
6 import org.junit.Before;
7 import org.junit.runner.RunWith;
8 import org.mockito.Mock;
9 import org.mockito.runners.MockitoJUnitRunner;
10 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
11 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
12 import org.opendaylight.openflowplugin.api.OFConstants;
13 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
14 import org.opendaylight.openflowplugin.api.openflow.device.*;
15 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
16 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
17 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.AddMeterOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
29 import java.math.BigInteger;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
34 @RunWith(MockitoJUnitRunner.class)
35 public abstract class ServiceMocking {
36 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
37 private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
38 private static final Long DUMMY_XID_VALUE = 2121L;
39 private static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
41 private static final String DUMMY_NODE_ID = "dummyNodeID";
42 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
43 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
47 RequestContextStack mockedRequestContextStack;
49 ConnectionContext mockedPrimConnectionContext;
51 FeaturesReply mockedFeatures;
53 ConnectionAdapter mockedConnectionAdapter;
55 MessageSpy mockedMessagSpy;
57 DeviceContext mockedDeviceContext;
59 DeviceState mockedDeviceState;
61 DeviceInitializationPhaseHandler mockedDevicePhaseHandler;
63 RequestContext mockedRequestContext;
65 OutboundQueue mockedOutboundQueue;
68 public void initialization() {
69 when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
70 when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
72 when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
73 when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
75 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
76 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
77 when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
78 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
80 when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(NODE_II);
82 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
83 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
84 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl());
85 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
86 when(mockedDeviceContext.getTimer()).thenReturn(mock(HashedWheelTimer.class));
90 protected <T> void mockSuccessfulFuture() {
91 ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
92 when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);