Add missing license headers
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / ServiceMocking.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.services;
9
10 import static org.mockito.Mockito.when;
11
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.math.BigInteger;
15 import org.junit.Before;
16 import org.junit.runner.RunWith;
17 import org.mockito.Matchers;
18 import org.mockito.Mock;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
22 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
23 import org.opendaylight.openflowplugin.api.OFConstants;
24 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
27 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
29 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
30 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
31 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
32 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
33 import org.opendaylight.openflowplugin.impl.device.DeviceContextImpl;
34 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public abstract class ServiceMocking {
48     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
49     private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
50     private static final Long DUMMY_XID_VALUE = 2121L;
51     private static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
52
53     protected static final String DUMMY_NODE_ID = "dummyNodeID";
54     private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
55             = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
56
57     @Mock
58     protected RequestContextStack mockedRequestContextStack;
59     @Mock
60     protected ConnectionContext mockedPrimConnectionContext;
61     @Mock
62     protected FeaturesReply mockedFeatures;
63     @Mock
64     protected GetFeaturesOutput mockedFeaturesOutput;
65     @Mock
66     protected ConnectionAdapter mockedConnectionAdapter;
67     @Mock
68     protected MessageSpy mockedMessagSpy;
69     @Mock
70     protected DeviceContextImpl mockedDeviceContext;
71     @Mock
72     protected DeviceState mockedDeviceState;
73     @Mock
74     protected DeviceInfo mockedDeviceInfo;
75     @Mock
76     protected DeviceInitializationPhaseHandler mockedDevicePhaseHandler;
77     @Mock
78     protected RequestContext mockedRequestContext;
79     @Mock
80     protected OutboundQueue mockedOutboundQueue;
81     @Mock
82     protected MultiMsgCollector multiMessageCollector;
83     @Mock
84     protected DataBroker dataBroker;
85
86     @Before
87     public void initialization() {
88         when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
89         when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
90
91         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
92         when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
93
94         when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
95         when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
96
97         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
98         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
99         when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
100         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
101
102         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
103         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
104         when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
105
106         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
107         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
108         when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, NODE_II));
109         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
110         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
111         when(mockedDeviceContext.getMultiMsgCollector(Matchers.any())).thenReturn(multiMessageCollector);
112
113         setup();
114     }
115
116     protected void setup() {
117         //NOOP - to be overloaded
118     }
119
120
121     protected  <T> void mockSuccessfulFuture() {
122         ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
123         when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
124     }
125
126 }