OPNFLWPLUG-968: The channelOutboundQueueSize is made configurable:
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / ConnectionAdapterImp02lTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
9
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;
45
46 /**
47  * Unit tests for ConnectionAdapterImp02l.
48  *
49  * @author madamjak
50  * @author michal.polkorab
51  */
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();
57
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;
80
81     /**
82      * Initialize mocks.
83      */
84     @Before
85     public void setUp() {
86         MockitoAnnotations.initMocks(this);
87     }
88
89     /**
90      * Disconnect adapter.
91      */
92     @After
93     public void tierDown() {
94         if (adapter != null && adapter.isAlive()) {
95             adapter.disconnect();
96         }
97     }
98
99     /**
100      * Test Rpc Calls.
101      */
102     @Test
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);
110         // -- barrier
111         adapter.barrier(barrierInput);
112         embChannel.runPendingTasks();
113         Assert.assertEquals("Wrong - barrier", barrierInput, responseOfCall);
114         // -- echo
115         adapter.echo(echoInput);
116         embChannel.runPendingTasks();
117         Assert.assertEquals("Wrong - echo", echoInput, responseOfCall);
118         // -- echoReply
119         adapter.echoReply(echoReplyInput);
120         embChannel.runPendingTasks();
121         Assert.assertEquals("Wrong - echoReply",echoReplyInput, responseOfCall);
122         // -- experimenter
123         adapter.experimenter(experimenterInput);
124         embChannel.runPendingTasks();
125         Assert.assertEquals("Wrong - experimenter",experimenterInput, responseOfCall);
126         // -- flowMod
127         adapter.flowMod(flowModInput);
128         embChannel.runPendingTasks();
129         Assert.assertEquals("Wrong - flowMod", flowModInput, responseOfCall);
130         // -- getConfig
131         adapter.getConfig(getConfigInput);
132         embChannel.runPendingTasks();
133         Assert.assertEquals("Wrong - getConfig", getConfigInput, responseOfCall);
134         // -- getFeatures
135         adapter.getFeatures(getFeaturesInput);
136         embChannel.runPendingTasks();
137         Assert.assertEquals("Wrong - getFeatures",getFeaturesInput, responseOfCall);
138         // -- getQueueConfig
139         adapter.getQueueConfig(getQueueConfigInput);
140         embChannel.runPendingTasks();
141         Assert.assertEquals("Wrong - getQueueConfig",getQueueConfigInput, responseOfCall);
142         // -- groupMod
143         adapter.groupMod(groupModInput);
144         embChannel.runPendingTasks();
145         Assert.assertEquals("Wrong - groupMod", groupModInput, responseOfCall);
146         // -- hello
147         adapter.hello(helloInput);
148         embChannel.runPendingTasks();
149         Assert.assertEquals("Wrong - helloInput",helloInput, responseOfCall);
150         // -- meterMod
151         adapter.meterMod(meterModInput);
152         embChannel.runPendingTasks();
153         Assert.assertEquals("Wrong - meterMod",meterModInput, responseOfCall);
154         // -- packetOut
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);
162         // -- portMod
163         adapter.portMod(portModInput);
164         embChannel.runPendingTasks();
165         Assert.assertEquals("Wrong - portMod", portModInput, responseOfCall);
166         // -- roleRequest
167         adapter.roleRequest(roleRequestInput);
168         embChannel.runPendingTasks();
169         Assert.assertEquals("Wrong - roleRequest", roleRequestInput, responseOfCall);
170         // -- setConfig
171         adapter.setConfig(setConfigInput);
172         embChannel.runPendingTasks();
173         Assert.assertEquals("Wrong - setConfig",setConfigInput, responseOfCall);
174         // -- tableMod
175         adapter.tableMod(tableModInput);
176         embChannel.runPendingTasks();
177         Assert.assertEquals("Wrong - tableMod", tableModInput, responseOfCall);
178         // -- getAsync
179         adapter.getAsync(getAsyncInput);
180         embChannel.runPendingTasks();
181         Assert.assertEquals("Wrong - getAsync", getAsyncInput, responseOfCall);
182         // -- setAsync
183         adapter.setAsync(setAsyncInput);
184         embChannel.runPendingTasks();
185         Assert.assertEquals("Wrong - setAsync", setAsyncInput, responseOfCall);
186         adapter.disconnect();
187     }
188
189     /**
190      * Channel Handler for testing.
191      * @author madamjak
192      */
193     private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
194         @Override
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;
202             }
203         }
204     }
205 }