0e561fdc74803ceb739c9d87df66fa5b13b235a6
[openflowjava.git] / 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 io.netty.channel.ChannelHandlerContext;
11 import io.netty.channel.ChannelOutboundHandlerAdapter;
12 import io.netty.channel.ChannelPromise;
13 import io.netty.channel.embedded.EmbeddedChannel;
14
15 import java.net.InetSocketAddress;
16 import java.util.concurrent.TimeUnit;
17
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;
48
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;
53
54 /**
55  * @author madamjak
56  * @author michal.polkorab
57  */
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<?>>() {
62         @Override
63         public void onRemoval(
64                 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
65             notification.getValue().discard();
66         }
67     };
68
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;
91     /**
92      * Initialize mocks
93      */
94     @Before
95     public void setUp() {
96         MockitoAnnotations.initMocks(this);
97     }
98     /**
99      * Disconnect adapter
100      */
101     @After
102     public void tierDown(){
103         if (adapter != null && adapter.isAlive()) {
104             adapter.disconnect();
105         }
106     }
107     /**
108      * Test Rpc Calls 
109      */
110     @Test
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);
117         // -- barrier
118         adapter.barrier(barrierInput);
119         embChannel.runPendingTasks();
120         Assert.assertEquals("Wrong - barrier", barrierInput, responseOfCall);
121         // -- echo
122         adapter.echo(echoInput);
123         embChannel.runPendingTasks();
124         Assert.assertEquals("Wrong - echo", echoInput, responseOfCall);
125         // -- echoReply
126         adapter.echoReply(echoReplyInput);
127         embChannel.runPendingTasks();
128         Assert.assertEquals("Wrong - echoReply",echoReplyInput, responseOfCall);
129         // -- experimenter
130         adapter.experimenter(experimenterInput);
131         embChannel.runPendingTasks();
132         Assert.assertEquals("Wrong - experimenter",experimenterInput, responseOfCall);
133         // -- flowMod
134         adapter.flowMod(flowModInput);
135         embChannel.runPendingTasks();
136         Assert.assertEquals("Wrong - flowMod", flowModInput, responseOfCall);
137         // -- getConfig
138         adapter.getConfig(getConfigInput);
139         embChannel.runPendingTasks();
140         Assert.assertEquals("Wrong - getConfig", getConfigInput, responseOfCall);
141         // -- getFeatures
142         adapter.getFeatures(getFeaturesInput);
143         embChannel.runPendingTasks();
144         Assert.assertEquals("Wrong - getFeatures",getFeaturesInput, responseOfCall);
145         // -- getQueueConfig
146         adapter.getQueueConfig(getQueueConfigInput);
147         embChannel.runPendingTasks();
148         Assert.assertEquals("Wrong - getQueueConfig",getQueueConfigInput, responseOfCall);
149         // -- groupMod
150         adapter.groupMod(groupModInput);
151         embChannel.runPendingTasks();
152         Assert.assertEquals("Wrong - groupMod", groupModInput, responseOfCall);
153         // -- hello
154         adapter.hello(helloInput);
155         embChannel.runPendingTasks();
156         Assert.assertEquals("Wrong - helloInput",helloInput, responseOfCall);
157         // -- meterMod
158         adapter.meterMod(meterModInput);
159         embChannel.runPendingTasks();
160         Assert.assertEquals("Wrong - meterMod",meterModInput, responseOfCall);
161         // -- packetOut
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);
169         // -- portMod
170         adapter.portMod(portModInput);
171         embChannel.runPendingTasks();
172         Assert.assertEquals("Wrong - portMod", portModInput, responseOfCall);
173         // -- roleRequest
174         adapter.roleRequest(roleRequestInput);
175         embChannel.runPendingTasks();
176         Assert.assertEquals("Wrong - roleRequest", roleRequestInput, responseOfCall);
177         // -- setConfig
178         adapter.setConfig(setConfigInput);
179         embChannel.runPendingTasks();
180         Assert.assertEquals("Wrong - setConfig",setConfigInput, responseOfCall);
181         // -- tableMod
182         adapter.tableMod(tableModInput);
183         embChannel.runPendingTasks();
184         Assert.assertEquals("Wrong - tableMod", tableModInput, responseOfCall);
185         // -- getAsync
186         adapter.getAsync(getAsyncInput);
187         embChannel.runPendingTasks();
188         Assert.assertEquals("Wrong - getAsync", getAsyncInput, responseOfCall);
189         // -- setAsync
190         adapter.setAsync(setAsyncInput);
191         embChannel.runPendingTasks();
192         Assert.assertEquals("Wrong - setAsync", setAsyncInput, responseOfCall);
193         adapter.disconnect();
194     }
195
196     /**
197      * Channel Handler for testing
198      * @author madamjak
199      *
200      */
201     private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
202         @Override
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;
210             }
211         }
212     }
213 }