Use ByteBuf.readRetainedSlice()
[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.ArgumentMatchers.any;
11 import static org.mockito.Mockito.lenient;
12 import static org.mockito.Mockito.when;
13
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import org.junit.Before;
17 import org.junit.runner.RunWith;
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.device.history.FlowGroupInfoHistoryAppender;
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;
52 import org.opendaylight.yangtools.yang.common.Uint32;
53 import org.opendaylight.yangtools.yang.common.Uint64;
54 import org.opendaylight.yangtools.yang.common.Uint8;
55
56 @RunWith(MockitoJUnitRunner.class)
57 public abstract class ServiceMocking {
58     protected static final Uint64 DUMMY_DATAPATH_ID = Uint64.valueOf(444);
59     protected static final Uint8 DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
60     protected static final Uint32 DUMMY_XID_VALUE = Uint32.valueOf(2121L);
61     protected static final Xid DUMMY_XID = new Xid(DUMMY_XID_VALUE);
62     protected static final Uint32 DUMMY_EXPERIMENTER_ID = Uint32.valueOf(42);
63
64     protected static final String DUMMY_NODE_ID = "444";
65     protected static final KeyedInstanceIdentifier<Node, NodeKey> DUMMY_NODE_II = InstanceIdentifier
66             .create(Nodes.class)
67             .child(Node.class, new NodeKey(new NodeId(DUMMY_NODE_ID)));
68
69     @Mock
70     protected RequestContextStack mockedRequestContextStack;
71     @Mock
72     protected ConnectionContext mockedPrimConnectionContext;
73     @Mock
74     protected FeaturesReply mockedFeatures;
75     @Mock
76     protected GetFeaturesOutput mockedFeaturesOutput;
77     @Mock
78     protected ConnectionAdapter mockedConnectionAdapter;
79     @Mock
80     protected MessageSpy mockedMessagSpy;
81     @Mock
82     protected DeviceContextImpl mockedDeviceContext;
83     @Mock
84     protected DeviceState mockedDeviceState;
85     @Mock
86     protected DeviceInfo mockedDeviceInfo;
87     @Mock
88     protected RequestContext mockedRequestContext;
89     @Mock
90     protected OutboundQueue mockedOutboundQueue;
91     @Mock
92     protected MultiMsgCollector multiMessageCollector;
93     @Mock
94     protected DataBroker dataBroker;
95     @Mock
96     protected ExtensionConverterProvider mockedExtensionConverterProvider;
97     @Mock
98     protected ConverterMessageToOFJava<ExperimenterMessageOfChoice, DataContainer,
99         ConvertorData> mockedExtensionConverter;
100     @Mock
101     protected FlowGroupInfoHistoryAppender historyAppender;
102
103     @Before
104     @SuppressWarnings("unchecked")
105     public void initialization() {
106         lenient().when(mockedExtensionConverter.getExperimenterId())
107                 .thenReturn(new ExperimenterId(DUMMY_EXPERIMENTER_ID));
108         lenient().when(mockedExtensionConverterProvider.getMessageConverter(any(TypeVersionKey.class)))
109                 .thenReturn(mockedExtensionConverter);
110         lenient().when(mockedRequestContextStack.createRequestContext()).thenReturn(mockedRequestContext);
111         lenient().when(mockedRequestContext.getXid()).thenReturn(DUMMY_XID);
112         lenient().when(mockedFeatures.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
113         lenient().when(mockedFeatures.getVersion()).thenReturn(DUMMY_VERSION);
114
115         lenient().when(mockedFeaturesOutput.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
116         lenient().when(mockedFeaturesOutput.getVersion()).thenReturn(DUMMY_VERSION);
117
118         lenient().when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
119         lenient().when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
120         lenient().when(mockedPrimConnectionContext.getConnectionState())
121                 .thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
122         lenient().when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(mockedOutboundQueue);
123
124         lenient().when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_II);
125         when(mockedDeviceInfo.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
126         when(mockedDeviceInfo.getVersion()).thenReturn(DUMMY_VERSION);
127
128         lenient().when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
129         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
130         lenient().when(mockedDeviceContext.getDeviceFlowRegistry())
131                 .thenReturn(new DeviceFlowRegistryImpl(DUMMY_VERSION, dataBroker, DUMMY_NODE_II, historyAppender));
132         lenient().when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
133         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
134         lenient().when(mockedDeviceContext.getMultiMsgCollector(any())).thenReturn(multiMessageCollector);
135
136         setup();
137     }
138
139     protected void setup() {
140         //NOOP - to be overloaded
141     }
142
143     protected  <T> void mockSuccessfulFuture() {
144         ListenableFuture<RpcResult<T>> dummySuccessfulFuture =
145                 Futures.immediateFuture(RpcResultBuilder.success((T) null).build());
146         when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
147     }
148
149     protected  <T> void mockSuccessfulFuture(final T result) {
150         ListenableFuture<RpcResult<T>> dummySuccessfulFuture = Futures.immediateFuture(RpcResultBuilder.success(result)
151                 .build());
152         when(mockedRequestContext.getFuture()).thenReturn(dummySuccessfulFuture);
153     }
154
155     protected ExperimenterMessageOfChoice mockExperimenter() {
156         return new DummyExperimenter();
157     }
158
159     public class DummyExperimenter implements ExperimenterMessageOfChoice {
160         @Override
161         public Class<DummyExperimenter> implementedInterface() {
162             return DummyExperimenter.class;
163         }
164     }
165 }