f0a37a076a709bd901639b8a132f9a37abfd2ac8
[openflowplugin.git] / applications / southbound-cli / src / main / java / org / opendaylight / openflowplugin / applications / southboundcli / DefaultDpnTracker.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 java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
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;
34
35 @Singleton
36 @Component(service = DpnTracker.class)
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 = ":";
41
42     private final Map<Long, String> dpnIdToNameCache = new HashMap<>();
43     private final Registration listenerReg;
44
45     @Inject
46     @Activate
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);
51     }
52
53     @PreDestroy
54     @Deactivate
55     @Override
56     public void close() {
57         listenerReg.close();
58     }
59
60     @Override
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());
65             dpnList.add(dpn);
66             LOG.trace("Added OFNode: {} to the list", dpn.getNodeId());
67         }
68         dpnList.sort(null);
69         return dpnList;
70     }
71
72     @Override
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()) {
79                 case DELETE:
80                     remove(nodeIdent, mod.dataBefore());
81                     break;
82                 case SUBTREE_MODIFIED:
83                     update(nodeIdent, mod.dataBefore(), mod.dataAfter());
84                     break;
85                 case WRITE:
86                     final var dataBefore = mod.dataBefore();
87                     if (dataBefore == null) {
88                         add(nodeIdent, mod.dataAfter());
89                     } else {
90                         update(nodeIdent, dataBefore, mod.dataAfter());
91                     }
92                     break;
93                 default:
94                     throw new IllegalArgumentException("Unhandled modification type " + mod.modificationType());
95             }
96         }
97     }
98
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()
104                     .getValue());
105             return;
106         }
107         dpnIdToNameCache.remove(Long.parseLong(node[1]));
108     }
109
110     private void update(final InstanceIdentifier<FlowCapableNode> instId,
111             final FlowCapableNode dataObjectModificationBefore, final FlowCapableNode dataObjectModificationAfter) {
112
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());
117             return;
118         }
119         long dpnId = Long.parseLong(node[1]);
120         String nodeName = dataObjectModificationAfter == null ? null : dataObjectModificationAfter.getDescription();
121         if (nodeName != null) {
122             dpnIdToNameCache.put(dpnId, nodeName);
123         } else {
124             dpnIdToNameCache.put(dpnId, DEFAULT_DPN_NAME);
125         }
126     }
127
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());
133             return;
134         }
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;
140         }
141         dpnIdToNameCache.put(dpnId, dpnName);
142     }
143 }