Add method to register listener for unknown msg
[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 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;
46
47 /**
48  * @author madamjak
49  * @author michal.polkorab
50  */
51 public class ConnectionAdapterImp02lTest {
52     private static final int RPC_RESPONSE_EXPIRATION = 1;
53     private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
54             new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
55         @Override
56         public void onRemoval(
57                 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
58             notification.getValue().discard();
59         }
60     };
61
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;
84     /**
85      * Initialize mocks
86      */
87     @Before
88     public void setUp() {
89         MockitoAnnotations.initMocks(this);
90     }
91     /**
92      * Disconnect adapter
93      */
94     @After
95     public void tierDown(){
96         if (adapter != null && adapter.isAlive()) {
97             adapter.disconnect();
98         }
99     }
100     /**
101      * Test Rpc Calls
102      */
103     @Test
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);
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      */
194     private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
195         @Override
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;
203             }
204         }
205     }
206 }