Merge "Use a utility function for key-value to map conversions"
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepConnectionInstance.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
10
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.concurrent.ExecutionException;
16
17 import javax.annotation.Nonnull;
18
19 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactCommand;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvoker;
23 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvokerImpl;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
25 import org.opendaylight.ovsdb.lib.EchoServiceCallbackFilters;
26 import org.opendaylight.ovsdb.lib.LockAquisitionCallback;
27 import org.opendaylight.ovsdb.lib.LockStolenCallback;
28 import org.opendaylight.ovsdb.lib.MonitorCallBack;
29 import org.opendaylight.ovsdb.lib.MonitorHandle;
30 import org.opendaylight.ovsdb.lib.OvsdbClient;
31 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
32 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
33 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
34 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
35 import org.opendaylight.ovsdb.lib.message.TableUpdates;
36 import org.opendaylight.ovsdb.lib.notation.Row;
37 import org.opendaylight.ovsdb.lib.operations.Operation;
38 import org.opendaylight.ovsdb.lib.operations.OperationResult;
39 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
40 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
41 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
42 import org.opendaylight.ovsdb.lib.schema.TableSchema;
43 import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 import com.google.common.collect.Lists;
54 import com.google.common.util.concurrent.ListenableFuture;
55
56 public class HwvtepConnectionInstance implements OvsdbClient{
57     private static final Logger LOG = LoggerFactory.getLogger(HwvtepConnectionInstance.class);
58     private ConnectionInfo connectionInfo;
59     private OvsdbClient client;
60     private InstanceIdentifier<Node> instanceIdentifier;
61     private TransactionInvoker txInvoker;
62     private Map<DatabaseSchema,TransactInvoker> transactInvokers;
63     private MonitorCallBack callback;
64     private volatile boolean hasDeviceOwnership = false;
65     private Entity connectedEntity;
66     private EntityOwnershipCandidateRegistration deviceOwnershipCandidateRegistration;
67     private HwvtepGlobalAugmentation initialCreatedData = null;
68
69
70     HwvtepConnectionInstance (ConnectionInfo key,OvsdbClient client,
71                     InstanceIdentifier<Node> iid, TransactionInvoker txInvoker) {
72         this.connectionInfo = key;
73         this.client = client;
74         this.instanceIdentifier = iid;
75         this.txInvoker = txInvoker;
76     }
77
78     public void transact(TransactCommand command) {
79         for (TransactInvoker transactInvoker: transactInvokers.values()) {
80             transactInvoker.invoke(command);
81         }
82     }
83
84     public void registerCallbacks() {
85         if ( this.callback == null) {
86             if(this.initialCreatedData != null) {
87                 this.updateConnectionAttributes();
88             }
89
90             try {
91                 String database = HwvtepSchemaConstants.HARDWARE_VTEP;
92                 DatabaseSchema dbSchema = getSchema(database).get();
93                 if (dbSchema != null) {
94                     LOG.info("Monitoring database: {}", database);
95                     callback = new HwvtepMonitorCallback(this, txInvoker);
96                     monitorAllTables(database, dbSchema);
97                 } else {
98                     LOG.info("No database {} found on {}", database, connectionInfo);
99                 }
100             } catch (InterruptedException | ExecutionException e) {
101                 LOG.warn("Exception attempting to registerCallbacks {}: ", connectionInfo, e);
102             }
103         }
104     }
105
106     public void createTransactInvokers() {
107         if (transactInvokers == null) {
108             try {
109                 transactInvokers = new HashMap<>();
110                 DatabaseSchema dbSchema = getSchema(HwvtepSchemaConstants.HARDWARE_VTEP).get();
111                 if(dbSchema != null) {
112                     transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
113                 }
114             } catch (InterruptedException | ExecutionException e) {
115                 LOG.warn("Exception attempting to createTransactionInvokers {}", connectionInfo, e);
116             }
117         }
118     }
119
120     private void monitorAllTables(String database, DatabaseSchema dbSchema) {
121         Set<String> tables = dbSchema.getTables();
122         if (tables != null) {
123             List<MonitorRequest> monitorRequests = Lists.newArrayList();
124             for (String tableName : tables) {
125                 LOG.debug("HwvtepSouthbound monitoring table {} in {}", tableName, dbSchema.getName());
126                 GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
127                 Set<String> columns = tableSchema.getColumns();
128                 MonitorRequestBuilder<GenericTableSchema> monitorBuilder = MonitorRequestBuilder.builder(tableSchema);
129                 for (String column : columns) {
130                     monitorBuilder.addColumn(column);
131                 }
132                 monitorRequests.add(monitorBuilder.with(new MonitorSelect(true, true, true, true)).build());
133             }
134             this.callback.update(monitor(dbSchema, monitorRequests, callback),dbSchema);
135         } else {
136             LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
137         }
138     }
139
140     private void updateConnectionAttributes() {
141         LOG.debug("Update attributes of ovsdb node ip: {} port: {}",
142                     this.initialCreatedData.getConnectionInfo().getRemoteIp(),
143                     this.initialCreatedData.getConnectionInfo().getRemotePort());
144         /*
145          * TODO: Do we have anything to update?
146          * Hwvtep doesn't have other_config or external_ids like
147          * Open_vSwitch. What else will be needed?
148          */
149     }
150
151     public ListenableFuture<List<String>> getDatabases() {
152         return client.getDatabases();
153     }
154
155     public ListenableFuture<DatabaseSchema> getSchema(String database) {
156         return client.getSchema(database);
157     }
158
159     public TransactionBuilder transactBuilder(DatabaseSchema dbSchema) {
160         return client.transactBuilder(dbSchema);
161     }
162
163     public ListenableFuture<List<OperationResult>> transact(DatabaseSchema dbSchema, List<Operation> operations) {
164         return client.transact(dbSchema, operations);
165     }
166
167     public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
168                     List<MonitorRequest> monitorRequests, MonitorCallBack callback) {
169         return client.monitor(schema, monitorRequests, callback);
170     }
171
172     @Override
173     public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
174                     List<MonitorRequest> monitorRequests, MonitorHandle monitorHandle, MonitorCallBack callback) {
175         return null;
176     }
177
178     public void cancelMonitor(MonitorHandle handler) {
179         client.cancelMonitor(handler);
180     }
181
182     public void lock(String lockId, LockAquisitionCallback lockedCallBack, LockStolenCallback stolenCallback) {
183         client.lock(lockId, lockedCallBack, stolenCallback);
184     }
185
186     public ListenableFuture<Boolean> steal(String lockId) {
187         return client.steal(lockId);
188     }
189
190     public ListenableFuture<Boolean> unLock(String lockId) {
191         return client.unLock(lockId);
192     }
193
194     public void startEchoService(EchoServiceCallbackFilters callbackFilters) {
195         client.startEchoService(callbackFilters);
196     }
197
198     public void stopEchoService() {
199         client.stopEchoService();
200     }
201
202     public OvsdbConnectionInfo getConnectionInfo() {
203         return client.getConnectionInfo();
204     }
205
206     public boolean isActive() {
207         return client.isActive();
208     }
209
210     public void disconnect() {
211         client.disconnect();
212     }
213
214     public DatabaseSchema getDatabaseSchema(String dbName) {
215         return client.getDatabaseSchema(dbName);
216     }
217
218     public <T extends TypedBaseTable<?>> T createTypedRowWrapper(Class<T> klazz) {
219         return client.createTypedRowWrapper(klazz);
220     }
221
222     public <T extends TypedBaseTable<?>> T createTypedRowWrapper(DatabaseSchema dbSchema, Class<T> klazz) {
223         return client.createTypedRowWrapper(dbSchema, klazz);
224     }
225
226     public <T extends TypedBaseTable<?>> T getTypedRowWrapper(Class<T> klazz, Row<GenericTableSchema> row) {
227         return client.getTypedRowWrapper(klazz, row);
228     }
229
230     public ConnectionInfo getMDConnectionInfo() {
231         return connectionInfo;
232     }
233
234     public void setMDConnectionInfo(ConnectionInfo key) {
235         this.connectionInfo = key;
236     }
237
238     public InstanceIdentifier<Node> getInstanceIdentifier() {
239         return instanceIdentifier;
240     }
241
242     public NodeKey getNodeKey() {
243         //TODO: What is the alternative here?
244         return getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
245     }
246
247     public NodeId getNodeId() {
248         return getNodeKey().getNodeId();
249     }
250
251     public void setInstanceIdentifier(InstanceIdentifier<Node> iid) {
252         this.instanceIdentifier = iid;
253     }
254
255     public Entity getConnectedEntity() {
256         return this.connectedEntity;
257     }
258
259     public void setConnectedEntity(Entity entity ) {
260         this.connectedEntity = entity;
261     }
262
263     public Boolean hasOvsdbClient(OvsdbClient otherClient) {
264         return client.equals(otherClient);
265     }
266
267     public Boolean getHasDeviceOwnership() {
268         return hasDeviceOwnership;
269     }
270
271     public void setHasDeviceOwnership(Boolean hasDeviceOwnership) {
272         if (hasDeviceOwnership != null) {
273             this.hasDeviceOwnership = hasDeviceOwnership;
274         }
275     }
276
277     public void setDeviceOwnershipCandidateRegistration(@Nonnull EntityOwnershipCandidateRegistration registration) {
278         this.deviceOwnershipCandidateRegistration = registration;
279     }
280
281     public void closeDeviceOwnershipCandidateRegistration() {
282         if (deviceOwnershipCandidateRegistration != null) {
283             this.deviceOwnershipCandidateRegistration.close();
284             setHasDeviceOwnership(Boolean.FALSE);
285         }
286     }
287
288     public void setHwvtepGlobalAugmentation(HwvtepGlobalAugmentation hwvtepGlobalData) {
289         this.initialCreatedData = hwvtepGlobalData;
290     }
291 }