d52ae593211d2190789462a633812ba0893fd826
[ovsdb.git] / library / src / test / java / org / opendaylight / ovsdb / lib / OvsDBClientTestITTyped.java
1 /*
2  * Copyright (C) 2014 EBay Software Foundation
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 : Ashwin Raveendran
9  */
10 package org.opendaylight.ovsdb.lib;
11
12 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
13
14 import java.io.IOException;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.ExecutorService;
19 import java.util.concurrent.Executors;
20
21 import junit.framework.Assert;
22
23 import org.junit.Test;
24 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
25 import org.opendaylight.ovsdb.lib.message.UpdateNotification;
26 import org.opendaylight.ovsdb.lib.operations.OperationResult;
27 import org.opendaylight.ovsdb.lib.schema.ColumnSchema;
28 import org.opendaylight.ovsdb.lib.schema.TableSchema;
29 import org.opendaylight.ovsdb.lib.schema.temp.Reference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.google.common.util.concurrent.ListenableFuture;
34
35 public class OvsDBClientTestITTyped extends OvsdbTestBase {
36
37     Logger logger = LoggerFactory.getLogger(OvsDBClientTestITTyped.class);
38
39
40     static class Bridge extends TableSchema<Bridge> {
41
42         Bridge(String name, Map<String, ColumnSchema> columns) {
43             super(name, columns);
44         }
45
46         public ColumnSchema<Bridge, String> name() {
47             return column("name", String.class);
48         }
49
50         public ColumnSchema<Bridge, Integer> floodVlans() {
51             return column("flood_vlans", Integer.class);
52         }
53
54         public ColumnSchema<Bridge, String> status() {
55             return column("status", String.class);
56         }
57
58         public ColumnSchema<Bridge, Reference> netflow() {
59             return column("netflow", Reference.class);
60         }
61     }
62
63
64     @Test
65     public void test() throws IOException, InterruptedException, ExecutionException {
66         OvsDBClientImpl ovs = getVswitch();
67
68         Bridge bridge = ovs.getSchema(OvsDBClient.OPEN_VSWITCH_SCHEMA, true).get().table("Bridge", Bridge.class);
69
70         ListenableFuture<List<OperationResult>> results = ovs.transactBuilder()
71                 .add(op.insert(bridge).value(bridge.name(), "br-int"))
72                 .add(op.update(bridge)
73                         .set(bridge.status(), "br-blah")
74                         .set(bridge.floodVlans(), 34)
75                         .where(bridge.name().opEqual("br-int"))
76                         .and(bridge.name().opEqual("br-int")).operation())
77                 .execute();
78
79         List<OperationResult> operationResults = results.get();
80         Assert.assertFalse(operationResults.isEmpty());
81         System.out.println("operationResults = " + operationResults);
82     }
83
84     private OvsDBClientImpl getVswitch() throws IOException, InterruptedException {
85         OvsdbRPC rpc = getTestConnection();
86         if (rpc == null) {
87             System.out.println("Unable to Establish Test Connection");
88         }
89
90         ExecutorService executorService = Executors.newFixedThreadPool(3);
91         OvsDBClientImpl ovs = new OvsDBClientImpl(rpc, executorService);
92
93         for (int i = 0; i < 100; i++) {
94            if (ovs.isReady(0)) {
95               break;
96            }
97            Thread.sleep(1000);
98         }
99         return ovs;
100     }
101
102
103     @Override
104     public void update(Object node, UpdateNotification upadateNotification) {
105         // TODO Auto-generated method stub
106
107     }
108     @Override
109     public void locked(Object node, List<String> ids) {
110         // TODO Auto-generated method stub
111
112     }
113     @Override
114     public void stolen(Object node, List<String> ids) {
115         // TODO Auto-generated method stub
116
117     }
118
119 }