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