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