bug 7599 avoid mdsal read
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepConnectionInstance.java
1 /*
2  * Copyright (c) 2015, 2017 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
10
11 import java.util.HashMap;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16 import java.util.concurrent.ExecutionException;
17
18 import javax.annotation.Nonnull;
19
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;
54
55 import com.google.common.collect.Lists;
56 import com.google.common.util.concurrent.ListenableFuture;
57
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;
73
74     HwvtepConnectionInstance (HwvtepConnectionManager hwvtepConnectionManager, ConnectionInfo key, OvsdbClient client,
75                               InstanceIdentifier<Node> iid, TransactionInvoker txInvoker, DataBroker dataBroker) {
76         this.hwvtepConnectionManager = hwvtepConnectionManager;
77         this.connectionInfo = key;
78         this.client = client;
79         this.instanceIdentifier = iid;
80         this.txInvoker = txInvoker;
81         this.deviceInfo = new HwvtepDeviceInfo(this);
82         this.dataBroker = dataBroker;
83     }
84
85     public synchronized void transact(TransactCommand command) {
86         for (TransactInvoker transactInvoker: transactInvokers.values()) {
87             transactInvoker.invoke(command);
88         }
89     }
90
91     public void registerCallbacks() {
92         if ( this.callback == null) {
93             if(this.initialCreatedData != null) {
94                 this.updateConnectionAttributes();
95             }
96
97             try {
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);
104                 } else {
105                     LOG.info("No database {} found on {}", database, connectionInfo);
106                 }
107             } catch (InterruptedException | ExecutionException e) {
108                 LOG.warn("Exception attempting to registerCallbacks {}: ", connectionInfo, e);
109             }
110         }
111     }
112
113     public void createTransactInvokers() {
114         if (transactInvokers == null) {
115             try {
116                 transactInvokers = new HashMap<>();
117                 DatabaseSchema dbSchema = getSchema(HwvtepSchemaConstants.HARDWARE_VTEP).get();
118                 if(dbSchema != null) {
119                     transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
120                 }
121             } catch (InterruptedException | ExecutionException e) {
122                 LOG.warn("Exception attempting to createTransactionInvokers {}", connectionInfo, e);
123             }
124         }
125     }
126
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);
140                     }
141                     monitorRequests.add(new MonitorRequestBuilder<>(tableSchema)
142                             .addColumns(columns)
143                             .with(new MonitorSelect(true, true, true, true)).build());
144                 }
145             }
146             this.callback.update(monitor(dbSchema, monitorRequests, callback), dbSchema);
147         } else {
148             LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
149         }
150     }
151
152     private void updateConnectionAttributes() {
153         LOG.debug("Update attributes of ovsdb node ip: {} port: {}",
154                     this.initialCreatedData.getConnectionInfo().getRemoteIp(),
155                     this.initialCreatedData.getConnectionInfo().getRemotePort());
156         /*
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?
160          */
161     }
162
163     public DataBroker getDataBroker() {
164         return dataBroker;
165     }
166
167     public ListenableFuture<List<String>> getDatabases() {
168         return client.getDatabases();
169     }
170
171     public ListenableFuture<DatabaseSchema> getSchema(String database) {
172         return client.getSchema(database);
173     }
174
175     public TransactionBuilder transactBuilder(DatabaseSchema dbSchema) {
176         return client.transactBuilder(dbSchema);
177     }
178
179     public ListenableFuture<List<OperationResult>> transact(DatabaseSchema dbSchema, List<Operation> operations) {
180         return client.transact(dbSchema, operations);
181     }
182
183     public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
184                     List<MonitorRequest> monitorRequests, MonitorCallBack callback) {
185         return client.monitor(schema, monitorRequests, callback);
186     }
187
188     public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
189                     List<MonitorRequest> monitorRequests, MonitorHandle monitorHandle, MonitorCallBack callback) {
190         return null;
191     }
192
193     public void cancelMonitor(MonitorHandle handler) {
194         client.cancelMonitor(handler);
195     }
196
197     public void lock(String lockId, LockAquisitionCallback lockedCallBack, LockStolenCallback stolenCallback) {
198         client.lock(lockId, lockedCallBack, stolenCallback);
199     }
200
201     public ListenableFuture<Boolean> steal(String lockId) {
202         return client.steal(lockId);
203     }
204
205     public ListenableFuture<Boolean> unLock(String lockId) {
206         return client.unLock(lockId);
207     }
208
209     public OvsdbConnectionInfo getConnectionInfo() {
210         return client.getConnectionInfo();
211     }
212
213     public boolean isActive() {
214         return client.isActive();
215     }
216
217     public void disconnect() {
218         client.disconnect();
219     }
220
221     public DatabaseSchema getDatabaseSchema(String dbName) {
222         return client.getDatabaseSchema(dbName);
223     }
224
225     public <T extends TypedBaseTable<?>> T createTypedRowWrapper(Class<T> klazz) {
226         return client.createTypedRowWrapper(klazz);
227     }
228
229     public <T extends TypedBaseTable<?>> T createTypedRowWrapper(DatabaseSchema dbSchema, Class<T> klazz) {
230         return client.createTypedRowWrapper(dbSchema, klazz);
231     }
232
233     public <T extends TypedBaseTable<?>> T getTypedRowWrapper(Class<T> klazz, Row<GenericTableSchema> row) {
234         return client.getTypedRowWrapper(klazz, row);
235     }
236
237     public ConnectionInfo getMDConnectionInfo() {
238         return connectionInfo;
239     }
240
241     public void setMDConnectionInfo(ConnectionInfo key) {
242         this.connectionInfo = key;
243     }
244
245     public InstanceIdentifier<Node> getInstanceIdentifier() {
246         return instanceIdentifier;
247     }
248
249     public NodeKey getNodeKey() {
250         //TODO: What is the alternative here?
251         return getInstanceIdentifier().firstKeyOf(Node.class);
252     }
253
254     public NodeId getNodeId() {
255         return getNodeKey().getNodeId();
256     }
257
258     public void setInstanceIdentifier(InstanceIdentifier<Node> iid) {
259         this.instanceIdentifier = iid;
260         hwvtepConnectionManager.putConnectionInstance(instanceIdentifier, this);
261     }
262
263     public Entity getConnectedEntity() {
264         return this.connectedEntity;
265     }
266
267     public void setConnectedEntity(Entity entity ) {
268         this.connectedEntity = entity;
269     }
270
271     public Boolean hasOvsdbClient(OvsdbClient otherClient) {
272         return client.equals(otherClient);
273     }
274
275     public Boolean getHasDeviceOwnership() {
276         return hasDeviceOwnership;
277     }
278
279     public void setHasDeviceOwnership(Boolean hasDeviceOwnership) {
280         if (hasDeviceOwnership != null) {
281             this.hasDeviceOwnership = hasDeviceOwnership;
282         }
283     }
284
285     public void setDeviceOwnershipCandidateRegistration(@Nonnull EntityOwnershipCandidateRegistration registration) {
286         this.deviceOwnershipCandidateRegistration = registration;
287     }
288
289     public void closeDeviceOwnershipCandidateRegistration() {
290         if (deviceOwnershipCandidateRegistration != null) {
291             this.deviceOwnershipCandidateRegistration.close();
292             setHasDeviceOwnership(Boolean.FALSE);
293         }
294     }
295
296     public void setHwvtepGlobalAugmentation(HwvtepGlobalAugmentation hwvtepGlobalData) {
297         this.initialCreatedData = hwvtepGlobalData;
298     }
299
300     public HwvtepGlobalAugmentation getHwvtepGlobalAugmentation() {
301         return this.initialCreatedData;
302     }
303     public HwvtepDeviceInfo getDeviceInfo() {
304         return this.deviceInfo;
305     }
306
307     public OvsdbClient getOvsdbClient() {
308         return client;
309     }
310 }