Enhance the use of cache in HwvtepDeviceInfo
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepConnectionInstance.java
1 /*
2  * Copyright (c) 2015, 2017 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 package org.opendaylight.ovsdb.hwvtepsouthbound;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import com.google.common.util.concurrent.SettableFuture;
16 import com.google.common.util.concurrent.ThreadFactoryBuilder;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.concurrent.ExecutionException;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.ScheduledExecutorService;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicBoolean;
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.mdsal.eos.binding.api.Entity;
31 import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
32 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactCommand;
33 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvoker;
34 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvokerImpl;
35 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
36 import org.opendaylight.ovsdb.lib.LockAquisitionCallback;
37 import org.opendaylight.ovsdb.lib.LockStolenCallback;
38 import org.opendaylight.ovsdb.lib.MonitorCallBack;
39 import org.opendaylight.ovsdb.lib.MonitorHandle;
40 import org.opendaylight.ovsdb.lib.OvsdbClient;
41 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
42 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
43 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
44 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
45 import org.opendaylight.ovsdb.lib.message.TableUpdates;
46 import org.opendaylight.ovsdb.lib.notation.Row;
47 import org.opendaylight.ovsdb.lib.operations.Operation;
48 import org.opendaylight.ovsdb.lib.operations.OperationResult;
49 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
50 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
51 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
52 import org.opendaylight.ovsdb.lib.schema.TableSchema;
53 import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
54 import org.opendaylight.ovsdb.lib.schema.typed.TypedDatabaseSchema;
55 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionHistory;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
59 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
60 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
61 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64
65 public class HwvtepConnectionInstance {
66     private static final Logger LOG = LoggerFactory.getLogger(HwvtepConnectionInstance.class);
67     private ConnectionInfo connectionInfo;
68     private final OvsdbClient client;
69     private final HwvtepTableReader hwvtepTableReader;
70     private InstanceIdentifier<Node> instanceIdentifier;
71     private final TransactionInvoker txInvoker;
72     private Map<TypedDatabaseSchema, TransactInvoker> transactInvokers;
73     private MonitorCallBack callback;
74     private volatile boolean hasDeviceOwnership = false;
75     private Entity connectedEntity;
76     private EntityOwnershipCandidateRegistration deviceOwnershipCandidateRegistration;
77     private HwvtepGlobalAugmentation initialCreatedData = null;
78     private final HwvtepDeviceInfo deviceInfo;
79     private final DataBroker dataBroker;
80     private final HwvtepConnectionManager hwvtepConnectionManager;
81     private static final ScheduledExecutorService SCHEDULED_EXECUTOR_SERVICE = Executors
82         .newSingleThreadScheduledExecutor(new ThreadFactoryBuilder()
83         .setNameFormat("HwvtepReconciliationFT-%d").build());
84     @VisibleForTesting
85     final SettableFuture<Boolean> reconciliationFt = SettableFuture.create();
86     @VisibleForTesting
87     final AtomicBoolean firstUpdateTriggered = new AtomicBoolean(false);
88     private TransactionHistory controllerTxHistory;
89     private TransactionHistory deviceUpdateHistory;
90
91     HwvtepConnectionInstance(final HwvtepConnectionManager hwvtepConnectionManager, final ConnectionInfo key,
92             final OvsdbClient client, final InstanceIdentifier<Node> iid, final TransactionInvoker txInvoker,
93             final DataBroker dataBroker) {
94         this.hwvtepConnectionManager = hwvtepConnectionManager;
95         this.connectionInfo = key;
96         this.client = client;
97         this.instanceIdentifier = iid;
98         this.txInvoker = txInvoker;
99         this.deviceInfo = new HwvtepDeviceInfo(this);
100         this.dataBroker = dataBroker;
101         this.hwvtepTableReader = new HwvtepTableReader(this);
102     }
103
104     public void transact(final TransactCommand command) {
105         String nodeId = getNodeId().getValue();
106         boolean firstUpdate = firstUpdateTriggered.compareAndSet(false, true);
107         if (reconciliationFt.isDone()) {
108             transact(command, false);
109         } else {
110             LOG.info("Job waiting for reconciliation {}", nodeId);
111             Futures.addCallback(reconciliationFt, new FutureCallback<Boolean>() {
112                 @Override
113                 public void onSuccess(final Boolean notUsed) {
114                     LOG.info("Running the job waiting for reconciliation {}", nodeId);
115                     transact(command, false);
116                 }
117
118                 @Override
119                 public void onFailure(final Throwable throwable) {
120                     LOG.info("Running the job waiting for reconciliation {}", nodeId);
121                     transact(command, false);
122                 }
123             }, MoreExecutors.directExecutor());
124
125             if (firstUpdate) {
126                 LOG.info("Scheduling the reconciliation timeout task {}", nodeId);
127                 SCHEDULED_EXECUTOR_SERVICE.schedule(() -> reconciliationFt.set(Boolean.TRUE),
128                         HwvtepSouthboundConstants.CONFIG_NODE_UPDATE_MAX_DELAY_MS, TimeUnit.MILLISECONDS);
129             }
130         }
131     }
132
133     public synchronized void transact(final TransactCommand command, final boolean reconcile) {
134         try {
135             for (TransactInvoker transactInvoker : transactInvokers.values()) {
136                 transactInvoker.invoke(command);
137             }
138         } finally {
139             if (reconcile) {
140                 reconciliationFt.set(Boolean.TRUE);
141             }
142         }
143     }
144
145     public ListenableFuture<List<OperationResult>> transact(final DatabaseSchema dbSchema,
146             final List<Operation> operations) {
147         return client.transact(dbSchema, operations);
148     }
149
150     public void registerCallbacks() {
151         if (this.callback == null) {
152             if (this.initialCreatedData != null) {
153                 this.updateConnectionAttributes();
154             }
155
156             try {
157                 String database = HwvtepSchemaConstants.HARDWARE_VTEP;
158                 DatabaseSchema dbSchema = getSchema(database).get();
159                 if (dbSchema != null) {
160                     LOG.info("Monitoring database: {}", database);
161                     callback = new HwvtepMonitorCallback(this, txInvoker);
162                     monitorAllTables(database, dbSchema);
163                 } else {
164                     LOG.info("No database {} found on {}", database, connectionInfo);
165                 }
166             } catch (InterruptedException | ExecutionException e) {
167                 LOG.warn("Exception attempting to registerCallbacks {}: ", connectionInfo, e);
168             }
169         }
170     }
171
172     public void createTransactInvokers() {
173         if (transactInvokers == null) {
174             try {
175                 transactInvokers = new HashMap<>();
176                 TypedDatabaseSchema dbSchema = getSchema(HwvtepSchemaConstants.HARDWARE_VTEP).get();
177                 if (dbSchema != null) {
178                     transactInvokers.put(dbSchema, new TransactInvokerImpl(this, dbSchema));
179                 }
180             } catch (InterruptedException | ExecutionException e) {
181                 LOG.warn("Exception attempting to createTransactionInvokers {}", connectionInfo, e);
182             }
183         }
184     }
185
186     private void monitorAllTables(final String database, final DatabaseSchema dbSchema) {
187         Set<String> tables = dbSchema.getTables();
188         if (tables != null) {
189             List<MonitorRequest> monitorRequests = new ArrayList<>();
190             for (String tableName : tables) {
191                 if (!HwvtepSouthboundConstants.SKIP_HWVTEP_TABLE.containsKey(tableName)) {
192                     LOG.info("HwvtepSouthbound monitoring Hwvtep schema table {}", tableName);
193                     GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
194                     final Set<String> columns = new HashSet<>(tableSchema.getColumns());
195                     List<String> skipColumns = HwvtepSouthboundConstants.SKIP_COLUMN_FROM_HWVTEP_TABLE.get(tableName);
196                     skipColumns = skipColumns == null ? new ArrayList<>() : new ArrayList<>(skipColumns);
197                     skipColumns.add(HwvtepSouthboundConstants.VERSION_COLUMN);
198
199                     LOG.info("HwvtepSouthbound NOT monitoring columns {} in table {}", skipColumns, tableName);
200                     columns.removeAll(skipColumns);
201
202                     monitorRequests.add(new MonitorRequestBuilder<>(tableSchema)
203                             .addColumns(columns)
204                             .with(new MonitorSelect(true, true, true, true)).build());
205                 }
206             }
207             this.callback.update(monitor(dbSchema, monitorRequests, callback), dbSchema);
208         } else {
209             LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
210         }
211     }
212
213     private void updateConnectionAttributes() {
214         LOG.debug("Update attributes of ovsdb node ip: {} port: {}",
215                     this.initialCreatedData.getConnectionInfo().getRemoteIp(),
216                     this.initialCreatedData.getConnectionInfo().getRemotePort());
217         /*
218          * TODO: Do we have anything to update?
219          * Hwvtep doesn't have other_config or external_ids like
220          * Open_vSwitch. What else will be needed?
221          */
222     }
223
224     public DataBroker getDataBroker() {
225         return dataBroker;
226     }
227
228     public ListenableFuture<List<String>> getDatabases() {
229         return client.getDatabases();
230     }
231
232     public ListenableFuture<TypedDatabaseSchema> getSchema(final String database) {
233         return client.getSchema(database);
234     }
235
236     public TransactionBuilder transactBuilder(final DatabaseSchema dbSchema) {
237         return client.transactBuilder(dbSchema);
238     }
239
240     public <E extends TableSchema<E>> TableUpdates monitor(final DatabaseSchema schema,
241                     final List<MonitorRequest> monitorRequests, final MonitorCallBack monitorCallBack) {
242         return client.monitor(schema, monitorRequests, monitorCallBack);
243     }
244
245     public <E extends TableSchema<E>> TableUpdates monitor(final DatabaseSchema schema,
246             final List<MonitorRequest> monitorRequests, final MonitorHandle monitorHandle,
247             final MonitorCallBack monitorCallBack) {
248         return null;
249     }
250
251     public void cancelMonitor(final MonitorHandle handler) {
252         client.cancelMonitor(handler);
253     }
254
255     public void lock(final String lockId, final LockAquisitionCallback lockedCallBack,
256             final LockStolenCallback stolenCallback) {
257         client.lock(lockId, lockedCallBack, stolenCallback);
258     }
259
260     public ListenableFuture<Boolean> steal(final String lockId) {
261         return client.steal(lockId);
262     }
263
264     public ListenableFuture<Boolean> unLock(final String lockId) {
265         return client.unLock(lockId);
266     }
267
268     public OvsdbConnectionInfo getConnectionInfo() {
269         return client.getConnectionInfo();
270     }
271
272     public boolean isActive() {
273         return client.isActive();
274     }
275
276     public void disconnect() {
277         client.disconnect();
278     }
279
280     public DatabaseSchema getDatabaseSchema(final String dbName) {
281         return client.getDatabaseSchema(dbName);
282     }
283
284     public <T extends TypedBaseTable<?>> T createTypedRowWrapper(final Class<T> klazz) {
285         return client.createTypedRowWrapper(klazz);
286     }
287
288     public <T extends TypedBaseTable<?>> T createTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz) {
289         return client.createTypedRowWrapper(dbSchema, klazz);
290     }
291
292     public <T extends TypedBaseTable<?>> T getTypedRowWrapper(final Class<T> klazz, final Row<GenericTableSchema> row) {
293         return client.getTypedRowWrapper(klazz, row);
294     }
295
296     public ConnectionInfo getMDConnectionInfo() {
297         return connectionInfo;
298     }
299
300     public void setMDConnectionInfo(final ConnectionInfo key) {
301         this.connectionInfo = key;
302     }
303
304     public InstanceIdentifier<Node> getInstanceIdentifier() {
305         return instanceIdentifier;
306     }
307
308     public NodeKey getNodeKey() {
309         //TODO: What is the alternative here?
310         return getInstanceIdentifier().firstKeyOf(Node.class);
311     }
312
313     public NodeId getNodeId() {
314         return getNodeKey().getNodeId();
315     }
316
317     public void setInstanceIdentifier(final InstanceIdentifier<Node> iid) {
318         this.instanceIdentifier = iid;
319         hwvtepConnectionManager.putConnectionInstance(instanceIdentifier, this);
320     }
321
322     public Entity getConnectedEntity() {
323         return this.connectedEntity;
324     }
325
326     public void setConnectedEntity(final Entity entity) {
327         this.connectedEntity = entity;
328     }
329
330     public Boolean hasOvsdbClient(final OvsdbClient otherClient) {
331         return client.equals(otherClient);
332     }
333
334     public Boolean getHasDeviceOwnership() {
335         return hasDeviceOwnership;
336     }
337
338     public void setHasDeviceOwnership(final Boolean hasDeviceOwnership) {
339         if (hasDeviceOwnership != null) {
340             if (hasDeviceOwnership != this.hasDeviceOwnership) {
341                 LOG.info("Ownership status changed for {} old {} new {}", instanceIdentifier,
342                         this.hasDeviceOwnership, hasDeviceOwnership);
343             }
344             this.hasDeviceOwnership = hasDeviceOwnership;
345         }
346     }
347
348     public void setDeviceOwnershipCandidateRegistration(
349             @NonNull final EntityOwnershipCandidateRegistration registration) {
350         this.deviceOwnershipCandidateRegistration = registration;
351     }
352
353     public void closeDeviceOwnershipCandidateRegistration() {
354         if (deviceOwnershipCandidateRegistration != null) {
355             this.deviceOwnershipCandidateRegistration.close();
356             setHasDeviceOwnership(Boolean.FALSE);
357         }
358     }
359
360     public void setHwvtepGlobalAugmentation(final HwvtepGlobalAugmentation hwvtepGlobalData) {
361         this.initialCreatedData = hwvtepGlobalData;
362     }
363
364     public HwvtepGlobalAugmentation getHwvtepGlobalAugmentation() {
365         return this.initialCreatedData;
366     }
367
368     public HwvtepDeviceInfo getDeviceInfo() {
369         return this.deviceInfo;
370     }
371
372     public OvsdbClient getOvsdbClient() {
373         return client;
374     }
375
376     public HwvtepTableReader getHwvtepTableReader() {
377         return hwvtepTableReader;
378     }
379
380     public void refreshOperNode() throws ExecutionException, InterruptedException {
381         TableUpdates tableUpdates = hwvtepTableReader.readAllTables();
382         callback.update(tableUpdates, getDatabaseSchema(HwvtepSchemaConstants.HARDWARE_VTEP));
383     }
384
385     public MonitorCallBack getCallback() {
386         return callback;
387     }
388
389     public void setCallback(final MonitorCallBack callback) {
390         this.callback = callback;
391     }
392
393     public TransactionHistory getControllerTxHistory() {
394         return controllerTxHistory;
395     }
396
397     public void setControllerTxHistory(final TransactionHistory controllerTxLog) {
398         deviceInfo.setControllerTxHistory(controllerTxLog);
399         this.controllerTxHistory = controllerTxLog;
400     }
401
402     public TransactionHistory getDeviceUpdateHistory() {
403         return deviceUpdateHistory;
404     }
405
406     public void setDeviceUpdateHistory(final TransactionHistory deviceUpdateLog) {
407         deviceInfo.setDeviceUpdateHistory(deviceUpdateLog);
408         this.deviceUpdateHistory = deviceUpdateLog;
409     }
410 }