2 * [[ Authors will Fill in the Copyright header ]]
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
8 * Authors : Brent Salisbury, Hugo Trippaers
10 package org.opendaylight.ovsdb.lib;
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;
24 import java.io.IOException;
25 import java.net.InetAddress;
26 import java.util.Properties;
27 import java.util.concurrent.ExecutionException;
29 import junit.framework.Assert;
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;
36 import com.fasterxml.jackson.annotation.JsonInclude.Include;
37 import com.fasterxml.jackson.databind.DeserializationFeature;
38 import com.fasterxml.jackson.databind.ObjectMapper;
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";
45 public Properties loadProperties() {
46 Properties props = new Properties(System.getProperties());
50 private Channel connect(String addressStr, String portStr) {
53 address = InetAddress.getByName(addressStr);
54 } catch (Exception e) {
55 System.out.println("Unable to resolve " + addressStr);
62 port = Integer.parseInt(portStr);
63 } catch (NumberFormatException e) {
64 System.out.println("Invalid port number : " + portStr);
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));
76 bootstrap.handler(new ChannelInitializer<SocketChannel>() {
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));
86 ChannelFuture future = bootstrap.connect(address, port).sync();
87 Channel channel = future.channel();
89 } catch (InterruptedException e) {
90 System.out.println("Thread was interrupted during connect");
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);
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");
104 Channel channel = this.connect(address, port);
105 if (channel == null) {
106 throw new IOException("Failed to connect to ovsdb server");
109 return this.handleNewConnection(channel);
110 } catch (InterruptedException | ExecutionException e) {
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);
121 JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
122 JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
123 binderHandler.setContext(channel);
124 channel.pipeline().addLast(binderHandler);
126 OvsdbRPC ovsdb = factory.getClient(channel, OvsdbRPC.class);
127 ovsdb.registerCallback(this);