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