2 * Copyright (c) 2015 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.List;
15 import java.util.concurrent.ExecutionException;
17 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
21 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactCommand;
22 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvoker;
23 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.TransactInvokerImpl;
24 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
25 import org.opendaylight.ovsdb.lib.EchoServiceCallbackFilters;
26 import org.opendaylight.ovsdb.lib.LockAquisitionCallback;
27 import org.opendaylight.ovsdb.lib.LockStolenCallback;
28 import org.opendaylight.ovsdb.lib.MonitorCallBack;
29 import org.opendaylight.ovsdb.lib.MonitorHandle;
30 import org.opendaylight.ovsdb.lib.OvsdbClient;
31 import org.opendaylight.ovsdb.lib.OvsdbConnectionInfo;
32 import org.opendaylight.ovsdb.lib.message.MonitorRequest;
33 import org.opendaylight.ovsdb.lib.message.MonitorRequestBuilder;
34 import org.opendaylight.ovsdb.lib.message.MonitorSelect;
35 import org.opendaylight.ovsdb.lib.message.TableUpdates;
36 import org.opendaylight.ovsdb.lib.notation.Row;
37 import org.opendaylight.ovsdb.lib.operations.Operation;
38 import org.opendaylight.ovsdb.lib.operations.OperationResult;
39 import org.opendaylight.ovsdb.lib.operations.TransactionBuilder;
40 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
41 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
42 import org.opendaylight.ovsdb.lib.schema.TableSchema;
43 import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 import com.google.common.collect.Lists;
54 import com.google.common.util.concurrent.ListenableFuture;
56 public class HwvtepConnectionInstance implements OvsdbClient{
57 private static final Logger LOG = LoggerFactory.getLogger(HwvtepConnectionInstance.class);
58 private ConnectionInfo connectionInfo;
59 private OvsdbClient client;
60 private InstanceIdentifier<Node> instanceIdentifier;
61 private TransactionInvoker txInvoker;
62 private Map<DatabaseSchema,TransactInvoker> transactInvokers;
63 private MonitorCallBack callback;
64 private volatile boolean hasDeviceOwnership = false;
65 private Entity connectedEntity;
66 private EntityOwnershipCandidateRegistration deviceOwnershipCandidateRegistration;
67 private HwvtepGlobalAugmentation initialCreatedData = null;
70 HwvtepConnectionInstance (ConnectionInfo key,OvsdbClient client,
71 InstanceIdentifier<Node> iid, TransactionInvoker txInvoker) {
72 this.connectionInfo = key;
74 this.instanceIdentifier = iid;
75 this.txInvoker = txInvoker;
78 public void transact(TransactCommand command) {
79 for (TransactInvoker transactInvoker: transactInvokers.values()) {
80 transactInvoker.invoke(command);
84 public void registerCallbacks() {
85 if ( this.callback == null) {
86 if(this.initialCreatedData != null) {
87 this.updateConnectionAttributes();
91 List<String> databases = getDatabases().get();
92 this.callback = new HwvtepMonitorCallback(this,txInvoker);
93 for (String database : databases) {
94 DatabaseSchema dbSchema = getSchema(database).get();
95 if (dbSchema != null) {
96 monitorAllTables(database, dbSchema, HwvtepSchemaConstants.databaseName);
98 LOG.warn("No schema reported for database {} for key {}",database,connectionInfo);
101 } catch (InterruptedException | ExecutionException e) {
102 LOG.warn("Exception attempting to registerCallbacks {}: {}",connectionInfo,e);
107 public void createTransactInvokers() {
108 if (transactInvokers == null) {
110 transactInvokers = new HashMap<>();
111 DatabaseSchema dbSchema = getSchema(HwvtepSchemaConstants.databaseName).get();
112 if(dbSchema != null) {
113 transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
115 } catch (InterruptedException | ExecutionException e) {
116 LOG.warn("Exception attempting to createTransactionInvokers {}: {}",connectionInfo,e);
121 private void monitorAllTables(String database, DatabaseSchema dbSchema, String filter) {
122 if((filter != null) && (!dbSchema.getName().equals(filter))) {
123 LOG.debug("Not monitoring tables in {}, filter: {}", dbSchema.getName(), filter);
126 Set<String> tables = dbSchema.getTables();
127 if (tables != null) {
128 List<MonitorRequest> monitorRequests = Lists.newArrayList();
129 for (String tableName : tables) {
130 LOG.debug("HwvtepSouthbound monitoring table {} in {}", tableName, dbSchema.getName());
131 GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
132 Set<String> columns = tableSchema.getColumns();
133 MonitorRequestBuilder<GenericTableSchema> monitorBuilder = MonitorRequestBuilder.builder(tableSchema);
134 for (String column : columns) {
135 monitorBuilder.addColumn(column);
137 monitorRequests.add(monitorBuilder.with(new MonitorSelect(true, true, true, true)).build());
139 this.callback.update(monitor(dbSchema, monitorRequests, callback),dbSchema);
141 LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
145 private void updateConnectionAttributes() {
146 LOG.debug("Update attributes of ovsdb node ip: {} port: {}",
147 this.initialCreatedData.getConnectionInfo().getRemoteIp(),
148 this.initialCreatedData.getConnectionInfo().getRemotePort());
150 * TODO: Do we have anything to update?
151 * Hwvtep doesn't have other_config or external_ids like
152 * Open_vSwitch. What else will be needed?
156 public ListenableFuture<List<String>> getDatabases() {
157 return client.getDatabases();
160 public ListenableFuture<DatabaseSchema> getSchema(String database) {
161 return client.getSchema(database);
164 public TransactionBuilder transactBuilder(DatabaseSchema dbSchema) {
165 return client.transactBuilder(dbSchema);
168 public ListenableFuture<List<OperationResult>> transact(DatabaseSchema dbSchema, List<Operation> operations) {
169 return client.transact(dbSchema, operations);
172 public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
173 List<MonitorRequest> monitorRequests, MonitorCallBack callback) {
174 return client.monitor(schema, monitorRequests, callback);
178 public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
179 List<MonitorRequest> monitorRequests, MonitorHandle monitorHandle, MonitorCallBack callback) {
183 public void cancelMonitor(MonitorHandle handler) {
184 client.cancelMonitor(handler);
187 public void lock(String lockId, LockAquisitionCallback lockedCallBack, LockStolenCallback stolenCallback) {
188 client.lock(lockId, lockedCallBack, stolenCallback);
191 public ListenableFuture<Boolean> steal(String lockId) {
192 return client.steal(lockId);
195 public ListenableFuture<Boolean> unLock(String lockId) {
196 return client.unLock(lockId);
199 public void startEchoService(EchoServiceCallbackFilters callbackFilters) {
200 client.startEchoService(callbackFilters);
203 public void stopEchoService() {
204 client.stopEchoService();
207 public OvsdbConnectionInfo getConnectionInfo() {
208 return client.getConnectionInfo();
211 public boolean isActive() {
212 return client.isActive();
215 public void disconnect() {
219 public DatabaseSchema getDatabaseSchema(String dbName) {
220 return client.getDatabaseSchema(dbName);
223 public <T extends TypedBaseTable<?>> T createTypedRowWrapper(Class<T> klazz) {
224 return client.createTypedRowWrapper(klazz);
227 public <T extends TypedBaseTable<?>> T createTypedRowWrapper(DatabaseSchema dbSchema, Class<T> klazz) {
228 return client.createTypedRowWrapper(dbSchema, klazz);
231 public <T extends TypedBaseTable<?>> T getTypedRowWrapper(Class<T> klazz, Row<GenericTableSchema> row) {
232 return client.getTypedRowWrapper(klazz, row);
235 public ConnectionInfo getMDConnectionInfo() {
236 return connectionInfo;
239 public void setMDConnectionInfo(ConnectionInfo key) {
240 this.connectionInfo = key;
243 public InstanceIdentifier<Node> getInstanceIdentifier() {
244 return instanceIdentifier;
247 public NodeKey getNodeKey() {
248 //TODO: What is the alternative here?
249 return getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
252 public NodeId getNodeId() {
253 return getNodeKey().getNodeId();
256 public void setInstanceIdentifier(InstanceIdentifier<Node> iid) {
257 this.instanceIdentifier = iid;
260 public Entity getConnectedEntity() {
261 return this.connectedEntity;
264 public void setConnectedEntity(Entity entity ) {
265 this.connectedEntity = entity;
268 public Boolean hasOvsdbClient(OvsdbClient otherClient) {
269 return client.equals(otherClient);
272 public Boolean getHasDeviceOwnership() {
273 return hasDeviceOwnership;
276 public void setHasDeviceOwnership(Boolean hasDeviceOwnership) {
277 if (hasDeviceOwnership != null) {
278 this.hasDeviceOwnership = hasDeviceOwnership;
282 public void setDeviceOwnershipCandidateRegistration(@Nonnull EntityOwnershipCandidateRegistration registration) {
283 this.deviceOwnershipCandidateRegistration = registration;
286 public void closeDeviceOwnershipCandidateRegistration() {
287 if (deviceOwnershipCandidateRegistration != null) {
288 this.deviceOwnershipCandidateRegistration.close();
289 setHasDeviceOwnership(Boolean.FALSE);
293 public void setHwvtepGlobalAugmentation(HwvtepGlobalAugmentation hwvtepGlobalData) {
294 this.initialCreatedData = hwvtepGlobalData;