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
9 package org.opendaylight.openflowplugin.applications.southboundcli;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Collection;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
18 import org.opendaylight.mdsal.binding.api.DataBroker;
19 import org.opendaylight.mdsal.binding.api.DataObjectModification;
20 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.mdsal.binding.api.DataTreeModification;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class NodeListener implements ClusteredDataTreeChangeListener<FlowCapableNode>, AutoCloseable {
32 private static final Logger LOG = LoggerFactory.getLogger(NodeListener.class);
33 public static final String DEFAULT_DPN_NAME = "UNKNOWN";
34 public static final String SEPARATOR = ":";
36 private final DataBroker dataBroker;
37 private ListenerRegistration<?> listenerReg;
38 private Map<Long, String> dpnIdToNameCache;
40 public NodeListener(DataBroker broker) {
41 this.dataBroker = broker;
45 final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
46 .augmentation(FlowCapableNode.class);
47 final DataTreeIdentifier<FlowCapableNode> identifier =
48 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, path);
49 listenerReg = dataBroker.registerDataTreeChangeListener(identifier, NodeListener.this);
50 dpnIdToNameCache = new ConcurrentHashMap<>();
55 if (listenerReg != null) {
61 public void onDataTreeChanged(@NonNull Collection<DataTreeModification<FlowCapableNode>> changes) {
62 requireNonNull(changes, "Changes may not be null!");
63 for (DataTreeModification<FlowCapableNode> change : changes) {
64 final InstanceIdentifier<FlowCapableNode> key = change.getRootPath().getRootIdentifier();
65 final DataObjectModification<FlowCapableNode> mod = change.getRootNode();
66 final InstanceIdentifier<FlowCapableNode> nodeIdent = key.firstIdentifierOf(FlowCapableNode.class);
67 switch (mod.getModificationType()) {
69 remove(nodeIdent, mod.getDataBefore());
71 case SUBTREE_MODIFIED:
72 update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
75 if (mod.getDataBefore() == null) {
76 add(nodeIdent, mod.getDataAfter());
78 update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
82 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
87 private void remove(InstanceIdentifier<FlowCapableNode> instId, FlowCapableNode delNode) {
88 LOG.trace("Received remove notification for {}", delNode);
89 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
90 if (node.length < 2) {
91 LOG.error("Failed to remove Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId()
95 long dpnId = Long.parseLong(node[1]);
96 dpnIdToNameCache.remove(dpnId);
99 private void update(InstanceIdentifier<FlowCapableNode> instId, FlowCapableNode dataObjectModificationBefore,
100 FlowCapableNode dataObjectModificationAfter) {
102 LOG.trace("Received update notification {}", instId);
103 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
104 if (node.length < 2) {
105 LOG.error("Failed to add Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId()
109 long dpnId = Long.parseLong(node[1]);
111 String nodeName = dataObjectModificationAfter.getDescription();
112 if (nodeName != null) {
113 dpnIdToNameCache.put(dpnId, nodeName);
115 dpnIdToNameCache.put(dpnId , DEFAULT_DPN_NAME);
117 } catch (NullPointerException e) {
118 LOG.error("Error while converting Node:{} to FlowCapableNode: ", dpnId, e);
122 private void add(InstanceIdentifier<FlowCapableNode> instId, FlowCapableNode addNode) {
123 LOG.trace("Received ADD notification for {}", instId);
124 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
125 if (node.length < 2) {
126 LOG.error("Failed to add Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId()
130 long dpnId = Long.parseLong(node[1]);
131 String dpnName = null;
133 dpnName = addNode.getDescription();
134 } catch (NullPointerException e) {
135 LOG.error("Error while converting Node:{} to FlowCapableNode: ", dpnId, e);
137 LOG.trace("Adding DPNID {} to cache", dpnId);
138 if (dpnName == null) {
139 dpnName = DEFAULT_DPN_NAME;
141 dpnIdToNameCache.put(dpnId, dpnName);
144 public Map<Long, String> getDpnIdToNameCache() {
145 return dpnIdToNameCache;