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;
19 import javax.annotation.Nonnull;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactCommand;
25 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvoker;
26 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvokerImpl;
27 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
28 import org.opendaylight.ovsdb.lib.LockAquisitionCallback;
29 import org.opendaylight.ovsdb.lib.LockStolenCallback;
30 import org.opendaylight.ovsdb.lib.MonitorCallBack;
31 import org.opendaylight.ovsdb.lib.MonitorHandle;
32 import org.opendaylight.ovsdb.lib.OvsdbClient;
33 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
34 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
35 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
36 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
37 import org.opendaylight.ovsdb.lib.message.TableUpdates;
38 import org.opendaylight.ovsdb.lib.notation.Row;
39 import org.opendaylight.ovsdb.lib.operations.Operation;
40 import org.opendaylight.ovsdb.lib.operations.OperationResult;
41 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
42 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
43 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
44 import org.opendaylight.ovsdb.lib.schema.TableSchema;
45 import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.google.common.util.concurrent.ListenableFuture;
57 public class HwvtepConnectionInstance {
58 private static final Logger LOG = LoggerFactory.getLogger(HwvtepConnectionInstance.class);
59 private ConnectionInfo connectionInfo;
60 private OvsdbClient client;
61 private final HwvtepTableReader hwvtepTableReader;
62 private InstanceIdentifier<Node> instanceIdentifier;
63 private TransactionInvoker txInvoker;
64 private Map<DatabaseSchema,TransactInvoker> transactInvokers;
65 private MonitorCallBack callback;
66 private volatile boolean hasDeviceOwnership = false;
67 private Entity connectedEntity;
68 private EntityOwnershipCandidateRegistration deviceOwnershipCandidateRegistration;
69 private HwvtepGlobalAugmentation initialCreatedData = null;
70 private HwvtepDeviceInfo deviceInfo;
71 private DataBroker dataBroker;
72 private final HwvtepConnectionManager hwvtepConnectionManager;
74 HwvtepConnectionInstance (HwvtepConnectionManager hwvtepConnectionManager, ConnectionInfo key, OvsdbClient client,
75 InstanceIdentifier<Node> iid, TransactionInvoker txInvoker, DataBroker dataBroker) {
76 this.hwvtepConnectionManager = hwvtepConnectionManager;
77 this.connectionInfo = key;
79 this.instanceIdentifier = iid;
80 this.txInvoker = txInvoker;
81 this.deviceInfo = new HwvtepDeviceInfo(this);
82 this.dataBroker = dataBroker;
83 this.hwvtepTableReader = new HwvtepTableReader(this);
86 public synchronized void transact(TransactCommand command) {
87 for (TransactInvoker transactInvoker: transactInvokers.values()) {
88 transactInvoker.invoke(command);
92 public void registerCallbacks() {
93 if ( this.callback == null) {
94 if(this.initialCreatedData != null) {
95 this.updateConnectionAttributes();
99 String database = HwvtepSchemaConstants.HARDWARE_VTEP;
100 DatabaseSchema dbSchema = getSchema(database).get();
101 if (dbSchema != null) {
102 LOG.info("Monitoring database: {}", database);
103 callback = new HwvtepMonitorCallback(this, txInvoker);
104 monitorAllTables(database, dbSchema);
106 LOG.info("No database {} found on {}", database, connectionInfo);
108 } catch (InterruptedException | ExecutionException e) {
109 LOG.warn("Exception attempting to registerCallbacks {}: ", connectionInfo, e);
114 public void createTransactInvokers() {
115 if (transactInvokers == null) {
117 transactInvokers = new HashMap<>();
118 DatabaseSchema dbSchema = getSchema(HwvtepSchemaConstants.HARDWARE_VTEP).get();
119 if(dbSchema != null) {
120 transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
122 } catch (InterruptedException | ExecutionException e) {
123 LOG.warn("Exception attempting to createTransactionInvokers {}", connectionInfo, e);
128 private void monitorAllTables(String database, DatabaseSchema dbSchema) {
129 Set<String> tables = dbSchema.getTables();
130 if (tables != null) {
131 List<MonitorRequest> monitorRequests = new ArrayList<>();
132 for (String tableName : tables) {
133 if (!HwvtepSouthboundConstants.SKIP_HWVTEP_TABLE.containsKey(tableName)) {
134 LOG.info("HwvtepSouthbound monitoring Hwvtep schema table {}", tableName);
135 GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
136 Set<String> columns = new HashSet<>(tableSchema.getColumns());
137 List<String> skipColumns = HwvtepSouthboundConstants.SKIP_COLUMN_FROM_HWVTEP_TABLE.get(tableName);
138 skipColumns = skipColumns == null ? new ArrayList<>() : new ArrayList<>(skipColumns);
139 skipColumns.add(HwvtepSouthboundConstants.VERSION_COLUMN);
140 if (skipColumns != null) {
141 LOG.info("HwvtepSouthbound NOT monitoring columns {} in table {}", skipColumns, tableName);
142 columns.removeAll(skipColumns);
144 monitorRequests.add(new MonitorRequestBuilder<>(tableSchema)
146 .with(new MonitorSelect(true, true, true, true)).build());
149 this.callback.update(monitor(dbSchema, monitorRequests, callback), dbSchema);
151 LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
155 private void updateConnectionAttributes() {
156 LOG.debug("Update attributes of ovsdb node ip: {} port: {}",
157 this.initialCreatedData.getConnectionInfo().getRemoteIp(),
158 this.initialCreatedData.getConnectionInfo().getRemotePort());
160 * TODO: Do we have anything to update?
161 * Hwvtep doesn't have other_config or external_ids like
162 * Open_vSwitch. What else will be needed?
166 public DataBroker getDataBroker() {
170 public ListenableFuture<List<String>> getDatabases() {
171 return client.getDatabases();
174 public ListenableFuture<DatabaseSchema> getSchema(String database) {
175 return client.getSchema(database);
178 public TransactionBuilder transactBuilder(DatabaseSchema dbSchema) {
179 return client.transactBuilder(dbSchema);
182 public ListenableFuture<List<OperationResult>> transact(DatabaseSchema dbSchema, List<Operation> operations) {
183 return client.transact(dbSchema, operations);
186 public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
187 List<MonitorRequest> monitorRequests, MonitorCallBack callback) {
188 return client.monitor(schema, monitorRequests, callback);
191 public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
192 List<MonitorRequest> monitorRequests, MonitorHandle monitorHandle, MonitorCallBack callback) {
196 public void cancelMonitor(MonitorHandle handler) {
197 client.cancelMonitor(handler);
200 public void lock(String lockId, LockAquisitionCallback lockedCallBack, LockStolenCallback stolenCallback) {
201 client.lock(lockId, lockedCallBack, stolenCallback);
204 public ListenableFuture<Boolean> steal(String lockId) {
205 return client.steal(lockId);
208 public ListenableFuture<Boolean> unLock(String lockId) {
209 return client.unLock(lockId);
212 public OvsdbConnectionInfo getConnectionInfo() {
213 return client.getConnectionInfo();
216 public boolean isActive() {
217 return client.isActive();
220 public void disconnect() {
224 public DatabaseSchema getDatabaseSchema(String dbName) {
225 return client.getDatabaseSchema(dbName);
228 public <T extends TypedBaseTable<?>> T createTypedRowWrapper(Class<T> klazz) {
229 return client.createTypedRowWrapper(klazz);
232 public <T extends TypedBaseTable<?>> T createTypedRowWrapper(DatabaseSchema dbSchema, Class<T> klazz) {
233 return client.createTypedRowWrapper(dbSchema, klazz);
236 public <T extends TypedBaseTable<?>> T getTypedRowWrapper(Class<T> klazz, Row<GenericTableSchema> row) {
237 return client.getTypedRowWrapper(klazz, row);
240 public ConnectionInfo getMDConnectionInfo() {
241 return connectionInfo;
244 public void setMDConnectionInfo(ConnectionInfo key) {
245 this.connectionInfo = key;
248 public InstanceIdentifier<Node> getInstanceIdentifier() {
249 return instanceIdentifier;
252 public NodeKey getNodeKey() {
253 //TODO: What is the alternative here?
254 return getInstanceIdentifier().firstKeyOf(Node.class);
257 public NodeId getNodeId() {
258 return getNodeKey().getNodeId();
261 public void setInstanceIdentifier(InstanceIdentifier<Node> iid) {
262 this.instanceIdentifier = iid;
263 hwvtepConnectionManager.putConnectionInstance(instanceIdentifier, this);
266 public Entity getConnectedEntity() {
267 return this.connectedEntity;
270 public void setConnectedEntity(Entity entity ) {
271 this.connectedEntity = entity;
274 public Boolean hasOvsdbClient(OvsdbClient otherClient) {
275 return client.equals(otherClient);
278 public Boolean getHasDeviceOwnership() {
279 return hasDeviceOwnership;
282 public void setHasDeviceOwnership(Boolean hasDeviceOwnership) {
283 if (hasDeviceOwnership != null) {
284 this.hasDeviceOwnership = hasDeviceOwnership;
288 public void setDeviceOwnershipCandidateRegistration(@Nonnull EntityOwnershipCandidateRegistration registration) {
289 this.deviceOwnershipCandidateRegistration = registration;
292 public void closeDeviceOwnershipCandidateRegistration() {
293 if (deviceOwnershipCandidateRegistration != null) {
294 this.deviceOwnershipCandidateRegistration.close();
295 setHasDeviceOwnership(Boolean.FALSE);
299 public void setHwvtepGlobalAugmentation(HwvtepGlobalAugmentation hwvtepGlobalData) {
300 this.initialCreatedData = hwvtepGlobalData;
303 public HwvtepGlobalAugmentation getHwvtepGlobalAugmentation() {
304 return this.initialCreatedData;
306 public HwvtepDeviceInfo getDeviceInfo() {
307 return this.deviceInfo;
310 public OvsdbClient getOvsdbClient() {
314 public HwvtepTableReader getHwvtepTableReader() {
315 return hwvtepTableReader;