Removing Plugin dependancies from the Library in lieu of bundle isolation.
[netvirt.git] / ovsdb / src / test / java / org / opendaylight / ovsdb / lib / message / OVSDBNettyFactoryTest.java
1 package org.opendaylight.ovsdb.lib.message;
2
3 import com.fasterxml.jackson.databind.DeserializationFeature;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.google.common.collect.Lists;
6 import com.google.common.util.concurrent.ListenableFuture;
7
8 import io.netty.channel.Channel;
9 import io.netty.channel.ChannelHandler;
10 import io.netty.handler.codec.string.StringEncoder;
11 import io.netty.handler.logging.LogLevel;
12 import io.netty.handler.logging.LoggingHandler;
13 import io.netty.util.CharsetUtil;
14
15 import org.junit.Test;
16 import org.opendaylight.controller.sal.connection.ConnectionConstants;
17 import org.opendaylight.controller.sal.core.Node;
18 import org.opendaylight.ovsdb.lib.database.DatabaseSchema;
19 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcDecoder;
20 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcEndpoint;
21 import org.opendaylight.ovsdb.lib.jsonrpc.JsonRpcServiceBinderHandler;
22 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
23 import org.opendaylight.ovsdb.lib.message.OVSDB;
24 import org.opendaylight.ovsdb.lib.message.TableUpdates;
25 import org.opendaylight.ovsdb.lib.message.operations.InsertOperation;
26 import org.opendaylight.ovsdb.lib.message.operations.MutateOperation;
27 import org.opendaylight.ovsdb.lib.message.operations.Operation;
28 import org.opendaylight.ovsdb.lib.message.operations.OperationResult;
29 import org.opendaylight.ovsdb.lib.notation.Condition;
30 import org.opendaylight.ovsdb.lib.notation.Function;
31 import org.opendaylight.ovsdb.lib.notation.Mutation;
32 import org.opendaylight.ovsdb.lib.notation.Mutator;
33 import org.opendaylight.ovsdb.lib.notation.UUID;
34 import org.opendaylight.ovsdb.lib.table.Bridge;
35 import org.opendaylight.ovsdb.lib.table.Interface;
36 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
37 import org.opendaylight.ovsdb.lib.table.Port;
38 import org.opendaylight.ovsdb.lib.table.internal.Table;
39 import org.opendaylight.ovsdb.lib.table.internal.Tables;
40 import org.opendaylight.ovsdb.plugin.ConnectionService;
41 import org.opendaylight.ovsdb.plugin.InventoryService;
42 import org.opendaylight.ovsdb.plugin.InventoryServiceInternal;
43
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49 import java.util.concurrent.ExecutionException;
50
51 public class OVSDBNettyFactoryTest implements OVSDB.Callback {
52     InventoryServiceInternal inventoryService;
53     private static String bridgeIdentifier = "br1";
54
55     @Test
56     public void testSome() throws InterruptedException, ExecutionException {
57         ConnectionService connectionService = new ConnectionService();
58         connectionService.init();
59         List<ChannelHandler> _handlers = Lists.newArrayList();
60         _handlers.add(new LoggingHandler(LogLevel.INFO));
61         _handlers.add(new JsonRpcDecoder(100000));
62         _handlers.add(new StringEncoder(CharsetUtil.UTF_8));
63         connectionService.setHandlers(_handlers);
64
65         inventoryService = new InventoryService();
66         Node.NodeIDType.registerIDType("OVS", String.class);
67         Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
68         params.put(ConnectionConstants.ADDRESS, "192.168.56.101");
69         params.put(ConnectionConstants.PORT, "6634");
70         Node node = connectionService.connect("TEST", params);
71         if (node == null) {
72             System.out.println("ERROR : Unable to connect to the host");
73             return;
74         }
75
76         ObjectMapper objectMapper = new ObjectMapper();
77         objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
78         Channel channel = connectionService.getConnection(node).getChannel();
79         JsonRpcEndpoint factory = new JsonRpcEndpoint(objectMapper, channel);
80         JsonRpcServiceBinderHandler binderHandler = new JsonRpcServiceBinderHandler(factory);
81         binderHandler.setNode(node);
82         channel.pipeline().addLast(binderHandler);
83
84         OVSDB ovsdb = factory.getClient(node, OVSDB.class);
85         ovsdb.registerCallback(this);
86
87         //GET DB-SCHEMA
88         List<String> dbNames = Arrays.asList(Open_vSwitch.NAME.getName());
89         ListenableFuture<DatabaseSchema> dbSchemaF = ovsdb.get_schema(dbNames);
90         DatabaseSchema databaseSchema = dbSchemaF.get();
91         System.out.println(databaseSchema);
92
93         //TEST MONITOR
94         MonitorRequestBuilder monitorReq = new MonitorRequestBuilder();
95         for (Table<?> table : Tables.getTables()) {
96             monitorReq.monitor(table);
97         }
98
99         ListenableFuture<TableUpdates> monResponse = ovsdb.monitor(monitorReq);
100         System.out.println("Monitor Request sent :");
101         TableUpdates updates = monResponse.get();
102         inventoryService.processTableUpdates(node, updates);
103         inventoryService.printCache(node);
104
105         // TRANSACT INSERT TEST
106
107         Map<String, Table<?>> ovsTable = inventoryService.getTableCache(node, Open_vSwitch.NAME.getName());
108         String newBridge = "new_bridge";
109         String newInterface = "new_interface";
110         String newPort = "new_port";
111         String newSwitch = "new_switch";
112
113         Operation addSwitchRequest = null;
114
115         if(ovsTable != null){
116             String ovsTableUUID = (String) ovsTable.keySet().toArray()[0];
117             UUID bridgeUuidPair = new UUID(newBridge);
118             Mutation bm = new Mutation("bridges", Mutator.INSERT, bridgeUuidPair);
119             List<Mutation> mutations = new ArrayList<Mutation>();
120             mutations.add(bm);
121
122             UUID uuid = new UUID(ovsTableUUID);
123             Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
124             List<Condition> where = new ArrayList<Condition>();
125             where.add(condition);
126             addSwitchRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
127         }
128         else{
129             Map<String, Object> vswitchRow = new HashMap<String, Object>();
130             UUID bridgeUuidPair = new UUID(newBridge);
131             vswitchRow.put("bridges", bridgeUuidPair);
132             addSwitchRequest = new InsertOperation(Open_vSwitch.NAME.getName(), newSwitch, vswitchRow);
133         }
134
135         Map<String, Object> bridgeRow = new HashMap<String, Object>();
136         bridgeRow.put("name", bridgeIdentifier);
137         UUID ports = new UUID(newPort);
138         bridgeRow.put("ports", ports);
139         InsertOperation addBridgeRequest = new InsertOperation(Bridge.NAME.getName(), newBridge, bridgeRow);
140
141         Map<String, Object> portRow = new HashMap<String, Object>();
142         portRow.put("name", bridgeIdentifier);
143         UUID interfaces = new UUID(newInterface);
144         portRow.put("interfaces", interfaces);
145         InsertOperation addPortRequest = new InsertOperation(Port.NAME.getName(), newPort, portRow);
146
147         Map<String, Object> interfaceRow = new HashMap<String, Object>();
148         interfaceRow.put("name", bridgeIdentifier);
149         interfaceRow.put("type", "internal");
150         InsertOperation addIntfRequest = new InsertOperation(Interface.NAME.getName(), newInterface, interfaceRow);
151
152         TransactBuilder transaction = new TransactBuilder();
153         transaction.addOperations(new ArrayList<Operation>(
154                                   Arrays.asList(addSwitchRequest, addIntfRequest, addPortRequest, addBridgeRequest)));
155
156         ListenableFuture<List<OperationResult>> transResponse = ovsdb.transact(transaction);
157         System.out.println("Transcation sent :");
158         List<OperationResult> tr = transResponse.get();
159         System.out.println("Transaction response : "+transResponse.toString());
160         List<Operation> requests = transaction.getRequests();
161         for (int i = 0; i < tr.size() ; i++) {
162             if (i < requests.size()) requests.get(i).setResult(tr.get(i));
163         }
164
165         System.out.println("Request + Response : "+requests.toString());
166         if (tr.size() > requests.size()) {
167             System.out.println("ERROR : "+tr.get(tr.size()-1).getError());
168             System.out.println("Details : "+tr.get(tr.size()-1).getDetails());
169         }
170
171         // TEST ECHO
172
173         ListenableFuture<List<String>> some = ovsdb.echo();
174         Object s = some.get();
175         System.out.printf("Result of echo is %s \n", s);
176
177         // TEST ECHO REQUEST/REPLY
178         Thread.sleep(10000);
179
180         connectionService.disconnect(node);
181     }
182
183     @Override
184     public void update(Node node, UpdateNotification updateNotification) {
185         inventoryService.processTableUpdates(node, updateNotification.getUpdate());
186         inventoryService.printCache(node);
187     }
188
189     @Override
190     public void locked(Node node, List<String> ids) {
191         // TODO Auto-generated method stub
192     }
193
194     @Override
195     public void stolen(Node node, List<String> ids) {
196         // TODO Auto-generated method stub
197     }
198 }