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.impl.device.DeviceContextImpl;
33 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 @RunWith(MockitoJUnitRunner.class)
46 public abstract class ServiceMocking {
47 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
48 private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
49 private static final Long DUMMY_XID_VALUE = 2121L;
50 private static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
52 protected static final String DUMMY_NODE_ID = "dummyNodeID";
53 private static final KeyedInstanceIdentifier<Node, NodeKey> NODE_II
54 = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
57 protected RequestContextStack mockedRequestContextStack;
59 protected ConnectionContext mockedPrimConnectionContext;
61 protected FeaturesReply mockedFeatures;
63 protected GetFeaturesOutput mockedFeaturesOutput;
65 protected ConnectionAdapter mockedConnectionAdapter;
67 protected MessageSpy mockedMessagSpy;
69 protected DeviceContextImpl mockedDeviceContext;
71 protected DeviceState mockedDeviceState;
73 protected DeviceInfo mockedDeviceInfo;
75 protected RequestContext mockedRequestContext;
77 protected OutboundQueue mockedOutboundQueue;
79 protected MultiMsgCollector multiMessageCollector;
81 protected DataBroker dataBroker;
84 public void initialization() {
85 when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
86 when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
87 when(mockedRequestContext.getFuture()).thenReturn(Futures.immediateFuture(null));
89 when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
90 when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
92 when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
93 when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
95 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
96 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
97 when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
98 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
100 when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(NODE_II);
101 when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
102 when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
104 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
105 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
106 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(DUMMY_VERSION, dataBroker, NODE_II));
107 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
108 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
109 when(mockedDeviceContext.getMultiMsgCollector(Matchers.any())).thenReturn(multiMessageCollector);
114 protected void setup() {
115 //NOOP - to be overloaded
119 protected <T> void mockSuccessfulFuture() {
120 ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
121 when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);