Fix findbugs violations in netconf
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / messagebus / eventsources / netconf / NetconfEventSourceManager.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.netconf.messagebus.eventsources.netconf;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.Map;
14 import java.util.concurrent.ConcurrentHashMap;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
17 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
22 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
23 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
38  * updating and removing registrations of event sources.
39  */
40 public final class NetconfEventSourceManager implements DataTreeChangeListener<Node>, AutoCloseable {
41
42     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSourceManager.class);
43     private static final TopologyKey NETCONF_TOPOLOGY_KEY = new TopologyKey(
44             new TopologyId(TopologyNetconf.QNAME.getLocalName()));
45     private static final InstanceIdentifier<Node> NETCONF_DEVICE_PATH = InstanceIdentifier.create(NetworkTopology.class)
46             .child(Topology.class, NETCONF_TOPOLOGY_KEY).child(Node.class);
47
48     private Map<String, String> streamMap;
49     private final ConcurrentHashMap<InstanceIdentifier<?>, NetconfEventSourceRegistration> registrationMap =
50             new ConcurrentHashMap<>();
51     private final DOMNotificationPublishService publishService;
52     private final DOMMountPointService domMounts;
53     private ListenerRegistration<NetconfEventSourceManager> listenerRegistration;
54     private final EventSourceRegistry eventSourceRegistry;
55     private final DataBroker dataBroker;
56
57     public NetconfEventSourceManager(final DataBroker dataBroker,
58                                      final DOMNotificationPublishService domPublish,
59                                      final DOMMountPointService domMount,
60                                      final EventSourceRegistry eventSourceRegistry) {
61         Preconditions.checkNotNull(dataBroker);
62         Preconditions.checkNotNull(domPublish);
63         Preconditions.checkNotNull(domMount);
64         Preconditions.checkNotNull(eventSourceRegistry);
65         this.dataBroker = dataBroker;
66         this.domMounts = domMount;
67         this.publishService = domPublish;
68         this.eventSourceRegistry = eventSourceRegistry;
69     }
70
71     /**
72      * Invoked by blueprint.
73      */
74     public void initialize() {
75         Preconditions.checkNotNull(dataBroker);
76         listenerRegistration = dataBroker.registerDataTreeChangeListener(new DataTreeIdentifier<>(
77                 LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH), this);
78         LOG.info("NetconfEventSourceManager initialized.");
79     }
80
81     @Override
82     public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
83         for (DataTreeModification<Node> change: changes) {
84             LOG.debug("DataTreeModification: {}", change);
85             final DataObjectModification<Node> rootNode = change.getRootNode();
86             final InstanceIdentifier<Node> identifier = change.getRootPath().getRootIdentifier();
87             switch (rootNode.getModificationType()) {
88                 case WRITE:
89                 case SUBTREE_MODIFIED:
90                     nodeCreated(identifier, rootNode.getDataAfter());
91                     break;
92                 case DELETE:
93                     nodeRemoved(identifier);
94                     break;
95                 default:
96                     break;
97             }
98         }
99     }
100
101     private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
102         Preconditions.checkNotNull(key);
103         if (!validateNode(node)) {
104             LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key.toString());
105             return;
106         }
107         LOG.info("Netconf event source [{}] is creating...", key.toString());
108         NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
109         if (nesr != null) {
110             NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
111             if (nesrOld != null) {
112                 nesrOld.close();
113             }
114         }
115     }
116
117     private void nodeRemoved(final InstanceIdentifier<?> key) {
118         Preconditions.checkNotNull(key);
119         LOG.info("Netconf event source [{}] is removing...", key.toString());
120         NetconfEventSourceRegistration nesr = registrationMap.remove(key);
121         if (nesr != null) {
122             nesr.close();
123         }
124     }
125
126     private boolean validateNode(final Node node) {
127         if (node == null) {
128             return false;
129         }
130         return isNetconfNode(node);
131     }
132
133     Map<String, String> getStreamMap() {
134         return streamMap;
135     }
136
137     DOMNotificationPublishService getPublishService() {
138         return publishService;
139     }
140
141     DOMMountPointService getDomMounts() {
142         return domMounts;
143     }
144
145     EventSourceRegistry getEventSourceRegistry() {
146         return eventSourceRegistry;
147     }
148
149     /**
150      * Invoked by blueprint.
151      *
152      * @param streamMap Stream map
153      */
154     public void setStreamMap(Map<String, String> streamMap) {
155         this.streamMap = streamMap;
156     }
157
158     private boolean isNetconfNode(final Node node) {
159         return node.getAugmentation(NetconfNode.class) != null;
160     }
161
162     @Override
163     public void close() {
164         listenerRegistration.close();
165         for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
166             reg.close();
167         }
168         registrationMap.clear();
169     }
170 }