2 * Copyright (c) 2017 Inocybe Technologies 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
8 package org.opendaylight.genius.utils.hwvtep.internal;
10 import com.google.common.collect.ImmutableMap;
11 import java.util.Collections;
14 import java.util.concurrent.ConcurrentHashMap;
15 import javax.inject.Singleton;
16 import org.opendaylight.genius.utils.hwvtep.HwvtepNodeHACache;
17 import org.opendaylight.genius.utils.hwvtep.HwvtepSouthboundConstants;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
22 * Implementation of HwvtepNodeHACache.
24 * @author Thomas Pantelis
27 public class HwvtepNodeHACacheImpl implements HwvtepNodeHACache {
28 private final ConcurrentHashMap<InstanceIdentifier<Node>, Set<InstanceIdentifier<Node>>> parentToChildMap =
29 new ConcurrentHashMap<>();
31 private final ConcurrentHashMap<InstanceIdentifier<Node>, InstanceIdentifier<Node>> childToParentMap =
32 new ConcurrentHashMap<>();
34 private final Set<String> childNodeIds = ConcurrentHashMap.newKeySet();
36 private final ConcurrentHashMap<String, Boolean> connectedNodes = new ConcurrentHashMap<>();
39 public void addChild(InstanceIdentifier<Node> parentId, InstanceIdentifier<Node> childId) {
40 if (parentId == null || childId == null) {
44 parentToChildMap.computeIfAbsent(parentId, key -> ConcurrentHashMap.newKeySet()).add(childId);
45 childToParentMap.put(childId, parentId);
46 String childNodeId = childId.firstKeyOf(Node.class).getNodeId().getValue();
47 childNodeIds.add(childNodeId);
51 public boolean isHAEnabledDevice(InstanceIdentifier<?> iid) {
55 boolean enabled = childToParentMap.containsKey(iid.firstIdentifierOf(Node.class));
57 String psNodeId = iid.firstKeyOf(Node.class).getNodeId().getValue();
58 int idx = psNodeId.indexOf(HwvtepSouthboundConstants.PSWITCH_URI_PREFIX);
60 String globalNodeId = psNodeId.substring(0, idx - 1);
61 return childNodeIds.contains(globalNodeId);
68 public boolean isHAParentNode(InstanceIdentifier<Node> nodeId) {
69 return parentToChildMap.containsKey(nodeId);
73 public Set<InstanceIdentifier<Node>> getChildrenForHANode(InstanceIdentifier<Node> parentId) {
74 Set<InstanceIdentifier<Node>> children = parentId != null ? parentToChildMap.get(parentId) : null;
75 return children != null ? children : Collections.emptySet();
79 public Set<InstanceIdentifier<Node>> getHAParentNodes() {
80 return parentToChildMap.keySet();
84 public Set<InstanceIdentifier<Node>> getHAChildNodes() {
85 return childToParentMap.keySet();
89 public InstanceIdentifier<Node> getParent(InstanceIdentifier<Node> childId) {
90 return childId != null ? childToParentMap.get(childId) : null;
94 public void removeParent(InstanceIdentifier<Node> parentId) {
95 if (parentId == null) {
99 if (parentToChildMap.get(parentId) != null) {
100 Set<InstanceIdentifier<Node>> childs = parentToChildMap.get(parentId);
101 for (InstanceIdentifier<Node> child : childs) {
102 childToParentMap.remove(child);
103 String childNodeId = child.firstKeyOf(Node.class).getNodeId().getValue();
104 childNodeIds.remove(childNodeId);
107 parentToChildMap.remove(parentId);
111 public void updateConnectedNodeStatus(InstanceIdentifier<Node> iid) {
112 String nodeId = iid.firstKeyOf(Node.class).getNodeId().getValue();
113 connectedNodes.put(nodeId, true);
117 public void updateDisconnectedNodeStatus(InstanceIdentifier<Node> iid) {
118 String nodeId = iid.firstKeyOf(Node.class).getNodeId().getValue();
119 connectedNodes.put(nodeId, false);
123 public Map<String, Boolean> getNodeConnectionStatuses() {
124 return ImmutableMap.copyOf(connectedNodes);