Bug 5592 - NodeId+DatapathId in Services (remove dependency on DeviceContext)
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / ServiceMocking.java
1 package org.opendaylight.openflowplugin.impl.services;
2
3 import static org.mockito.Mockito.mock;
4 import static org.mockito.Mockito.when;
5
6 import com.google.common.util.concurrent.Futures;
7 import com.google.common.util.concurrent.ListenableFuture;
8 import io.netty.util.HashedWheelTimer;
9 import java.math.BigInteger;
10 import java.util.List;
11 import org.junit.Before;
12 import org.junit.runner.RunWith;
13 import org.mockito.Matchers;
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.DeviceState;
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.device.handlers.DeviceInitializationPhaseHandler;
25 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
26 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
27 import org.opendaylight.openflowplugin.impl.device.DeviceContextImpl;
28 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public abstract class ServiceMocking {
43     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
44     private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
45     private static final Long DUMMY_XID_VALUE = 2121L;
46     private static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
47
48     protected static final String DUMMY_NODE_ID = "dummyNodeID";
49     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
50             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
51
52
53     @Mock
54     protected RequestContextStack mockedRequestContextStack;
55     @Mock
56     protected ConnectionContext mockedPrimConnectionContext;
57     @Mock
58     protected FeaturesReply mockedFeatures;
59     @Mock
60     protected GetFeaturesOutput mockedFeaturesOutput;
61     @Mock
62     protected ConnectionAdapter mockedConnectionAdapter;
63     @Mock
64     protected MessageSpy mockedMessagSpy;
65     @Mock
66     protected DeviceContextImpl mockedDeviceContext;
67     @Mock
68     protected DeviceState mockedDeviceState;
69     @Mock
70     protected DeviceInitializationPhaseHandler mockedDevicePhaseHandler;
71     @Mock
72     protected RequestContext mockedRequestContext;
73     @Mock
74     protected OutboundQueue mockedOutboundQueue;
75     @Mock
76     protected MultiMsgCollector multiMessageCollector;
77
78     @Before
79     public void initialization() {
80         when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
81         when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
82
83         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
84         when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
85
86         when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
87         when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
88
89         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
90         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
91         when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
92         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
93
94         when(mockedDeviceState.getNodeInstanceIdentifier()).thenReturn(NODE_II);
95         when(mockedDeviceState.getFeatures()).thenReturn(mockedFeaturesOutput);
96
97         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
98         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
99         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl());
100         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
101         when(mockedDeviceContext.getMultiMsgCollector(Matchers.<RequestContext<List<MultipartReply>>>any())).thenReturn(multiMessageCollector);
102
103         setup();
104     }
105
106     protected void setup() {
107         //NOOP - to be overloaded
108     }
109
110
111     protected  <T> void mockSuccessfulFuture() {
112         ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
113         when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
114     }
115
116 }