Bug 8055: use lambdas instead of anonymous classes
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / OvsdbConnectionManager.java
1 /*
2  * Copyright © 2014, 2017 Cisco Systems, Inc. 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 package org.opendaylight.ovsdb.southbound;
9
10 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
11
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17
18 import java.net.ConnectException;
19 import java.net.InetAddress;
20 import java.net.UnknownHostException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28 import javax.annotation.Nonnull;
29 import javax.annotation.Nullable;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
32 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
33 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
34 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
35 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
36 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
37 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
38 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
39 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
40 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
41 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
42 import org.opendaylight.ovsdb.lib.OvsdbClient;
43 import org.opendaylight.ovsdb.lib.OvsdbConnection;
44 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
45 import org.opendaylight.ovsdb.lib.operations.Operation;
46 import org.opendaylight.ovsdb.lib.operations.OperationResult;
47 import org.opendaylight.ovsdb.lib.operations.Select;
48 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
49 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
50 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
51 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
52 import org.opendaylight.ovsdb.southbound.reconciliation.ReconciliationManager;
53 import org.opendaylight.ovsdb.southbound.reconciliation.ReconciliationTask;
54 import org.opendaylight.ovsdb.southbound.reconciliation.configuration.BridgeConfigReconciliationTask;
55 import org.opendaylight.ovsdb.southbound.reconciliation.connection.ConnectionReconciliationTask;
56 import org.opendaylight.ovsdb.southbound.transactions.md.OvsdbNodeRemoveCommand;
57 import org.opendaylight.ovsdb.southbound.transactions.md.TransactionInvoker;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAttributes;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
63 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
64 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
65 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
66 import org.slf4j.Logger;
67 import org.slf4j.LoggerFactory;
68
69 public class OvsdbConnectionManager implements OvsdbConnectionListener, AutoCloseable {
70     private Map<ConnectionInfo, OvsdbConnectionInstance> clients =
71             new ConcurrentHashMap<>();
72     private static final Logger LOG = LoggerFactory.getLogger(OvsdbConnectionManager.class);
73     private static final String ENTITY_TYPE = "ovsdb";
74     private static final int DB_FETCH_TIMEOUT = 1000;
75
76     private DataBroker db;
77     private TransactionInvoker txInvoker;
78     private Map<ConnectionInfo,InstanceIdentifier<Node>> instanceIdentifiers =
79             new ConcurrentHashMap<>();
80     private Map<Entity, OvsdbConnectionInstance> entityConnectionMap =
81             new ConcurrentHashMap<>();
82     private EntityOwnershipService entityOwnershipService;
83     private OvsdbDeviceEntityOwnershipListener ovsdbDeviceEntityOwnershipListener;
84     private OvsdbConnection ovsdbConnection;
85     private final ReconciliationManager reconciliationManager;
86     private final InstanceIdentifierCodec instanceIdentifierCodec;
87
88     public OvsdbConnectionManager(DataBroker db,TransactionInvoker txInvoker,
89                                   EntityOwnershipService entityOwnershipService,
90                                   OvsdbConnection ovsdbConnection,
91                                   InstanceIdentifierCodec instanceIdentifierCodec) {
92         this.db = db;
93         this.txInvoker = txInvoker;
94         this.entityOwnershipService = entityOwnershipService;
95         this.ovsdbDeviceEntityOwnershipListener = new OvsdbDeviceEntityOwnershipListener(this, entityOwnershipService);
96         this.ovsdbConnection = ovsdbConnection;
97         this.reconciliationManager = new ReconciliationManager(db, instanceIdentifierCodec);
98         this.instanceIdentifierCodec = instanceIdentifierCodec;
99     }
100
101     @Override
102     public void connected(@Nonnull final OvsdbClient externalClient) {
103         LOG.info("Library connected {} from {}:{} to {}:{}",
104                 externalClient.getConnectionInfo().getType(),
105                 externalClient.getConnectionInfo().getRemoteAddress(),
106                 externalClient.getConnectionInfo().getRemotePort(),
107                 externalClient.getConnectionInfo().getLocalAddress(),
108                 externalClient.getConnectionInfo().getLocalPort());
109         List<String> databases = new ArrayList<>();
110         try {
111             databases = externalClient.getDatabases().get(DB_FETCH_TIMEOUT, TimeUnit.MILLISECONDS);
112             if (databases.contains(SouthboundConstants.OPEN_V_SWITCH)) {
113                 OvsdbConnectionInstance client = connectedButCallBacksNotRegistered(externalClient);
114                 // Register Cluster Ownership for ConnectionInfo
115                 registerEntityForOwnership(client);
116             }
117         } catch (InterruptedException | ExecutionException | TimeoutException e) {
118             LOG.warn("Unable to fetch Database list from device {}. Disconnecting from the device.",
119                     externalClient.getConnectionInfo().getRemoteAddress(), e);
120             externalClient.disconnect();
121         }
122
123     }
124
125     public OvsdbConnectionInstance connectedButCallBacksNotRegistered(final OvsdbClient externalClient) {
126         LOG.info("OVSDB Connection from {}:{}",externalClient.getConnectionInfo().getRemoteAddress(),
127                 externalClient.getConnectionInfo().getRemotePort());
128         ConnectionInfo key = SouthboundMapper.createConnectionInfo(externalClient);
129         OvsdbConnectionInstance ovsdbConnectionInstance = getConnectionInstance(key);
130
131         // Check if existing ovsdbConnectionInstance for the OvsdbClient present.
132         // In such cases, we will see if the ovsdbConnectionInstance has same externalClient.
133         if (ovsdbConnectionInstance != null) {
134             if (ovsdbConnectionInstance.hasOvsdbClient(externalClient)) {
135                 LOG.warn("OVSDB Connection Instance {} already exists for client {}", key, externalClient);
136                 return ovsdbConnectionInstance;
137             }
138             LOG.warn("OVSDB Connection Instance {} being replaced with client {}", key, externalClient);
139
140             // Unregister Cluster Ownership for ConnectionInfo
141             // Because the ovsdbConnectionInstance is about to be completely replaced!
142             unregisterEntityForOwnership(ovsdbConnectionInstance);
143
144             ovsdbConnectionInstance.disconnect();
145
146             removeConnectionInstance(key);
147
148             stopBridgeConfigReconciliationIfActive(ovsdbConnectionInstance.getInstanceIdentifier());
149         }
150
151         ovsdbConnectionInstance = new OvsdbConnectionInstance(key, externalClient, txInvoker,
152                 getInstanceIdentifier(key));
153         ovsdbConnectionInstance.createTransactInvokers();
154         return ovsdbConnectionInstance;
155     }
156
157     @Override
158     public void disconnected(OvsdbClient client) {
159         LOG.info("Library disconnected {} from {}:{} to {}:{}. Cleaning up the operational data store",
160                 client.getConnectionInfo().getType(),
161                 client.getConnectionInfo().getRemoteAddress(),
162                 client.getConnectionInfo().getRemotePort(),
163                 client.getConnectionInfo().getLocalAddress(),
164                 client.getConnectionInfo().getLocalPort());
165         ConnectionInfo key = SouthboundMapper.createConnectionInfo(client);
166         OvsdbConnectionInstance ovsdbConnectionInstance = getConnectionInstance(key);
167         if (ovsdbConnectionInstance != null) {
168             // Unregister Entity ownership as soon as possible ,so this instance should
169             // not be used as a candidate in Entity election (given that this instance is
170             // about to disconnect as well), if current owner get disconnected from
171             // OVSDB device.
172             unregisterEntityForOwnership(ovsdbConnectionInstance);
173
174             txInvoker.invoke(new OvsdbNodeRemoveCommand(ovsdbConnectionInstance, null, null));
175
176             removeConnectionInstance(key);
177
178             //Controller initiated connection can be terminated from switch side.
179             //So cleanup the instance identifier cache.
180             removeInstanceIdentifier(key);
181             stopBridgeConfigReconciliationIfActive(ovsdbConnectionInstance.getInstanceIdentifier());
182             retryConnection(ovsdbConnectionInstance.getInstanceIdentifier(),
183                     ovsdbConnectionInstance.getOvsdbNodeAugmentation(),
184                     ConnectionReconciliationTriggers.ON_DISCONNECT);
185         } else {
186             LOG.warn("disconnected : Connection instance not found for OVSDB Node {} ", key);
187         }
188         LOG.trace("OvsdbConnectionManager: exit disconnected client: {}", client);
189     }
190
191     public OvsdbClient connect(InstanceIdentifier<Node> iid,
192             OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException, ConnectException {
193         LOG.info("Connecting to {}", SouthboundUtil.connectionInfoToString(ovsdbNode.getConnectionInfo()));
194
195         // TODO handle case where we already have a connection
196         // TODO use transaction chains to handle ordering issues between disconnected
197         // TODO and connected when writing to the operational store
198         InetAddress ip = SouthboundMapper.createInetAddress(ovsdbNode.getConnectionInfo().getRemoteIp());
199         OvsdbClient client = ovsdbConnection.connect(ip,
200                 ovsdbNode.getConnectionInfo().getRemotePort().getValue());
201         // For connections from the controller to the ovs instance, the library doesn't call
202         // this method for us
203         if (client != null) {
204             putInstanceIdentifier(ovsdbNode.getConnectionInfo(), iid.firstIdentifierOf(Node.class));
205             OvsdbConnectionInstance ovsdbConnectionInstance = connectedButCallBacksNotRegistered(client);
206             ovsdbConnectionInstance.setOvsdbNodeAugmentation(ovsdbNode);
207
208             // Register Cluster Ownership for ConnectionInfo
209             registerEntityForOwnership(ovsdbConnectionInstance);
210         } else {
211             LOG.warn("Failed to connect to OVSDB Node {}", ovsdbNode.getConnectionInfo());
212         }
213         return client;
214     }
215
216     public void disconnect(OvsdbNodeAugmentation ovsdbNode) throws UnknownHostException {
217         LOG.info("Disconnecting from {}", SouthboundUtil.connectionInfoToString(ovsdbNode.getConnectionInfo()));
218         OvsdbConnectionInstance client = getConnectionInstance(ovsdbNode.getConnectionInfo());
219         if (client != null) {
220             // Unregister Cluster Onwership for ConnectionInfo
221             unregisterEntityForOwnership(client);
222
223             client.disconnect();
224
225             removeInstanceIdentifier(ovsdbNode.getConnectionInfo());
226
227             stopBridgeConfigReconciliationIfActive(client.getInstanceIdentifier());
228         } else {
229             LOG.debug("disconnect : connection instance not found for {}",ovsdbNode.getConnectionInfo());
230         }
231     }
232
233 /*    public void init(ConnectionInfo key) {
234         OvsdbConnectionInstance client = getConnectionInstance(key);
235
236         // TODO (FF): make sure that this cluster instance is the 'entity owner' fo the given OvsdbConnectionInstance ?
237
238         if (client != null) {
239
240              *  Note: registerCallbacks() is idemPotent... so if you call it repeatedly all is safe,
241              *  it only registersCallbacks on the *first* call.
242
243             client.registerCallbacks();
244         }
245     }
246 */
247     @Override
248     public void close() {
249         if (ovsdbDeviceEntityOwnershipListener != null) {
250             ovsdbDeviceEntityOwnershipListener.close();
251         }
252
253         for (OvsdbConnectionInstance client: clients.values()) {
254             client.disconnect();
255         }
256     }
257
258     private void putConnectionInstance(ConnectionInfo key,OvsdbConnectionInstance instance) {
259         ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
260         clients.put(connectionInfo, instance);
261     }
262
263     private void removeConnectionInstance(ConnectionInfo key) {
264         ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
265         clients.remove(connectionInfo);
266     }
267
268     private void putInstanceIdentifier(ConnectionInfo key,InstanceIdentifier<Node> iid) {
269         ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
270         instanceIdentifiers.put(connectionInfo, iid);
271     }
272
273     private void removeInstanceIdentifier(ConnectionInfo key) {
274         ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
275         instanceIdentifiers.remove(connectionInfo);
276     }
277
278     public InstanceIdentifier<Node> getInstanceIdentifier(ConnectionInfo key) {
279         ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
280         return instanceIdentifiers.get(connectionInfo);
281     }
282
283     public OvsdbConnectionInstance getConnectionInstance(ConnectionInfo key) {
284         ConnectionInfo connectionInfo = SouthboundMapper.suppressLocalIpPort(key);
285         return clients.get(connectionInfo);
286     }
287
288     public OvsdbConnectionInstance getConnectionInstance(OvsdbBridgeAttributes mn) {
289         Optional<OvsdbNodeAugmentation> optional = SouthboundUtil.getManagingNode(db, mn);
290         if (optional.isPresent()) {
291             return getConnectionInstance(optional.get().getConnectionInfo());
292         } else {
293             return null;
294         }
295     }
296
297     public OvsdbConnectionInstance getConnectionInstance(Node node) {
298         Preconditions.checkNotNull(node);
299         OvsdbNodeAugmentation ovsdbNode = node.getAugmentation(OvsdbNodeAugmentation.class);
300         OvsdbBridgeAugmentation ovsdbManagedNode = node.getAugmentation(OvsdbBridgeAugmentation.class);
301         if (ovsdbNode != null) {
302             return getConnectionInstance(ovsdbNode.getConnectionInfo());
303         } else if (ovsdbManagedNode != null) {
304             return getConnectionInstance(ovsdbManagedNode);
305         } else {
306             LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
307             return null;
308         }
309     }
310
311     public OvsdbConnectionInstance getConnectionInstance(InstanceIdentifier<Node> nodePath) {
312         try {
313             ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
314             CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = transaction.read(
315                     LogicalDatastoreType.OPERATIONAL, nodePath);
316             transaction.close();
317             Optional<Node> optional = nodeFuture.get();
318             if (optional.isPresent()) {
319                 return this.getConnectionInstance(optional.get());
320             } else {
321                 LOG.debug("Node was not found on the path in the operational DS: {}", nodePath);
322                 return null;
323             }
324         } catch (InterruptedException | ExecutionException e) {
325             LOG.warn("Failed to get Ovsdb Node {}",nodePath, e);
326             return null;
327         }
328     }
329
330     public OvsdbClient getClient(ConnectionInfo connectionInfo) {
331         OvsdbConnectionInstance connectionInstance = getConnectionInstance(connectionInfo);
332         if (connectionInstance != null) {
333             return connectionInstance.getOvsdbClient();
334         }
335         return null;
336     }
337
338     public OvsdbClient getClient(OvsdbBridgeAttributes mn) {
339         return getConnectionInstance(mn).getOvsdbClient();
340     }
341
342     public OvsdbClient getClient(Node node) {
343         return getConnectionInstance(node).getOvsdbClient();
344     }
345
346     public Boolean getHasDeviceOwnership(ConnectionInfo connectionInfo) {
347         OvsdbConnectionInstance ovsdbConnectionInstance = getConnectionInstance(connectionInfo);
348         if (ovsdbConnectionInstance == null) {
349             return Boolean.FALSE;
350         }
351         return ovsdbConnectionInstance.getHasDeviceOwnership();
352     }
353
354     public void reconcileConnection(InstanceIdentifier<Node> iid, OvsdbNodeAugmentation ovsdbNode) {
355         this.retryConnection(iid, ovsdbNode,
356                 ConnectionReconciliationTriggers.ON_CONTROLLER_INITIATED_CONNECTION_FAILURE);
357
358     }
359
360     public void stopConnectionReconciliationIfActive(InstanceIdentifier<?> iid, OvsdbNodeAugmentation ovsdbNode) {
361         final ReconciliationTask task = new ConnectionReconciliationTask(
362                 reconciliationManager,
363                 this,
364                 iid,
365                 ovsdbNode);
366         reconciliationManager.dequeue(task);
367     }
368
369     public void stopBridgeConfigReconciliationIfActive(InstanceIdentifier<?> iid) {
370         final ReconciliationTask task =
371                 new BridgeConfigReconciliationTask(reconciliationManager, this, iid, null, instanceIdentifierCodec);
372         reconciliationManager.dequeue(task);
373         reconciliationManager.cancelTerminationPointReconciliation();
374     }
375
376     private void handleOwnershipChanged(EntityOwnershipChange ownershipChange) {
377         OvsdbConnectionInstance ovsdbConnectionInstance = getConnectionInstanceFromEntity(ownershipChange.getEntity());
378         LOG.debug("handleOwnershipChanged: {} event received for device {}",
379                 ownershipChange, ovsdbConnectionInstance != null ? ovsdbConnectionInstance.getConnectionInfo()
380                         : "that's currently NOT registered by *this* southbound plugin instance");
381
382         if (ovsdbConnectionInstance == null) {
383             if (ownershipChange.isOwner()) {
384                 LOG.warn("handleOwnershipChanged: *this* instance is elected as an owner of the device {} but it "
385                         + "is NOT registered for ownership", ownershipChange.getEntity());
386             } else {
387                 // EntityOwnershipService sends notification to all the nodes, irrespective of whether
388                 // that instance registered for the device ownership or not. It is to make sure that
389                 // If all the controller instance that was connected to the device are down, so the
390                 // running instance can clear up the operational data store even though it was not
391                 // connected to the device.
392                 LOG.debug("handleOwnershipChanged: No connection instance found for {}", ownershipChange.getEntity());
393             }
394
395             // If entity has no owner, clean up the operational data store (it's possible because owner controller
396             // might went down abruptly and didn't get a chance to clean up the operational data store.
397             if (!ownershipChange.hasOwner()) {
398                 LOG.info("{} has no owner, cleaning up the operational data store", ownershipChange.getEntity());
399                 cleanEntityOperationalData(ownershipChange.getEntity());
400             }
401             return;
402         }
403         //Connection detail need to be cached, irrespective of ownership result.
404         putConnectionInstance(ovsdbConnectionInstance.getMDConnectionInfo(),ovsdbConnectionInstance);
405
406         if (ownershipChange.isOwner() == ovsdbConnectionInstance.getHasDeviceOwnership()) {
407             LOG.info("handleOwnershipChanged: no change in ownership for {}. Ownership status is : {}",
408                     ovsdbConnectionInstance.getConnectionInfo(), ovsdbConnectionInstance.getHasDeviceOwnership()
409                             ? SouthboundConstants.OwnershipStates.OWNER.getState()
410                             : SouthboundConstants.OwnershipStates.NONOWNER.getState());
411             return;
412         }
413
414         ovsdbConnectionInstance.setHasDeviceOwnership(ownershipChange.isOwner());
415         // You were not an owner, but now you are
416         if (ownershipChange.isOwner()) {
417             LOG.info("handleOwnershipChanged: *this* southbound plugin instance is an OWNER of the device {}",
418                     ovsdbConnectionInstance.getConnectionInfo());
419
420             //*this* instance of southbound plugin is owner of the device,
421             //so register for monitor callbacks
422             ovsdbConnectionInstance.registerCallbacks(instanceIdentifierCodec);
423
424             reconcileBridgeConfigurations(ovsdbConnectionInstance);
425         } else {
426             //You were owner of the device, but now you are not. With the current ownership
427             //grant mechanism, this scenario should not occur. Because this scenario will occur
428             //when this controller went down or switch flap the connection, but in both the case
429             //it will go through the re-registration process. We need to implement this condition
430             //when clustering service implement a ownership grant strategy which can revoke the
431             //device ownership for load balancing the devices across the instances.
432             //Once this condition occur, we should unregister the callback.
433             LOG.error("handleOwnershipChanged: *this* southbound plugin instance is no longer the owner of device {}."
434                     + "This should NOT happen.",
435                     ovsdbConnectionInstance.getNodeId().getValue());
436         }
437     }
438
439     private void cleanEntityOperationalData(Entity entity) {
440
441         //Do explicit cleanup rather than using OvsdbNodeRemoveCommand, because there
442         // are chances that other controller instance went down abruptly and it does
443         // not clear manager entry, which OvsdbNodeRemoveCommand look for before cleanup.
444
445         @SuppressWarnings("unchecked") final InstanceIdentifier<Node> nodeIid =
446                 (InstanceIdentifier<Node>) instanceIdentifierCodec.bindingDeserializer(entity.getId());
447
448         txInvoker.invoke(transaction -> {
449             Optional<Node> ovsdbNodeOpt = SouthboundUtil.readNode(transaction, nodeIid);
450             if (ovsdbNodeOpt.isPresent()) {
451                 Node ovsdbNode = ovsdbNodeOpt.get();
452                 OvsdbNodeAugmentation nodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
453                 if (nodeAugmentation != null) {
454                     if (nodeAugmentation.getManagedNodeEntry() != null) {
455                         for (ManagedNodeEntry managedNode : nodeAugmentation.getManagedNodeEntry()) {
456                             transaction.delete(
457                                     LogicalDatastoreType.OPERATIONAL, managedNode.getBridgeRef().getValue());
458                         }
459                     } else {
460                         LOG.debug("{} had no managed nodes", ovsdbNode.getNodeId().getValue());
461                     }
462                 }
463                 transaction.delete(LogicalDatastoreType.OPERATIONAL, nodeIid);
464             }
465         });
466
467     }
468
469     private OpenVSwitch getOpenVswitchTableEntry(OvsdbConnectionInstance connectionInstance) {
470         DatabaseSchema dbSchema = null;
471         OpenVSwitch openVSwitchRow = null;
472         try {
473             dbSchema = connectionInstance.getSchema(OvsdbSchemaContants.DATABASE_NAME).get();
474         } catch (InterruptedException | ExecutionException e) {
475             LOG.warn("Not able to fetch schema for database {} from device {}",
476                     OvsdbSchemaContants.DATABASE_NAME,connectionInstance.getConnectionInfo(),e);
477         }
478         if (dbSchema != null) {
479             GenericTableSchema openVSwitchSchema = TyperUtils.getTableSchema(dbSchema, OpenVSwitch.class);
480
481             List<String> openVSwitchTableColumn = new ArrayList<>();
482             openVSwitchTableColumn.addAll(openVSwitchSchema.getColumns());
483             Select<GenericTableSchema> selectOperation = op.select(openVSwitchSchema);
484             selectOperation.setColumns(openVSwitchTableColumn);
485
486             List<Operation> operations = new ArrayList<>();
487             operations.add(selectOperation);
488             operations.add(op.comment("Fetching Open_VSwitch table rows"));
489             try {
490                 List<OperationResult> results = connectionInstance.transact(dbSchema, operations).get();
491                 if (results != null) {
492                     OperationResult selectResult = results.get(0);
493                     openVSwitchRow = TyperUtils.getTypedRowWrapper(
494                             dbSchema,OpenVSwitch.class,selectResult.getRows().get(0));
495
496                 }
497             } catch (InterruptedException | ExecutionException e) {
498                 LOG.warn("Not able to fetch OpenVswitch table row from device {}",
499                         connectionInstance.getConnectionInfo(),e);
500             }
501         }
502         return openVSwitchRow;
503     }
504
505     private Entity getEntityFromConnectionInstance(@Nonnull OvsdbConnectionInstance ovsdbConnectionInstance) {
506         InstanceIdentifier<Node> iid = ovsdbConnectionInstance.getInstanceIdentifier();
507         if (iid == null) {
508             /* Switch initiated connection won't have iid, till it gets OpenVSwitch
509              * table update but update callback is always registered after ownership
510              * is granted. So we are explicitly fetch the row here to get the iid.
511              */
512             OpenVSwitch openvswitchRow = getOpenVswitchTableEntry(ovsdbConnectionInstance);
513             iid = SouthboundMapper.getInstanceIdentifier(instanceIdentifierCodec, openvswitchRow);
514             LOG.info("InstanceIdentifier {} generated for device "
515                     + "connection {}",iid,ovsdbConnectionInstance.getConnectionInfo());
516             ovsdbConnectionInstance.setInstanceIdentifier(iid);
517         }
518         YangInstanceIdentifier entityId = instanceIdentifierCodec.getYangInstanceIdentifier(iid);
519         Entity deviceEntity = new Entity(ENTITY_TYPE, entityId);
520         LOG.debug("Entity {} created for device connection {}",
521                 deviceEntity, ovsdbConnectionInstance.getConnectionInfo());
522         return deviceEntity;
523     }
524
525     private OvsdbConnectionInstance getConnectionInstanceFromEntity(Entity entity) {
526         return entityConnectionMap.get(entity);
527     }
528
529     private void registerEntityForOwnership(OvsdbConnectionInstance ovsdbConnectionInstance) {
530
531         Entity candidateEntity = getEntityFromConnectionInstance(ovsdbConnectionInstance);
532         entityConnectionMap.put(candidateEntity, ovsdbConnectionInstance);
533         ovsdbConnectionInstance.setConnectedEntity(candidateEntity);
534         try {
535             EntityOwnershipCandidateRegistration registration =
536                     entityOwnershipService.registerCandidate(candidateEntity);
537             ovsdbConnectionInstance.setDeviceOwnershipCandidateRegistration(registration);
538             LOG.info("OVSDB entity {} is registered for ownership.", candidateEntity);
539
540             //If entity already has owner, it won't get notification from EntityOwnershipService
541             //so cache the connection instances.
542             Optional<EntityOwnershipState> ownershipStateOpt =
543                     entityOwnershipService.getOwnershipState(candidateEntity);
544             if (ownershipStateOpt.isPresent()) {
545                 EntityOwnershipState ownershipState = ownershipStateOpt.get();
546                 if (ownershipState.hasOwner() && !ownershipState.isOwner()) {
547                     LOG.info("OVSDB entity {} is already owned by other southbound plugin "
548                                     + "instance, so *this* instance is NOT an OWNER of the device",
549                             ovsdbConnectionInstance.getConnectionInfo());
550                     putConnectionInstance(ovsdbConnectionInstance.getMDConnectionInfo(),ovsdbConnectionInstance);
551                 }
552             }
553         } catch (CandidateAlreadyRegisteredException e) {
554             LOG.warn("OVSDB entity {} was already registered for ownership", candidateEntity, e);
555         }
556
557     }
558
559     private void unregisterEntityForOwnership(OvsdbConnectionInstance ovsdbConnectionInstance) {
560         ovsdbConnectionInstance.closeDeviceOwnershipCandidateRegistration();
561         entityConnectionMap.remove(ovsdbConnectionInstance.getConnectedEntity());
562     }
563
564     private void retryConnection(final InstanceIdentifier<Node> iid, final OvsdbNodeAugmentation ovsdbNode,
565                                  ConnectionReconciliationTriggers trigger) {
566         final ReconciliationTask task = new ConnectionReconciliationTask(
567                 reconciliationManager,
568                 this,
569                 iid,
570                 ovsdbNode);
571
572         if (reconciliationManager.isEnqueued(task)) {
573             return;
574         }
575         switch (trigger) {
576             case ON_CONTROLLER_INITIATED_CONNECTION_FAILURE:
577                 reconciliationManager.enqueueForRetry(task);
578                 break;
579             case ON_DISCONNECT:
580             {
581                 ReadOnlyTransaction tx = db.newReadOnlyTransaction();
582                 CheckedFuture<Optional<Node>, ReadFailedException> readNodeFuture =
583                         tx.read(LogicalDatastoreType.CONFIGURATION, iid);
584
585                 final OvsdbConnectionManager connectionManager = this;
586                 Futures.addCallback(readNodeFuture, new FutureCallback<Optional<Node>>() {
587                     @Override
588                     public void onSuccess(@Nullable Optional<Node> node) {
589                         if (node.isPresent()) {
590                             LOG.info("Disconnected/Failed connection {} was controller initiated, attempting "
591                                     + "reconnection", ovsdbNode.getConnectionInfo());
592                             reconciliationManager.enqueue(task);
593
594                         } else {
595                             LOG.debug("Connection {} was switch initiated, no reconciliation is required",
596                                     iid.firstKeyOf(Node.class).getNodeId());
597                         }
598                     }
599
600                     @Override
601                     public void onFailure(Throwable throwable) {
602                         LOG.warn("Read Config/DS for Node failed! {}", iid, throwable);
603                     }
604                 });
605                 break;
606             }
607             default:
608                 break;
609         }
610     }
611
612     private void reconcileBridgeConfigurations(final OvsdbConnectionInstance client) {
613         final InstanceIdentifier<Node> nodeIid = client.getInstanceIdentifier();
614         final ReconciliationTask task = new BridgeConfigReconciliationTask(
615                 reconciliationManager, OvsdbConnectionManager.this, nodeIid, client, instanceIdentifierCodec);
616
617         reconciliationManager.enqueue(task);
618     }
619
620     private class OvsdbDeviceEntityOwnershipListener implements EntityOwnershipListener {
621         private OvsdbConnectionManager cm;
622         private EntityOwnershipListenerRegistration listenerRegistration;
623
624         OvsdbDeviceEntityOwnershipListener(OvsdbConnectionManager cm, EntityOwnershipService entityOwnershipService) {
625             this.cm = cm;
626             listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
627         }
628
629         public void close() {
630             listenerRegistration.close();
631         }
632
633         @Override
634         public void ownershipChanged(EntityOwnershipChange ownershipChange) {
635             cm.handleOwnershipChanged(ownershipChange);
636         }
637     }
638
639     private enum ConnectionReconciliationTriggers {
640         /*
641         Reconciliation trigger for scenario where controller's attempt
642         to connect to switch fails on config data store notification
643         */
644         ON_CONTROLLER_INITIATED_CONNECTION_FAILURE,
645
646         /*
647         Reconciliation trigger for the scenario where controller
648         initiated connection disconnects.
649         */
650         ON_DISCONNECT
651     }
652 }