Added more ignorable files to .gitignore
[ovsdb.git] / ovsdb / src / test / java / org / opendaylight / ovsdb / lib / message / OVSDBNettyFactoryIT.java
1 /*
2  * [[ Authors will Fill in the Copyright header ]]
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 : Brent Salisbury, Madhu Venugopal, Aswin Raveendran
9  */
10 package org.opendaylight.ovsdb.lib.message;
11
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Properties;
20 import java.util.concurrent.ExecutionException;
21
22 import org.apache.commons.collections.MapUtils;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.controller.sal.connection.ConnectionConstants;
26 import org.opendaylight.controller.sal.core.Node;
27 import org.opendaylight.ovsdb.lib.database.DatabaseSchema;
28 import org.opendaylight.ovsdb.lib.operations.InsertOperation;
29 import org.opendaylight.ovsdb.lib.operations.MutateOperation;
30 import org.opendaylight.ovsdb.lib.operations.Operation;
31 import org.opendaylight.ovsdb.lib.operations.OperationResult;
32 import org.opendaylight.ovsdb.lib.notation.Condition;
33 import org.opendaylight.ovsdb.lib.notation.Function;
34 import org.opendaylight.ovsdb.lib.notation.Mutation;
35 import org.opendaylight.ovsdb.lib.notation.Mutator;
36 import org.opendaylight.ovsdb.lib.notation.OvsDBSet;
37 import org.opendaylight.ovsdb.lib.notation.UUID;
38 import org.opendaylight.ovsdb.lib.table.Bridge;
39 import org.opendaylight.ovsdb.lib.table.Interface;
40 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
41 import org.opendaylight.ovsdb.lib.table.Port;
42 import org.opendaylight.ovsdb.lib.table.internal.Table;
43 import org.opendaylight.ovsdb.lib.table.internal.Tables;
44 import org.opendaylight.ovsdb.plugin.Connection;
45 import org.opendaylight.ovsdb.plugin.ConnectionService;
46 import org.opendaylight.ovsdb.plugin.InventoryService;
47
48 import com.google.common.util.concurrent.ListenableFuture;
49
50 public class OVSDBNettyFactoryIT {
51     InventoryService inventoryService;
52     private static String bridgeIdentifier = "br1";
53     private Properties props;
54
55     @Before
56     public void initialize() throws IOException {
57         InputStream is = this.getClass().getClassLoader()
58                 .getResourceAsStream(
59                         "org/opendaylight/ovsdb/lib/message/integration-test.properties");
60         if (is == null) {
61             throw new IOException("Unable to load integration-test.properties");
62         }
63         props = new Properties();
64         props.load(is);
65
66     }
67
68     @Test
69     public void testSome() throws InterruptedException, ExecutionException,
70             IOException {
71         ConnectionService connectionService = new ConnectionService();
72         connectionService.init();
73         inventoryService = new InventoryService();
74         inventoryService.init();
75         connectionService.setInventoryServiceInternal(inventoryService);
76         Node.NodeIDType.registerIDType("OVS", String.class);
77         Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
78         params.put(ConnectionConstants.ADDRESS,
79                 props.getProperty("ovsdbserver.ipaddress"));
80         params.put(ConnectionConstants.PORT,
81                 props.getProperty("ovsdbserver.port", "6640"));
82         Node node = connectionService.connect("TEST", params);
83         if (node == null) {
84             throw new IOException("Unable to connect to the host");
85         }
86
87         Connection connection = connectionService.getConnection(node);
88         if (connection == null) {
89             throw new IOException("Unable to connect to the host");
90         }
91
92         OvsdbRPC ovsdb = connection.getRpc();
93         if (ovsdb == null) {
94             throw new IOException("Unable to obtain RPC instance");
95         }
96
97         //GET DB-SCHEMA
98         List<String> dbNames = Arrays.asList(Open_vSwitch.NAME.getName());
99         ListenableFuture<DatabaseSchema> dbSchemaF = null; //ovsdb.get_schema(dbNames);
100         DatabaseSchema databaseSchema = dbSchemaF.get();
101         MapUtils.debugPrint(System.out, null, databaseSchema.getTables());
102
103         // TEST MONITOR
104         // YES it is expected to fail with "duplicate monitor ID" as we have a perpetual monitor in Inventory Service
105         MonitorRequestBuilder monitorReq = new MonitorRequestBuilder();
106         for (Table<?> table : Tables.getTables()) {
107             monitorReq.monitor(table);
108         }
109
110         ListenableFuture<TableUpdates> monResponse = ovsdb.monitor(monitorReq);
111         System.out.println("Monitor Request sent :");
112         TableUpdates updates = monResponse.get();
113         inventoryService.processTableUpdates(node, updates);
114         inventoryService.printCache(node);
115
116         // TRANSACT INSERT TEST
117
118         Map<String, Table<?>> ovsTable = inventoryService.getTableCache(node, Open_vSwitch.NAME.getName());
119         String newBridge = "new_bridge";
120         String newInterface = "new_interface";
121         String newPort = "new_port";
122         String newSwitch = "new_switch";
123
124         Operation addSwitchRequest = null;
125
126         if(ovsTable != null){
127             String ovsTableUUID = (String) ovsTable.keySet().toArray()[0];
128             UUID bridgeUuidPair = new UUID(newBridge);
129             Mutation bm = new Mutation("bridges", Mutator.INSERT, bridgeUuidPair);
130             List<Mutation> mutations = new ArrayList<Mutation>();
131             mutations.add(bm);
132
133             UUID uuid = new UUID(ovsTableUUID);
134             Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
135             List<Condition> where = new ArrayList<Condition>();
136             where.add(condition);
137             addSwitchRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
138         }
139         else{
140             Open_vSwitch ovsTableRow = new Open_vSwitch();
141             OvsDBSet<UUID> bridges = new OvsDBSet<UUID>();
142             UUID bridgeUuidPair = new UUID(newBridge);
143             bridges.add(bridgeUuidPair);
144             ovsTableRow.setBridges(bridges);
145             addSwitchRequest = new InsertOperation(Open_vSwitch.NAME.getName(), newSwitch, ovsTableRow);
146         }
147
148         Bridge bridgeRow = new Bridge();
149         bridgeRow.setName(bridgeIdentifier);
150         OvsDBSet<UUID> ports = new OvsDBSet<UUID>();
151         UUID port = new UUID(newPort);
152         ports.add(port);
153         bridgeRow.setPorts(ports);
154         InsertOperation addBridgeRequest = new InsertOperation(Bridge.NAME.getName(), newBridge, bridgeRow);
155
156         Port portRow = new Port();
157         portRow.setName(bridgeIdentifier);
158         OvsDBSet<UUID> interfaces = new OvsDBSet<UUID>();
159         UUID interfaceid = new UUID(newInterface);
160         interfaces.add(interfaceid);
161         portRow.setInterfaces(interfaces);
162         InsertOperation addPortRequest = new InsertOperation(Port.NAME.getName(), newPort, portRow);
163
164         Interface interfaceRow = new Interface();
165         interfaceRow.setName(bridgeIdentifier);
166         interfaceRow.setType("internal");
167         InsertOperation addIntfRequest = new InsertOperation(Interface.NAME.getName(), newInterface, interfaceRow);
168
169         TransactBuilder transaction = new TransactBuilder();
170         transaction.addOperations(new ArrayList<Operation>(
171                 Arrays.asList(addSwitchRequest, addIntfRequest, addPortRequest, addBridgeRequest)));
172
173         ListenableFuture<List<OperationResult>> transResponse = ovsdb.transact(transaction);
174         System.out.println("Transcation sent :");
175         List<OperationResult> tr = transResponse.get();
176         System.out.println("Transaction response : "+transResponse.toString());
177         List<Operation> requests = transaction.getRequests();
178         for (int i = 0; i < tr.size() ; i++) {
179             if (i < requests.size()) requests.get(i).setResult(tr.get(i));
180         }
181
182         System.out.println("Request + Response : "+requests.toString());
183         if (tr.size() > requests.size()) {
184             System.out.println("ERROR : "+tr.get(tr.size()-1).getError());
185             System.out.println("Details : "+tr.get(tr.size()-1).getDetails());
186         }
187
188         // TEST ECHO
189
190         ListenableFuture<List<String>> some = ovsdb.echo();
191         Object s = some.get();
192         System.out.printf("Result of echo is %s \n", s);
193
194         // TEST ECHO REQUEST/REPLY
195         Thread.sleep(10000);
196
197         connectionService.disconnect(node);
198     }
199 }