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";
46 * Represents the Open Vswitch Schema
48 public final static String OPEN_VSWITCH_SCHEMA = "Open_vSwitch";
50 public Properties loadProperties() {
51 Properties props = new Properties(System.getProperties());
55 private Channel connect(String addressStr, String portStr) {
58 address = InetAddress.getByName(addressStr);
59 } catch (Exception e) {
60 System.out.println("Unable to resolve " + addressStr);
67 port = Integer.parseInt(portStr);
68 } catch (NumberFormatException e) {
69 System.out.println("Invalid port number : " + portStr);
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));
81 bootstrap.handler(new ChannelInitializer<SocketChannel>() {
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));
91 ChannelFuture future = bootstrap.connect(address, port).sync();
92 Channel channel = future.channel();
94 } catch (InterruptedException e) {
95 System.out.println("Thread was interrupted during connect");
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);
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");
109 Channel channel = this.connect(address, port);
110 if (channel == null) {
111 throw new IOException("Failed to connect to ovsdb server");
114 return this.handleNewConnection(channel);
115 } catch (InterruptedException | ExecutionException e) {
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);
126 JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
127 JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
128 binderHandler.setContext(channel);
129 channel.pipeline().addLast(binderHandler);
131 OvsdbRPC ovsdb = factory.getClient(channel, OvsdbRPC.class);
132 ovsdb.registerCallback(this);