3a489cb17a30097d907a65683fa2796889a48a72
[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.MultiMsgCollector;
31 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
32 import org.opendaylight.openflowplugin.extension.api.ConverterMessageToOFJava;
33 import org.opendaylight.openflowplugin.extension.api.ConvertorData;
34 import org.opendaylight.openflowplugin.extension.api.TypeVersionKey;
35 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
36 import org.opendaylight.openflowplugin.impl.device.DeviceContextImpl;
37 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.experimenter.types.rev151020.experimenter.core.message.ExperimenterMessageOfChoice;
46 import org.opendaylight.yangtools.yang.binding.DataContainer;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.common.RpcResult;
50 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public abstract class ServiceMocking {
54     protected static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
55     protected static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
56     protected static final Long DUMMY_XID_VALUE = 2121L;
57     protected static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
58     protected static final long DUMMY_EXPERIMENTER_ID = 42L;
59
60     protected static final String DUMMY_NODE_ID = "444";
61     protected static final KeyedInstanceIdentifier<Node, NodeKey> DUMMY_NODE_II = InstanceIdentifier
62             .create(Nodes.class)
63             .child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
64
65     @Mock
66     protected RequestContextStack mockedRequestContextStack;
67     @Mock
68     protected ConnectionContext mockedPrimConnectionContext;
69     @Mock
70     protected FeaturesReply mockedFeatures;
71     @Mock
72     protected GetFeaturesOutput mockedFeaturesOutput;
73     @Mock
74     protected ConnectionAdapter mockedConnectionAdapter;
75     @Mock
76     protected MessageSpy mockedMessagSpy;
77     @Mock
78     protected DeviceContextImpl mockedDeviceContext;
79     @Mock
80     protected DeviceState mockedDeviceState;
81     @Mock
82     protected DeviceInfo mockedDeviceInfo;
83     @Mock
84     protected RequestContext mockedRequestContext;
85     @Mock
86     protected OutboundQueue mockedOutboundQueue;
87     @Mock
88     protected MultiMsgCollector multiMessageCollector;
89     @Mock
90     protected DataBroker dataBroker;
91     @Mock
92     protected ExtensionConverterProvider mockedExtensionConverterProvider;
93     @Mock
94     protected ConverterMessageToOFJava<ExperimenterMessageOfChoice, DataContainer,
95         ConvertorData> mockedExtensionConverter;
96
97     @Before
98     @SuppressWarnings("unchecked")
99     public void initialization() throws Exception {
100         when(mockedExtensionConverter.getExperimenterId())
101                 .thenReturn(new ExperimenterId(DUMMY_EXPERIMENTER_ID));
102         when(mockedExtensionConverterProvider.getMessageConverter(Matchers.<TypeVersionKey>any()))
103                 .thenReturn(mockedExtensionConverter);
104         when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
105         when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
106         when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
107         when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
108
109         when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
110         when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
111
112         when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
113         when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
114         when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
115         when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
116
117         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_II);
118         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
119         when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
120
121         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
122         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
123         when(mockedDeviceContext.getDeviceFlowRegistry())
124                 .thenReturn(new DeviceFlowRegistryImpl(DUMMY_VERSION, dataBroker, DUMMY_NODE_II));
125         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
126         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
127         when(mockedDeviceContext.getMultiMsgCollector(Matchers.any())).thenReturn(multiMessageCollector);
128
129         setup();
130     }
131
132     protected void setup() throws Exception {
133         //NOOP - to be overloaded
134     }
135
136     protected  <T> void mockSuccessfulFuture() {
137         ListenableFuture<RpcResult<T>> dummySuccessfulFuture =
138                 Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
139         when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
140     }
141
142     protected  <T> void mockSuccessfulFuture(T result) {
143         ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success(result)
144                 .build());
145         when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
146     }
147
148     protected ExperimenterMessageOfChoice mockExperimenter() {
149         return new DummyExperimenter();
150     }
151
152     public class DummyExperimenter implements ExperimenterMessageOfChoice {
153         @Override
154         public Class<? extends DataContainer> getImplementedInterface() {
155             return DummyExperimenter.class;
156         }
157     }
158 }