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 java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.HashMap;
13 import java.util.List;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
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.openflowplugin.applications.southboundcli.util.OFNode;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
27 import org.opendaylight.yangtools.concepts.ListenerRegistration;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Deactivate;
32 import org.osgi.service.component.annotations.Reference;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 @Component(service = DpnTracker.class)
38 public final class DefaultDpnTracker
39 implements DpnTracker, ClusteredDataTreeChangeListener<FlowCapableNode>, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(DefaultDpnTracker.class);
41 public static final String DEFAULT_DPN_NAME = "UNKNOWN";
42 public static final String SEPARATOR = ":";
44 private final Map<Long, String> dpnIdToNameCache = new HashMap<>();
45 private final ListenerRegistration<?> listenerReg;
49 public DefaultDpnTracker(@Reference final DataBroker dataBroker) {
50 listenerReg = dataBroker.registerDataTreeChangeListener(
51 DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
52 InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)), this);
63 public synchronized List<OFNode> currentNodes() {
64 final var dpnList = new ArrayList<OFNode>();
65 for (var entry : dpnIdToNameCache.entrySet()) {
66 final var dpn = new OFNode(entry.getKey(), entry.getValue());
68 LOG.trace("Added OFNode: {} to the list", dpn.getNodeId());
75 public synchronized void onDataTreeChanged(final Collection<DataTreeModification<FlowCapableNode>> changes) {
76 for (var change : changes) {
77 final var key = change.getRootPath().getRootIdentifier();
78 final var mod = change.getRootNode();
79 final var nodeIdent = key.firstIdentifierOf(FlowCapableNode.class);
80 switch (mod.getModificationType()) {
82 remove(nodeIdent, mod.getDataBefore());
84 case SUBTREE_MODIFIED:
85 update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
88 if (mod.getDataBefore() == null) {
89 add(nodeIdent, mod.getDataAfter());
91 update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
95 throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
100 private void remove(final InstanceIdentifier<FlowCapableNode> instId, final FlowCapableNode delNode) {
101 LOG.trace("Received remove notification for {}", delNode);
102 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
103 if (node.length < 2) {
104 LOG.error("Failed to remove Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId()
108 dpnIdToNameCache.remove(Long.parseLong(node[1]));
111 private void update(final InstanceIdentifier<FlowCapableNode> instId,
112 final FlowCapableNode dataObjectModificationBefore, final FlowCapableNode dataObjectModificationAfter) {
114 LOG.trace("Received update notification {}", instId);
115 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
116 if (node.length < 2) {
117 LOG.error("Failed to add Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId().getValue());
120 long dpnId = Long.parseLong(node[1]);
121 String nodeName = dataObjectModificationAfter == null ? null : dataObjectModificationAfter.getDescription();
122 if (nodeName != null) {
123 dpnIdToNameCache.put(dpnId, nodeName);
125 dpnIdToNameCache.put(dpnId, DEFAULT_DPN_NAME);
129 private void add(final InstanceIdentifier<FlowCapableNode> instId, final FlowCapableNode addNode) {
130 LOG.trace("Received ADD notification for {}", instId);
131 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
132 if (node.length < 2) {
133 LOG.error("Failed to add Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId().getValue());
136 long dpnId = Long.parseLong(node[1]);
137 String dpnName = addNode == null ? null : addNode.getDescription();
138 LOG.trace("Adding DPNID {} to cache", dpnId);
139 if (dpnName == null) {
140 dpnName = DEFAULT_DPN_NAME;
142 dpnIdToNameCache.put(dpnId, dpnName);