Barrier turn on/off-add switcher value to Config-Subsystem
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / ChannelOutboundQueue02Test.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.ChannelHandler;
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.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
31
32 /**
33  * @author madamjak
34  *
35  */
36 public class ChannelOutboundQueue02Test {
37     private static int counter;
38     private static final int RPC_RESPONSE_EXPIRATION = 1;
39     private static final RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>> REMOVAL_LISTENER =
40             new RemovalListener<RpcResponseKey, ResponseExpectedRpcListener<?>>() {
41         @Override
42         public void onRemoval(
43                 final RemovalNotification<RpcResponseKey, ResponseExpectedRpcListener<?>> notification) {
44             notification.getValue().discard();
45         }
46     };
47
48     @Mock EchoInput echoInput;
49     @Mock BarrierInput barrierInput;
50     @Mock EchoReplyInput echoReplyInput;
51     @Mock ExperimenterInput experimenterInput;
52     private ConnectionAdapterImpl adapter;
53     private Cache<RpcResponseKey, ResponseExpectedRpcListener<?>> cache;
54     /**
55      * Initialize mocks
56      */
57     @Before
58     public void setUp() {
59         MockitoAnnotations.initMocks(this);
60     }
61     /**
62      * Disconnect adapter after each test
63      */
64     @After
65     public void tierDown(){
66         if (adapter != null && adapter.isAlive()) {
67             adapter.disconnect();
68         }
69     }
70
71     /**
72      * Test write to closed / opened channel
73      * @throws Exception
74      */
75     @Test
76     public void test01() throws Exception {
77         final EmbeddedChannel ec = new EmbeddedChannel(new EmbededChannelHandler());
78         adapter = new ConnectionAdapterImpl(ec, InetSocketAddress.createUnresolved("localhost", 9876), true);
79         cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
80                 .removalListener(REMOVAL_LISTENER).build();
81         adapter.setResponseCache(cache);
82         final ChannelOutboundQueue cq = (ChannelOutboundQueue) ec.pipeline().last();
83         counter=0;
84         adapter.barrier(barrierInput);
85         adapter.echo(echoInput);
86         cq.channelInactive(ec.pipeline().lastContext());
87         ec.runPendingTasks();
88         Assert.assertEquals("Wrong - ChannelOutboundHandlerAdapter.write was invoked on closed channel",0, counter);
89         cq.channelActive(ec.pipeline().lastContext());
90         counter=0;
91         adapter.barrier(barrierInput);
92         adapter.experimenter(experimenterInput);
93         ec.runPendingTasks();
94         Assert.assertEquals("Wrong - ChannelOutboundHandlerAdapter.write has not been invoked on opened channel",2, counter);
95     }
96
97     /**
98      * Test write to read only / writable channel
99      */
100     @Test
101     public void test02(){
102         final ChangeWritableEmbededChannel ec = new ChangeWritableEmbededChannel(new EmbededChannelHandler());
103         adapter = new ConnectionAdapterImpl(ec, InetSocketAddress.createUnresolved("localhost", 9876), true);
104         cache = CacheBuilder.newBuilder().concurrencyLevel(1).expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
105                 .removalListener(REMOVAL_LISTENER).build();
106         adapter.setResponseCache(cache);
107         ec.setReadOnly();
108         counter=0;
109         adapter.barrier(barrierInput);
110         adapter.echo(echoInput);
111         ec.runPendingTasks();
112         Assert.assertEquals("Wrong - write to readonly channel",0, counter);
113         ec.setWritable();
114         adapter.echoReply(echoReplyInput);
115         adapter.echo(echoInput);
116         ec.runPendingTasks();
117         Assert.assertEquals("Wrong - write to writtable channel",4, counter);
118     }
119
120     /**
121      * Channel Handler for testing
122      * @author madamjak
123      *
124      */
125     private class EmbededChannelHandler extends ChannelOutboundHandlerAdapter {
126         @Override
127         public void write(final ChannelHandlerContext ctx, final Object msg,
128                 final ChannelPromise promise) throws Exception {
129             if(msg instanceof MessageListenerWrapper){
130                 counter++;
131             }
132         }
133     }
134
135     /**
136      * Class for testing - channel can change state to read only or writable
137      * @author madamjak
138      *
139      */
140     private class ChangeWritableEmbededChannel extends EmbeddedChannel {
141         private boolean isWrittable;
142         public ChangeWritableEmbededChannel(final ChannelHandler channelHandler){
143             super(channelHandler);
144             setReadOnly();
145         }
146
147         @Override
148         public boolean isWritable() {
149             return isWrittable;
150         }
151
152         public void setWritable(){
153             isWrittable = true;
154         }
155
156         public void setReadOnly(){
157             isWrittable = false;
158         }
159     }
160 }