Eliminate AlarmAgent
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / NodeListener.java
1 /*
2  * Copyright (c) 2019 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.applications.southboundcli;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.Collection;
13 import java.util.Map;
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;
28
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 = ":";
33
34     private final Map<Long, String> dpnIdToNameCache = new ConcurrentHashMap<>();
35     private final DataBroker dataBroker;
36     private ListenerRegistration<?> listenerReg;
37
38     public NodeListener(final DataBroker broker) {
39         dataBroker = broker;
40     }
41
42     public void start() {
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);
48     }
49
50     @Override
51     public void close() {
52         if (listenerReg != null) {
53             listenerReg.close();
54         }
55     }
56
57     @Override
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()) {
65                 case DELETE:
66                     remove(nodeIdent, mod.getDataBefore());
67                     break;
68                 case SUBTREE_MODIFIED:
69                     update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
70                     break;
71                 case WRITE:
72                     if (mod.getDataBefore() == null) {
73                         add(nodeIdent, mod.getDataAfter());
74                     } else {
75                         update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
76                     }
77                     break;
78                 default:
79                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
80             }
81         }
82     }
83
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()
89                     .getValue());
90             return;
91         }
92         long dpnId = Long.parseLong(node[1]);
93         dpnIdToNameCache.remove(dpnId);
94     }
95
96     private void update(final InstanceIdentifier<FlowCapableNode> instId,
97             final FlowCapableNode dataObjectModificationBefore, final FlowCapableNode dataObjectModificationAfter) {
98
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());
103             return;
104         }
105         long dpnId = Long.parseLong(node[1]);
106         String nodeName = dataObjectModificationAfter == null ? null : dataObjectModificationAfter.getDescription();
107         if (nodeName != null) {
108             dpnIdToNameCache.put(dpnId, nodeName);
109         } else {
110             dpnIdToNameCache.put(dpnId, DEFAULT_DPN_NAME);
111         }
112     }
113
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());
119             return;
120         }
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;
126         }
127         dpnIdToNameCache.put(dpnId, dpnName);
128     }
129
130     public Map<Long, String> getDpnIdToNameCache() {
131         return dpnIdToNameCache;
132     }
133 }