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 com.google.common.cache.RemovalNotification;
14 import io.netty.channel.ChannelHandlerContext;
15 import io.netty.channel.ChannelOutboundHandlerAdapter;
16 import io.netty.channel.ChannelPromise;
17 import io.netty.channel.embedded.EmbeddedChannel;
18 import java.net.InetSocketAddress;
19 import java.util.concurrent.TimeUnit;
20 import org.junit.After;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
49 * @author michal.polkorab
51 public class ConnectionAdapterImpl02Test {
52 private static final int RPC_RESPONSE_EXPIRATION = 1;
53 private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
54 new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
56 public void onRemoval(
57 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
58 notification.getValue().discard();
62 @Mock EchoInput echoInput;
63 @Mock BarrierInput barrierInput;
64 @Mock EchoReplyInput echoReplyInput;
65 @Mock ExperimenterInput experimenterInput;
66 @Mock FlowModInput flowModInput;
67 @Mock GetConfigInput getConfigInput;
68 @Mock GetFeaturesInput getFeaturesInput;
69 @Mock GetQueueConfigInput getQueueConfigInput;
70 @Mock GroupModInput groupModInput;
71 @Mock HelloInput helloInput;
72 @Mock MeterModInput meterModInput;
73 @Mock PacketOutInput packetOutInput;
74 @Mock MultipartRequestInput multipartRequestInput;
75 @Mock PortModInput portModInput;
76 @Mock RoleRequestInput roleRequestInput;
77 @Mock SetConfigInput setConfigInput;
78 @Mock TableModInput tableModInput;
79 @Mock GetAsyncInput getAsyncInput;
80 @Mock SetAsyncInput setAsyncInput;
81 private ConnectionAdapterImpl adapter;
82 private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
83 private OfHeader responseOfCall;
89 MockitoAnnotations.initMocks(this);
95 public void tierDown(){
96 if (adapter != null && adapter.isAlive()) {
104 public void testRcp() {
105 final EmbeddedChannel embChannel = new EmbeddedChannel(new EmbededChannelHandler());
106 adapter = new ConnectionAdapterImpl(embChannel, InetSocketAddress.createUnresolved("localhost", 9876), true);
107 cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
108 .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
194 private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
196 public void write(final ChannelHandlerContext ctx, final Object msg,
197 final ChannelPromise promise) throws Exception {
198 responseOfCall = null;
199 if(msg instanceof MessageListenerWrapper){
200 final MessageListenerWrapper listener = (MessageListenerWrapper) msg;
201 final OfHeader ofHeader = listener.getMsg();
202 responseOfCall = ofHeader;