Bug 8153: Enforce check-style on messagebus-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.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
21 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
37  * updating and removing registrations of event sources.
38  */
39 public final class NetconfEventSourceManager implements DataChangeListener, AutoCloseable {
40
41     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSourceManager.class);
42     private static final TopologyKey NETCONF_TOPOLOGY_KEY = new TopologyKey(
43             new TopologyId(TopologyNetconf.QNAME.getLocalName()));
44     private static final InstanceIdentifier<Node> NETCONF_DEVICE_PATH = InstanceIdentifier.create(NetworkTopology.class)
45             .child(Topology.class, NETCONF_TOPOLOGY_KEY).child(Node.class);
46
47     private Map<String, String> streamMap;
48     private final ConcurrentHashMap<InstanceIdentifier<?>, NetconfEventSourceRegistration> registrationMap =
49             new ConcurrentHashMap<>();
50     private final DOMNotificationPublishService publishService;
51     private final DOMMountPointService domMounts;
52     private ListenerRegistration<DataChangeListener> listenerRegistration;
53     private final EventSourceRegistry eventSourceRegistry;
54     private final DataBroker dataBroker;
55
56     public NetconfEventSourceManager(final DataBroker dataBroker,
57                                      final DOMNotificationPublishService domPublish,
58                                      final DOMMountPointService domMount,
59                                      final EventSourceRegistry eventSourceRegistry) {
60         Preconditions.checkNotNull(dataBroker);
61         Preconditions.checkNotNull(domPublish);
62         Preconditions.checkNotNull(domMount);
63         Preconditions.checkNotNull(eventSourceRegistry);
64         this.dataBroker = dataBroker;
65         this.domMounts = domMount;
66         this.publishService = domPublish;
67         this.eventSourceRegistry = eventSourceRegistry;
68     }
69
70     /**
71      * Invoked by blueprint.
72      */
73     public void initialize() {
74         Preconditions.checkNotNull(dataBroker);
75         listenerRegistration = dataBroker
76                 .registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH, this,
77                         DataChangeScope.SUBTREE);
78         LOG.info("NetconfEventSourceManager initialized.");
79     }
80
81     @Override
82     public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
83
84         LOG.debug("[DataChangeEvent<InstanceIdentifier<?>, DataObject>: {}]", event);
85         for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getCreatedData().entrySet()) {
86             if (changeEntry.getValue() instanceof Node) {
87                 nodeCreated(changeEntry.getKey(), (Node) changeEntry.getValue());
88             }
89         }
90
91         for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
92             if (changeEntry.getValue() instanceof Node) {
93                 nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
94             }
95         }
96
97         for (InstanceIdentifier<?> removePath : event.getRemovedPaths()) {
98             DataObject removeObject = event.getOriginalData().get(removePath);
99             if (removeObject instanceof Node) {
100                 nodeRemoved(removePath);
101             }
102         }
103
104     }
105
106     private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
107         Preconditions.checkNotNull(key);
108         if (!validateNode(node)) {
109             LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key.toString());
110             return;
111         }
112         LOG.info("Netconf event source [{}] is creating...", key.toString());
113         NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
114         if (nesr != null) {
115             NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
116             if (nesrOld != null) {
117                 nesrOld.close();
118             }
119         }
120     }
121
122     private void nodeUpdated(final InstanceIdentifier<?> key, final Node node) {
123         Preconditions.checkNotNull(key);
124         if (!validateNode(node)) {
125             LOG.warn("NodeUpdated event : Node [{}] is null or not valid.", key.toString());
126             return;
127         }
128
129         LOG.info("Netconf event source [{}] is updating...", key.toString());
130         NetconfEventSourceRegistration nesr = registrationMap.get(key);
131         if (nesr != null) {
132             nesr.updateStatus();
133         } else {
134             nodeCreated(key, node);
135         }
136     }
137
138     private void nodeRemoved(final InstanceIdentifier<?> key) {
139         Preconditions.checkNotNull(key);
140         LOG.info("Netconf event source [{}] is removing...", key.toString());
141         NetconfEventSourceRegistration nesr = registrationMap.remove(key);
142         if (nesr != null) {
143             nesr.close();
144         }
145     }
146
147     private boolean validateNode(final Node node) {
148         if (node == null) {
149             return false;
150         }
151         return isNetconfNode(node);
152     }
153
154     Map<String, String> getStreamMap() {
155         return streamMap;
156     }
157
158     DOMNotificationPublishService getPublishService() {
159         return publishService;
160     }
161
162     DOMMountPointService getDomMounts() {
163         return domMounts;
164     }
165
166     EventSourceRegistry getEventSourceRegistry() {
167         return eventSourceRegistry;
168     }
169
170     /**
171      * Invoked by blueprint.
172      *
173      * @param streamMap Stream map
174      */
175     public void setStreamMap(Map<String, String> streamMap) {
176         this.streamMap = streamMap;
177     }
178
179     private boolean isNetconfNode(final Node node) {
180         return node.getAugmentation(NetconfNode.class) != null;
181     }
182
183     @Override
184     public void close() {
185         listenerRegistration.close();
186         for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
187             reg.close();
188         }
189         registrationMap.clear();
190     }
191
192 }