Getallnodes should read from cache instead of DS
[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
9 package org.opendaylight.openflowplugin.applications.southboundcli;
10
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Collection;
14 import java.util.Map;
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;
30
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 = ":";
35
36     private final DataBroker dataBroker;
37     private ListenerRegistration<?> listenerReg;
38     private Map<Long, String> dpnIdToNameCache;
39
40     public NodeListener(DataBroker broker) {
41         this.dataBroker = broker;
42     }
43
44     public void start() {
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<>();
51     }
52
53     @Override
54     public void close() {
55         if (listenerReg != null) {
56             listenerReg.close();
57         }
58     }
59
60     @Override
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()) {
68                 case DELETE:
69                     remove(nodeIdent, mod.getDataBefore());
70                     break;
71                 case SUBTREE_MODIFIED:
72                     update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
73                     break;
74                 case WRITE:
75                     if (mod.getDataBefore() == null) {
76                         add(nodeIdent, mod.getDataAfter());
77                     } else {
78                         update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
79                     }
80                     break;
81                 default:
82                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
83             }
84         }
85     }
86
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()
92                     .getValue());
93             return;
94         }
95         long dpnId = Long.parseLong(node[1]);
96         dpnIdToNameCache.remove(dpnId);
97     }
98
99     private void update(InstanceIdentifier<FlowCapableNode> instId, FlowCapableNode dataObjectModificationBefore,
100                           FlowCapableNode dataObjectModificationAfter) {
101
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()
106                     .getValue());
107             return;
108         }
109         long dpnId = Long.parseLong(node[1]);
110         try {
111             String nodeName = dataObjectModificationAfter.getDescription();
112             if (nodeName != null) {
113                 dpnIdToNameCache.put(dpnId, nodeName);
114             } else {
115                 dpnIdToNameCache.put(dpnId , DEFAULT_DPN_NAME);
116             }
117         } catch (NullPointerException e) {
118             LOG.error("Error while converting Node:{} to FlowCapableNode: ", dpnId, e);
119         }
120     }
121
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()
127                     .getValue());
128             return;
129         }
130         long dpnId = Long.parseLong(node[1]);
131         String dpnName = null;
132         try {
133             dpnName = addNode.getDescription();
134         } catch (NullPointerException e) {
135             LOG.error("Error while converting Node:{} to FlowCapableNode: ", dpnId, e);
136         }
137         LOG.trace("Adding DPNID {} to cache", dpnId);
138         if (dpnName == null) {
139             dpnName = DEFAULT_DPN_NAME;
140         }
141         dpnIdToNameCache.put(dpnId, dpnName);
142     }
143
144     public Map<Long, String> getDpnIdToNameCache() {
145         return dpnIdToNameCache;
146     }
147 }