2 * Copyright (c) 2014 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.core.connection;
10 import static org.mockito.Mockito.doReturn;
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.cache.RemovalListener;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.channel.ChannelOutboundHandlerAdapter;
17 import io.netty.channel.ChannelPromise;
18 import io.netty.channel.embedded.EmbeddedChannel;
19 import java.net.InetSocketAddress;
20 import java.util.concurrent.TimeUnit;
21 import org.junit.After;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
48 import org.opendaylight.yangtools.yang.common.Uint32;
51 * Unit tests for ConnectionAdapterImp02l.
54 * @author michal.polkorab
56 @RunWith(MockitoJUnitRunner.class)
57 public class ConnectionAdapterImp02lTest {
58 private static final int RPC_RESPONSE_EXPIRATION = 1;
59 private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
60 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
61 notification -> notification.getValue().discard();
63 @Mock EchoInput echoInput;
64 @Mock BarrierInput barrierInput;
65 @Mock EchoReplyInput echoReplyInput;
66 @Mock ExperimenterInput experimenterInput;
67 @Mock FlowModInput flowModInput;
68 @Mock GetConfigInput getConfigInput;
69 @Mock GetFeaturesInput getFeaturesInput;
70 @Mock GetQueueConfigInput getQueueConfigInput;
71 @Mock GroupModInput groupModInput;
72 @Mock HelloInput helloInput;
73 @Mock MeterModInput meterModInput;
74 @Mock PacketOutInput packetOutInput;
75 @Mock MultipartRequestInput multipartRequestInput;
76 @Mock PortModInput portModInput;
77 @Mock RoleRequestInput roleRequestInput;
78 @Mock SetConfigInput setConfigInput;
79 @Mock TableModInput tableModInput;
80 @Mock GetAsyncInput getAsyncInput;
81 @Mock SetAsyncInput setAsyncInput;
82 private ConnectionAdapterImpl adapter;
83 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
84 private OfHeader responseOfCall;
91 mockXid(barrierInput);
93 mockXid(echoReplyInput);
94 mockXid(getConfigInput);
95 mockXid(getFeaturesInput);
96 mockXid(getQueueConfigInput);
97 mockXid(groupModInput);
98 mockXid(roleRequestInput);
99 mockXid(setConfigInput);
100 mockXid(tableModInput);
101 mockXid(getAsyncInput);
102 mockXid(setAsyncInput);
105 private static void mockXid(final OfHeader message) {
106 doReturn(Uint32.ZERO).when(message).getXid();
110 * Disconnect adapter.
113 public void tearDown() {
114 if (adapter != null && adapter.isAlive()) {
115 adapter.disconnect();
123 public void testRcp() {
124 final EmbeddedChannel embChannel = new EmbeddedChannel(new EmbededChannelHandler());
125 adapter = new ConnectionAdapterImpl(embChannel, InetSocketAddress.createUnresolved("localhost", 9876), true,
126 CHANNEL_OUTBOUND_QUEUE_SIZE);
127 cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(
128 RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES).removalListener(REMOVAL_LISTENER).build();
129 adapter.setResponseCache(cache);
131 adapter.barrier(barrierInput);
132 embChannel.runPendingTasks();
133 Assert.assertEquals("Wrong - barrier", barrierInput, responseOfCall);
135 adapter.echo(echoInput);
136 embChannel.runPendingTasks();
137 Assert.assertEquals("Wrong - echo", echoInput, responseOfCall);
139 adapter.echoReply(echoReplyInput);
140 embChannel.runPendingTasks();
141 Assert.assertEquals("Wrong - echoReply",echoReplyInput, responseOfCall);
143 adapter.experimenter(experimenterInput);
144 embChannel.runPendingTasks();
145 Assert.assertEquals("Wrong - experimenter",experimenterInput, responseOfCall);
147 adapter.flowMod(flowModInput);
148 embChannel.runPendingTasks();
149 Assert.assertEquals("Wrong - flowMod", flowModInput, responseOfCall);
151 adapter.getConfig(getConfigInput);
152 embChannel.runPendingTasks();
153 Assert.assertEquals("Wrong - getConfig", getConfigInput, responseOfCall);
155 adapter.getFeatures(getFeaturesInput);
156 embChannel.runPendingTasks();
157 Assert.assertEquals("Wrong - getFeatures",getFeaturesInput, responseOfCall);
159 adapter.getQueueConfig(getQueueConfigInput);
160 embChannel.runPendingTasks();
161 Assert.assertEquals("Wrong - getQueueConfig",getQueueConfigInput, responseOfCall);
163 adapter.groupMod(groupModInput);
164 embChannel.runPendingTasks();
165 Assert.assertEquals("Wrong - groupMod", groupModInput, responseOfCall);
167 adapter.hello(helloInput);
168 embChannel.runPendingTasks();
169 Assert.assertEquals("Wrong - helloInput",helloInput, responseOfCall);
171 adapter.meterMod(meterModInput);
172 embChannel.runPendingTasks();
173 Assert.assertEquals("Wrong - meterMod",meterModInput, responseOfCall);
175 adapter.packetOut(packetOutInput);
176 embChannel.runPendingTasks();
177 Assert.assertEquals("Wrong - packetOut",packetOutInput, responseOfCall);
178 // -- multipartRequest
179 adapter.multipartRequest(multipartRequestInput);
180 embChannel.runPendingTasks();
181 Assert.assertEquals("Wrong - multipartRequest", multipartRequestInput, responseOfCall);
183 adapter.portMod(portModInput);
184 embChannel.runPendingTasks();
185 Assert.assertEquals("Wrong - portMod", portModInput, responseOfCall);
187 adapter.roleRequest(roleRequestInput);
188 embChannel.runPendingTasks();
189 Assert.assertEquals("Wrong - roleRequest", roleRequestInput, responseOfCall);
191 adapter.setConfig(setConfigInput);
192 embChannel.runPendingTasks();
193 Assert.assertEquals("Wrong - setConfig",setConfigInput, responseOfCall);
195 adapter.tableMod(tableModInput);
196 embChannel.runPendingTasks();
197 Assert.assertEquals("Wrong - tableMod", tableModInput, responseOfCall);
199 adapter.getAsync(getAsyncInput);
200 embChannel.runPendingTasks();
201 Assert.assertEquals("Wrong - getAsync", getAsyncInput, responseOfCall);
203 adapter.setAsync(setAsyncInput);
204 embChannel.runPendingTasks();
205 Assert.assertEquals("Wrong - setAsync", setAsyncInput, responseOfCall);
206 adapter.disconnect();
210 * Channel Handler for testing.
213 private final class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
215 public void write(final ChannelHandlerContext ctx, final Object msg,
216 final ChannelPromise promise) {
217 responseOfCall = null;
218 if (msg instanceof MessageListenerWrapper listener) {
219 responseOfCall = listener.getMsg();