2 * Copyright (c) 2015, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.ovsdb.hwvtepsouthbound;
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;
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.mdsal.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;
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());
85 final SettableFuture<Boolean> reconciliationFt = SettableFuture.create();
87 final AtomicBoolean firstUpdateTriggered = new AtomicBoolean(false);
88 private TransactionHistory controllerTxHistory;
89 private TransactionHistory deviceUpdateHistory;
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;
97 this.instanceIdentifier = iid;
98 this.txInvoker = txInvoker;
99 this.deviceInfo = new HwvtepDeviceInfo(this);
100 this.dataBroker = dataBroker;
101 this.hwvtepTableReader = new HwvtepTableReader(this);
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);
110 LOG.info("Job waiting for reconciliation {}", nodeId);
111 Futures.addCallback(reconciliationFt, new FutureCallback<Boolean>() {
113 public void onSuccess(final Boolean notUsed) {
114 LOG.info("Running the job waiting for reconciliation {}", nodeId);
115 transact(command, false);
119 public void onFailure(final Throwable throwable) {
120 LOG.info("Running the job waiting for reconciliation {}", nodeId);
121 transact(command, false);
123 }, MoreExecutors.directExecutor());
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);
133 public synchronized void transact(final TransactCommand command, final boolean reconcile) {
135 for (TransactInvoker transactInvoker : transactInvokers.values()) {
136 transactInvoker.invoke(command);
140 reconciliationFt.set(Boolean.TRUE);
145 public ListenableFuture<List<OperationResult>> transact(final DatabaseSchema dbSchema,
146 final List<Operation> operations) {
147 return client.transact(dbSchema, operations);
150 public void registerCallbacks() {
151 if (this.callback == null) {
152 if (this.initialCreatedData != null) {
153 this.updateConnectionAttributes();
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);
164 LOG.info("No database {} found on {}", database, connectionInfo);
166 } catch (InterruptedException | ExecutionException e) {
167 LOG.warn("Exception attempting to registerCallbacks {}: ", connectionInfo, e);
172 public void createTransactInvokers() {
173 if (transactInvokers == null) {
175 transactInvokers = new HashMap<>();
176 TypedDatabaseSchema dbSchema = getSchema(HwvtepSchemaConstants.HARDWARE_VTEP).get();
177 if (dbSchema != null) {
178 transactInvokers.put(dbSchema, new TransactInvokerImpl(this, dbSchema));
180 } catch (InterruptedException | ExecutionException e) {
181 LOG.warn("Exception attempting to createTransactionInvokers {}", connectionInfo, e);
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);
199 LOG.info("HwvtepSouthbound NOT monitoring columns {} in table {}", skipColumns, tableName);
200 columns.removeAll(skipColumns);
202 monitorRequests.add(new MonitorRequestBuilder<>(tableSchema)
204 .with(new MonitorSelect(true, true, true, true)).build());
207 this.callback.update(monitor(dbSchema, monitorRequests, callback), dbSchema);
209 LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
213 private void updateConnectionAttributes() {
214 LOG.debug("Update attributes of ovsdb node ip: {} port: {}",
215 this.initialCreatedData.getConnectionInfo().getRemoteIp(),
216 this.initialCreatedData.getConnectionInfo().getRemotePort());
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?
224 public DataBroker getDataBroker() {
228 public ListenableFuture<List<String>> getDatabases() {
229 return client.getDatabases();
232 public ListenableFuture<TypedDatabaseSchema> getSchema(final String database) {
233 return client.getSchema(database);
236 public TransactionBuilder transactBuilder(final DatabaseSchema dbSchema) {
237 return client.transactBuilder(dbSchema);
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);
245 public <E extends TableSchema<E>> TableUpdates monitor(final DatabaseSchema schema,
246 final List<MonitorRequest> monitorRequests, final MonitorHandle monitorHandle,
247 final MonitorCallBack monitorCallBack) {
251 public void cancelMonitor(final MonitorHandle handler) {
252 client.cancelMonitor(handler);
255 public void lock(final String lockId, final LockAquisitionCallback lockedCallBack,
256 final LockStolenCallback stolenCallback) {
257 client.lock(lockId, lockedCallBack, stolenCallback);
260 public ListenableFuture<Boolean> steal(final String lockId) {
261 return client.steal(lockId);
264 public ListenableFuture<Boolean> unLock(final String lockId) {
265 return client.unLock(lockId);
268 public OvsdbConnectionInfo getConnectionInfo() {
269 return client.getConnectionInfo();
272 public boolean isActive() {
273 return client.isActive();
276 public void disconnect() {
280 public DatabaseSchema getDatabaseSchema(final String dbName) {
281 return client.getDatabaseSchema(dbName);
284 public <T extends TypedBaseTable<?>> T createTypedRowWrapper(final Class<T> klazz) {
285 return client.createTypedRowWrapper(klazz);
288 public <T extends TypedBaseTable<?>> T createTypedRowWrapper(final DatabaseSchema dbSchema, final Class<T> klazz) {
289 return client.createTypedRowWrapper(dbSchema, klazz);
292 public <T extends TypedBaseTable<?>> T getTypedRowWrapper(final Class<T> klazz, final Row<GenericTableSchema> row) {
293 return client.getTypedRowWrapper(klazz, row);
296 public ConnectionInfo getMDConnectionInfo() {
297 return connectionInfo;
300 public void setMDConnectionInfo(final ConnectionInfo key) {
301 this.connectionInfo = key;
304 public InstanceIdentifier<Node> getInstanceIdentifier() {
305 return instanceIdentifier;
308 public NodeKey getNodeKey() {
309 //TODO: What is the alternative here?
310 return getInstanceIdentifier().firstKeyOf(Node.class);
313 public NodeId getNodeId() {
314 return getNodeKey().getNodeId();
317 public void setInstanceIdentifier(final InstanceIdentifier<Node> iid) {
318 this.instanceIdentifier = iid;
319 hwvtepConnectionManager.putConnectionInstance(instanceIdentifier, this);
322 public Entity getConnectedEntity() {
323 return this.connectedEntity;
326 public void setConnectedEntity(final Entity entity) {
327 this.connectedEntity = entity;
330 public Boolean hasOvsdbClient(final OvsdbClient otherClient) {
331 return client.equals(otherClient);
334 public Boolean getHasDeviceOwnership() {
335 return hasDeviceOwnership;
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);
344 this.hasDeviceOwnership = hasDeviceOwnership;
348 public void setDeviceOwnershipCandidateRegistration(
349 @NonNull final EntityOwnershipCandidateRegistration registration) {
350 this.deviceOwnershipCandidateRegistration = registration;
353 public void closeDeviceOwnershipCandidateRegistration() {
354 if (deviceOwnershipCandidateRegistration != null) {
355 this.deviceOwnershipCandidateRegistration.close();
356 setHasDeviceOwnership(Boolean.FALSE);
360 public void setHwvtepGlobalAugmentation(final HwvtepGlobalAugmentation hwvtepGlobalData) {
361 this.initialCreatedData = hwvtepGlobalData;
364 public HwvtepGlobalAugmentation getHwvtepGlobalAugmentation() {
365 return this.initialCreatedData;
368 public HwvtepDeviceInfo getDeviceInfo() {
369 return this.deviceInfo;
372 public OvsdbClient getOvsdbClient() {
376 public HwvtepTableReader getHwvtepTableReader() {
377 return hwvtepTableReader;
380 public void refreshOperNode() throws ExecutionException, InterruptedException {
381 TableUpdates tableUpdates = hwvtepTableReader.readAllTables();
382 callback.update(tableUpdates, getDatabaseSchema(HwvtepSchemaConstants.HARDWARE_VTEP));
385 public MonitorCallBack getCallback() {
389 public void setCallback(final MonitorCallBack callback) {
390 this.callback = callback;
393 public TransactionHistory getControllerTxHistory() {
394 return controllerTxHistory;
397 public void setControllerTxHistory(final TransactionHistory controllerTxLog) {
398 deviceInfo.setControllerTxHistory(controllerTxLog);
399 this.controllerTxHistory = controllerTxLog;
402 public TransactionHistory getDeviceUpdateHistory() {
403 return deviceUpdateHistory;
406 public void setDeviceUpdateHistory(final TransactionHistory deviceUpdateLog) {
407 deviceInfo.setDeviceUpdateHistory(deviceUpdateLog);
408 this.deviceUpdateHistory = deviceUpdateLog;
411 public TransactionInvoker getTxInvoker() {