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 = new ConcurrentHashMap<>();
49 private final DOMNotificationPublishService publishService;
50 private final DOMMountPointService domMounts;
51 private ListenerRegistration<DataChangeListener> listenerRegistration;
52 private final EventSourceRegistry eventSourceRegistry;
53 private final DataBroker dataBroker;
55 public NetconfEventSourceManager(final DataBroker dataBroker,
56 final DOMNotificationPublishService domPublish,
57 final DOMMountPointService domMount,
58 final EventSourceRegistry eventSourceRegistry) {
59 Preconditions.checkNotNull(dataBroker);
60 Preconditions.checkNotNull(domPublish);
61 Preconditions.checkNotNull(domMount);
62 Preconditions.checkNotNull(eventSourceRegistry);
63 this.dataBroker = dataBroker;
64 this.domMounts = domMount;
65 this.publishService = domPublish;
66 this.eventSourceRegistry = eventSourceRegistry;
72 public void initialize() {
73 Preconditions.checkNotNull(dataBroker);
74 listenerRegistration = dataBroker
75 .registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH, this,
76 DataChangeScope.SUBTREE);
77 LOG.info("NetconfEventSourceManager initialized.");
80 @Override public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
82 LOG.debug("[DataChangeEvent<InstanceIdentifier<?>, DataObject>: {}]", event);
83 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getCreatedData().entrySet()) {
84 if (changeEntry.getValue() instanceof Node) {
85 nodeCreated(changeEntry.getKey(), (Node) changeEntry.getValue());
89 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
90 if (changeEntry.getValue() instanceof Node) {
91 nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
95 for (InstanceIdentifier<?> removePath : event.getRemovedPaths()) {
96 DataObject removeObject = event.getOriginalData().get(removePath);
97 if (removeObject instanceof Node) {
98 nodeRemoved(removePath);
104 private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
105 Preconditions.checkNotNull(key);
106 if (!validateNode(node)) {
107 LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key.toString());
110 LOG.info("Netconf event source [{}] is creating...", key.toString());
111 NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
113 NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
114 if (nesrOld != null) {
120 private void nodeUpdated(final InstanceIdentifier<?> key, final Node node) {
121 Preconditions.checkNotNull(key);
122 if (!validateNode(node)) {
123 LOG.warn("NodeUpdated event : Node [{}] is null or not valid.", key.toString());
127 LOG.info("Netconf event source [{}] is updating...", key.toString());
128 NetconfEventSourceRegistration nesr = registrationMap.get(key);
132 nodeCreated(key, node);
136 private void nodeRemoved(final InstanceIdentifier<?> key) {
137 Preconditions.checkNotNull(key);
138 LOG.info("Netconf event source [{}] is removing...", key.toString());
139 NetconfEventSourceRegistration nesr = registrationMap.remove(key);
145 private boolean validateNode(final Node node) {
149 return isNetconfNode(node);
152 Map<String, String> getStreamMap() {
156 DOMNotificationPublishService getPublishService() {
157 return publishService;
160 DOMMountPointService getDomMounts() {
164 EventSourceRegistry getEventSourceRegistry() {
165 return eventSourceRegistry;
169 * Invoke by blueprint
172 public void setStreamMap(Map<String, String> streamMap) {
173 this.streamMap = streamMap;
176 private boolean isNetconfNode(final Node node) {
177 return node.getAugmentation(NetconfNode.class) != null;
180 @Override public void close() {
181 listenerRegistration.close();
182 for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
185 registrationMap.clear();