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 io.netty.channel.ChannelHandlerContext;
11 import io.netty.channel.ChannelOutboundHandlerAdapter;
12 import io.netty.channel.ChannelPromise;
13 import io.netty.channel.embedded.EmbeddedChannel;
15 import java.net.InetSocketAddress;
16 import java.util.concurrent.TimeUnit;
18 import org.junit.After;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterImpl;
25 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
26 import org.opendaylight.openflowjava.protocol.impl.core.connection.ResponseExpectedRpcListener;
27 import org.opendaylight.openflowjava.protocol.impl.core.connection.RpcResponseKey;
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;
49 import com.google.common.cache.Cache;
50 import com.google.common.cache.CacheBuilder;
51 import com.google.common.cache.RemovalListener;
52 import com.google.common.cache.RemovalNotification;
56 * @author michal.polkorab
58 public class ConnectionAdapterImp02lTest {
59 private static final int RPC_RESPONSE_EXPIRATION = 1;
60 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
61 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
63 public void onRemoval(
64 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
65 notification.getValue().discard();
69 @Mock EchoInput echoInput;
70 @Mock BarrierInput barrierInput;
71 @Mock EchoReplyInput echoReplyInput;
72 @Mock ExperimenterInput experimenterInput;
73 @Mock FlowModInput flowModInput;
74 @Mock GetConfigInput getConfigInput;
75 @Mock GetFeaturesInput getFeaturesInput;
76 @Mock GetQueueConfigInput getQueueConfigInput;
77 @Mock GroupModInput groupModInput;
78 @Mock HelloInput helloInput;
79 @Mock MeterModInput meterModInput;
80 @Mock PacketOutInput packetOutInput;
81 @Mock MultipartRequestInput multipartRequestInput;
82 @Mock PortModInput portModInput;
83 @Mock RoleRequestInput roleRequestInput;
84 @Mock SetConfigInput setConfigInput;
85 @Mock TableModInput tableModInput;
86 @Mock GetAsyncInput getAsyncInput;
87 @Mock SetAsyncInput setAsyncInput;
88 private ConnectionAdapterImpl adapter;
89 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
90 private OfHeader responseOfCall;
96 MockitoAnnotations.initMocks(this);
102 public void tierDown(){
103 if (adapter != null && adapter.isAlive()) {
104 adapter.disconnect();
111 public void testRcp() {
112 EmbeddedChannel embChannel = new EmbeddedChannel(new EmbededChannelHandler());
113 adapter = new ConnectionAdapterImpl(embChannel,InetSocketAddress.createUnresolved("localhost", 9876));
114 cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
115 .removalListener(REMOVAL_LISTENER).build();
116 adapter.setResponseCache(cache);
118 adapter.barrier(barrierInput);
119 embChannel.runPendingTasks();
120 Assert.assertEquals("Wrong - barrier", barrierInput, responseOfCall);
122 adapter.echo(echoInput);
123 embChannel.runPendingTasks();
124 Assert.assertEquals("Wrong - echo", echoInput, responseOfCall);
126 adapter.echoReply(echoReplyInput);
127 embChannel.runPendingTasks();
128 Assert.assertEquals("Wrong - echoReply",echoReplyInput, responseOfCall);
130 adapter.experimenter(experimenterInput);
131 embChannel.runPendingTasks();
132 Assert.assertEquals("Wrong - experimenter",experimenterInput, responseOfCall);
134 adapter.flowMod(flowModInput);
135 embChannel.runPendingTasks();
136 Assert.assertEquals("Wrong - flowMod", flowModInput, responseOfCall);
138 adapter.getConfig(getConfigInput);
139 embChannel.runPendingTasks();
140 Assert.assertEquals("Wrong - getConfig", getConfigInput, responseOfCall);
142 adapter.getFeatures(getFeaturesInput);
143 embChannel.runPendingTasks();
144 Assert.assertEquals("Wrong - getFeatures",getFeaturesInput, responseOfCall);
146 adapter.getQueueConfig(getQueueConfigInput);
147 embChannel.runPendingTasks();
148 Assert.assertEquals("Wrong - getQueueConfig",getQueueConfigInput, responseOfCall);
150 adapter.groupMod(groupModInput);
151 embChannel.runPendingTasks();
152 Assert.assertEquals("Wrong - groupMod", groupModInput, responseOfCall);
154 adapter.hello(helloInput);
155 embChannel.runPendingTasks();
156 Assert.assertEquals("Wrong - helloInput",helloInput, responseOfCall);
158 adapter.meterMod(meterModInput);
159 embChannel.runPendingTasks();
160 Assert.assertEquals("Wrong - meterMod",meterModInput, responseOfCall);
162 adapter.packetOut(packetOutInput);
163 embChannel.runPendingTasks();
164 Assert.assertEquals("Wrong - packetOut",packetOutInput, responseOfCall);
165 // -- multipartRequest
166 adapter.multipartRequest(multipartRequestInput);
167 embChannel.runPendingTasks();
168 Assert.assertEquals("Wrong - multipartRequest", multipartRequestInput, responseOfCall);
170 adapter.portMod(portModInput);
171 embChannel.runPendingTasks();
172 Assert.assertEquals("Wrong - portMod", portModInput, responseOfCall);
174 adapter.roleRequest(roleRequestInput);
175 embChannel.runPendingTasks();
176 Assert.assertEquals("Wrong - roleRequest", roleRequestInput, responseOfCall);
178 adapter.setConfig(setConfigInput);
179 embChannel.runPendingTasks();
180 Assert.assertEquals("Wrong - setConfig",setConfigInput, responseOfCall);
182 adapter.tableMod(tableModInput);
183 embChannel.runPendingTasks();
184 Assert.assertEquals("Wrong - tableMod", tableModInput, responseOfCall);
186 adapter.getAsync(getAsyncInput);
187 embChannel.runPendingTasks();
188 Assert.assertEquals("Wrong - getAsync", getAsyncInput, responseOfCall);
190 adapter.setAsync(setAsyncInput);
191 embChannel.runPendingTasks();
192 Assert.assertEquals("Wrong - setAsync", setAsyncInput, responseOfCall);
193 adapter.disconnect();
197 * Channel Handler for testing
201 private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
203 public void write(ChannelHandlerContext ctx, Object msg,
204 ChannelPromise promise) throws Exception {
205 responseOfCall = null;
206 if(msg instanceof MessageListenerWrapper){
207 MessageListenerWrapper listener = (MessageListenerWrapper) msg;
208 OfHeader ofHeader = listener.getMsg();
209 responseOfCall = ofHeader;