2 * Copyright (C) 2014 Red Hat, Inc.
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 : Madhu Venugopal
11 package org.opendaylight.ovsdb.schema.openvswitch;
13 import io.netty.bootstrap.Bootstrap;
14 import io.netty.channel.AdaptiveRecvByteBufAllocator;
15 import io.netty.channel.Channel;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.ChannelInitializer;
18 import io.netty.channel.ChannelOption;
19 import io.netty.channel.nio.NioEventLoopGroup;
20 import io.netty.channel.socket.SocketChannel;
21 import io.netty.channel.socket.nio.NioSocketChannel;
22 import io.netty.handler.codec.string.StringEncoder;
23 import io.netty.util.CharsetUtil;
25 import java.io.IOException;
26 import java.net.InetAddress;
27 import java.util.Properties;
28 import java.util.concurrent.ExecutionException;
30 import junit.framework.Assert;
32 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder;
33 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcEndpoint;
34 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcServiceBinderHandler;
35 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
37 import com.fasterxml.jackson.annotation.JsonInclude.Include;
38 import com.fasterxml.jackson.databind.DeserializationFeature;
39 import com.fasterxml.jackson.databind.ObjectMapper;
41 public abstract class OvsdbTestBase implements OvsdbRPC.Callback{
42 private final static String SERVER_IPADDRESS = "ovsdbserver.ipaddress";
43 private final static String SERVER_PORT = "ovsdbserver.port";
44 private final static String DEFAULT_SERVER_PORT = "6640";
47 * Represents the Open Vswitch Schema
49 public final static String OPEN_VSWITCH_SCHEMA = "Open_vSwitch";
51 public Properties loadProperties() {
52 Properties props = new Properties(System.getProperties());
56 private Channel connect(String addressStr, String portStr) {
59 address = InetAddress.getByName(addressStr);
60 } catch (Exception e) {
61 System.out.println("Unable to resolve " + addressStr);
68 port = Integer.parseInt(portStr);
69 } catch (NumberFormatException e) {
70 System.out.println("Invalid port number : " + portStr);
76 Bootstrap bootstrap = new Bootstrap();
77 bootstrap.group(new NioEventLoopGroup());
78 bootstrap.channel(NioSocketChannel.class);
79 bootstrap.option(ChannelOption.TCP_NODELAY, true);
80 bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator(65535, 65535, 65535));
82 bootstrap.handler(new ChannelInitializer<SocketChannel>() {
84 public void initChannel(SocketChannel channel) throws Exception {
85 channel.pipeline().addLast(
86 //new LoggingHandler(LogLevel.INFO),
87 new JsonRpcDecoder(100000),
88 new StringEncoder(CharsetUtil.UTF_8));
92 ChannelFuture future = bootstrap.connect(address, port).sync();
93 Channel channel = future.channel();
95 } catch (InterruptedException e) {
96 System.out.println("Thread was interrupted during connect");
101 public OvsdbRPC getTestConnection() throws IOException {
102 Properties props = loadProperties();
103 String address = props.getProperty(SERVER_IPADDRESS);
104 String port = props.getProperty(SERVER_PORT, DEFAULT_SERVER_PORT);
106 if (address == null) {
107 Assert.fail("Integration Test requires a valid ovsdbserver.ipaddress value.\n" +
108 "Usage : mvn -Pintegrationtest -Dovsdbserver.ipaddress=x.x.x.x -Dovsdbserver.port=yyyy verify");
110 Channel channel = this.connect(address, port);
111 if (channel == null) {
112 throw new IOException("Failed to connect to ovsdb server");
115 return this.handleNewConnection(channel);
116 } catch (InterruptedException | ExecutionException e) {
122 private OvsdbRPC handleNewConnection(Channel channel) throws InterruptedException, ExecutionException {
123 ObjectMapper objectMapper = new ObjectMapper();
124 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
125 objectMapper.setSerializationInclusion(Include.NON_NULL);
127 JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
128 JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
129 binderHandler.setContext(channel);
130 channel.pipeline().addLast(binderHandler);
132 OvsdbRPC ovsdb = factory.getClient(channel, OvsdbRPC.class);
133 ovsdb.registerCallback(this);