Bug 6692: use non-deprecated firstKeyOf() variant
[ovsdb.git] / hwvtepsouthbound / hwvtepsouthbound-impl / src / main / java / org / opendaylight / ovsdb / hwvtepsouthbound / HwvtepConnectionInstance.java
1 /*
2  * Copyright (c) 2015, 2016 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.List;
13 import java.util.Map;
14 import java.util.Set;
15 import java.util.concurrent.ExecutionException;
16
17 import javax.annotation.Nonnull;
18
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;
52
53 import com.google.common.collect.Lists;
54 import com.google.common.util.concurrent.ListenableFuture;
55
56 public class HwvtepConnectionInstance {
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;
68     private HwvtepDeviceInfo deviceInfo;
69
70     HwvtepConnectionInstance (ConnectionInfo key, OvsdbClient client,
71                               InstanceIdentifier<Node> iid, TransactionInvoker txInvoker) {
72         this.connectionInfo = key;
73         this.client = client;
74         this.instanceIdentifier = iid;
75         this.txInvoker = txInvoker;
76         this.deviceInfo = new HwvtepDeviceInfo();
77     }
78
79     public void transact(TransactCommand command) {
80         for (TransactInvoker transactInvoker: transactInvokers.values()) {
81             transactInvoker.invoke(command);
82         }
83     }
84
85     public void registerCallbacks() {
86         if ( this.callback == null) {
87             if(this.initialCreatedData != null) {
88                 this.updateConnectionAttributes();
89             }
90
91             try {
92                 String database = HwvtepSchemaConstants.HARDWARE_VTEP;
93                 DatabaseSchema dbSchema = getSchema(database).get();
94                 if (dbSchema != null) {
95                     LOG.info("Monitoring database: {}", database);
96                     callback = new HwvtepMonitorCallback(this, txInvoker);
97                     monitorAllTables(database, dbSchema);
98                 } else {
99                     LOG.info("No database {} found on {}", database, connectionInfo);
100                 }
101             } catch (InterruptedException | ExecutionException e) {
102                 LOG.warn("Exception attempting to registerCallbacks {}: ", connectionInfo, e);
103             }
104         }
105     }
106
107     public void createTransactInvokers() {
108         if (transactInvokers == null) {
109             try {
110                 transactInvokers = new HashMap<>();
111                 DatabaseSchema dbSchema = getSchema(HwvtepSchemaConstants.HARDWARE_VTEP).get();
112                 if(dbSchema != null) {
113                     transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
114                 }
115             } catch (InterruptedException | ExecutionException e) {
116                 LOG.warn("Exception attempting to createTransactionInvokers {}", connectionInfo, e);
117             }
118         }
119     }
120
121     private void monitorAllTables(String database, DatabaseSchema dbSchema) {
122         Set<String> tables = dbSchema.getTables();
123         if (tables != null) {
124             List<MonitorRequest> monitorRequests = Lists.newArrayList();
125             for (String tableName : tables) {
126                 LOG.debug("HwvtepSouthbound monitoring table {} in {}", tableName, dbSchema.getName());
127                 GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
128                 Set<String> columns = tableSchema.getColumns();
129                 monitorRequests.add(new MonitorRequestBuilder<>(tableSchema)
130                         .addColumns(columns)
131                         .with(new MonitorSelect(true, true, true, true)).build());
132             }
133             this.callback.update(monitor(dbSchema, monitorRequests, callback),dbSchema);
134         } else {
135             LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
136         }
137     }
138
139     private void updateConnectionAttributes() {
140         LOG.debug("Update attributes of ovsdb node ip: {} port: {}",
141                     this.initialCreatedData.getConnectionInfo().getRemoteIp(),
142                     this.initialCreatedData.getConnectionInfo().getRemotePort());
143         /*
144          * TODO: Do we have anything to update?
145          * Hwvtep doesn't have other_config or external_ids like
146          * Open_vSwitch. What else will be needed?
147          */
148     }
149
150     public ListenableFuture<List<String>> getDatabases() {
151         return client.getDatabases();
152     }
153
154     public ListenableFuture<DatabaseSchema> getSchema(String database) {
155         return client.getSchema(database);
156     }
157
158     public TransactionBuilder transactBuilder(DatabaseSchema dbSchema) {
159         return client.transactBuilder(dbSchema);
160     }
161
162     public ListenableFuture<List<OperationResult>> transact(DatabaseSchema dbSchema, List<Operation> operations) {
163         return client.transact(dbSchema, operations);
164     }
165
166     public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
167                     List<MonitorRequest> monitorRequests, MonitorCallBack callback) {
168         return client.monitor(schema, monitorRequests, callback);
169     }
170
171     public <E extends TableSchema<E>> TableUpdates monitor(DatabaseSchema schema,
172                     List<MonitorRequest> monitorRequests, MonitorHandle monitorHandle, MonitorCallBack callback) {
173         return null;
174     }
175
176     public void cancelMonitor(MonitorHandle handler) {
177         client.cancelMonitor(handler);
178     }
179
180     public void lock(String lockId, LockAquisitionCallback lockedCallBack, LockStolenCallback stolenCallback) {
181         client.lock(lockId, lockedCallBack, stolenCallback);
182     }
183
184     public ListenableFuture<Boolean> steal(String lockId) {
185         return client.steal(lockId);
186     }
187
188     public ListenableFuture<Boolean> unLock(String lockId) {
189         return client.unLock(lockId);
190     }
191
192     public OvsdbConnectionInfo getConnectionInfo() {
193         return client.getConnectionInfo();
194     }
195
196     public boolean isActive() {
197         return client.isActive();
198     }
199
200     public void disconnect() {
201         client.disconnect();
202     }
203
204     public DatabaseSchema getDatabaseSchema(String dbName) {
205         return client.getDatabaseSchema(dbName);
206     }
207
208     public <T extends TypedBaseTable<?>> T createTypedRowWrapper(Class<T> klazz) {
209         return client.createTypedRowWrapper(klazz);
210     }
211
212     public <T extends TypedBaseTable<?>> T createTypedRowWrapper(DatabaseSchema dbSchema, Class<T> klazz) {
213         return client.createTypedRowWrapper(dbSchema, klazz);
214     }
215
216     public <T extends TypedBaseTable<?>> T getTypedRowWrapper(Class<T> klazz, Row<GenericTableSchema> row) {
217         return client.getTypedRowWrapper(klazz, row);
218     }
219
220     public ConnectionInfo getMDConnectionInfo() {
221         return connectionInfo;
222     }
223
224     public void setMDConnectionInfo(ConnectionInfo key) {
225         this.connectionInfo = key;
226     }
227
228     public InstanceIdentifier<Node> getInstanceIdentifier() {
229         return instanceIdentifier;
230     }
231
232     public NodeKey getNodeKey() {
233         //TODO: What is the alternative here?
234         return getInstanceIdentifier().firstKeyOf(Node.class);
235     }
236
237     public NodeId getNodeId() {
238         return getNodeKey().getNodeId();
239     }
240
241     public void setInstanceIdentifier(InstanceIdentifier<Node> iid) {
242         this.instanceIdentifier = iid;
243     }
244
245     public Entity getConnectedEntity() {
246         return this.connectedEntity;
247     }
248
249     public void setConnectedEntity(Entity entity ) {
250         this.connectedEntity = entity;
251     }
252
253     public Boolean hasOvsdbClient(OvsdbClient otherClient) {
254         return client.equals(otherClient);
255     }
256
257     public Boolean getHasDeviceOwnership() {
258         return hasDeviceOwnership;
259     }
260
261     public void setHasDeviceOwnership(Boolean hasDeviceOwnership) {
262         if (hasDeviceOwnership != null) {
263             this.hasDeviceOwnership = hasDeviceOwnership;
264         }
265     }
266
267     public void setDeviceOwnershipCandidateRegistration(@Nonnull EntityOwnershipCandidateRegistration registration) {
268         this.deviceOwnershipCandidateRegistration = registration;
269     }
270
271     public void closeDeviceOwnershipCandidateRegistration() {
272         if (deviceOwnershipCandidateRegistration != null) {
273             this.deviceOwnershipCandidateRegistration.close();
274             setHasDeviceOwnership(Boolean.FALSE);
275         }
276     }
277
278     public void setHwvtepGlobalAugmentation(HwvtepGlobalAugmentation hwvtepGlobalData) {
279         this.initialCreatedData = hwvtepGlobalData;
280     }
281
282     public HwvtepGlobalAugmentation getHwvtepGlobalAugmentation() {
283         return this.initialCreatedData;
284     }
285     public HwvtepDeviceInfo getDeviceInfo() {
286         return this.deviceInfo;
287     }
288
289     public OvsdbClient getOvsdbClient() {
290         return client;
291     }
292 }