2 * Copyright (c) 2019 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
8 package org.opendaylight.openflowplugin.applications.southboundcli;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Collection;
14 import java.util.concurrent.ConcurrentHashMap;
15 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.DataObjectModification;
18 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.mdsal.binding.api.DataTreeModification;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class NodeListener implements ClusteredDataTreeChangeListener<FlowCapableNode>, AutoCloseable {
30 private static final Logger LOG = LoggerFactory.getLogger(NodeListener.class);
31 public static final String DEFAULT_DPN_NAME = "UNKNOWN";
32 public static final String SEPARATOR = ":";
34 private final Map<Long, String> dpnIdToNameCache = new ConcurrentHashMap<>();
35 private final DataBroker dataBroker;
36 private ListenerRegistration<?> listenerReg;
38 public NodeListener(final DataBroker broker) {
43 final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
44 .augmentation(FlowCapableNode.class);
45 final DataTreeIdentifier<FlowCapableNode> identifier =
46 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, path);
47 listenerReg = dataBroker.registerDataTreeChangeListener(identifier, NodeListener.this);
52 if (listenerReg != null) {
58 public void onDataTreeChanged(final Collection<DataTreeModification<FlowCapableNode>> changes) {
59 requireNonNull(changes, "Changes may not be null!");
60 for (DataTreeModification<FlowCapableNode> change : changes) {
61 final InstanceIdentifier<FlowCapableNode> key = change.getRootPath().getRootIdentifier();
62 final DataObjectModification<FlowCapableNode> mod = change.getRootNode();
63 final InstanceIdentifier<FlowCapableNode> nodeIdent = key.firstIdentifierOf(FlowCapableNode.class);
64 switch (mod.getModificationType()) {
66 remove(nodeIdent, mod.getDataBefore());
68 case SUBTREE_MODIFIED:
69 update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
72 if (mod.getDataBefore() == null) {
73 add(nodeIdent, mod.getDataAfter());
75 update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
79 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
84 private void remove(final InstanceIdentifier<FlowCapableNode> instId, final FlowCapableNode delNode) {
85 LOG.trace("Received remove notification for {}", delNode);
86 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
87 if (node.length < 2) {
88 LOG.error("Failed to remove Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId()
92 long dpnId = Long.parseLong(node[1]);
93 dpnIdToNameCache.remove(dpnId);
96 private void update(final InstanceIdentifier<FlowCapableNode> instId,
97 final FlowCapableNode dataObjectModificationBefore, final FlowCapableNode dataObjectModificationAfter) {
99 LOG.trace("Received update notification {}", instId);
100 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
101 if (node.length < 2) {
102 LOG.error("Failed to add Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId().getValue());
105 long dpnId = Long.parseLong(node[1]);
106 String nodeName = dataObjectModificationAfter == null ? null : dataObjectModificationAfter.getDescription();
107 if (nodeName != null) {
108 dpnIdToNameCache.put(dpnId, nodeName);
110 dpnIdToNameCache.put(dpnId, DEFAULT_DPN_NAME);
114 private void add(final InstanceIdentifier<FlowCapableNode> instId, final FlowCapableNode addNode) {
115 LOG.trace("Received ADD notification for {}", instId);
116 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
117 if (node.length < 2) {
118 LOG.error("Failed to add Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId().getValue());
121 long dpnId = Long.parseLong(node[1]);
122 String dpnName = addNode == null ? null : addNode.getDescription();
123 LOG.trace("Adding DPNID {} to cache", dpnId);
124 if (dpnName == null) {
125 dpnName = DEFAULT_DPN_NAME;
127 dpnIdToNameCache.put(dpnId, dpnName);
130 public Map<Long, String> getDpnIdToNameCache() {
131 return dpnIdToNameCache;