1 package org.opendaylight.openflowplugin.impl.services;
3 import static org.mockito.Mockito.when;
4 import com.google.common.util.concurrent.Futures;
5 import com.google.common.util.concurrent.ListenableFuture;
6 import java.math.BigInteger;
8 import org.junit.Before;
9 import org.junit.runner.RunWith;
10 import org.mockito.Matchers;
11 import org.mockito.Mock;
12 import org.mockito.runners.MockitoJUnitRunner;
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
14 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
15 import org.opendaylight.openflowplugin.api.OFConstants;
16 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
20 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
21 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
22 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
23 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
24 import org.opendaylight.openflowplugin.impl.device.DeviceContextImpl;
25 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 @RunWith(MockitoJUnitRunner.class)
39 public abstract class ServiceMocking {
40 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
41 private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
42 private static final Long DUMMY_XID_VALUE = 2121L;
43 private static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
45 protected static final String DUMMY_NODE_ID = "dummyNodeID";
46 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
47 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
51 protected RequestContextStack mockedRequestContextStack;
53 protected ConnectionContext mockedPrimConnectionContext;
55 protected FeaturesReply mockedFeatures;
57 protected GetFeaturesOutput mockedFeaturesOutput;
59 protected ConnectionAdapter mockedConnectionAdapter;
61 protected MessageSpy mockedMessagSpy;
63 protected DeviceContextImpl mockedDeviceContext;
65 protected DeviceState mockedDeviceState;
67 protected DeviceInitializationPhaseHandler mockedDevicePhaseHandler;
69 protected RequestContext mockedRequestContext;
71 protected OutboundQueue mockedOutboundQueue;
73 protected MultiMsgCollector multiMessageCollector;
76 public void initialization() {
77 when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
78 when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
80 when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
81 when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
83 when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
84 when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
86 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
87 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
88 when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
89 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
91 when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(NODE_II);
92 when(mockedDeviceState.getFeatures()).thenReturn(mockedFeaturesOutput);
94 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
95 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
96 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl());
97 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
98 when(mockedDeviceContext.getMultiMsgCollector(Matchers.<RequestContext<List<MultipartReply>>>any())).thenReturn(multiMessageCollector);
103 protected void setup() {
104 //NOOP - to be overloaded
108 protected <T> void mockSuccessfulFuture() {
109 ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
110 when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);