From baafccbcbdd121c7a8804d5ea98d9f110a4aab18 Mon Sep 17 00:00:00 2001 From: Chetan Arakere Gowdru Date: Tue, 4 Aug 2020 13:51:29 +0530 Subject: [PATCH] L2GW HwvtepHACache Utility class add in genius Description: Moved the existing HwvtepHANodeCacheImpl to HwvtepHACache. Signed-off-by: Chetan Arakere Gowdru Change-Id: I494c34be054f45e5bd020f9e30a5a6bfdf0d557f Signed-off-by: Chetan Arakere Gowdru --- .../listeners/HwVTEPTunnelsStateListener.java | 4 +- .../test/InterfaceManagerTestModule.java | 4 +- .../HwvtepAbstractDataTreeChangeListener.java | 13 +- ...HwvtepClusteredDataTreeChangeListener.java | 14 +- .../genius/utils/hwvtep/DebugEvent.java | 21 ++ .../genius/utils/hwvtep/HwvtepHACache.java | 181 ++++++++++++++++++ .../utils/hwvtep/HwvtepNodeHACache.java | 48 ----- .../genius/utils/hwvtep/NodeEvent.java | 80 ++++++++ .../internal/HwvtepNodeHACacheImpl.java | 126 ------------ .../OSGI-INF/blueprint/mdsalutil.xml | 2 - .../internal/HwvtepNodeHACacheImplTest.java | 82 -------- 11 files changed, 297 insertions(+), 278 deletions(-) create mode 100644 mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/DebugEvent.java create mode 100644 mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/HwvtepHACache.java delete mode 100644 mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/HwvtepNodeHACache.java create mode 100644 mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/NodeEvent.java delete mode 100644 mdsalutil/mdsalutil-impl/src/main/java/org/opendaylight/genius/utils/hwvtep/internal/HwvtepNodeHACacheImpl.java delete mode 100644 mdsalutil/mdsalutil-impl/src/test/java/org/opendaylight/genius/utils/hwvtep/internal/HwvtepNodeHACacheImplTest.java diff --git a/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/listeners/HwVTEPTunnelsStateListener.java b/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/listeners/HwVTEPTunnelsStateListener.java index d71515af5..ffe40179d 100644 --- a/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/listeners/HwVTEPTunnelsStateListener.java +++ b/interfacemanager/interfacemanager-impl/src/main/java/org/opendaylight/genius/interfacemanager/listeners/HwVTEPTunnelsStateListener.java @@ -15,7 +15,6 @@ import org.opendaylight.genius.interfacemanager.IfmConstants; import org.opendaylight.genius.interfacemanager.recovery.impl.InterfaceServiceRecoveryHandler; import org.opendaylight.genius.interfacemanager.renderer.hwvtep.statehelpers.HwVTEPInterfaceStateRemoveHelper; import org.opendaylight.genius.interfacemanager.renderer.hwvtep.statehelpers.HwVTEPInterfaceStateUpdateHelper; -import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache; import org.opendaylight.infrautils.jobcoordinator.JobCoordinator; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.util.ManagedNewTransactionRunner; @@ -46,8 +45,7 @@ public class HwVTEPTunnelsStateListener extends AbstractSyncDataTreeChangeListen public HwVTEPTunnelsStateListener(@Reference DataBroker dataBroker, @Reference JobCoordinator coordinator, InterfaceServiceRecoveryHandler interfaceServiceRecoveryHandler, - @Reference ServiceRecoveryRegistry serviceRecoveryRegistry, - @Reference HwvtepNodeHACache hwvtepNodeHACache) { + @Reference ServiceRecoveryRegistry serviceRecoveryRegistry) { super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(NetworkTopology.class).child(Topology.class).child(Node.class) .augmentation(PhysicalSwitchAugmentation.class).child(Tunnels.class).build()); diff --git a/interfacemanager/interfacemanager-impl/src/test/java/org/opendaylight/genius/interfacemanager/test/InterfaceManagerTestModule.java b/interfacemanager/interfacemanager-impl/src/test/java/org/opendaylight/genius/interfacemanager/test/InterfaceManagerTestModule.java index 4675d3bb8..3fa7bd1f1 100644 --- a/interfacemanager/interfacemanager-impl/src/test/java/org/opendaylight/genius/interfacemanager/test/InterfaceManagerTestModule.java +++ b/interfacemanager/interfacemanager-impl/src/test/java/org/opendaylight/genius/interfacemanager/test/InterfaceManagerTestModule.java @@ -42,7 +42,7 @@ import org.opendaylight.genius.mdsalutil.interfaces.ShardStatusMonitor; import org.opendaylight.genius.mdsalutil.interfaces.testutils.TestIMdsalApiManager; import org.opendaylight.genius.mdsalutil.testutils.TestShardStatusMonitor; import org.opendaylight.genius.utils.clustering.EntityOwnershipUtils; -import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache; +import org.opendaylight.genius.utils.hwvtep.HwvtepHACache; import org.opendaylight.infrautils.caches.CacheProvider; import org.opendaylight.infrautils.inject.guice.testutils.AbstractGuiceJsr250Module; import org.opendaylight.mdsal.binding.api.DataBroker; @@ -116,7 +116,7 @@ public class InterfaceManagerTestModule extends AbstractGuiceJsr250Module { bind(TerminationPointStateListener.class); bind(VlanMemberConfigListener.class); bind(InterfaceStateListener.class); - bind(HwvtepNodeHACache.class).toInstance(mock(HwvtepNodeHACache.class)); + bind(HwvtepHACache.class).toInstance(mock(HwvtepHACache.class)); bind(IfmConfig.class).toInstance(mock(IfmConfig.class)); bind(CacheProvider.class).toInstance(mock(CacheProvider.class)); bind(IfmDiagStatusProvider.class).toInstance(mock(IfmDiagStatusProvider.class)); diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/hwvtep/HwvtepAbstractDataTreeChangeListener.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/hwvtep/HwvtepAbstractDataTreeChangeListener.java index 4edf58176..8d2050d43 100644 --- a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/hwvtep/HwvtepAbstractDataTreeChangeListener.java +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/hwvtep/HwvtepAbstractDataTreeChangeListener.java @@ -8,7 +8,7 @@ package org.opendaylight.genius.datastoreutils.hwvtep; import java.util.concurrent.ExecutorService; -import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache; +import org.opendaylight.genius.utils.hwvtep.HwvtepHACache; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.DataTreeChangeListener; import org.opendaylight.mdsal.binding.api.DataTreeIdentifier; @@ -19,17 +19,14 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; public abstract class HwvtepAbstractDataTreeChangeListener> extends AbstractAsyncDataTreeChangeListener { - private final HwvtepNodeHACache hwvtepNodeHACache; - public HwvtepAbstractDataTreeChangeListener(DataBroker dataBroker,DataTreeIdentifier dataTreeIdentifier, - ExecutorService executorService, HwvtepNodeHACache hwvtepNodeHACache) { + ExecutorService executorService) { super(dataBroker, dataTreeIdentifier, executorService); - this.hwvtepNodeHACache = hwvtepNodeHACache; } @Override public void remove(InstanceIdentifier identifier, T del) { - if (hwvtepNodeHACache.isHAEnabledDevice(identifier)) { + if (HwvtepHACache.getInstance().isHAEnabledDevice(identifier)) { return; } removed(identifier, del); @@ -37,7 +34,7 @@ public abstract class HwvtepAbstractDataTreeChangeListener identifier, T original, T update) { - if (hwvtepNodeHACache.isHAEnabledDevice(identifier)) { + if (HwvtepHACache.getInstance().isHAEnabledDevice(identifier)) { return; } updated(identifier,original,update); @@ -45,7 +42,7 @@ public abstract class HwvtepAbstractDataTreeChangeListener identifier, T add) { - if (hwvtepNodeHACache.isHAEnabledDevice(identifier)) { + if (HwvtepHACache.getInstance().isHAEnabledDevice(identifier)) { return; } added(identifier,add); diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/hwvtep/HwvtepClusteredDataTreeChangeListener.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/hwvtep/HwvtepClusteredDataTreeChangeListener.java index e8dd40a65..ee4fdd709 100644 --- a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/hwvtep/HwvtepClusteredDataTreeChangeListener.java +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/hwvtep/HwvtepClusteredDataTreeChangeListener.java @@ -8,7 +8,7 @@ package org.opendaylight.genius.datastoreutils.hwvtep; import java.util.concurrent.ExecutorService; -import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache; +import org.opendaylight.genius.utils.hwvtep.HwvtepHACache; import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.DataTreeIdentifier; @@ -20,17 +20,17 @@ public abstract class HwvtepClusteredDataTreeChangeListener< T extends DataObject, K extends ClusteredDataTreeChangeListener> extends AbstractClusteredAsyncDataTreeChangeListener { - private final HwvtepNodeHACache hwvtepNodeHACache; + private final HwvtepHACache hwvtepHACache; public HwvtepClusteredDataTreeChangeListener(DataBroker dataBroker, DataTreeIdentifier dataTreeIdentifier, - ExecutorService executorService, HwvtepNodeHACache hwvtepNodeHACache) { + ExecutorService executorService, HwvtepHACache hwvtepHACache) { super(dataBroker, dataTreeIdentifier, executorService); - this.hwvtepNodeHACache = hwvtepNodeHACache; + this.hwvtepHACache = hwvtepHACache; } @Override public void remove(InstanceIdentifier identifier, T del) { - if (hwvtepNodeHACache.isHAEnabledDevice(identifier)) { + if (hwvtepHACache.isHAEnabledDevice(identifier)) { return; } removed(identifier,del); @@ -38,7 +38,7 @@ public abstract class HwvtepClusteredDataTreeChangeListener< @Override public void update(InstanceIdentifier identifier, T original, T update) { - if (hwvtepNodeHACache.isHAEnabledDevice(identifier)) { + if (hwvtepHACache.isHAEnabledDevice(identifier)) { return; } updated(identifier,original, update); @@ -46,7 +46,7 @@ public abstract class HwvtepClusteredDataTreeChangeListener< @Override public void add(InstanceIdentifier identifier, T add) { - if (hwvtepNodeHACache.isHAEnabledDevice(identifier)) { + if (hwvtepHACache.isHAEnabledDevice(identifier)) { return; } added(identifier, add); diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/DebugEvent.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/DebugEvent.java new file mode 100644 index 000000000..27459156f --- /dev/null +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/DebugEvent.java @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.genius.utils.hwvtep; + +public abstract class DebugEvent { + + private final long eventTimeStamp; + + public DebugEvent() { + this.eventTimeStamp = System.currentTimeMillis(); + } + + public long getEventTimeStamp() { + return eventTimeStamp; + } +} diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/HwvtepHACache.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/HwvtepHACache.java new file mode 100644 index 000000000..5a11e934a --- /dev/null +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/HwvtepHACache.java @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2020 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.genius.utils.hwvtep; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.LinkedBlockingQueue; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; +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; + +public class HwvtepHACache { + + private static final Logger LOG = LoggerFactory.getLogger(HwvtepHACache.class); + + private static final int MAX_EVENT_BUFFER_SIZE = 500000; + private static final int EVENT_DRAIN_BUFFER_SIZE = 100000; + + private static HwvtepHACache instance = new HwvtepHACache(); + + private final ConcurrentHashMap, Set>> + parentToChildMap = new ConcurrentHashMap<>(); + + private final ConcurrentHashMap, InstanceIdentifier> + childToParentMap = new ConcurrentHashMap<>(); + + private final ConcurrentHashMap childNodeIds = new ConcurrentHashMap<>(); + + private final ConcurrentHashMap connectedNodes = new ConcurrentHashMap<>(); + + private final LinkedBlockingQueue debugEvents = new LinkedBlockingQueue<>(MAX_EVENT_BUFFER_SIZE); + private Map, IpAddress> tepIps = new ConcurrentHashMap(); + private Map> nodeIds = new ConcurrentHashMap(); + + public HwvtepHACache() { + } + + public static HwvtepHACache getInstance() { + return instance; + } + + public synchronized void addChild(InstanceIdentifier parent, InstanceIdentifier child) { + if (parent == null || child == null) { + return; + } + + parentToChildMap.computeIfAbsent(parent, key -> new HashSet<>()).add(child); + childToParentMap.put(child, parent); + String childNodeId = child.firstKeyOf(Node.class).getNodeId().getValue(); + childNodeIds.put(childNodeId, Boolean.TRUE); + addDebugEvent(new NodeEvent.ChildAddedEvent(childNodeId)); + } + + public IpAddress getTepIpOfNode(InstanceIdentifier iid) { + return (IpAddress)this.tepIps.get(iid); + } + + public InstanceIdentifier getNodeIdFromTepIp(IpAddress ipAddress) { + return (InstanceIdentifier)this.nodeIds.get(ipAddress); + } + + public void setTepIpOfNode(InstanceIdentifier iid, IpAddress ipAddress) { + this.tepIps.put(iid, ipAddress); + if (this.isHAParentNode(iid)) { + this.nodeIds.put(ipAddress, iid); + } else { + this.nodeIds.putIfAbsent(ipAddress, iid); + } + + } + + public boolean isHAEnabledDevice(InstanceIdentifier iid) { + if (iid == null) { + return false; + } + boolean enabled = childToParentMap.containsKey(iid.firstIdentifierOf(Node.class)); + if (!enabled) { + String psNodeId = iid.firstKeyOf(Node.class).getNodeId().getValue(); + int idx = psNodeId.indexOf(HwvtepSouthboundConstants.PSWITCH_URI_PREFIX); + if (idx > 0) { + String globalNodeId = psNodeId.substring(0, idx - 1); + return childNodeIds.containsKey(globalNodeId); + } + } + return enabled; + } + + public boolean isHAParentNode(InstanceIdentifier node) { + return parentToChildMap.containsKey(node); + } + + public Set> getChildrenForHANode(InstanceIdentifier parent) { + if (parent != null && parentToChildMap.containsKey(parent)) { + return new HashSet<>(parentToChildMap.get(parent)); + } else { + return Collections.emptySet(); + } + } + + public Set> getHAParentNodes() { + return parentToChildMap.keySet(); + } + + public Set> getHAChildNodes() { + return childToParentMap.keySet(); + } + + public InstanceIdentifier getParent(InstanceIdentifier child) { + if (child != null) { + return childToParentMap.get(child); + } + return null; + } + + public synchronized void cleanupParent(InstanceIdentifier parent) { + if (parent == null) { + return; + } + + if (parentToChildMap.get(parent) != null) { + Set> childs = parentToChildMap.get(parent); + for (InstanceIdentifier child : childs) { + childToParentMap.remove(child); + String childNodeId = child.firstKeyOf(Node.class).getNodeId().getValue(); + childNodeIds.remove(childNodeId); + } + } + parentToChildMap.remove(parent); + } + + public void updateConnectedNodeStatus(InstanceIdentifier iid) { + String nodeId = iid.firstKeyOf(Node.class).getNodeId().getValue(); + connectedNodes.put(nodeId, true); + DebugEvent event = new NodeEvent.NodeConnectedEvent(nodeId); + addDebugEvent(event); + } + + public void updateDisconnectedNodeStatus(InstanceIdentifier iid) { + String nodeId = iid.firstKeyOf(Node.class).getNodeId().getValue(); + connectedNodes.put(nodeId, false); + DebugEvent event = new NodeEvent.NodeDisconnectedEvent(nodeId); + addDebugEvent(event); + } + + public Map getConnectedNodes() { + return ImmutableMap.copyOf(connectedNodes); + } + + public void addDebugEvent(DebugEvent debugEvent) { + //Try adding the event to event queue + if (!debugEvents.offer(debugEvent)) { + //buffer is exhausted + Collection list = new ArrayList<>(); + //do not clear all events , make some place by clearing few old events + debugEvents.drainTo(list, EVENT_DRAIN_BUFFER_SIZE); + + if (!debugEvents.offer(debugEvent)) { + LOG.debug("Unable to add debug event"); + } + } + } + + public List getNodeEvents() { + return ImmutableList.copyOf(debugEvents); + } +} diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/HwvtepNodeHACache.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/HwvtepNodeHACache.java deleted file mode 100644 index e9c233644..000000000 --- a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/HwvtepNodeHACache.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.genius.utils.hwvtep; - -import java.util.Map; -import java.util.Set; -import org.eclipse.jdt.annotation.NonNull; -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; - -/** - * Caches hwvtep Node HA info. - * - * @author Thomas Pantelis - */ -public interface HwvtepNodeHACache { - void addChild(InstanceIdentifier parentId, InstanceIdentifier childId); - - boolean isHAEnabledDevice(InstanceIdentifier nodeId); - - boolean isHAParentNode(InstanceIdentifier nodeId); - - // Commented out for now - causes findbugs violation in netvirt - //@NonNull - Set> getChildrenForHANode(InstanceIdentifier parentId); - - @NonNull - Set> getHAParentNodes(); - - @NonNull - Set> getHAChildNodes(); - - InstanceIdentifier getParent(InstanceIdentifier childId); - - void removeParent(InstanceIdentifier parentId); - - void updateConnectedNodeStatus(InstanceIdentifier nodeId); - - void updateDisconnectedNodeStatus(InstanceIdentifier nodeId); - - @NonNull - Map getNodeConnectionStatuses(); -} diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/NodeEvent.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/NodeEvent.java new file mode 100644 index 000000000..8868d8516 --- /dev/null +++ b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/utils/hwvtep/NodeEvent.java @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.genius.utils.hwvtep; + +import java.io.PrintStream; +import java.util.Objects; + +public abstract class NodeEvent extends DebugEvent { + + protected final String nodeId; + + public NodeEvent(String nodeId) { + this.nodeId = nodeId; + } + + public String getNodeId() { + return nodeId; + } + + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } + if (other instanceof NodeEvent) { + return Objects.equals(nodeId, ((NodeEvent) other).nodeId); + } + return false; + } + + @Override + public int hashCode() { + return nodeId != null ? nodeId.hashCode() : 0; + } + + enum NodeStatus { + Connected,Disconnected + } + + public static class NodeConnectedEvent extends NodeEvent { + + public NodeConnectedEvent(String nodeId) { + super(nodeId); + } + + public void print(PrintStream out) { + out.print(nodeId); + out.print(" connected"); + } + } + + public static class NodeDisconnectedEvent extends NodeEvent { + + public NodeDisconnectedEvent(String nodeId) { + super(nodeId); + } + + public void print(PrintStream out) { + out.print(nodeId); + out.print(" disconnected"); + } + } + + public static class ChildAddedEvent extends NodeEvent { + + public ChildAddedEvent(String nodeId) { + super(nodeId); + } + + public void print(PrintStream out) { + out.print(nodeId); + out.print(" became HA child"); + } + } +} diff --git a/mdsalutil/mdsalutil-impl/src/main/java/org/opendaylight/genius/utils/hwvtep/internal/HwvtepNodeHACacheImpl.java b/mdsalutil/mdsalutil-impl/src/main/java/org/opendaylight/genius/utils/hwvtep/internal/HwvtepNodeHACacheImpl.java deleted file mode 100644 index 1458b6fca..000000000 --- a/mdsalutil/mdsalutil-impl/src/main/java/org/opendaylight/genius/utils/hwvtep/internal/HwvtepNodeHACacheImpl.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.genius.utils.hwvtep.internal; - -import com.google.common.collect.ImmutableMap; -import java.util.Collections; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import javax.inject.Singleton; -import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache; -import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants; -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; - -/** - * Implementation of HwvtepNodeHACache. - * - * @author Thomas Pantelis - */ -@Singleton -public class HwvtepNodeHACacheImpl implements HwvtepNodeHACache { - private final ConcurrentHashMap, Set>> parentToChildMap = - new ConcurrentHashMap<>(); - - private final ConcurrentHashMap, InstanceIdentifier> childToParentMap = - new ConcurrentHashMap<>(); - - private final Set childNodeIds = ConcurrentHashMap.newKeySet(); - - private final ConcurrentHashMap connectedNodes = new ConcurrentHashMap<>(); - - @Override - public void addChild(InstanceIdentifier parentId, InstanceIdentifier childId) { - if (parentId == null || childId == null) { - return; - } - - parentToChildMap.computeIfAbsent(parentId, key -> ConcurrentHashMap.newKeySet()).add(childId); - childToParentMap.put(childId, parentId); - String childNodeId = childId.firstKeyOf(Node.class).getNodeId().getValue(); - childNodeIds.add(childNodeId); - } - - @Override - public boolean isHAEnabledDevice(InstanceIdentifier iid) { - if (iid == null) { - return false; - } - boolean enabled = childToParentMap.containsKey(iid.firstIdentifierOf(Node.class)); - if (!enabled) { - String psNodeId = iid.firstKeyOf(Node.class).getNodeId().getValue(); - int idx = psNodeId.indexOf(HwvtepSouthboundConstants.PSWITCH_URI_PREFIX); - if (idx > 0) { - String globalNodeId = psNodeId.substring(0, idx - 1); - return childNodeIds.contains(globalNodeId); - } - } - return enabled; - } - - @Override - public boolean isHAParentNode(InstanceIdentifier nodeId) { - return parentToChildMap.containsKey(nodeId); - } - - @Override - public Set> getChildrenForHANode(InstanceIdentifier parentId) { - Set> children = parentId != null ? parentToChildMap.get(parentId) : null; - return children != null ? children : Collections.emptySet(); - } - - @Override - public Set> getHAParentNodes() { - return parentToChildMap.keySet(); - } - - @Override - public Set> getHAChildNodes() { - return childToParentMap.keySet(); - } - - @Override - public InstanceIdentifier getParent(InstanceIdentifier childId) { - return childId != null ? childToParentMap.get(childId) : null; - } - - @Override - public void removeParent(InstanceIdentifier parentId) { - if (parentId == null) { - return; - } - - if (parentToChildMap.get(parentId) != null) { - Set> childs = parentToChildMap.get(parentId); - for (InstanceIdentifier child : childs) { - childToParentMap.remove(child); - String childNodeId = child.firstKeyOf(Node.class).getNodeId().getValue(); - childNodeIds.remove(childNodeId); - } - } - parentToChildMap.remove(parentId); - } - - @Override - public void updateConnectedNodeStatus(InstanceIdentifier iid) { - String nodeId = iid.firstKeyOf(Node.class).getNodeId().getValue(); - connectedNodes.put(nodeId, true); - } - - @Override - public void updateDisconnectedNodeStatus(InstanceIdentifier iid) { - String nodeId = iid.firstKeyOf(Node.class).getNodeId().getValue(); - connectedNodes.put(nodeId, false); - } - - @Override - public Map getNodeConnectionStatuses() { - return ImmutableMap.copyOf(connectedNodes); - } -} diff --git a/mdsalutil/mdsalutil-impl/src/main/resources/OSGI-INF/blueprint/mdsalutil.xml b/mdsalutil/mdsalutil-impl/src/main/resources/OSGI-INF/blueprint/mdsalutil.xml index 2842b7e19..1636e2e0e 100644 --- a/mdsalutil/mdsalutil-impl/src/main/resources/OSGI-INF/blueprint/mdsalutil.xml +++ b/mdsalutil/mdsalutil-impl/src/main/resources/OSGI-INF/blueprint/mdsalutil.xml @@ -16,8 +16,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html - diff --git a/mdsalutil/mdsalutil-impl/src/test/java/org/opendaylight/genius/utils/hwvtep/internal/HwvtepNodeHACacheImplTest.java b/mdsalutil/mdsalutil-impl/src/test/java/org/opendaylight/genius/utils/hwvtep/internal/HwvtepNodeHACacheImplTest.java deleted file mode 100644 index 5751e2556..000000000 --- a/mdsalutil/mdsalutil-impl/src/test/java/org/opendaylight/genius/utils/hwvtep/internal/HwvtepNodeHACacheImplTest.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.genius.utils.hwvtep.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import org.junit.Test; -import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants; -import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology; -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; -import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey; -import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; -import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class HwvtepNodeHACacheImplTest { - private final HwvtepNodeHACacheImpl hwvtepNodeHACacheImpl = new HwvtepNodeHACacheImpl(); - - @Test - public void testAddChild() { - InstanceIdentifier parent = newNodeInstanceIdentifier("ha"); - InstanceIdentifier child1 = newNodeInstanceIdentifier("d1"); - - hwvtepNodeHACacheImpl.addChild(parent, child1); - - assertTrue(hwvtepNodeHACacheImpl.isHAEnabledDevice(child1)); - assertTrue(hwvtepNodeHACacheImpl.isHAParentNode(parent)); - - InstanceIdentifier child2 = newNodeInstanceIdentifier("d1"); - hwvtepNodeHACacheImpl.addChild(parent, child2); - assertTrue(hwvtepNodeHACacheImpl.isHAEnabledDevice(child1)); - assertTrue(hwvtepNodeHACacheImpl.isHAEnabledDevice(child2)); - assertTrue(hwvtepNodeHACacheImpl.isHAParentNode(parent)); - - assertEquals(ImmutableSet.of(child1, child2), hwvtepNodeHACacheImpl.getHAChildNodes()); - assertEquals(ImmutableSet.of(parent), hwvtepNodeHACacheImpl.getHAParentNodes()); - - assertEquals(ImmutableSet.of(child1, child2), hwvtepNodeHACacheImpl.getChildrenForHANode(parent)); - - hwvtepNodeHACacheImpl.removeParent(parent); - assertFalse(hwvtepNodeHACacheImpl.isHAEnabledDevice(child1)); - assertFalse(hwvtepNodeHACacheImpl.isHAEnabledDevice(child2)); - assertFalse(hwvtepNodeHACacheImpl.isHAParentNode(parent)); - } - - @Test - public void testNodeConnectionStatus() { - InstanceIdentifier node1 = newNodeInstanceIdentifier("node1"); - InstanceIdentifier node2 = newNodeInstanceIdentifier("node2"); - - hwvtepNodeHACacheImpl.updateConnectedNodeStatus(node1); - assertEquals(ImmutableMap.of("node1", Boolean.TRUE), hwvtepNodeHACacheImpl.getNodeConnectionStatuses()); - - hwvtepNodeHACacheImpl.updateConnectedNodeStatus(node2); - assertEquals(ImmutableMap.of("node1", Boolean.TRUE, "node2", Boolean.TRUE), - hwvtepNodeHACacheImpl.getNodeConnectionStatuses()); - - hwvtepNodeHACacheImpl.updateDisconnectedNodeStatus(node1); - assertEquals(ImmutableMap.of("node1", Boolean.FALSE, "node2", Boolean.TRUE), - hwvtepNodeHACacheImpl.getNodeConnectionStatuses()); - } - - private static InstanceIdentifier newNodeInstanceIdentifier(String id) { - NodeId nodeId = new NodeId(id); - NodeKey nodeKey = new NodeKey(nodeId); - TopologyKey topoKey = new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID); - return InstanceIdentifier.builder(NetworkTopology.class) - .child(Topology.class, topoKey) - .child(Node.class, nodeKey) - .build(); - } -} -- 2.36.6