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.io.InputStream;
26 import java.net.InetAddress;
27 import java.util.Properties;
28 import java.util.concurrent.ExecutionException;
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;
35 import com.fasterxml.jackson.annotation.JsonInclude.Include;
36 import com.fasterxml.jackson.databind.DeserializationFeature;
37 import com.fasterxml.jackson.databind.ObjectMapper;
39 public abstract class OvsdbTestBase implements OvsdbRPC.Callback{
40 private final static String identifier = "TEST";
42 public Properties loadProperties() throws IOException {
47 "org/opendaylight/ovsdb/lib/message/integration-test.properties");
49 throw new IOException("Unable to load integration-test.properties");
51 Properties props = new Properties();
57 private Channel connect(String addressStr, String portStr) {
60 address = InetAddress.getByName(addressStr);
61 } catch (Exception e) {
62 System.out.println("Unable to resolve " + addressStr);
69 port = Integer.parseInt(portStr);
70 } catch (NumberFormatException e) {
71 System.out.println("Invalid port number : " + portStr);
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));
83 bootstrap.handler(new ChannelInitializer<SocketChannel>() {
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));
93 ChannelFuture future = bootstrap.connect(address, port).sync();
94 Channel channel = future.channel();
96 } catch (InterruptedException e) {
97 System.out.println("Thread was interrupted during connect");
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");
107 Channel channel = this.connect(address, port);
108 if (channel == null) {
109 throw new IOException("Failed to connecto to ovsdb server");
112 return this.handleNewConnection(channel);
113 } catch (InterruptedException | ExecutionException e) {
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);
124 JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
125 JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
126 binderHandler.setContext(channel);
127 channel.pipeline().addLast(binderHandler);
129 OvsdbRPC ovsdb = factory.getClient(channel, OvsdbRPC.class);
130 ovsdb.registerCallback(this);