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 com.google.common.cache.Cache;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.RemovalListener;
13 import io.netty.channel.ChannelHandlerContext;
14 import io.netty.channel.ChannelOutboundHandlerAdapter;
15 import io.netty.channel.ChannelPromise;
16 import io.netty.channel.embedded.EmbeddedChannel;
17 import java.net.InetSocketAddress;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
47 * Unit tests for ConnectionAdapterImp02l.
50 * @author michal.polkorab
52 public class ConnectionAdapterImp02lTest {
53 private static final int RPC_RESPONSE_EXPIRATION = 1;
54 private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
55 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
56 notification -> notification.getValue().discard();
58 @Mock EchoInput echoInput;
59 @Mock BarrierInput barrierInput;
60 @Mock EchoReplyInput echoReplyInput;
61 @Mock ExperimenterInput experimenterInput;
62 @Mock FlowModInput flowModInput;
63 @Mock GetConfigInput getConfigInput;
64 @Mock GetFeaturesInput getFeaturesInput;
65 @Mock GetQueueConfigInput getQueueConfigInput;
66 @Mock GroupModInput groupModInput;
67 @Mock HelloInput helloInput;
68 @Mock MeterModInput meterModInput;
69 @Mock PacketOutInput packetOutInput;
70 @Mock MultipartRequestInput multipartRequestInput;
71 @Mock PortModInput portModInput;
72 @Mock RoleRequestInput roleRequestInput;
73 @Mock SetConfigInput setConfigInput;
74 @Mock TableModInput tableModInput;
75 @Mock GetAsyncInput getAsyncInput;
76 @Mock SetAsyncInput setAsyncInput;
77 private ConnectionAdapterImpl adapter;
78 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
79 private OfHeader responseOfCall;
86 MockitoAnnotations.initMocks(this);
93 public void tierDown() {
94 if (adapter != null && adapter.isAlive()) {
103 public void testRcp() {
104 final EmbeddedChannel embChannel = new EmbeddedChannel(new EmbededChannelHandler());
105 adapter = new ConnectionAdapterImpl(embChannel, InetSocketAddress.createUnresolved("localhost", 9876), true,
106 CHANNEL_OUTBOUND_QUEUE_SIZE);
107 cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(
108 RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES).removalListener(REMOVAL_LISTENER).build();
109 adapter.setResponseCache(cache);
111 adapter.barrier(barrierInput);
112 embChannel.runPendingTasks();
113 Assert.assertEquals("Wrong - barrier", barrierInput, responseOfCall);
115 adapter.echo(echoInput);
116 embChannel.runPendingTasks();
117 Assert.assertEquals("Wrong - echo", echoInput, responseOfCall);
119 adapter.echoReply(echoReplyInput);
120 embChannel.runPendingTasks();
121 Assert.assertEquals("Wrong - echoReply",echoReplyInput, responseOfCall);
123 adapter.experimenter(experimenterInput);
124 embChannel.runPendingTasks();
125 Assert.assertEquals("Wrong - experimenter",experimenterInput, responseOfCall);
127 adapter.flowMod(flowModInput);
128 embChannel.runPendingTasks();
129 Assert.assertEquals("Wrong - flowMod", flowModInput, responseOfCall);
131 adapter.getConfig(getConfigInput);
132 embChannel.runPendingTasks();
133 Assert.assertEquals("Wrong - getConfig", getConfigInput, responseOfCall);
135 adapter.getFeatures(getFeaturesInput);
136 embChannel.runPendingTasks();
137 Assert.assertEquals("Wrong - getFeatures",getFeaturesInput, responseOfCall);
139 adapter.getQueueConfig(getQueueConfigInput);
140 embChannel.runPendingTasks();
141 Assert.assertEquals("Wrong - getQueueConfig",getQueueConfigInput, responseOfCall);
143 adapter.groupMod(groupModInput);
144 embChannel.runPendingTasks();
145 Assert.assertEquals("Wrong - groupMod", groupModInput, responseOfCall);
147 adapter.hello(helloInput);
148 embChannel.runPendingTasks();
149 Assert.assertEquals("Wrong - helloInput",helloInput, responseOfCall);
151 adapter.meterMod(meterModInput);
152 embChannel.runPendingTasks();
153 Assert.assertEquals("Wrong - meterMod",meterModInput, responseOfCall);
155 adapter.packetOut(packetOutInput);
156 embChannel.runPendingTasks();
157 Assert.assertEquals("Wrong - packetOut",packetOutInput, responseOfCall);
158 // -- multipartRequest
159 adapter.multipartRequest(multipartRequestInput);
160 embChannel.runPendingTasks();
161 Assert.assertEquals("Wrong - multipartRequest", multipartRequestInput, responseOfCall);
163 adapter.portMod(portModInput);
164 embChannel.runPendingTasks();
165 Assert.assertEquals("Wrong - portMod", portModInput, responseOfCall);
167 adapter.roleRequest(roleRequestInput);
168 embChannel.runPendingTasks();
169 Assert.assertEquals("Wrong - roleRequest", roleRequestInput, responseOfCall);
171 adapter.setConfig(setConfigInput);
172 embChannel.runPendingTasks();
173 Assert.assertEquals("Wrong - setConfig",setConfigInput, responseOfCall);
175 adapter.tableMod(tableModInput);
176 embChannel.runPendingTasks();
177 Assert.assertEquals("Wrong - tableMod", tableModInput, responseOfCall);
179 adapter.getAsync(getAsyncInput);
180 embChannel.runPendingTasks();
181 Assert.assertEquals("Wrong - getAsync", getAsyncInput, responseOfCall);
183 adapter.setAsync(setAsyncInput);
184 embChannel.runPendingTasks();
185 Assert.assertEquals("Wrong - setAsync", setAsyncInput, responseOfCall);
186 adapter.disconnect();
190 * Channel Handler for testing.
193 private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
195 public void write(final ChannelHandlerContext ctx, final Object msg,
196 final ChannelPromise promise) throws Exception {
197 responseOfCall = null;
198 if (msg instanceof MessageListenerWrapper) {
199 final MessageListenerWrapper listener = (MessageListenerWrapper) msg;
200 final OfHeader ofHeader = listener.getMsg();
201 responseOfCall = ofHeader;