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.HashMap;
12 import java.util.List;
14 import javax.annotation.PreDestroy;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
19 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
20 import org.opendaylight.mdsal.binding.api.DataTreeModification;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.openflowplugin.applications.southboundcli.util.OFNode;
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.Registration;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Deactivate;
31 import org.osgi.service.component.annotations.Reference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 @Component(service = DpnTracker.class, immediate = true)
37 public final class DefaultDpnTracker implements DpnTracker, DataTreeChangeListener<FlowCapableNode>, AutoCloseable {
38 private static final Logger LOG = LoggerFactory.getLogger(DefaultDpnTracker.class);
39 public static final String DEFAULT_DPN_NAME = "UNKNOWN";
40 public static final String SEPARATOR = ":";
42 private final Map<Long, String> dpnIdToNameCache = new HashMap<>();
43 private final Registration listenerReg;
47 public DefaultDpnTracker(@Reference final DataBroker dataBroker) {
48 listenerReg = dataBroker.registerTreeChangeListener(
49 DataTreeIdentifier.of(LogicalDatastoreType.OPERATIONAL,
50 InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class)), this);
61 public synchronized List<OFNode> currentNodes() {
62 final var dpnList = new ArrayList<OFNode>();
63 for (var entry : dpnIdToNameCache.entrySet()) {
64 final var dpn = new OFNode(entry.getKey(), entry.getValue());
66 LOG.trace("Added OFNode: {} to the list", dpn.getNodeId());
73 public synchronized void onDataTreeChanged(final List<DataTreeModification<FlowCapableNode>> changes) {
74 for (var change : changes) {
75 final var key = change.getRootPath().path();
76 final var mod = change.getRootNode();
77 final var nodeIdent = key.firstIdentifierOf(FlowCapableNode.class);
78 switch (mod.modificationType()) {
80 remove(nodeIdent, mod.dataBefore());
82 case SUBTREE_MODIFIED:
83 update(nodeIdent, mod.dataBefore(), mod.dataAfter());
86 final var dataBefore = mod.dataBefore();
87 if (dataBefore == null) {
88 add(nodeIdent, mod.dataAfter());
90 update(nodeIdent, dataBefore, mod.dataAfter());
94 throw new IllegalArgumentException("Unhandled modification type " + mod.modificationType());
99 private void remove(final InstanceIdentifier<FlowCapableNode> instId, final FlowCapableNode delNode) {
100 LOG.trace("Received remove notification for {}", delNode);
101 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
102 if (node.length < 2) {
103 LOG.error("Failed to remove Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId()
107 dpnIdToNameCache.remove(Long.parseLong(node[1]));
110 private void update(final InstanceIdentifier<FlowCapableNode> instId,
111 final FlowCapableNode dataObjectModificationBefore, final FlowCapableNode dataObjectModificationAfter) {
113 LOG.trace("Received update notification {}", instId);
114 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
115 if (node.length < 2) {
116 LOG.error("Failed to add Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId().getValue());
119 long dpnId = Long.parseLong(node[1]);
120 String nodeName = dataObjectModificationAfter == null ? null : dataObjectModificationAfter.getDescription();
121 if (nodeName != null) {
122 dpnIdToNameCache.put(dpnId, nodeName);
124 dpnIdToNameCache.put(dpnId, DEFAULT_DPN_NAME);
128 private void add(final InstanceIdentifier<FlowCapableNode> instId, final FlowCapableNode addNode) {
129 LOG.trace("Received ADD notification for {}", instId);
130 String[] node = instId.firstKeyOf(Node.class).getId().getValue().split(SEPARATOR);
131 if (node.length < 2) {
132 LOG.error("Failed to add Unexpected nodeId {}", instId.firstKeyOf(Node.class).getId().getValue());
135 long dpnId = Long.parseLong(node[1]);
136 String dpnName = addNode == null ? null : addNode.getDescription();
137 LOG.trace("Adding DPNID {} to cache", dpnId);
138 if (dpnName == null) {
139 dpnName = DEFAULT_DPN_NAME;
141 dpnIdToNameCache.put(dpnId, dpnName);