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;
12 import java.util.Collection;
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;
37 * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
38 * updating and removing registrations of event sources.
40 public final class NetconfEventSourceManager implements DataTreeChangeListener<Node>, AutoCloseable {
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);
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;
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;
72 * Invoked by blueprint.
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.");
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()) {
89 case SUBTREE_MODIFIED:
90 nodeCreated(identifier, rootNode.getDataAfter());
93 nodeRemoved(identifier);
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());
107 LOG.info("Netconf event source [{}] is creating...", key.toString());
108 NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
110 NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
111 if (nesrOld != null) {
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);
126 private boolean validateNode(final Node node) {
130 return isNetconfNode(node);
133 Map<String, String> getStreamMap() {
137 DOMNotificationPublishService getPublishService() {
138 return publishService;
141 DOMMountPointService getDomMounts() {
145 EventSourceRegistry getEventSourceRegistry() {
146 return eventSourceRegistry;
150 * Invoked by blueprint.
152 * @param streamMap Stream map
154 public void setStreamMap(Map<String, String> streamMap) {
155 this.streamMap = streamMap;
158 private boolean isNetconfNode(final Node node) {
159 return node.getAugmentation(NetconfNode.class) != null;
163 public void close() {
164 listenerRegistration.close();
165 for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
168 registrationMap.clear();