2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.messagebus.eventsources.netconf;
11 import com.google.common.base.Preconditions;
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;
36 * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
37 * updating and removing registrations of event sources.
39 public final class NetconfEventSourceManager implements DataChangeListener, AutoCloseable {
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);
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;
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;
71 * Invoked by blueprint.
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.");
82 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
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());
91 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
92 if (changeEntry.getValue() instanceof Node) {
93 nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
97 for (InstanceIdentifier<?> removePath : event.getRemovedPaths()) {
98 DataObject removeObject = event.getOriginalData().get(removePath);
99 if (removeObject instanceof Node) {
100 nodeRemoved(removePath);
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());
112 LOG.info("Netconf event source [{}] is creating...", key.toString());
113 NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
115 NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
116 if (nesrOld != null) {
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());
129 LOG.info("Netconf event source [{}] is updating...", key.toString());
130 NetconfEventSourceRegistration nesr = registrationMap.get(key);
134 nodeCreated(key, node);
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);
147 private boolean validateNode(final Node node) {
151 return isNetconfNode(node);
154 Map<String, String> getStreamMap() {
158 DOMNotificationPublishService getPublishService() {
159 return publishService;
162 DOMMountPointService getDomMounts() {
166 EventSourceRegistry getEventSourceRegistry() {
167 return eventSourceRegistry;
171 * Invoked by blueprint.
173 * @param streamMap Stream map
175 public void setStreamMap(Map<String, String> streamMap) {
176 this.streamMap = streamMap;
179 private boolean isNetconfNode(final Node node) {
180 return node.getAugmentation(NetconfNode.class) != null;
184 public void close() {
185 listenerRegistration.close();
186 for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
189 registrationMap.clear();