Code ReOrganization and Re-Architecture changes
[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.io.InputStream;
26 import java.net.InetAddress;
27 import java.util.Properties;
28 import java.util.concurrent.ExecutionException;
29
30 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder;
31 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcEndpoint;
32 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcServiceBinderHandler;
33 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
34
35 import com.fasterxml.jackson.annotation.JsonInclude.Include;
36 import com.fasterxml.jackson.databind.DeserializationFeature;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38
39 public abstract class OvsdbTestBase implements OvsdbRPC.Callback{
40     private final static String identifier = "TEST";
41
42     public Properties loadProperties() throws IOException {
43         InputStream is = this
44                 .getClass()
45                 .getClassLoader()
46                 .getResourceAsStream(
47                         "org/opendaylight/ovsdb/lib/message/integration-test.properties");
48         if (is == null) {
49             throw new IOException("Unable to load integration-test.properties");
50         }
51         Properties props = new Properties();
52         props.load(is);
53
54         return props;
55     }
56
57     private Channel connect(String addressStr, String portStr) {
58         InetAddress address;
59         try {
60             address = InetAddress.getByName(addressStr);
61         } catch (Exception e) {
62             System.out.println("Unable to resolve " + addressStr);
63             e.printStackTrace();
64             return null;
65         }
66
67         Integer port;
68         try {
69             port = Integer.parseInt(portStr);
70         } catch (NumberFormatException e) {
71             System.out.println("Invalid port number : " + portStr);
72             e.printStackTrace();
73             return null;
74         }
75
76         try {
77             Bootstrap bootstrap = new Bootstrap();
78             bootstrap.group(new NioEventLoopGroup());
79             bootstrap.channel(NioSocketChannel.class);
80             bootstrap.option(ChannelOption.TCP_NODELAY, true);
81             bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(65535, 65535, 65535));
82
83             bootstrap.handler(new ChannelInitializer<SocketChannel>() {
84                 @Override
85                 public void initChannel(SocketChannel channel) throws Exception {
86                     channel.pipeline().addLast(
87                             //new LoggingHandler(LogLevel.INFO),
88                             new JsonRpcDecoder(100000),
89                             new StringEncoder(CharsetUtil.UTF_8));
90                 }
91             });
92
93             ChannelFuture future = bootstrap.connect(address, port).sync();
94             Channel channel = future.channel();
95             return channel;
96         } catch (InterruptedException e) {
97             System.out.println("Thread was interrupted during connect");
98         }
99         return null;
100     }
101
102     public OvsdbRPC getTestConnection() throws IOException {
103         Properties props = loadProperties();
104         String address = props.getProperty("ovsdbserver.ipaddress");
105         String port = props.getProperty("ovsdbserver.port", "6640");
106
107         Channel channel = this.connect(address, port);
108         if (channel == null) {
109             throw new IOException("Failed to connecto to ovsdb server");
110         }
111         try {
112             return this.handleNewConnection(channel);
113         } catch (InterruptedException | ExecutionException e) {
114             e.printStackTrace();
115         }
116         return null;
117     }
118
119     private OvsdbRPC handleNewConnection(Channel channel) throws InterruptedException, ExecutionException {
120         ObjectMapper objectMapper = new ObjectMapper();
121         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
122         objectMapper.setSerializationInclusion(Include.NON_NULL);
123
124         JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
125         JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
126         binderHandler.setContext(channel);
127         channel.pipeline().addLast(binderHandler);
128
129         OvsdbRPC ovsdb = factory.getClient(channel, OvsdbRPC.class);
130         ovsdb.registerCallback(this);
131         return ovsdb;
132     }
133 }