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
8 package org.opendaylight.netconf.messagebus.eventsources.netconf;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import java.util.Collection;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.DataObjectModification;
19 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
20 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.mdsal.binding.api.DataTreeModification;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
24 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
39 * updating and removing registrations of event sources.
41 public final class NetconfEventSourceManager implements DataTreeChangeListener<Node>, AutoCloseable {
43 private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSourceManager.class);
44 private static final TopologyKey NETCONF_TOPOLOGY_KEY = new TopologyKey(
45 new TopologyId(TopologyNetconf.QNAME.getLocalName()));
46 private static final InstanceIdentifier<Node> NETCONF_DEVICE_PATH = InstanceIdentifier.create(NetworkTopology.class)
47 .child(Topology.class, NETCONF_TOPOLOGY_KEY).child(Node.class);
49 private Map<String, String> streamMap;
50 private final ConcurrentHashMap<InstanceIdentifier<?>, NetconfEventSourceRegistration> registrationMap =
51 new ConcurrentHashMap<>();
52 private final DOMNotificationPublishService publishService;
53 private final DOMMountPointService domMounts;
54 private ListenerRegistration<NetconfEventSourceManager> listenerRegistration;
55 private final EventSourceRegistry eventSourceRegistry;
56 private final DataBroker dataBroker;
58 public NetconfEventSourceManager(final DataBroker dataBroker,
59 final DOMNotificationPublishService domPublish,
60 final DOMMountPointService domMount,
61 final EventSourceRegistry eventSourceRegistry) {
62 this.dataBroker = requireNonNull(dataBroker);
63 this.domMounts = requireNonNull(domMount);
64 this.publishService = requireNonNull(domPublish);
65 this.eventSourceRegistry = requireNonNull(eventSourceRegistry);
69 * Invoked by blueprint.
71 public void initialize() {
72 listenerRegistration = verifyNotNull(dataBroker).registerDataTreeChangeListener(DataTreeIdentifier.create(
73 LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH), this);
74 LOG.info("NetconfEventSourceManager initialized.");
78 public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
79 for (DataTreeModification<Node> change: changes) {
80 LOG.debug("DataTreeModification: {}", change);
81 final DataObjectModification<Node> rootNode = change.getRootNode();
82 final InstanceIdentifier<Node> identifier = change.getRootPath().getRootIdentifier();
83 switch (rootNode.getModificationType()) {
85 case SUBTREE_MODIFIED:
86 nodeCreated(identifier, rootNode.getDataAfter());
89 nodeRemoved(identifier);
97 private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
98 if (!validateNode(node)) {
99 LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key);
102 LOG.info("Netconf event source [{}] is creating...", key);
103 NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(requireNonNull(key), node, this);
105 NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
106 if (nesrOld != null) {
112 private void nodeRemoved(final InstanceIdentifier<?> key) {
113 LOG.info("Netconf event source [{}] is removing...", key);
114 NetconfEventSourceRegistration nesr = registrationMap.remove(requireNonNull(key));
120 private static boolean validateNode(final Node node) {
121 return node == null ? false : isNetconfNode(node);
124 Map<String, String> getStreamMap() {
128 DOMNotificationPublishService getPublishService() {
129 return publishService;
132 DOMMountPointService getDomMounts() {
136 EventSourceRegistry getEventSourceRegistry() {
137 return eventSourceRegistry;
141 * Invoked by blueprint.
143 * @param streamMap Stream map
145 public void setStreamMap(final Map<String, String> streamMap) {
146 this.streamMap = streamMap;
149 private static boolean isNetconfNode(final Node node) {
150 return node.augmentation(NetconfNode.class) != null;
154 public void close() {
155 listenerRegistration.close();
156 for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
159 registrationMap.clear();