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.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
16 import java.util.concurrent.ExecutionException;
18 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
23 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactCommand;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvoker;
25 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvokerImpl;
26 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
27 import org.opendaylight.ovsdb.lib.EchoServiceCallbackFilters;
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.collect.Lists;
56 import com.google.common.util.concurrent.ListenableFuture;
58 public class HwvtepConnectionInstance {
59 private static final Logger LOG = LoggerFactory.getLogger(HwvtepConnectionInstance.class);
60 private ConnectionInfo connectionInfo;
61 private OvsdbClient client;
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;
85 public synchronized void transact(TransactCommand command) {
86 for (TransactInvoker transactInvoker: transactInvokers.values()) {
87 transactInvoker.invoke(command);
91 public void registerCallbacks() {
92 if ( this.callback == null) {
93 if(this.initialCreatedData != null) {
94 this.updateConnectionAttributes();
98 String database = HwvtepSchemaConstants.HARDWARE_VTEP;
99 DatabaseSchema dbSchema = getSchema(database).get();
100 if (dbSchema != null) {
101 LOG.info("Monitoring database: {}", database);
102 callback = new HwvtepMonitorCallback(this, txInvoker);
103 monitorAllTables(database, dbSchema);
105 LOG.info("No database {} found on {}", database, connectionInfo);
107 } catch (InterruptedException | ExecutionException e) {
108 LOG.warn("Exception attempting to registerCallbacks {}: ", connectionInfo, e);
113 public void createTransactInvokers() {
114 if (transactInvokers == null) {
116 transactInvokers = new HashMap<>();
117 DatabaseSchema dbSchema = getSchema(HwvtepSchemaConstants.HARDWARE_VTEP).get();
118 if(dbSchema != null) {
119 transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
121 } catch (InterruptedException | ExecutionException e) {
122 LOG.warn("Exception attempting to createTransactionInvokers {}", connectionInfo, e);
127 private void monitorAllTables(String database, DatabaseSchema dbSchema) {
128 Set<String> tables = dbSchema.getTables();
129 if (tables != null) {
130 List<MonitorRequest> monitorRequests = Lists.newArrayList();
131 for (String tableName : tables) {
132 if (!HwvtepSouthboundConstants.SKIP_HWVTEP_TABLE.containsKey(tableName)) {
133 LOG.info("HwvtepSouthbound monitoring Hwvtep schema table {}", tableName);
134 GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
135 Set<String> columns = new HashSet<>(tableSchema.getColumns());
136 List<String> skipColumns = HwvtepSouthboundConstants.SKIP_COLUMN_FROM_HWVTEP_TABLE.get(tableName);
137 if (skipColumns != null) {
138 LOG.info("HwvtepSouthbound NOT monitoring columns {} in table {}", skipColumns, tableName);
139 columns.removeAll(skipColumns);
141 monitorRequests.add(new MonitorRequestBuilder<>(tableSchema)
143 .with(new MonitorSelect(true, true, true, true)).build());
146 this.callback.update(monitor(dbSchema, monitorRequests, callback), dbSchema);
148 LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
152 private void updateConnectionAttributes() {
153 LOG.debug("Update attributes of ovsdb node ip: {} port: {}",
154 this.initialCreatedData.getConnectionInfo().getRemoteIp(),
155 this.initialCreatedData.getConnectionInfo().getRemotePort());
157 * TODO: Do we have anything to update?
158 * Hwvtep doesn't have other_config or external_ids like
159 * Open_vSwitch. What else will be needed?
163 public DataBroker getDataBroker() {
167 public ListenableFuture<List<String>> getDatabases() {
168 return client.getDatabases();
171 public ListenableFuture<DatabaseSchema> getSchema(String database) {
172 return client.getSchema(database);
175 public TransactionBuilder transactBuilder(DatabaseSchema dbSchema) {
176 return client.transactBuilder(dbSchema);
179 public ListenableFuture<List<OperationResult>> transact(DatabaseSchema dbSchema, List<Operation> operations) {
180 return client.transact(dbSchema, operations);
183 public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
184 List<MonitorRequest> monitorRequests, MonitorCallBack callback) {
185 return client.monitor(schema, monitorRequests, callback);
188 public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
189 List<MonitorRequest> monitorRequests, MonitorHandle monitorHandle, MonitorCallBack callback) {
193 public void cancelMonitor(MonitorHandle handler) {
194 client.cancelMonitor(handler);
197 public void lock(String lockId, LockAquisitionCallback lockedCallBack, LockStolenCallback stolenCallback) {
198 client.lock(lockId, lockedCallBack, stolenCallback);
201 public ListenableFuture<Boolean> steal(String lockId) {
202 return client.steal(lockId);
205 public ListenableFuture<Boolean> unLock(String lockId) {
206 return client.unLock(lockId);
209 public OvsdbConnectionInfo getConnectionInfo() {
210 return client.getConnectionInfo();
213 public boolean isActive() {
214 return client.isActive();
217 public void disconnect() {
221 public DatabaseSchema getDatabaseSchema(String dbName) {
222 return client.getDatabaseSchema(dbName);
225 public <T extends TypedBaseTable<?>> T createTypedRowWrapper(Class<T> klazz) {
226 return client.createTypedRowWrapper(klazz);
229 public <T extends TypedBaseTable<?>> T createTypedRowWrapper(DatabaseSchema dbSchema, Class<T> klazz) {
230 return client.createTypedRowWrapper(dbSchema, klazz);
233 public <T extends TypedBaseTable<?>> T getTypedRowWrapper(Class<T> klazz, Row<GenericTableSchema> row) {
234 return client.getTypedRowWrapper(klazz, row);
237 public ConnectionInfo getMDConnectionInfo() {
238 return connectionInfo;
241 public void setMDConnectionInfo(ConnectionInfo key) {
242 this.connectionInfo = key;
245 public InstanceIdentifier<Node> getInstanceIdentifier() {
246 return instanceIdentifier;
249 public NodeKey getNodeKey() {
250 //TODO: What is the alternative here?
251 return getInstanceIdentifier().firstKeyOf(Node.class);
254 public NodeId getNodeId() {
255 return getNodeKey().getNodeId();
258 public void setInstanceIdentifier(InstanceIdentifier<Node> iid) {
259 this.instanceIdentifier = iid;
260 hwvtepConnectionManager.putConnectionInstance(instanceIdentifier, this);
263 public Entity getConnectedEntity() {
264 return this.connectedEntity;
267 public void setConnectedEntity(Entity entity ) {
268 this.connectedEntity = entity;
271 public Boolean hasOvsdbClient(OvsdbClient otherClient) {
272 return client.equals(otherClient);
275 public Boolean getHasDeviceOwnership() {
276 return hasDeviceOwnership;
279 public void setHasDeviceOwnership(Boolean hasDeviceOwnership) {
280 if (hasDeviceOwnership != null) {
281 this.hasDeviceOwnership = hasDeviceOwnership;
285 public void setDeviceOwnershipCandidateRegistration(@Nonnull EntityOwnershipCandidateRegistration registration) {
286 this.deviceOwnershipCandidateRegistration = registration;
289 public void closeDeviceOwnershipCandidateRegistration() {
290 if (deviceOwnershipCandidateRegistration != null) {
291 this.deviceOwnershipCandidateRegistration.close();
292 setHasDeviceOwnership(Boolean.FALSE);
296 public void setHwvtepGlobalAugmentation(HwvtepGlobalAugmentation hwvtepGlobalData) {
297 this.initialCreatedData = hwvtepGlobalData;
300 public HwvtepGlobalAugmentation getHwvtepGlobalAugmentation() {
301 return this.initialCreatedData;
303 public HwvtepDeviceInfo getDeviceInfo() {
304 return this.deviceInfo;
307 public OvsdbClient getOvsdbClient() {