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.HashMap;
13 import java.util.List;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.config.yang.messagebus.netconf.NamespaceToStream;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
20 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
21 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
24 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
25 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
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.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
41 * updating and removing registrations of event sources.
43 public final class NetconfEventSourceManager implements DataChangeListener, 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 final Map<String, String> streamMap;
52 private final ConcurrentHashMap<InstanceIdentifier<?>, NetconfEventSourceRegistration> registrationMap = new ConcurrentHashMap<>();
53 private final DOMNotificationPublishService publishService;
54 private final DOMMountPointService domMounts;
55 private final MountPointService mountPointService;
56 private ListenerRegistration<DataChangeListener> listenerRegistration;
57 private final EventSourceRegistry eventSourceRegistry;
59 public static NetconfEventSourceManager create(final DataBroker dataBroker,
60 final DOMNotificationPublishService domPublish, final DOMMountPointService domMount,
61 final MountPointService bindingMount, final EventSourceRegistry eventSourceRegistry,
62 final List<NamespaceToStream> namespaceMapping) {
64 final NetconfEventSourceManager eventSourceManager = new NetconfEventSourceManager(domPublish, domMount,
65 bindingMount, eventSourceRegistry, namespaceMapping);
67 eventSourceManager.initialize(dataBroker);
69 return eventSourceManager;
73 private NetconfEventSourceManager(final DOMNotificationPublishService domPublish,
74 final DOMMountPointService domMount, final MountPointService bindingMount,
75 final EventSourceRegistry eventSourceRegistry, final List<NamespaceToStream> namespaceMapping) {
77 Preconditions.checkNotNull(domPublish);
78 Preconditions.checkNotNull(domMount);
79 Preconditions.checkNotNull(bindingMount);
80 Preconditions.checkNotNull(eventSourceRegistry);
81 Preconditions.checkNotNull(namespaceMapping);
82 this.streamMap = namespaceToStreamMapping(namespaceMapping);
83 this.domMounts = domMount;
84 this.mountPointService = bindingMount;
85 this.publishService = domPublish;
86 this.eventSourceRegistry = eventSourceRegistry;
89 private void initialize(final DataBroker dataBroker) {
90 Preconditions.checkNotNull(dataBroker);
91 listenerRegistration = dataBroker
92 .registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH, this,
93 DataChangeScope.SUBTREE);
94 LOG.info("NetconfEventSourceManager initialized.");
97 private Map<String, String> namespaceToStreamMapping(final List<NamespaceToStream> namespaceMapping) {
98 final Map<String, String> streamMap = new HashMap<>(namespaceMapping.size());
100 for (final NamespaceToStream nToS : namespaceMapping) {
101 streamMap.put(nToS.getUrnPrefix(), nToS.getStreamName());
107 @Override public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
109 LOG.debug("[DataChangeEvent<InstanceIdentifier<?>, DataObject>: {}]", event);
110 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getCreatedData().entrySet()) {
111 if (changeEntry.getValue() instanceof Node) {
112 nodeCreated(changeEntry.getKey(), (Node) changeEntry.getValue());
116 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
117 if (changeEntry.getValue() instanceof Node) {
118 nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
122 for (InstanceIdentifier<?> removePath : event.getRemovedPaths()) {
123 DataObject removeObject = event.getOriginalData().get(removePath);
124 if (removeObject instanceof Node) {
125 nodeRemoved(removePath);
131 private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
132 Preconditions.checkNotNull(key);
133 if (validateNode(node) == false) {
134 LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key.toString());
137 LOG.info("Netconf event source [{}] is creating...", key.toString());
138 NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
140 NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
141 if (nesrOld != null) {
147 private void nodeUpdated(final InstanceIdentifier<?> key, final Node node) {
148 Preconditions.checkNotNull(key);
149 if (validateNode(node) == false) {
150 LOG.warn("NodeUpdated event : Node [{}] is null or not valid.", key.toString());
154 LOG.info("Netconf event source [{}] is updating...", key.toString());
155 NetconfEventSourceRegistration nesr = registrationMap.get(key);
159 nodeCreated(key, node);
163 private void nodeRemoved(final InstanceIdentifier<?> key) {
164 Preconditions.checkNotNull(key);
165 LOG.info("Netconf event source [{}] is removing...", key.toString());
166 NetconfEventSourceRegistration nesr = registrationMap.remove(key);
172 private boolean validateNode(final Node node) {
176 return isNetconfNode(node);
179 Map<String, String> getStreamMap() {
183 DOMNotificationPublishService getPublishService() {
184 return publishService;
187 DOMMountPointService getDomMounts() {
191 EventSourceRegistry getEventSourceRegistry() {
192 return eventSourceRegistry;
195 MountPointService getMountPointService() {
196 return mountPointService;
199 private boolean isNetconfNode(final Node node) {
200 return node.getAugmentation(NetconfNode.class) != null;
203 @Override public void close() {
204 listenerRegistration.close();
205 for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
208 registrationMap.clear();