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.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
40 * updating and removing registrations of event sources.
42 @Deprecated(forRemoval = true)
43 public final class NetconfEventSourceManager implements DataTreeChangeListener<Node>, AutoCloseable {
45 private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSourceManager.class);
46 private static final TopologyKey NETCONF_TOPOLOGY_KEY = new TopologyKey(
47 new TopologyId(TopologyNetconf.QNAME.getLocalName()));
48 private static final InstanceIdentifier<Node> NETCONF_DEVICE_PATH = InstanceIdentifier.create(NetworkTopology.class)
49 .child(Topology.class, NETCONF_TOPOLOGY_KEY).child(Node.class);
51 private Map<String, String> streamMap;
52 private final ConcurrentHashMap<InstanceIdentifier<?>, NetconfEventSourceRegistration> registrationMap =
53 new ConcurrentHashMap<>();
54 private final DOMNotificationPublishService publishService;
55 private final DOMMountPointService domMounts;
56 private ListenerRegistration<NetconfEventSourceManager> listenerRegistration;
57 private final EventSourceRegistry eventSourceRegistry;
58 private final DataBroker dataBroker;
59 private final BindingNormalizedNodeSerializer serializer;
61 public NetconfEventSourceManager(final DataBroker dataBroker,
62 final BindingNormalizedNodeSerializer serializer,
63 final DOMNotificationPublishService domPublish,
64 final DOMMountPointService domMount,
65 final EventSourceRegistry eventSourceRegistry) {
66 this.dataBroker = requireNonNull(dataBroker);
67 this.serializer = requireNonNull(serializer);
68 this.domMounts = requireNonNull(domMount);
69 this.publishService = requireNonNull(domPublish);
70 this.eventSourceRegistry = requireNonNull(eventSourceRegistry);
74 * Invoked by blueprint.
76 public void initialize() {
77 listenerRegistration = verifyNotNull(dataBroker).registerDataTreeChangeListener(DataTreeIdentifier.create(
78 LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH), this);
79 LOG.info("NetconfEventSourceManager initialized.");
83 public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
84 for (DataTreeModification<Node> change: changes) {
85 LOG.debug("DataTreeModification: {}", change);
86 final DataObjectModification<Node> rootNode = change.getRootNode();
87 final InstanceIdentifier<Node> identifier = change.getRootPath().getRootIdentifier();
88 switch (rootNode.getModificationType()) {
90 case SUBTREE_MODIFIED:
91 nodeCreated(identifier, rootNode.getDataAfter());
94 nodeRemoved(identifier);
102 private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
103 if (!validateNode(node)) {
104 LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key);
107 LOG.info("Netconf event source [{}] is creating...", key);
108 NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(serializer, requireNonNull(key),
111 NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
112 if (nesrOld != null) {
118 private void nodeRemoved(final InstanceIdentifier<?> key) {
119 LOG.info("Netconf event source [{}] is removing...", key);
120 NetconfEventSourceRegistration nesr = registrationMap.remove(requireNonNull(key));
126 private static boolean validateNode(final Node node) {
127 return node == null ? false : isNetconfNode(node);
130 Map<String, String> getStreamMap() {
134 DOMNotificationPublishService getPublishService() {
135 return publishService;
138 DOMMountPointService getDomMounts() {
142 EventSourceRegistry getEventSourceRegistry() {
143 return eventSourceRegistry;
147 * Invoked by blueprint.
149 * @param streamMap Stream map
151 public void setStreamMap(final Map<String, String> streamMap) {
152 this.streamMap = streamMap;
155 private static boolean isNetconfNode(final Node node) {
156 return node.augmentation(NetconfNode.class) != null;
160 public void close() {
161 listenerRegistration.close();
162 for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
165 registrationMap.clear();