c70f79a0543f35777f4b2f04aaedf80a5ea85e10
[ovsdb.git] / library / src / test / java / org / opendaylight / ovsdb / lib / OvsdbTestBase.java
1 /*
2  * [[ Authors will Fill in the Copyright header ]]
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  * Authors : Brent Salisbury, Hugo Trippaers
9  */
10 package org.opendaylight.ovsdb.lib;
11
12 import io.netty.bootstrap.Bootstrap;
13 import io.netty.channel.AdaptiveRecvByteBufAllocator;
14 import io.netty.channel.Channel;
15 import io.netty.channel.ChannelFuture;
16 import io.netty.channel.ChannelInitializer;
17 import io.netty.channel.ChannelOption;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.channel.socket.SocketChannel;
20 import io.netty.channel.socket.nio.NioSocketChannel;
21 import io.netty.handler.codec.string.StringEncoder;
22 import io.netty.util.CharsetUtil;
23
24 import java.io.IOException;
25 import java.net.InetAddress;
26 import java.util.Properties;
27 import java.util.concurrent.ExecutionException;
28
29 import junit.framework.Assert;
30
31 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder;
32 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcEndpoint;
33 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcServiceBinderHandler;
34 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
35
36 import com.fasterxml.jackson.annotation.JsonInclude.Include;
37 import com.fasterxml.jackson.databind.DeserializationFeature;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39
40 public abstract class OvsdbTestBase implements OvsdbRPC.Callback{
41     private final static String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
42     private final static String SERVER_PORT = "ovsdbserver.port";
43     private final static String DEFAULT_SERVER_PORT = "6640";
44
45     public Properties loadProperties() {
46         Properties props = new Properties(System.getProperties());
47         return props;
48     }
49
50     private Channel connect(String addressStr, String portStr) {
51         InetAddress address;
52         try {
53             address = InetAddress.getByName(addressStr);
54         } catch (Exception e) {
55             System.out.println("Unable to resolve " + addressStr);
56             e.printStackTrace();
57             return null;
58         }
59
60         Integer port;
61         try {
62             port = Integer.parseInt(portStr);
63         } catch (NumberFormatException e) {
64             System.out.println("Invalid port number : " + portStr);
65             e.printStackTrace();
66             return null;
67         }
68
69         try {
70             Bootstrap bootstrap = new Bootstrap();
71             bootstrap.group(new NioEventLoopGroup());
72             bootstrap.channel(NioSocketChannel.class);
73             bootstrap.option(ChannelOption.TCP_NODELAY, true);
74             bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(65535, 65535, 65535));
75
76             bootstrap.handler(new ChannelInitializer<SocketChannel>() {
77                 @Override
78                 public void initChannel(SocketChannel channel) throws Exception {
79                     channel.pipeline().addLast(
80                             //new LoggingHandler(LogLevel.INFO),
81                             new JsonRpcDecoder(100000),
82                             new StringEncoder(CharsetUtil.UTF_8));
83                 }
84             });
85
86             ChannelFuture future = bootstrap.connect(address, port).sync();
87             Channel channel = future.channel();
88             return channel;
89         } catch (InterruptedException e) {
90             System.out.println("Thread was interrupted during connect");
91         }
92         return null;
93     }
94
95     public OvsdbRPC getTestConnection() throws IOException {
96         Properties props = loadProperties();
97         String address = props.getProperty(SERVER_IPADDRESS);
98         String port = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
99
100         if (address == null) {
101             Assert.fail("Integration Test requires a valid ovsdbserver.ipaddress value.\n" +
102                         "Usage : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify");
103         }
104         Channel channel = this.connect(address, port);
105         if (channel == null) {
106             throw new IOException("Failed to connect to ovsdb server");
107         }
108         try {
109             return this.handleNewConnection(channel);
110         } catch (InterruptedException | ExecutionException e) {
111             e.printStackTrace();
112         }
113         return null;
114     }
115
116     private OvsdbRPC handleNewConnection(Channel channel) throws InterruptedException, ExecutionException {
117         ObjectMapper objectMapper = new ObjectMapper();
118         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
119         objectMapper.setSerializationInclusion(Include.NON_NULL);
120
121         JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
122         JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
123         binderHandler.setContext(channel);
124         channel.pipeline().addLast(binderHandler);
125
126         OvsdbRPC ovsdb = factory.getClient(channel, OvsdbRPC.class);
127         ovsdb.registerCallback(this);
128         return ovsdb;
129     }
130 }