Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / ConnectionAdapterImpl02Test.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 static org.mockito.Mockito.doReturn;
11
12 import com.google.common.cache.Cache;
13 import com.google.common.cache.CacheBuilder;
14 import com.google.common.cache.RemovalListener;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.channel.ChannelOutboundHandlerAdapter;
17 import io.netty.channel.ChannelPromise;
18 import io.netty.channel.embedded.EmbeddedChannel;
19 import java.net.InetSocketAddress;
20 import java.util.concurrent.TimeUnit;
21 import org.junit.After;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
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;
48 import org.opendaylight.yangtools.yang.common.Uint32;
49
50 /**
51  * Unit tests for ConnectionAdapterImpl02.
52  *
53  * @author madamjak
54  * @author michal.polkorab
55  */
56 @RunWith(MockitoJUnitRunner.class)
57 public class ConnectionAdapterImpl02Test {
58     private static final int RPC_RESPONSE_EXPIRATION = 1;
59     private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
60     private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
61         notification -> notification.getValue().discard();
62
63     @Mock EchoInput echoInput;
64     @Mock BarrierInput barrierInput;
65     @Mock EchoReplyInput echoReplyInput;
66     @Mock ExperimenterInput experimenterInput;
67     @Mock FlowModInput flowModInput;
68     @Mock GetConfigInput getConfigInput;
69     @Mock GetFeaturesInput getFeaturesInput;
70     @Mock GetQueueConfigInput getQueueConfigInput;
71     @Mock GroupModInput groupModInput;
72     @Mock HelloInput helloInput;
73     @Mock MeterModInput meterModInput;
74     @Mock PacketOutInput packetOutInput;
75     @Mock MultipartRequestInput multipartRequestInput;
76     @Mock PortModInput portModInput;
77     @Mock RoleRequestInput roleRequestInput;
78     @Mock SetConfigInput setConfigInput;
79     @Mock TableModInput tableModInput;
80     @Mock GetAsyncInput getAsyncInput;
81     @Mock SetAsyncInput setAsyncInput;
82     private ConnectionAdapterImpl adapter;
83     private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
84     private OfHeader responseOfCall;
85
86     /**
87      * Initialize mocks.
88      */
89     @Before
90     public void setUp() {
91         mockXid(barrierInput);
92         mockXid(echoInput);
93         mockXid(echoReplyInput);
94         mockXid(getConfigInput);
95         mockXid(getFeaturesInput);
96         mockXid(getQueueConfigInput);
97         mockXid(groupModInput);
98         mockXid(roleRequestInput);
99         mockXid(setConfigInput);
100         mockXid(tableModInput);
101         mockXid(getAsyncInput);
102         mockXid(setAsyncInput);
103     }
104
105     private static void mockXid(final OfHeader message) {
106         doReturn(Uint32.ZERO).when(message).getXid();
107     }
108
109     /**
110      * Disconnect adapter.
111      */
112     @After
113     public void tearDown() {
114         if (adapter != null && adapter.isAlive()) {
115             adapter.disconnect();
116         }
117     }
118
119     /**
120      * Test Rpc Calls.
121      */
122     @Test
123     public void testRcp() {
124         final EmbeddedChannel embChannel = new EmbeddedChannel(new EmbededChannelHandler());
125         adapter = new ConnectionAdapterImpl(embChannel, InetSocketAddress.createUnresolved("localhost", 9876), true,
126                 CHANNEL_OUTBOUND_QUEUE_SIZE);
127         cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(
128                 RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES).removalListener(REMOVAL_LISTENER).build();
129         adapter.setResponseCache(cache);
130         // -- barrier
131         adapter.barrier(barrierInput);
132         embChannel.runPendingTasks();
133         Assert.assertEquals("Wrong - barrier", barrierInput, responseOfCall);
134         // -- echo
135         adapter.echo(echoInput);
136         embChannel.runPendingTasks();
137         Assert.assertEquals("Wrong - echo", echoInput, responseOfCall);
138         // -- echoReply
139         adapter.echoReply(echoReplyInput);
140         embChannel.runPendingTasks();
141         Assert.assertEquals("Wrong - echoReply",echoReplyInput, responseOfCall);
142         // -- experimenter
143         adapter.experimenter(experimenterInput);
144         embChannel.runPendingTasks();
145         Assert.assertEquals("Wrong - experimenter",experimenterInput, responseOfCall);
146         // -- flowMod
147         adapter.flowMod(flowModInput);
148         embChannel.runPendingTasks();
149         Assert.assertEquals("Wrong - flowMod", flowModInput, responseOfCall);
150         // -- getConfig
151         adapter.getConfig(getConfigInput);
152         embChannel.runPendingTasks();
153         Assert.assertEquals("Wrong - getConfig", getConfigInput, responseOfCall);
154         // -- getFeatures
155         adapter.getFeatures(getFeaturesInput);
156         embChannel.runPendingTasks();
157         Assert.assertEquals("Wrong - getFeatures",getFeaturesInput, responseOfCall);
158         // -- getQueueConfig
159         adapter.getQueueConfig(getQueueConfigInput);
160         embChannel.runPendingTasks();
161         Assert.assertEquals("Wrong - getQueueConfig",getQueueConfigInput, responseOfCall);
162         // -- groupMod
163         adapter.groupMod(groupModInput);
164         embChannel.runPendingTasks();
165         Assert.assertEquals("Wrong - groupMod", groupModInput, responseOfCall);
166         // -- hello
167         adapter.hello(helloInput);
168         embChannel.runPendingTasks();
169         Assert.assertEquals("Wrong - helloInput",helloInput, responseOfCall);
170         // -- meterMod
171         adapter.meterMod(meterModInput);
172         embChannel.runPendingTasks();
173         Assert.assertEquals("Wrong - meterMod",meterModInput, responseOfCall);
174         // -- packetOut
175         adapter.packetOut(packetOutInput);
176         embChannel.runPendingTasks();
177         Assert.assertEquals("Wrong - packetOut",packetOutInput, responseOfCall);
178         // -- multipartRequest
179         adapter.multipartRequest(multipartRequestInput);
180         embChannel.runPendingTasks();
181         Assert.assertEquals("Wrong - multipartRequest", multipartRequestInput, responseOfCall);
182         // -- portMod
183         adapter.portMod(portModInput);
184         embChannel.runPendingTasks();
185         Assert.assertEquals("Wrong - portMod", portModInput, responseOfCall);
186         // -- roleRequest
187         adapter.roleRequest(roleRequestInput);
188         embChannel.runPendingTasks();
189         Assert.assertEquals("Wrong - roleRequest", roleRequestInput, responseOfCall);
190         // -- setConfig
191         adapter.setConfig(setConfigInput);
192         embChannel.runPendingTasks();
193         Assert.assertEquals("Wrong - setConfig",setConfigInput, responseOfCall);
194         // -- tableMod
195         adapter.tableMod(tableModInput);
196         embChannel.runPendingTasks();
197         Assert.assertEquals("Wrong - tableMod", tableModInput, responseOfCall);
198         // -- getAsync
199         adapter.getAsync(getAsyncInput);
200         embChannel.runPendingTasks();
201         Assert.assertEquals("Wrong - getAsync", getAsyncInput, responseOfCall);
202         // -- setAsync
203         adapter.setAsync(setAsyncInput);
204         embChannel.runPendingTasks();
205         Assert.assertEquals("Wrong - setAsync", setAsyncInput, responseOfCall);
206         adapter.disconnect();
207     }
208
209     /**
210      * Channel Handler for testing.
211      * @author madamjak
212      */
213     private final class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
214         @Override
215         public void write(final ChannelHandlerContext ctx, final Object msg,
216                 final ChannelPromise promise) {
217             responseOfCall = null;
218             if (msg instanceof MessageListenerWrapper listener) {
219                 responseOfCall = listener.getMsg();
220             }
221         }
222     }
223 }