X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=elanmanager%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fnetvirt%2Felan%2Fl2gw%2Fha%2Flisteners%2FHAConfigNodeListener.java;h=ad8d349d76996fc390e8f64c75baca21637442eb;hb=refs%2Fchanges%2F35%2F91835%2F21;hp=7ce8b12883c5dd94c7765303f869e1f2c41d2b80;hpb=faedf1e8997878432572db1909aac4a0e8ba0d83;p=netvirt.git diff --git a/elanmanager/impl/src/main/java/org/opendaylight/netvirt/elan/l2gw/ha/listeners/HAConfigNodeListener.java b/elanmanager/impl/src/main/java/org/opendaylight/netvirt/elan/l2gw/ha/listeners/HAConfigNodeListener.java index 7ce8b12883..ad8d349d76 100644 --- a/elanmanager/impl/src/main/java/org/opendaylight/netvirt/elan/l2gw/ha/listeners/HAConfigNodeListener.java +++ b/elanmanager/impl/src/main/java/org/opendaylight/netvirt/elan/l2gw/ha/listeners/HAConfigNodeListener.java @@ -7,78 +7,127 @@ */ package org.opendaylight.netvirt.elan.l2gw.ha.listeners; -import static org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION; +import static org.opendaylight.mdsal.binding.util.Datastore.CONFIGURATION; -import com.google.common.base.Optional; -import java.util.Collections; -import java.util.HashSet; +import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ExecutionException; import javax.inject.Inject; import javax.inject.Singleton; -import org.opendaylight.controller.md.sal.binding.api.DataBroker; -import org.opendaylight.controller.md.sal.binding.api.DataObjectModification; -import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction; -import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException; -import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache; +import org.opendaylight.genius.utils.batching.ResourceBatchingManager; +import org.opendaylight.mdsal.binding.api.DataBroker; +import org.opendaylight.mdsal.binding.api.DataObjectModification; +import org.opendaylight.mdsal.binding.util.Datastore.Configuration; +import org.opendaylight.mdsal.binding.util.TypedReadWriteTransaction; +import org.opendaylight.netvirt.elan.l2gw.ha.BatchedTransaction; import org.opendaylight.netvirt.elan.l2gw.ha.HwvtepHAUtil; import org.opendaylight.netvirt.elan.l2gw.ha.handlers.HAEventHandler; import org.opendaylight.netvirt.elan.l2gw.ha.handlers.IHAEventHandler; import org.opendaylight.netvirt.elan.l2gw.ha.handlers.NodeCopier; +import org.opendaylight.netvirt.elan.l2gw.recovery.impl.L2GatewayServiceRecoveryHandler; +import org.opendaylight.serviceutils.srm.RecoverableListener; +import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId; import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @Singleton -public class HAConfigNodeListener extends HwvtepNodeBaseListener { +public class HAConfigNodeListener extends HwvtepNodeBaseListener implements RecoverableListener { private static final Logger LOG = LoggerFactory.getLogger(HAConfigNodeListener.class); - private final IHAEventHandler haEventHandler; private final NodeCopier nodeCopier; @Inject public HAConfigNodeListener(DataBroker db, HAEventHandler haEventHandler, - NodeCopier nodeCopier, HwvtepNodeHACache hwvtepNodeHACache) throws Exception { - super(LogicalDatastoreType.CONFIGURATION, db, hwvtepNodeHACache); + NodeCopier nodeCopier, + final L2GatewayServiceRecoveryHandler l2GatewayServiceRecoveryHandler, + final ServiceRecoveryRegistry serviceRecoveryRegistry) throws Exception { + super(CONFIGURATION, db); this.haEventHandler = haEventHandler; this.nodeCopier = nodeCopier; + serviceRecoveryRegistry.addRecoverableListener(l2GatewayServiceRecoveryHandler.buildServiceRegistryKey(), this); + ResourceBatchingManager.getInstance().registerDefaultBatchHandlers(db); + } + + @Override + @SuppressWarnings("all") + public void registerListener() { + try { + LOG.info("Registering HAConfigNodeListener"); + registerListener(CONFIGURATION, getDataBroker()); + } catch (Exception e) { + LOG.error("HA Config Node register listener error.", e); + } + } + + public void deregisterListener() { + LOG.info("Deregistering HAConfigNodeListener"); + super.close(); } @Override void onPsNodeAdd(InstanceIdentifier haPsPath, - Node haPSNode, - ReadWriteTransaction tx) throws ReadFailedException { + Node haPSNode, + TypedReadWriteTransaction tx) + throws ExecutionException, InterruptedException { //copy the ps node data to children String psId = haPSNode.getNodeId().getValue(); - Set> childSwitchIds = getPSChildrenIdsForHAPSNode(psId); + Set> childSwitchIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId); if (childSwitchIds.isEmpty()) { - LOG.error("Failed to find any ha children {}", haPsPath); + if (!hwvtepHACache.isHAEnabledDevice(haPsPath)) { + LOG.error("HAConfigNodeListener Failed to find any ha children {}", haPsPath); + } return; } + for (InstanceIdentifier childPsPath : childSwitchIds) { String nodeId = HwvtepHAUtil.convertToGlobalNodeId(childPsPath.firstKeyOf(Node.class).getNodeId().getValue()); InstanceIdentifier childGlobalPath = HwvtepHAUtil.convertToInstanceIdentifier(nodeId); - nodeCopier.copyPSNode(Optional.fromNullable(haPSNode), haPsPath, childPsPath, childGlobalPath, - LogicalDatastoreType.CONFIGURATION, tx); + nodeCopier.copyPSNode(Optional.ofNullable(haPSNode), haPsPath, childPsPath, childGlobalPath, + CONFIGURATION, tx); } LOG.trace("Handle config ps node add {}", psId); } @Override void onPsNodeUpdate(Node haPSUpdated, - Node haPSOriginal, - DataObjectModification mod, - ReadWriteTransaction tx) throws InterruptedException, ExecutionException, ReadFailedException { + DataObjectModification mod, + TypedReadWriteTransaction tx) { //copy the ps node data to children String psId = haPSUpdated.getNodeId().getValue(); - Set> childSwitchIds = getPSChildrenIdsForHAPSNode(psId); + ((BatchedTransaction)tx).setSrcNodeId(haPSUpdated.getNodeId()); + ((BatchedTransaction)tx).updateMetric(true); + Set> childSwitchIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId); for (InstanceIdentifier childSwitchId : childSwitchIds) { haEventHandler.copyHAPSUpdateToChild(childSwitchId, mod, tx); + ((BatchedTransaction)tx).updateMetric(false); + } + } + + @Override + void onGlobalNodeAdd(InstanceIdentifier haGlobalPath, Node haGlobalNode, + TypedReadWriteTransaction tx) { + //copy the parent global node data to children + String haParentId = haGlobalNode.getNodeId().getValue(); + List childGlobalIds = HwvtepHAUtil + .getChildNodeIdsFromManagerOtherConfig(Optional.ofNullable(haGlobalNode)); + if (childGlobalIds.isEmpty()) { + if (!hwvtepHACache.isHAEnabledDevice(haGlobalPath)) { + LOG.error("HAConfigNodeListener Failed to find any ha children {}", haGlobalPath); + } + return; + } + for (NodeId nodeId : childGlobalIds) { + InstanceIdentifier childGlobalPath = HwvtepHAUtil.convertToInstanceIdentifier(nodeId.getValue()); + nodeCopier.copyGlobalNode(Optional.ofNullable(haGlobalNode), haGlobalPath, childGlobalPath, + CONFIGURATION, tx); } + LOG.trace("Handle config global node add {}", haParentId); } @Override @@ -86,55 +135,49 @@ public class HAConfigNodeListener extends HwvtepNodeBaseListener { Node haUpdated, Node haOriginal, DataObjectModification mod, - ReadWriteTransaction tx) - throws InterruptedException, ExecutionException, ReadFailedException { - Set> childNodeIds = getHwvtepNodeHACache().getChildrenForHANode(key); + TypedReadWriteTransaction tx) { + Set> childNodeIds = hwvtepHACache.getChildrenForHANode(key); + ((BatchedTransaction)tx).setSrcNodeId(haUpdated.getNodeId()); + ((BatchedTransaction)tx).updateMetric(true); for (InstanceIdentifier haChildNodeId : childNodeIds) { haEventHandler.copyHAGlobalUpdateToChild(haChildNodeId, mod, tx); + ((BatchedTransaction)tx).updateMetric(false); } } @Override void onPsNodeDelete(InstanceIdentifier key, Node deletedPsNode, - ReadWriteTransaction tx) throws ReadFailedException { + TypedReadWriteTransaction tx) { //delete ps children nodes String psId = deletedPsNode.getNodeId().getValue(); - Set> childPsIds = getPSChildrenIdsForHAPSNode(psId); + Set> childPsIds = HwvtepHAUtil.getPSChildrenIdsForHAPSNode(psId); for (InstanceIdentifier childPsId : childPsIds) { - HwvtepHAUtil.deleteNodeIfPresent(tx, CONFIGURATION, childPsId); + try { + HwvtepHAUtil.deleteNodeIfPresent(tx, childPsId); + } catch (ExecutionException | InterruptedException e) { + LOG.error("Exception while deleting PS node {} from config topo", childPsId); + } } } @Override void onGlobalNodeDelete(InstanceIdentifier key, Node haNode, - ReadWriteTransaction tx) - throws ReadFailedException { + TypedReadWriteTransaction tx) { //delete child nodes - Set> children = getHwvtepNodeHACache().getChildrenForHANode(key); + Set> children = hwvtepHACache.getChildrenForHANode(key); for (InstanceIdentifier childId : children) { - HwvtepHAUtil.deleteNodeIfPresent(tx, CONFIGURATION, childId); - } - HwvtepHAUtil.deletePSNodesOfNode(key, haNode, tx); - } - - private Set> getPSChildrenIdsForHAPSNode(String psNodId) { - if (!psNodId.contains(HwvtepHAUtil.PHYSICALSWITCH)) { - return Collections.emptySet(); - } - String nodeId = HwvtepHAUtil.convertToGlobalNodeId(psNodId); - InstanceIdentifier iid = HwvtepHAUtil.convertToInstanceIdentifier(nodeId); - if (getHwvtepNodeHACache().isHAParentNode(iid)) { - Set> childSwitchIds = new HashSet<>(); - Set> childGlobalIds = getHwvtepNodeHACache().getChildrenForHANode(iid); - final String append = psNodId.substring(psNodId.indexOf(HwvtepHAUtil.PHYSICALSWITCH)); - for (InstanceIdentifier childId : childGlobalIds) { - String childIdVal = childId.firstKeyOf(Node.class).getNodeId().getValue(); - childSwitchIds.add(HwvtepHAUtil.convertToInstanceIdentifier(childIdVal + append)); + try { + HwvtepHAUtil.deleteNodeIfPresent(tx, childId); + } catch (ExecutionException | InterruptedException e) { + LOG.error("Exception while deleting Global node {} from config topo ", childId); } - return childSwitchIds; } - return Collections.emptySet(); + try { + HwvtepHAUtil.deletePSNodesOfNode(key, haNode, tx); + } catch (ExecutionException | InterruptedException e) { + LOG.error("Exception while deleting PS nodes for HA Node {} from config topo", haNode.getNodeId()); + } } }