2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.services;
10 import static org.mockito.Mockito.lenient;
11 import static org.mockito.Mockito.when;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.math.BigInteger;
16 import org.junit.Before;
17 import org.junit.runner.RunWith;
18 import org.mockito.Matchers;
19 import org.mockito.Mock;
20 import org.mockito.runners.MockitoJUnitRunner;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
23 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
27 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
30 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
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.extension.api.ConverterMessageToOFJava;
34 import org.opendaylight.openflowplugin.extension.api.ConvertorData;
35 import org.opendaylight.openflowplugin.extension.api.TypeVersionKey;
36 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
37 import org.opendaylight.openflowplugin.impl.device.DeviceContextImpl;
38 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.experimenter.types.rev151020.experimenter.core.message.ExperimenterMessageOfChoice;
47 import org.opendaylight.yangtools.yang.binding.DataContainer;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
53 @RunWith(MockitoJUnitRunner.class)
54 public abstract class ServiceMocking {
55 protected static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
56 protected static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
57 protected static final Long DUMMY_XID_VALUE = 2121L;
58 protected static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
59 protected static final long DUMMY_EXPERIMENTER_ID = 42L;
61 protected static final String DUMMY_NODE_ID = "444";
62 protected static final KeyedInstanceIdentifier<Node, NodeKey> DUMMY_NODE_II = InstanceIdentifier
64 .child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
67 protected RequestContextStack mockedRequestContextStack;
69 protected ConnectionContext mockedPrimConnectionContext;
71 protected FeaturesReply mockedFeatures;
73 protected GetFeaturesOutput mockedFeaturesOutput;
75 protected ConnectionAdapter mockedConnectionAdapter;
77 protected MessageSpy mockedMessagSpy;
79 protected DeviceContextImpl mockedDeviceContext;
81 protected DeviceState mockedDeviceState;
83 protected DeviceInfo mockedDeviceInfo;
85 protected RequestContext mockedRequestContext;
87 protected OutboundQueue mockedOutboundQueue;
89 protected MultiMsgCollector multiMessageCollector;
91 protected DataBroker dataBroker;
93 protected ExtensionConverterProvider mockedExtensionConverterProvider;
95 protected ConverterMessageToOFJava<ExperimenterMessageOfChoice, DataContainer,
96 ConvertorData> mockedExtensionConverter;
99 @SuppressWarnings("unchecked")
100 public void initialization() throws Exception {
101 lenient().when(mockedExtensionConverter.getExperimenterId())
102 .thenReturn(new ExperimenterId(DUMMY_EXPERIMENTER_ID));
103 lenient().when(mockedExtensionConverterProvider.getMessageConverter(Matchers.<TypeVersionKey>any()))
104 .thenReturn(mockedExtensionConverter);
105 lenient().when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
106 lenient().when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
107 lenient().when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
108 lenient().when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
110 lenient().when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
111 lenient().when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
113 lenient().when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
114 lenient().when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
115 lenient().when(mockedPrimConnectionContext.getConnectionState())
116 .thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
117 lenient().when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
119 lenient().when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_II);
120 when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
121 when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
123 lenient().when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
124 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
125 lenient().when(mockedDeviceContext.getDeviceFlowRegistry())
126 .thenReturn(new DeviceFlowRegistryImpl(DUMMY_VERSION, dataBroker, DUMMY_NODE_II));
127 lenient().when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
128 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
129 lenient().when(mockedDeviceContext.getMultiMsgCollector(Matchers.any())).thenReturn(multiMessageCollector);
134 protected void setup() throws Exception {
135 //NOOP - to be overloaded
138 protected <T> void mockSuccessfulFuture() {
139 ListenableFuture<RpcResult<T>> dummySuccessfulFuture =
140 Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
141 when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
144 protected <T> void mockSuccessfulFuture(T result) {
145 ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success(result)
147 when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
150 protected ExperimenterMessageOfChoice mockExperimenter() {
151 return new DummyExperimenter();
154 public class DummyExperimenter implements ExperimenterMessageOfChoice {
156 public Class<? extends DataContainer> getImplementedInterface() {
157 return DummyExperimenter.class;