Add license info to new files
[ovsdb.git] / ovsdb / src / main / java / org / opendaylight / ovsdb / OpenVswitch.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;
11
12 import com.fasterxml.jackson.annotation.JsonIgnore;
13 import com.fasterxml.jackson.databind.JsonNode;
14 import com.google.common.collect.Lists;
15 import com.google.common.collect.Maps;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import org.opendaylight.ovsdb.lib.message.OvsdbRPC;
18 import org.opendaylight.ovsdb.lib.message.TransactBuilder;
19 import org.opendaylight.ovsdb.lib.message.operations.ConditionalOperation;
20 import org.opendaylight.ovsdb.lib.message.operations.Operation;
21 import org.opendaylight.ovsdb.lib.message.operations.OperationResult;
22 import org.opendaylight.ovsdb.lib.meta.ColumnSchema;
23 import org.opendaylight.ovsdb.lib.meta.DatabaseSchema;
24 import org.opendaylight.ovsdb.lib.meta.TableSchema;
25 import org.opendaylight.ovsdb.lib.notation.Condition;
26 import org.opendaylight.ovsdb.lib.notation.Function;
27
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Queue;
32 import java.util.concurrent.ExecutorService;
33
34
35 public class OpenVswitch {
36
37     ExecutorService executorService;
38     String schemaName;
39     OvsdbRPC rpc;
40     volatile DatabaseSchema schema;
41     Queue<Throwable> exceptions;
42
43     public OpenVswitch(OvsdbRPC rpc, ExecutorService executorService) {
44         this.rpc = rpc;
45         this.executorService = executorService;
46     }
47
48     public OpenVswitch() {
49     }
50
51
52     public void populateSchemaFromDevice() {
53         final ListenableFuture<JsonNode> fOfSchema = rpc.get_schema(Lists.newArrayList(DatabaseSchema.OPEN_VSWITCH_SCHEMA_NAME));
54         fOfSchema.addListener(new Runnable() {
55             @Override
56             public void run() {
57                 try {
58                     JsonNode jsonNode = fOfSchema.get();
59                     schema =  DatabaseSchema.fromJson(jsonNode);
60
61                 } catch (Exception e) {
62                     exceptions.add(e);
63                 }
64             }
65         }, executorService);
66     }
67
68     public Transaction transact(){
69         return new Transaction(this);
70     }
71
72     public ListenableFuture<List<OperationResult>> transact(List<Operation> operations) {
73
74         //todo, we may not need transactionbuilder if we can have JSON objects
75         TransactBuilder builder = new TransactBuilder();
76         for (Operation o : operations) {
77            builder.addOperation(o);
78         }
79
80         ListenableFuture<List<OperationResult>> transact = rpc.transact(builder);
81         return transact;
82     }
83
84     public boolean isReady(long timeout) {
85         //todo implement timeout
86         return null != schema;
87     }
88
89     public DatabaseSchema schema() {
90         return schema;
91     }
92
93
94     public static class Transaction {
95
96         private  DatabaseSchema eDatabaseSchema;
97         OpenVswitch ovs;
98         ArrayList<Operation> operations = Lists.newArrayList();
99
100         public Transaction(OpenVswitch ovs) {
101             this.ovs = ovs;
102         }
103
104         public Transaction(DatabaseSchema eDatabaseSchema) {
105             this.eDatabaseSchema = eDatabaseSchema;
106         }
107
108         public Transaction add(Operation operation) {
109             operations.add(operation);
110             return this;
111         }
112
113         public List<Operation> build() {
114             return operations;
115         }
116
117         public ListenableFuture<List<OperationResult>> execute() {
118             return ovs.transact(operations);
119         }
120     }
121
122     public static class Update<E extends TableSchema<E>> extends Operation<E> implements ConditionalOperation {
123
124         Map<String, Object> row = Maps.newHashMap();
125         String uuid;
126         //Where where;
127         List<Condition> where = Lists.newArrayList();
128
129         private String uuidName;
130
131         public Update(TableSchema<E> schema) {
132             super(schema, "update");
133         }
134
135         public Update<E> on(TableSchema schema){
136             return this;
137         }
138
139         public <T extends TableSchema<T>, D> Update<E> set(ColumnSchema<T, D> columnSchema, D value) {
140             columnSchema.validate(value);
141             this.row.put(columnSchema.getName(), value);
142             return this;
143         }
144
145         public Where where(Condition condition) {
146             return new Where(this);
147         }
148
149         public String getUuid() {
150             return uuid;
151         }
152
153         public void setUuid(String uuid) {
154             this.uuid = uuid;
155         }
156
157         public String getUuidName() {
158             return uuidName;
159         }
160
161         public void setUuidName(String uuidName) {
162             this.uuidName = uuidName;
163         }
164
165         public Map<String, Object> getRow() {
166             return row;
167         }
168
169         public void setRow(Map<String, Object> row) {
170             this.row = row;
171         }
172
173         @Override
174         public void addCondition(Condition condition) {
175             this.where.add(condition);
176         }
177
178         public List<Condition> getWhere() {
179             return where;
180         }
181
182         public void setWhere(List<Condition> where) {
183             this.where = where;
184         }
185     }
186
187
188     public static class Where {
189
190         @JsonIgnore
191         ConditionalOperation operation;
192
193         public Where() { }  public Where(ConditionalOperation operation) {
194             this.operation = operation;
195         }
196
197         public Where condition(Condition condition) {
198             operation.addCondition(condition);
199             return this;
200         }
201
202         public Where condition(ColumnSchema column, Function function, Object value) {
203             this.condition(new Condition(column.getName(), function, value));
204             return this;
205         }
206
207         public Where and(ColumnSchema column, Function function, Object value) {
208             condition(column, function, value);
209             return this;
210         }
211
212         public Where and(Condition condition) {
213            condition(condition);
214             return this;
215         }
216
217         public Operation operation() {
218             return (Operation) this.operation;
219         }
220
221     }
222
223
224     public static class Operations {
225         public static Operations op = new Operations();
226
227         public <E extends TableSchema<E>> Insert<E> insert(TableSchema<E> schema) {
228             return new Insert<>(schema);
229         }
230
231         public  <E extends TableSchema<E>> Update<E> update(TableSchema<E> schema) {
232             return new Update<>(schema);
233         }
234     }
235
236     public ExecutorService getExecutorService() {
237         return executorService;
238     }
239
240     public void setExecutorService(ExecutorService executorService) {
241         this.executorService = executorService;
242     }
243
244     public String getSchemaName() {
245         return schemaName;
246     }
247
248     public void setSchemaName(String schemaName) {
249         this.schemaName = schemaName;
250     }
251
252     public OvsdbRPC getRpc() {
253         return rpc;
254     }
255
256     public void setRpc(OvsdbRPC rpc) {
257         this.rpc = rpc;
258     }
259
260     public DatabaseSchema getSchema() {
261         return schema;
262     }
263
264     public void setSchema(DatabaseSchema schema) {
265         this.schema = schema;
266     }
267
268     public Queue<Throwable> getExceptions() {
269         return exceptions;
270     }
271
272     public void setExceptions(Queue<Throwable> exceptions) {
273         this.exceptions = exceptions;
274     }
275
276
277 }