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 junit.framework.Assert;
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterImpl;
26 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
27 import org.opendaylight.openflowjava.protocol.impl.core.connection.ResponseExpectedRpcListener;
28 import org.opendaylight.openflowjava.protocol.impl.core.connection.RpcResponseKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
50 import com.google.common.cache.Cache;
51 import com.google.common.cache.CacheBuilder;
52 import com.google.common.cache.RemovalListener;
53 import com.google.common.cache.RemovalNotification;
57 * @author michal.polkorab
59 public class ConnectionAdapterImp02lTest {
60 private static final int RPC_RESPONSE_EXPIRATION = 1;
61 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
62 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
64 public void onRemoval(
65 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
66 notification.getValue().discard();
70 @Mock EchoInput echoInput;
71 @Mock BarrierInput barrierInput;
72 @Mock EchoReplyInput echoReplyInput;
73 @Mock ExperimenterInput experimenterInput;
74 @Mock FlowModInput flowModInput;
75 @Mock GetConfigInput getConfigInput;
76 @Mock GetFeaturesInput getFeaturesInput;
77 @Mock GetQueueConfigInput getQueueConfigInput;
78 @Mock GroupModInput groupModInput;
79 @Mock HelloInput helloInput;
80 @Mock MeterModInput meterModInput;
81 @Mock PacketOutInput packetOutInput;
82 @Mock MultipartRequestInput multipartRequestInput;
83 @Mock PortModInput portModInput;
84 @Mock RoleRequestInput roleRequestInput;
85 @Mock SetConfigInput setConfigInput;
86 @Mock TableModInput tableModInput;
87 @Mock GetAsyncInput getAsyncInput;
88 @Mock SetAsyncInput setAsyncInput;
89 private ConnectionAdapterImpl adapter;
90 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
91 private OfHeader responseOfCall;
97 MockitoAnnotations.initMocks(this);
103 public void tierDown(){
104 if (adapter != null && adapter.isAlive()) {
105 adapter.disconnect();
112 public void testRcp() {
113 EmbeddedChannel embChannel = new EmbeddedChannel(new EmbededChannelHandler());
114 adapter = new ConnectionAdapterImpl(embChannel,InetSocketAddress.createUnresolved("localhost", 9876));
115 cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
116 .removalListener(REMOVAL_LISTENER).build();
117 adapter.setResponseCache(cache);
119 adapter.barrier(barrierInput);
120 embChannel.runPendingTasks();
121 Assert.assertEquals("Wrong - barrier", barrierInput, responseOfCall);
123 adapter.echo(echoInput);
124 embChannel.runPendingTasks();
125 Assert.assertEquals("Wrong - echo", echoInput, responseOfCall);
127 adapter.echoReply(echoReplyInput);
128 embChannel.runPendingTasks();
129 Assert.assertEquals("Wrong - echoReply",echoReplyInput, responseOfCall);
131 adapter.experimenter(experimenterInput);
132 embChannel.runPendingTasks();
133 Assert.assertEquals("Wrong - experimenter",experimenterInput, responseOfCall);
135 adapter.flowMod(flowModInput);
136 embChannel.runPendingTasks();
137 Assert.assertEquals("Wrong - flowMod", flowModInput, responseOfCall);
139 adapter.getConfig(getConfigInput);
140 embChannel.runPendingTasks();
141 Assert.assertEquals("Wrong - getConfig", getConfigInput, responseOfCall);
143 adapter.getFeatures(getFeaturesInput);
144 embChannel.runPendingTasks();
145 Assert.assertEquals("Wrong - getFeatures",getFeaturesInput, responseOfCall);
147 adapter.getQueueConfig(getQueueConfigInput);
148 embChannel.runPendingTasks();
149 Assert.assertEquals("Wrong - getQueueConfig",getQueueConfigInput, responseOfCall);
151 adapter.groupMod(groupModInput);
152 embChannel.runPendingTasks();
153 Assert.assertEquals("Wrong - groupMod", groupModInput, responseOfCall);
155 adapter.hello(helloInput);
156 embChannel.runPendingTasks();
157 Assert.assertEquals("Wrong - helloInput",helloInput, responseOfCall);
159 adapter.meterMod(meterModInput);
160 embChannel.runPendingTasks();
161 Assert.assertEquals("Wrong - meterMod",meterModInput, responseOfCall);
163 adapter.packetOut(packetOutInput);
164 embChannel.runPendingTasks();
165 Assert.assertEquals("Wrong - packetOut",packetOutInput, responseOfCall);
166 // -- multipartRequest
167 adapter.multipartRequest(multipartRequestInput);
168 embChannel.runPendingTasks();
169 Assert.assertEquals("Wrong - multipartRequest", multipartRequestInput, responseOfCall);
171 adapter.portMod(portModInput);
172 embChannel.runPendingTasks();
173 Assert.assertEquals("Wrong - portMod", portModInput, responseOfCall);
175 adapter.roleRequest(roleRequestInput);
176 embChannel.runPendingTasks();
177 Assert.assertEquals("Wrong - roleRequest", roleRequestInput, responseOfCall);
179 adapter.setConfig(setConfigInput);
180 embChannel.runPendingTasks();
181 Assert.assertEquals("Wrong - setConfig",setConfigInput, responseOfCall);
183 adapter.tableMod(tableModInput);
184 embChannel.runPendingTasks();
185 Assert.assertEquals("Wrong - tableMod", tableModInput, responseOfCall);
187 adapter.getAsync(getAsyncInput);
188 embChannel.runPendingTasks();
189 Assert.assertEquals("Wrong - getAsync", getAsyncInput, responseOfCall);
191 adapter.setAsync(setAsyncInput);
192 embChannel.runPendingTasks();
193 Assert.assertEquals("Wrong - setAsync", setAsyncInput, responseOfCall);
194 adapter.disconnect();
198 * Channel Handler for testing
202 private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
204 public void write(ChannelHandlerContext ctx, Object msg,
205 ChannelPromise promise) throws Exception {
206 responseOfCall = null;
207 if(msg instanceof MessageListenerWrapper){
208 MessageListenerWrapper listener = (MessageListenerWrapper) msg;
209 OfHeader ofHeader = listener.getMsg();
210 responseOfCall = ofHeader;