Removing the Open_vSwitch schema hardcoding in OvsDBClient.
[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     /**
46      * Represents the Open Vswitch Schema
47      */
48     public final static String OPEN_VSWITCH_SCHEMA = "Open_vSwitch";
49
50     public Properties loadProperties() {
51         Properties props = new Properties(System.getProperties());
52         return props;
53     }
54
55     private Channel connect(String addressStr, String portStr) {
56         InetAddress address;
57         try {
58             address = InetAddress.getByName(addressStr);
59         } catch (Exception e) {
60             System.out.println("Unable to resolve " + addressStr);
61             e.printStackTrace();
62             return null;
63         }
64
65         Integer port;
66         try {
67             port = Integer.parseInt(portStr);
68         } catch (NumberFormatException e) {
69             System.out.println("Invalid port number : " + portStr);
70             e.printStackTrace();
71             return null;
72         }
73
74         try {
75             Bootstrap bootstrap = new Bootstrap();
76             bootstrap.group(new NioEventLoopGroup());
77             bootstrap.channel(NioSocketChannel.class);
78             bootstrap.option(ChannelOption.TCP_NODELAY, true);
79             bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(65535, 65535, 65535));
80
81             bootstrap.handler(new ChannelInitializer<SocketChannel>() {
82                 @Override
83                 public void initChannel(SocketChannel channel) throws Exception {
84                     channel.pipeline().addLast(
85                             //new LoggingHandler(LogLevel.INFO),
86                             new JsonRpcDecoder(100000),
87                             new StringEncoder(CharsetUtil.UTF_8));
88                 }
89             });
90
91             ChannelFuture future = bootstrap.connect(address, port).sync();
92             Channel channel = future.channel();
93             return channel;
94         } catch (InterruptedException e) {
95             System.out.println("Thread was interrupted during connect");
96         }
97         return null;
98     }
99
100     public OvsdbRPC getTestConnection() throws IOException {
101         Properties props = loadProperties();
102         String address = props.getProperty(SERVER_IPADDRESS);
103         String port = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
104
105         if (address == null) {
106             Assert.fail("Integration Test requires a valid ovsdbserver.ipaddress value.\n" +
107                         "Usage : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify");
108         }
109         Channel channel = this.connect(address, port);
110         if (channel == null) {
111             throw new IOException("Failed to connect to ovsdb server");
112         }
113         try {
114             return this.handleNewConnection(channel);
115         } catch (InterruptedException | ExecutionException e) {
116             e.printStackTrace();
117         }
118         return null;
119     }
120
121     private OvsdbRPC handleNewConnection(Channel channel) throws InterruptedException, ExecutionException {
122         ObjectMapper objectMapper = new ObjectMapper();
123         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
124         objectMapper.setSerializationInclusion(Include.NON_NULL);
125
126         JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
127         JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
128         binderHandler.setContext(channel);
129         channel.pipeline().addLast(binderHandler);
130
131         OvsdbRPC ovsdb = factory.getClient(channel, OvsdbRPC.class);
132         ovsdb.registerCallback(this);
133         return ovsdb;
134     }
135 }