2 * Copyright (c) 2015, 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.hwvtepsouthbound;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
18 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
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.controller.md.sal.common.api.clustering.EntityOwnershipChange;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
24 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
25 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.ReconciliationManager;
29 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.ReconciliationTask;
30 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.configuration.HwvtepReconciliationTask;
31 import org.opendaylight.ovsdb.hwvtepsouthbound.reconciliation.connection.ConnectionReconciliationTask;
32 import org.opendaylight.ovsdb.hwvtepsouthbound.transact.DependencyQueue;
33 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.HwvtepGlobalRemoveCommand;
34 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
35 import org.opendaylight.ovsdb.lib.OvsdbClient;
36 import org.opendaylight.ovsdb.lib.OvsdbConnectionListener;
37 import org.opendaylight.ovsdb.lib.impl.OvsdbConnectionService;
38 import org.opendaylight.ovsdb.lib.operations.Operation;
39 import org.opendaylight.ovsdb.lib.operations.OperationResult;
40 import org.opendaylight.ovsdb.lib.operations.Select;
41 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
42 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
43 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
44 import org.opendaylight.ovsdb.schema.hardwarevtep.Global;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalSwitchAttributes;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import javax.annotation.Nonnull;
56 import javax.annotation.Nullable;
57 import java.net.ConnectException;
58 import java.net.InetAddress;
59 import java.net.UnknownHostException;
60 import java.util.ArrayList;
61 import java.util.List;
63 import java.util.concurrent.ConcurrentHashMap;
64 import java.util.concurrent.ExecutionException;
65 import java.util.concurrent.TimeUnit;
66 import java.util.concurrent.TimeoutException;
68 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
70 public class HwvtepConnectionManager implements OvsdbConnectionListener, AutoCloseable{
71 private Map<ConnectionInfo, HwvtepConnectionInstance> clients = new ConcurrentHashMap<>();
72 private static final Logger LOG = LoggerFactory.getLogger(HwvtepConnectionManager.class);
73 private static final String ENTITY_TYPE = "hwvtep";
74 private static final int DB_FETCH_TIMEOUT = 1000;
76 private DataBroker db;
77 private TransactionInvoker txInvoker;
78 private Map<ConnectionInfo,InstanceIdentifier<Node>> instanceIdentifiers = new ConcurrentHashMap<>();
79 private Map<Entity, HwvtepConnectionInstance> entityConnectionMap = new ConcurrentHashMap<>();
80 private EntityOwnershipService entityOwnershipService;
81 private HwvtepDeviceEntityOwnershipListener hwvtepDeviceEntityOwnershipListener;
82 private final ReconciliationManager reconciliationManager;
83 private final Map<InstanceIdentifier<Node>, HwvtepConnectionInstance> nodeIidVsConnectionInstance =
84 new ConcurrentHashMap<>();
86 public HwvtepConnectionManager(DataBroker db, TransactionInvoker txInvoker,
87 EntityOwnershipService entityOwnershipService) {
89 this.txInvoker = txInvoker;
90 this.entityOwnershipService = entityOwnershipService;
91 this.hwvtepDeviceEntityOwnershipListener = new HwvtepDeviceEntityOwnershipListener(this,entityOwnershipService);
92 this.reconciliationManager = new ReconciliationManager(db);
96 public void close() throws Exception {
97 if (hwvtepDeviceEntityOwnershipListener != null) {
98 hwvtepDeviceEntityOwnershipListener.close();
101 for (HwvtepConnectionInstance client: clients.values()) {
104 DependencyQueue.close();
108 public void connected(@Nonnull final OvsdbClient externalClient) {
109 LOG.info("Library connected {} from {}:{} to {}:{}",
110 externalClient.getConnectionInfo().getType(),
111 externalClient.getConnectionInfo().getRemoteAddress(),
112 externalClient.getConnectionInfo().getRemotePort(),
113 externalClient.getConnectionInfo().getLocalAddress(),
114 externalClient.getConnectionInfo().getLocalPort());
115 List<String> databases = new ArrayList<>();
117 databases = externalClient.getDatabases().get(DB_FETCH_TIMEOUT, TimeUnit.MILLISECONDS);
118 if(databases.contains(HwvtepSchemaConstants.HARDWARE_VTEP)) {
119 HwvtepConnectionInstance hwClient = connectedButCallBacksNotRegistered(externalClient);
120 registerEntityForOwnership(hwClient);
122 } catch (InterruptedException | ExecutionException | TimeoutException e) {
123 LOG.warn("Unable to fetch Database list from device {}. Disconnecting from the device.",
124 externalClient.getConnectionInfo().getRemoteAddress(), e);
125 externalClient.disconnect();
130 public void disconnected(OvsdbClient client) {
131 LOG.info("Library disconnected {} from {}:{} to {}:{}. Cleaning up the operational data store",
132 client.getConnectionInfo().getType(),
133 client.getConnectionInfo().getRemoteAddress(),
134 client.getConnectionInfo().getRemotePort(),
135 client.getConnectionInfo().getLocalAddress(),
136 client.getConnectionInfo().getLocalPort());
137 ConnectionInfo key = HwvtepSouthboundMapper.createConnectionInfo(client);
138 HwvtepConnectionInstance hwvtepConnectionInstance = getConnectionInstance(key);
139 if (hwvtepConnectionInstance != null) {
140 // Unregister Entity ownership as soon as possible ,so this instance should
141 // not be used as a candidate in Entity election (given that this instance is
142 // about to disconnect as well), if current owner get disconnected from
144 unregisterEntityForOwnership(hwvtepConnectionInstance);
146 //TODO: remove all the hwvtep nodes
147 txInvoker.invoke(new HwvtepGlobalRemoveCommand(hwvtepConnectionInstance, null, null));
149 removeConnectionInstance(key);
151 //Controller initiated connection can be terminated from switch side.
152 //So cleanup the instance identifier cache.
153 removeInstanceIdentifier(key);
154 removeConnectionInstance(hwvtepConnectionInstance.getInstanceIdentifier());
155 retryConnection(hwvtepConnectionInstance.getInstanceIdentifier(),
156 hwvtepConnectionInstance.getHwvtepGlobalAugmentation(),
157 ConnectionReconciliationTriggers.ON_DISCONNECT);
159 LOG.warn("HWVTEP disconnected event did not find connection instance for {}", key);
161 LOG.trace("HwvtepConnectionManager exit disconnected client: {}", client);
164 public OvsdbClient connect(InstanceIdentifier<Node> iid,
165 HwvtepGlobalAugmentation hwvtepGlobal) throws UnknownHostException, ConnectException {
166 LOG.info("Connecting to {}", HwvtepSouthboundUtil.connectionInfoToString(hwvtepGlobal.getConnectionInfo()));
167 InetAddress ip = HwvtepSouthboundMapper.createInetAddress(hwvtepGlobal.getConnectionInfo().getRemoteIp());
168 OvsdbClient client = OvsdbConnectionService.getService()
169 .connect(ip, hwvtepGlobal.getConnectionInfo().getRemotePort().getValue());
171 putInstanceIdentifier(hwvtepGlobal.getConnectionInfo(), iid.firstIdentifierOf(Node.class));
172 HwvtepConnectionInstance hwvtepConnectionInstance = connectedButCallBacksNotRegistered(client);
173 hwvtepConnectionInstance.setHwvtepGlobalAugmentation(hwvtepGlobal);
174 hwvtepConnectionInstance.setInstanceIdentifier(iid.firstIdentifierOf(Node.class));
176 // Register Cluster Ownership for ConnectionInfo
177 registerEntityForOwnership(hwvtepConnectionInstance);
179 LOG.warn("Failed to connect to OVSDB node: {}", hwvtepGlobal.getConnectionInfo());
183 public void disconnect(HwvtepGlobalAugmentation ovsdbNode) throws UnknownHostException {
184 LOG.info("Diconnecting from {}", HwvtepSouthboundUtil.connectionInfoToString(ovsdbNode.getConnectionInfo()));
185 HwvtepConnectionInstance client = getConnectionInstance(ovsdbNode.getConnectionInfo());
186 if (client != null) {
188 // Unregister Cluster Ownership for ConnectionInfo
189 unregisterEntityForOwnership(client);
190 removeInstanceIdentifier(ovsdbNode.getConnectionInfo());
194 public HwvtepConnectionInstance connectedButCallBacksNotRegistered(final OvsdbClient externalClient) {
195 LOG.info("OVSDB Connection from {}:{}",externalClient.getConnectionInfo().getRemoteAddress(),
196 externalClient.getConnectionInfo().getRemotePort());
197 ConnectionInfo key = HwvtepSouthboundMapper.createConnectionInfo(externalClient);
198 HwvtepConnectionInstance hwvtepConnectionInstance = getConnectionInstance(key);
200 // Check if existing hwvtepConnectionInstance for the OvsdbClient present.
201 // In such cases, we will see if the hwvtepConnectionInstance has same externalClient.
202 if (hwvtepConnectionInstance != null) {
203 if (hwvtepConnectionInstance.hasOvsdbClient(externalClient)) {
204 LOG.warn("HWVTEP Connection Instance {} already exists for client {}", key, externalClient);
205 return hwvtepConnectionInstance;
207 LOG.warn("HWVTEP Connection Instance {} being replaced with client {}", key, externalClient);
208 hwvtepConnectionInstance.disconnect();
210 // Unregister Cluster Ownership for ConnectionInfo
211 // Because the hwvtepConnectionInstance is about to be completely replaced!
212 unregisterEntityForOwnership(hwvtepConnectionInstance);
214 removeConnectionInstance(key);
217 hwvtepConnectionInstance = new HwvtepConnectionInstance(this, key, externalClient, getInstanceIdentifier(key),
219 hwvtepConnectionInstance.createTransactInvokers();
220 return hwvtepConnectionInstance;
223 private void putConnectionInstance(ConnectionInfo key,HwvtepConnectionInstance instance) {
224 ConnectionInfo connectionInfo = HwvtepSouthboundMapper.suppressLocalIpPort(key);
225 clients.put(connectionInfo, instance);
226 LOG.info("Clients after put: {}", clients);
229 public HwvtepConnectionInstance getConnectionInstance(final ConnectionInfo key) {
233 ConnectionInfo connectionInfo = HwvtepSouthboundMapper.suppressLocalIpPort(key);
234 return clients.get(connectionInfo);
237 public HwvtepConnectionInstance getConnectionInstanceFromNodeIid(final InstanceIdentifier<Node> nodeIid) {
238 HwvtepConnectionInstance hwvtepConnectionInstance = nodeIidVsConnectionInstance.get(nodeIid);
239 if (hwvtepConnectionInstance != null) {
240 return hwvtepConnectionInstance;
242 InstanceIdentifier<Node> globalNodeIid = HwvtepSouthboundUtil.getGlobalNodeIid(nodeIid);
243 if (globalNodeIid != null) {
244 return nodeIidVsConnectionInstance.get(globalNodeIid);
249 public HwvtepConnectionInstance getConnectionInstance(Node node) {
250 Preconditions.checkNotNull(node);
251 HwvtepGlobalAugmentation hwvtepGlobal = node.getAugmentation(HwvtepGlobalAugmentation.class);
252 PhysicalSwitchAugmentation pSwitchNode = node.getAugmentation(PhysicalSwitchAugmentation.class);
253 if (hwvtepGlobal != null) {
254 if(hwvtepGlobal.getConnectionInfo() != null) {
255 return getConnectionInstance(hwvtepGlobal.getConnectionInfo());
257 //TODO: Case of user configured connection for now
258 //TODO: We could get it from Managers also.
262 else if(pSwitchNode != null){
263 return getConnectionInstance(pSwitchNode);
265 LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
270 public HwvtepConnectionInstance getConnectionInstance(HwvtepPhysicalSwitchAttributes pNode) {
271 Optional<HwvtepGlobalAugmentation> optional = HwvtepSouthboundUtil.getManagingNode(db, pNode);
272 if(optional.isPresent()) {
273 return getConnectionInstance(optional.get().getConnectionInfo());
279 public void stopConfigurationReconciliation(final InstanceIdentifier<Node> nodeIid) {
280 final ReconciliationTask task = new HwvtepReconciliationTask(
281 reconciliationManager, HwvtepConnectionManager.this, nodeIid, null, null, db);
283 reconciliationManager.dequeue(task);
286 public void reconcileConfigurations(final HwvtepConnectionInstance client, Node psNode) {
287 final InstanceIdentifier<Node> nodeIid = client.getInstanceIdentifier();
288 final ReconciliationTask task = new HwvtepReconciliationTask(
289 reconciliationManager, HwvtepConnectionManager.this, nodeIid, psNode, client, db);
291 reconciliationManager.enqueue(task);
294 private void removeConnectionInstance(ConnectionInfo key) {
295 ConnectionInfo connectionInfo = HwvtepSouthboundMapper.suppressLocalIpPort(key);
296 clients.remove(connectionInfo);
297 LOG.info("Clients after remove: {}", clients);
300 private void putInstanceIdentifier(ConnectionInfo key,InstanceIdentifier<Node> iid) {
301 ConnectionInfo connectionInfo = HwvtepSouthboundMapper.suppressLocalIpPort(key);
302 instanceIdentifiers.put(connectionInfo, iid);
305 public InstanceIdentifier<Node> getInstanceIdentifier(ConnectionInfo key) {
306 ConnectionInfo connectionInfo = HwvtepSouthboundMapper.suppressLocalIpPort(key);
307 return instanceIdentifiers.get(connectionInfo);
310 private void removeInstanceIdentifier(ConnectionInfo key) {
311 ConnectionInfo connectionInfo = HwvtepSouthboundMapper.suppressLocalIpPort(key);
312 instanceIdentifiers.remove(connectionInfo);
315 public OvsdbClient getClient(ConnectionInfo connectionInfo) {
316 return getConnectionInstance(connectionInfo).getOvsdbClient();
319 private void registerEntityForOwnership(HwvtepConnectionInstance hwvtepConnectionInstance) {
321 Entity candidateEntity = getEntityFromConnectionInstance(hwvtepConnectionInstance);
322 entityConnectionMap.put(candidateEntity, hwvtepConnectionInstance);
323 hwvtepConnectionInstance.setConnectedEntity(candidateEntity);
326 EntityOwnershipCandidateRegistration registration =
327 entityOwnershipService.registerCandidate(candidateEntity);
328 hwvtepConnectionInstance.setDeviceOwnershipCandidateRegistration(registration);
329 LOG.info("HWVTEP entity {} is registered for ownership.", candidateEntity);
331 //If entity already has owner, it won't get notification from EntityOwnershipService
332 //so cache the connection instances.
333 Optional<EntityOwnershipState> ownershipStateOpt =
334 entityOwnershipService.getOwnershipState(candidateEntity);
335 if (ownershipStateOpt.isPresent()) {
336 EntityOwnershipState ownershipState = ownershipStateOpt.get();
337 if (ownershipState.hasOwner() && !ownershipState.isOwner()) {
338 if (getConnectionInstance(hwvtepConnectionInstance.getMDConnectionInfo()) != null) {
339 LOG.info("OVSDB entity {} is already owned by other southbound plugin "
340 + "instance, so *this* instance is NOT an OWNER of the device",
341 hwvtepConnectionInstance.getConnectionInfo());
342 putConnectionInstance(hwvtepConnectionInstance.getMDConnectionInfo(),hwvtepConnectionInstance);
346 } catch (CandidateAlreadyRegisteredException e) {
347 LOG.warn("OVSDB entity {} was already registered for ownership", candidateEntity, e);
352 private Global getHwvtepGlobalTableEntry(HwvtepConnectionInstance connectionInstance) {
353 DatabaseSchema dbSchema = null;
354 Global globalRow = null;
357 dbSchema = connectionInstance.getSchema(HwvtepSchemaConstants.HARDWARE_VTEP).get();
358 } catch (InterruptedException | ExecutionException e) {
359 LOG.warn("Not able to fetch schema for database {} from device {}",
360 HwvtepSchemaConstants.HARDWARE_VTEP,connectionInstance.getConnectionInfo(),e);
363 if (dbSchema != null) {
364 GenericTableSchema hwvtepSchema = TyperUtils.getTableSchema(dbSchema, Global.class);
366 List<String> hwvtepTableColumn = new ArrayList<>();
367 hwvtepTableColumn.addAll(hwvtepSchema.getColumns());
368 Select<GenericTableSchema> selectOperation = op.select(hwvtepSchema);
369 selectOperation.setColumns(hwvtepTableColumn);
371 ArrayList<Operation> operations = new ArrayList<>();
372 operations.add(selectOperation);
373 operations.add(op.comment("Fetching hardware_vtep table rows"));
376 List<OperationResult> results = connectionInstance.transact(dbSchema, operations).get();
377 if (results != null ) {
378 OperationResult selectResult = results.get(0);
379 globalRow = TyperUtils.getTypedRowWrapper(
380 dbSchema,Global.class,selectResult.getRows().get(0));
382 } catch (InterruptedException | ExecutionException e) {
383 LOG.warn("Not able to fetch hardware_vtep table row from device {}",
384 connectionInstance.getConnectionInfo(),e);
387 LOG.trace("Fetched global {} from hardware_vtep schema",globalRow);
391 private Entity getEntityFromConnectionInstance(@Nonnull HwvtepConnectionInstance hwvtepConnectionInstance) {
392 InstanceIdentifier<Node> iid = hwvtepConnectionInstance.getInstanceIdentifier();
394 //TODO: Is Global the right one?
395 Global hwvtepGlobalRow = getHwvtepGlobalTableEntry(hwvtepConnectionInstance);
396 iid = HwvtepSouthboundMapper.getInstanceIdentifier(hwvtepGlobalRow);
397 /* Let's set the iid now */
398 hwvtepConnectionInstance.setInstanceIdentifier(iid);
399 LOG.info("InstanceIdentifier {} generated for device "
400 + "connection {}",iid, hwvtepConnectionInstance.getConnectionInfo());
403 YangInstanceIdentifier entityId =
404 HwvtepSouthboundUtil.getInstanceIdentifierCodec().getYangInstanceIdentifier(iid);
405 Entity deviceEntity = new Entity(ENTITY_TYPE, entityId);
406 LOG.debug("Entity {} created for device connection {}",
407 deviceEntity, hwvtepConnectionInstance.getConnectionInfo());
410 private void unregisterEntityForOwnership(HwvtepConnectionInstance hwvtepConnectionInstance) {
411 hwvtepConnectionInstance.closeDeviceOwnershipCandidateRegistration();
412 entityConnectionMap.remove(hwvtepConnectionInstance.getConnectedEntity());
415 public void reconcileConnection(InstanceIdentifier<Node> iid, HwvtepGlobalAugmentation hwvtepNode) {
416 this.retryConnection(iid, hwvtepNode,
417 ConnectionReconciliationTriggers.ON_CONTROLLER_INITIATED_CONNECTION_FAILURE);
420 public void stopConnectionReconciliationIfActive(InstanceIdentifier<?> iid, HwvtepGlobalAugmentation hwvtepNode) {
421 final ReconciliationTask task = new ConnectionReconciliationTask(
422 reconciliationManager,
426 reconciliationManager.dequeue(task);
429 private void retryConnection(final InstanceIdentifier<Node> iid, final HwvtepGlobalAugmentation hwvtepNode,
430 ConnectionReconciliationTriggers trigger) {
431 final ReconciliationTask task = new ConnectionReconciliationTask(
432 reconciliationManager,
437 if(reconciliationManager.isEnqueued(task)){
441 case ON_CONTROLLER_INITIATED_CONNECTION_FAILURE:
442 reconciliationManager.enqueueForRetry(task);
446 ReadOnlyTransaction tx = db.newReadOnlyTransaction();
447 CheckedFuture<Optional<Node>, ReadFailedException> readNodeFuture =
448 tx.read(LogicalDatastoreType.CONFIGURATION, iid);
450 final HwvtepConnectionManager connectionManager = this;
451 Futures.addCallback(readNodeFuture, new FutureCallback<Optional<Node>>() {
453 public void onSuccess(@Nullable Optional<Node> node) {
454 if (node.isPresent()) {
455 LOG.info("Disconnected/Failed connection {} was controller initiated, attempting " +
456 "reconnection", hwvtepNode.getConnectionInfo());
457 reconciliationManager.enqueue(task);
460 LOG.debug("Connection {} was switch initiated, no reconciliation is required"
461 , iid.firstKeyOf(Node.class).getNodeId());
466 public void onFailure(Throwable t) {
467 LOG.warn("Read Config/DS for Node failed! {}", iid, t);
477 public void handleOwnershipChanged(EntityOwnershipChange ownershipChange) {
478 HwvtepConnectionInstance hwvtepConnectionInstance = getConnectionInstanceFromEntity(ownershipChange.getEntity());
479 LOG.info("handleOwnershipChanged: {} event received for device {}",
480 ownershipChange, hwvtepConnectionInstance != null ? hwvtepConnectionInstance.getConnectionInfo()
481 : "THAT'S NOT REGISTERED BY THIS SOUTHBOUND PLUGIN INSTANCE");
483 if (hwvtepConnectionInstance == null) {
484 if (ownershipChange.isOwner()) {
485 LOG.warn("handleOwnershipChanged: found no connection instance for {}", ownershipChange.getEntity());
487 // EntityOwnershipService sends notification to all the nodes, irrespective of whether
488 // that instance registered for the device ownership or not. It is to make sure that
489 // If all the controller instance that was connected to the device are down, so the
490 // running instance can clear up the operational data store even though it was not
491 // connected to the device.
492 LOG.debug("handleOwnershipChanged: found no connection instance for {}", ownershipChange.getEntity());
495 // If entity has no owner, clean up the operational data store (it's possible because owner controller
496 // might went down abruptly and didn't get a chance to clean up the operational data store.
497 if (!ownershipChange.hasOwner()) {
498 LOG.debug("{} has no owner, cleaning up the operational data store", ownershipChange.getEntity());
499 // If first cleanEntityOperationalData() was called, this call will be no-op.
500 cleanEntityOperationalData(ownershipChange.getEntity());
504 //Connection detail need to be cached, irrespective of ownership result.
505 putConnectionInstance(hwvtepConnectionInstance.getMDConnectionInfo(), hwvtepConnectionInstance);
507 if (ownershipChange.isOwner() == hwvtepConnectionInstance.getHasDeviceOwnership()) {
508 LOG.debug("handleOwnershipChanged: no change in ownership for {}. Ownership status is : {}",
509 hwvtepConnectionInstance.getConnectionInfo(), hwvtepConnectionInstance.getHasDeviceOwnership());
513 hwvtepConnectionInstance.setHasDeviceOwnership(ownershipChange.isOwner());
514 // You were not an owner, but now you are
515 if (ownershipChange.isOwner()) {
516 LOG.info("handleOwnershipChanged: *this* southbound plugin instance is owner of device {}",
517 hwvtepConnectionInstance.getConnectionInfo());
519 //*this* instance of southbound plugin is owner of the device,
520 //so register for monitor callbacks
521 hwvtepConnectionInstance.registerCallbacks();
524 //You were owner of the device, but now you are not. With the current ownership
525 //grant mechanism, this scenario should not occur. Because this scenario will occur
526 //when this controller went down or switch flap the connection, but in both the case
527 //it will go through the re-registration process. We need to implement this condition
528 //when clustering service implement a ownership grant strategy which can revoke the
529 //device ownership for load balancing the devices across the instances.
530 //Once this condition occur, we should unregister the callback.
531 LOG.error("handleOwnershipChanged: *this* southbound plugin instance is no longer the owner of device {}",
532 hwvtepConnectionInstance.getNodeId().getValue());
536 private void cleanEntityOperationalData(Entity entity) {
537 @SuppressWarnings("unchecked") final InstanceIdentifier<Node> nodeIid =
538 (InstanceIdentifier<Node>) HwvtepSouthboundUtil
539 .getInstanceIdentifierCodec().bindingDeserializer(entity.getId());
540 txInvoker.invoke(new HwvtepGlobalRemoveCommand(nodeIid));
543 private HwvtepConnectionInstance getConnectionInstanceFromEntity(Entity entity) {
544 return entityConnectionMap.get(entity);
547 void putConnectionInstance(final InstanceIdentifier<Node> nodeIid,
548 final HwvtepConnectionInstance connectionInstance) {
549 nodeIidVsConnectionInstance.put(nodeIid, connectionInstance);
552 private void removeConnectionInstance(final InstanceIdentifier<Node> nodeIid) {
553 if (nodeIid != null) {
554 nodeIidVsConnectionInstance.remove(nodeIid);
558 private class HwvtepDeviceEntityOwnershipListener implements EntityOwnershipListener {
559 private HwvtepConnectionManager hcm;
560 private EntityOwnershipListenerRegistration listenerRegistration;
562 HwvtepDeviceEntityOwnershipListener(HwvtepConnectionManager hcm, EntityOwnershipService entityOwnershipService) {
564 listenerRegistration = entityOwnershipService.registerListener(ENTITY_TYPE, this);
566 public void close() {
567 listenerRegistration.close();
570 public void ownershipChanged(EntityOwnershipChange ownershipChange) {
571 hcm.handleOwnershipChanged(ownershipChange);
575 private enum ConnectionReconciliationTriggers {
577 Reconciliation trigger for scenario where controller's attempt
578 to connect to switch fails on config data store notification
580 ON_CONTROLLER_INITIATED_CONNECTION_FAILURE,
583 Reconciliation trigger for the scenario where controller
584 initiated connection disconnects.