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 org.junit.Before;
16 import org.junit.runner.RunWith;
17 import org.mockito.Matchers;
18 import org.mockito.Mock;
19 import org.mockito.junit.MockitoJUnitRunner;
20 import org.opendaylight.mdsal.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 import org.opendaylight.yangtools.yang.common.Uint32;
52 import org.opendaylight.yangtools.yang.common.Uint64;
53 import org.opendaylight.yangtools.yang.common.Uint8;
55 @RunWith(MockitoJUnitRunner.class)
56 public abstract class ServiceMocking {
57 protected static final Uint64 DUMMY_DATAPATH_ID = Uint64.valueOf(444);
58 protected static final Uint8 DUMMY_VERSION = Uint8.valueOf(OFConstants.OFP_VERSION_1_3);
59 protected static final Uint32 DUMMY_XID_VALUE = Uint32.valueOf(2121L);
60 protected static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
61 protected static final long DUMMY_EXPERIMENTER_ID = 42L;
63 protected static final String DUMMY_NODE_ID = "444";
64 protected static final KeyedInstanceIdentifier<Node, NodeKey> DUMMY_NODE_II = InstanceIdentifier
66 .child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
69 protected RequestContextStack mockedRequestContextStack;
71 protected ConnectionContext mockedPrimConnectionContext;
73 protected FeaturesReply mockedFeatures;
75 protected GetFeaturesOutput mockedFeaturesOutput;
77 protected ConnectionAdapter mockedConnectionAdapter;
79 protected MessageSpy mockedMessagSpy;
81 protected DeviceContextImpl mockedDeviceContext;
83 protected DeviceState mockedDeviceState;
85 protected DeviceInfo mockedDeviceInfo;
87 protected RequestContext mockedRequestContext;
89 protected OutboundQueue mockedOutboundQueue;
91 protected MultiMsgCollector multiMessageCollector;
93 protected DataBroker dataBroker;
95 protected ExtensionConverterProvider mockedExtensionConverterProvider;
97 protected ConverterMessageToOFJava<ExperimenterMessageOfChoice, DataContainer,
98 ConvertorData> mockedExtensionConverter;
101 @SuppressWarnings("unchecked")
102 public void initialization() {
103 lenient().when(mockedExtensionConverter.getExperimenterId())
104 .thenReturn(new ExperimenterId(DUMMY_EXPERIMENTER_ID));
105 lenient().when(mockedExtensionConverterProvider.getMessageConverter(Matchers.<TypeVersionKey>any()))
106 .thenReturn(mockedExtensionConverter);
107 lenient().when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
108 lenient().when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
109 lenient().when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
110 lenient().when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
112 lenient().when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
113 lenient().when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
115 lenient().when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
116 lenient().when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
117 lenient().when(mockedPrimConnectionContext.getConnectionState())
118 .thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
119 lenient().when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
121 lenient().when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_II);
122 when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
123 when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION.toJava());
125 lenient().when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
126 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
127 lenient().when(mockedDeviceContext.getDeviceFlowRegistry())
128 .thenReturn(new DeviceFlowRegistryImpl(DUMMY_VERSION.toJava(), dataBroker, DUMMY_NODE_II));
129 lenient().when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
130 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
131 lenient().when(mockedDeviceContext.getMultiMsgCollector(Matchers.any())).thenReturn(multiMessageCollector);
136 protected void setup() {
137 //NOOP - to be overloaded
140 protected <T> void mockSuccessfulFuture() {
141 ListenableFuture<RpcResult<T>> dummySuccessfulFuture =
142 Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
143 when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
146 protected <T> void mockSuccessfulFuture(final T result) {
147 ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success(result)
149 when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
152 protected ExperimenterMessageOfChoice mockExperimenter() {
153 return new DummyExperimenter();
156 public class DummyExperimenter implements ExperimenterMessageOfChoice {
158 public Class<DummyExperimenter> implementedInterface() {
159 return DummyExperimenter.class;