Eliminate AlarmAgent
[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.Collection;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
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;
35
36 @Singleton
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 = ":";
43
44     private final Map<Long, String> dpnIdToNameCache = new HashMap<>();
45     private final ListenerRegistration<?> listenerReg;
46
47     @Inject
48     @Activate
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);
53     }
54
55     @PreDestroy
56     @Deactivate
57     @Override
58     public void close() {
59         listenerReg.close();
60     }
61
62     @Override
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());
67             dpnList.add(dpn);
68             LOG.trace("Added OFNode: {} to the list", dpn.getNodeId());
69         }
70         dpnList.sort(null);
71         return dpnList;
72     }
73
74     @Override
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()) {
81                 case DELETE:
82                     remove(nodeIdent, mod.getDataBefore());
83                     break;
84                 case SUBTREE_MODIFIED:
85                     update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
86                     break;
87                 case WRITE:
88                     if (mod.getDataBefore() == null) {
89                         add(nodeIdent, mod.getDataAfter());
90                     } else {
91                         update(nodeIdent, mod.getDataBefore(), mod.getDataAfter());
92                     }
93                     break;
94                 default:
95                     throw new IllegalArgumentException("Unhandled modification type " + mod.getModificationType());
96             }
97         }
98     }
99
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()
105                     .getValue());
106             return;
107         }
108         dpnIdToNameCache.remove(Long.parseLong(node[1]));
109     }
110
111     private void update(final InstanceIdentifier<FlowCapableNode> instId,
112             final FlowCapableNode dataObjectModificationBefore, final FlowCapableNode dataObjectModificationAfter) {
113
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());
118             return;
119         }
120         long dpnId = Long.parseLong(node[1]);
121         String nodeName = dataObjectModificationAfter == null ? null : dataObjectModificationAfter.getDescription();
122         if (nodeName != null) {
123             dpnIdToNameCache.put(dpnId, nodeName);
124         } else {
125             dpnIdToNameCache.put(dpnId, DEFAULT_DPN_NAME);
126         }
127     }
128
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());
134             return;
135         }
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;
141         }
142         dpnIdToNameCache.put(dpnId, dpnName);
143     }
144 }