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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.HashSet;
14 import java.util.List;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.ScheduledExecutorService;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.atomic.AtomicBoolean;
23 import javax.annotation.Nonnull;
25 import com.google.common.util.concurrent.FutureCallback;
26 import com.google.common.util.concurrent.Futures;
27 import com.google.common.util.concurrent.SettableFuture;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
30 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
31 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactCommand;
32 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvoker;
33 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvokerImpl;
34 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
35 import org.opendaylight.ovsdb.lib.LockAquisitionCallback;
36 import org.opendaylight.ovsdb.lib.LockStolenCallback;
37 import org.opendaylight.ovsdb.lib.MonitorCallBack;
38 import org.opendaylight.ovsdb.lib.MonitorHandle;
39 import org.opendaylight.ovsdb.lib.OvsdbClient;
40 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
41 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
42 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
43 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
44 import org.opendaylight.ovsdb.lib.message.TableUpdates;
45 import org.opendaylight.ovsdb.lib.notation.Row;
46 import org.opendaylight.ovsdb.lib.operations.Operation;
47 import org.opendaylight.ovsdb.lib.operations.OperationResult;
48 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
49 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
50 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
51 import org.opendaylight.ovsdb.lib.schema.TableSchema;
52 import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
53 import org.opendaylight.ovsdb.utils.mdsal.utils.TransactionHistory;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
59 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
63 import com.google.common.util.concurrent.ListenableFuture;
65 public class HwvtepConnectionInstance {
66 private static final Logger LOG = LoggerFactory.getLogger(HwvtepConnectionInstance.class);
67 private ConnectionInfo connectionInfo;
68 private OvsdbClient client;
69 private final HwvtepTableReader hwvtepTableReader;
70 private InstanceIdentifier<Node> instanceIdentifier;
71 private TransactionInvoker txInvoker;
72 private Map<DatabaseSchema,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 HwvtepDeviceInfo deviceInfo;
79 private DataBroker dataBroker;
80 private final HwvtepConnectionManager hwvtepConnectionManager;
81 private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
82 private final SettableFuture<Boolean> reconciliationFt = SettableFuture.create();
83 private final AtomicBoolean firstUpdateTriggered = new AtomicBoolean(false);
84 private TransactionHistory controllerTxHistory;
85 private TransactionHistory deviceUpdateHistory;
87 HwvtepConnectionInstance (HwvtepConnectionManager hwvtepConnectionManager, ConnectionInfo key, OvsdbClient client,
88 InstanceIdentifier<Node> iid, TransactionInvoker txInvoker, DataBroker dataBroker) {
89 this.hwvtepConnectionManager = hwvtepConnectionManager;
90 this.connectionInfo = key;
92 this.instanceIdentifier = iid;
93 this.txInvoker = txInvoker;
94 this.deviceInfo = new HwvtepDeviceInfo(this);
95 this.dataBroker = dataBroker;
96 this.hwvtepTableReader = new HwvtepTableReader(this);
99 public void transact(final TransactCommand command) {
100 String nodeId = getNodeId().getValue();
101 boolean firstUpdate = firstUpdateTriggered.compareAndSet(false, true);
102 if (reconciliationFt.isDone()) {
103 transact(command, false);
105 LOG.info("Job waiting for reconciliation {}", nodeId);
106 Futures.addCallback(reconciliationFt, new FutureCallback<Boolean>() {
108 public void onSuccess(Boolean aBoolean) {
109 LOG.info("Running the job waiting for reconciliation {}", nodeId);
110 transact(command, false);
114 public void onFailure(Throwable throwable) {
115 LOG.info("Running the job waiting for reconciliation {}", nodeId);
116 transact(command, false);
120 LOG.info("Scheduling the reconciliation timeout task {}", nodeId);
121 scheduledExecutorService.schedule( () -> reconciliationFt.set(Boolean.TRUE),
122 HwvtepSouthboundConstants.CONFIG_NODE_UPDATE_MAX_DELAY_MS, TimeUnit.MILLISECONDS);
127 public synchronized void transact(TransactCommand command, boolean reconcile) {
129 for (TransactInvoker transactInvoker : transactInvokers.values()) {
130 transactInvoker.invoke(command);
134 reconciliationFt.set(Boolean.TRUE);
139 public void registerCallbacks() {
140 if ( this.callback == null) {
141 if(this.initialCreatedData != null) {
142 this.updateConnectionAttributes();
146 String database = HwvtepSchemaConstants.HARDWARE_VTEP;
147 DatabaseSchema dbSchema = getSchema(database).get();
148 if (dbSchema != null) {
149 LOG.info("Monitoring database: {}", database);
150 callback = new HwvtepMonitorCallback(this, txInvoker);
151 monitorAllTables(database, dbSchema);
153 LOG.info("No database {} found on {}", database, connectionInfo);
155 } catch (InterruptedException | ExecutionException e) {
156 LOG.warn("Exception attempting to registerCallbacks {}: ", connectionInfo, e);
161 public void createTransactInvokers() {
162 if (transactInvokers == null) {
164 transactInvokers = new HashMap<>();
165 DatabaseSchema dbSchema = getSchema(HwvtepSchemaConstants.HARDWARE_VTEP).get();
166 if(dbSchema != null) {
167 transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
169 } catch (InterruptedException | ExecutionException e) {
170 LOG.warn("Exception attempting to createTransactionInvokers {}", connectionInfo, e);
175 private void monitorAllTables(String database, DatabaseSchema dbSchema) {
176 Set<String> tables = dbSchema.getTables();
177 if (tables != null) {
178 List<MonitorRequest> monitorRequests = new ArrayList<>();
179 for (String tableName : tables) {
180 if (!HwvtepSouthboundConstants.SKIP_HWVTEP_TABLE.containsKey(tableName)) {
181 LOG.info("HwvtepSouthbound monitoring Hwvtep schema table {}", tableName);
182 GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
183 Set<String> columns = new HashSet<>(tableSchema.getColumns());
184 List<String> skipColumns = HwvtepSouthboundConstants.SKIP_COLUMN_FROM_HWVTEP_TABLE.get(tableName);
185 skipColumns = skipColumns == null ? new ArrayList<>() : new ArrayList<>(skipColumns);
186 skipColumns.add(HwvtepSouthboundConstants.VERSION_COLUMN);
187 if (skipColumns != null) {
188 LOG.info("HwvtepSouthbound NOT monitoring columns {} in table {}", skipColumns, tableName);
189 columns.removeAll(skipColumns);
191 monitorRequests.add(new MonitorRequestBuilder<>(tableSchema)
193 .with(new MonitorSelect(true, true, true, true)).build());
196 this.callback.update(monitor(dbSchema, monitorRequests, callback), dbSchema);
198 LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
202 private void updateConnectionAttributes() {
203 LOG.debug("Update attributes of ovsdb node ip: {} port: {}",
204 this.initialCreatedData.getConnectionInfo().getRemoteIp(),
205 this.initialCreatedData.getConnectionInfo().getRemotePort());
207 * TODO: Do we have anything to update?
208 * Hwvtep doesn't have other_config or external_ids like
209 * Open_vSwitch. What else will be needed?
213 public DataBroker getDataBroker() {
217 public ListenableFuture<List<String>> getDatabases() {
218 return client.getDatabases();
221 public ListenableFuture<DatabaseSchema> getSchema(String database) {
222 return client.getSchema(database);
225 public TransactionBuilder transactBuilder(DatabaseSchema dbSchema) {
226 return client.transactBuilder(dbSchema);
229 public ListenableFuture<List<OperationResult>> transact(DatabaseSchema dbSchema, List<Operation> operations) {
230 return client.transact(dbSchema, operations);
233 public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
234 List<MonitorRequest> monitorRequests, MonitorCallBack callback) {
235 return client.monitor(schema, monitorRequests, callback);
238 public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
239 List<MonitorRequest> monitorRequests, MonitorHandle monitorHandle, MonitorCallBack callback) {
243 public void cancelMonitor(MonitorHandle handler) {
244 client.cancelMonitor(handler);
247 public void lock(String lockId, LockAquisitionCallback lockedCallBack, LockStolenCallback stolenCallback) {
248 client.lock(lockId, lockedCallBack, stolenCallback);
251 public ListenableFuture<Boolean> steal(String lockId) {
252 return client.steal(lockId);
255 public ListenableFuture<Boolean> unLock(String lockId) {
256 return client.unLock(lockId);
259 public OvsdbConnectionInfo getConnectionInfo() {
260 return client.getConnectionInfo();
263 public boolean isActive() {
264 return client.isActive();
267 public void disconnect() {
271 public DatabaseSchema getDatabaseSchema(String dbName) {
272 return client.getDatabaseSchema(dbName);
275 public <T extends TypedBaseTable<?>> T createTypedRowWrapper(Class<T> klazz) {
276 return client.createTypedRowWrapper(klazz);
279 public <T extends TypedBaseTable<?>> T createTypedRowWrapper(DatabaseSchema dbSchema, Class<T> klazz) {
280 return client.createTypedRowWrapper(dbSchema, klazz);
283 public <T extends TypedBaseTable<?>> T getTypedRowWrapper(Class<T> klazz, Row<GenericTableSchema> row) {
284 return client.getTypedRowWrapper(klazz, row);
287 public ConnectionInfo getMDConnectionInfo() {
288 return connectionInfo;
291 public void setMDConnectionInfo(ConnectionInfo key) {
292 this.connectionInfo = key;
295 public InstanceIdentifier<Node> getInstanceIdentifier() {
296 return instanceIdentifier;
299 public NodeKey getNodeKey() {
300 //TODO: What is the alternative here?
301 return getInstanceIdentifier().firstKeyOf(Node.class);
304 public NodeId getNodeId() {
305 return getNodeKey().getNodeId();
308 public void setInstanceIdentifier(InstanceIdentifier<Node> iid) {
309 this.instanceIdentifier = iid;
310 hwvtepConnectionManager.putConnectionInstance(instanceIdentifier, this);
313 public Entity getConnectedEntity() {
314 return this.connectedEntity;
317 public void setConnectedEntity(Entity entity ) {
318 this.connectedEntity = entity;
321 public Boolean hasOvsdbClient(OvsdbClient otherClient) {
322 return client.equals(otherClient);
325 public Boolean getHasDeviceOwnership() {
326 return hasDeviceOwnership;
329 public void setHasDeviceOwnership(Boolean hasDeviceOwnership) {
330 if (hasDeviceOwnership != null) {
331 this.hasDeviceOwnership = hasDeviceOwnership;
335 public void setDeviceOwnershipCandidateRegistration(@Nonnull EntityOwnershipCandidateRegistration registration) {
336 this.deviceOwnershipCandidateRegistration = registration;
339 public void closeDeviceOwnershipCandidateRegistration() {
340 if (deviceOwnershipCandidateRegistration != null) {
341 this.deviceOwnershipCandidateRegistration.close();
342 setHasDeviceOwnership(Boolean.FALSE);
346 public void setHwvtepGlobalAugmentation(HwvtepGlobalAugmentation hwvtepGlobalData) {
347 this.initialCreatedData = hwvtepGlobalData;
350 public HwvtepGlobalAugmentation getHwvtepGlobalAugmentation() {
351 return this.initialCreatedData;
353 public HwvtepDeviceInfo getDeviceInfo() {
354 return this.deviceInfo;
357 public OvsdbClient getOvsdbClient() {
361 public HwvtepTableReader getHwvtepTableReader() {
362 return hwvtepTableReader;
365 public void refreshOperNode() throws ExecutionException, InterruptedException {
366 TableUpdates tableUpdates = hwvtepTableReader.readAllTables();
367 callback.update(tableUpdates, getDatabaseSchema(HwvtepSchemaConstants.HARDWARE_VTEP));
370 public MonitorCallBack getCallback() {
374 public void setCallback(MonitorCallBack callback) {
375 this.callback = callback;
378 public TransactionHistory getControllerTxHistory() {
379 return controllerTxHistory;
382 public void setControllerTxHistory(TransactionHistory controllerTxLog) {
383 deviceInfo.setControllerTxHistory(controllerTxLog);
384 this.controllerTxHistory = controllerTxLog;
387 public TransactionHistory getDeviceUpdateHistory() {
388 return deviceUpdateHistory;
391 public void setDeviceUpdateHistory(TransactionHistory deviceUpdateLog) {
392 deviceInfo.setDeviceUpdateHistory(deviceUpdateLog);
393 this.deviceUpdateHistory = deviceUpdateLog;