Introducing Open_vSwitch Typed Schema wrapper on top of the Schema indepedent Library.
[ovsdb.git] / schemas / Open_vSwitch / src / test / java / org / opendaylight / ovsdb / schema / openvswitch / OvsdbTestBase.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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 : Madhu Venugopal
9  */
10
11 package org.opendaylight.ovsdb.schema.openvswitch;
12
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;
24
25 import java.io.IOException;
26 import java.net.InetAddress;
27 import java.util.Properties;
28 import java.util.concurrent.ExecutionException;
29
30 import junit.framework.Assert;
31
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;
36
37 import com.fasterxml.jackson.annotation.JsonInclude.Include;
38 import com.fasterxml.jackson.databind.DeserializationFeature;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
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";
45
46     /**
47      * Represents the Open Vswitch Schema
48      */
49     public final static String OPEN_VSWITCH_SCHEMA = "Open_vSwitch";
50
51     public Properties loadProperties() {
52         Properties props = new Properties(System.getProperties());
53         return props;
54     }
55
56     private Channel connect(String addressStr, String portStr) {
57         InetAddress address;
58         try {
59             address = InetAddress.getByName(addressStr);
60         } catch (Exception e) {
61             System.out.println("Unable to resolve " + addressStr);
62             e.printStackTrace();
63             return null;
64         }
65
66         Integer port;
67         try {
68             port = Integer.parseInt(portStr);
69         } catch (NumberFormatException e) {
70             System.out.println("Invalid port number : " + portStr);
71             e.printStackTrace();
72             return null;
73         }
74
75         try {
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));
81
82             bootstrap.handler(new ChannelInitializer<SocketChannel>() {
83                 @Override
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));
89                 }
90             });
91
92             ChannelFuture future = bootstrap.connect(address, port).sync();
93             Channel channel = future.channel();
94             return channel;
95         } catch (InterruptedException e) {
96             System.out.println("Thread was interrupted during connect");
97         }
98         return null;
99     }
100
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);
105
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");
109         }
110         Channel channel = this.connect(address, port);
111         if (channel == null) {
112             throw new IOException("Failed to connect to ovsdb server");
113         }
114         try {
115             return this.handleNewConnection(channel);
116         } catch (InterruptedException | ExecutionException e) {
117             e.printStackTrace();
118         }
119         return null;
120     }
121
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);
126
127         JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
128         JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
129         binderHandler.setContext(channel);
130         channel.pipeline().addLast(binderHandler);
131
132         OvsdbRPC ovsdb = factory.getClient(channel, OvsdbRPC.class);
133         ovsdb.registerCallback(this);
134         return ovsdb;
135     }
136 }