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.controller.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;
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 final 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 final MountPointService mountPointService;
52 private ListenerRegistration<DataChangeListener> listenerRegistration;
53 private final EventSourceRegistry eventSourceRegistry;
55 public static NetconfEventSourceManager create(final DataBroker dataBroker,
56 final DOMNotificationPublishService domPublish, final DOMMountPointService domMount,
57 final MountPointService bindingMount, final EventSourceRegistry eventSourceRegistry,
58 final List<NamespaceToStream> namespaceMapping) {
60 final NetconfEventSourceManager eventSourceManager = new NetconfEventSourceManager(domPublish, domMount,
61 bindingMount, eventSourceRegistry, namespaceMapping);
63 eventSourceManager.initialize(dataBroker);
65 return eventSourceManager;
69 private NetconfEventSourceManager(final DOMNotificationPublishService domPublish,
70 final DOMMountPointService domMount, final MountPointService bindingMount,
71 final EventSourceRegistry eventSourceRegistry, final List<NamespaceToStream> namespaceMapping) {
73 Preconditions.checkNotNull(domPublish);
74 Preconditions.checkNotNull(domMount);
75 Preconditions.checkNotNull(bindingMount);
76 Preconditions.checkNotNull(eventSourceRegistry);
77 Preconditions.checkNotNull(namespaceMapping);
78 this.streamMap = namespaceToStreamMapping(namespaceMapping);
79 this.domMounts = domMount;
80 this.mountPointService = bindingMount;
81 this.publishService = domPublish;
82 this.eventSourceRegistry = eventSourceRegistry;
85 private void initialize(final DataBroker dataBroker) {
86 Preconditions.checkNotNull(dataBroker);
87 listenerRegistration = dataBroker
88 .registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH, this,
89 DataChangeScope.SUBTREE);
90 LOG.info("NetconfEventSourceManager initialized.");
93 private Map<String, String> namespaceToStreamMapping(final List<NamespaceToStream> namespaceMapping) {
94 final Map<String, String> streamMap = new HashMap<>(namespaceMapping.size());
96 for (final NamespaceToStream nToS : namespaceMapping) {
97 streamMap.put(nToS.getUrnPrefix(), nToS.getStreamName());
103 @Override public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
105 LOG.debug("[DataChangeEvent<InstanceIdentifier<?>, DataObject>: {}]", event);
106 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getCreatedData().entrySet()) {
107 if (changeEntry.getValue() instanceof Node) {
108 nodeCreated(changeEntry.getKey(), (Node) changeEntry.getValue());
112 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
113 if (changeEntry.getValue() instanceof Node) {
114 nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
118 for (InstanceIdentifier<?> removePath : event.getRemovedPaths()) {
119 DataObject removeObject = event.getOriginalData().get(removePath);
120 if (removeObject instanceof Node) {
121 nodeRemoved(removePath);
127 private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
128 Preconditions.checkNotNull(key);
129 if (validateNode(node) == false) {
130 LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key.toString());
133 LOG.info("Netconf event source [{}] is creating...", key.toString());
134 NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
136 NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
137 if (nesrOld != null) {
143 private void nodeUpdated(final InstanceIdentifier<?> key, final Node node) {
144 Preconditions.checkNotNull(key);
145 if (validateNode(node) == false) {
146 LOG.warn("NodeUpdated event : Node [{}] is null or not valid.", key.toString());
150 LOG.info("Netconf event source [{}] is updating...", key.toString());
151 NetconfEventSourceRegistration nesr = registrationMap.get(key);
155 nodeCreated(key, node);
159 private void nodeRemoved(final InstanceIdentifier<?> key) {
160 Preconditions.checkNotNull(key);
161 LOG.info("Netconf event source [{}] is removing...", key.toString());
162 NetconfEventSourceRegistration nesr = registrationMap.remove(key);
168 private boolean validateNode(final Node node) {
172 return isNetconfNode(node);
175 Map<String, String> getStreamMap() {
179 DOMNotificationPublishService getPublishService() {
180 return publishService;
183 DOMMountPointService getDomMounts() {
187 EventSourceRegistry getEventSourceRegistry() {
188 return eventSourceRegistry;
191 MountPointService getMountPointService() {
192 return mountPointService;
195 private boolean isNetconfNode(final Node node) {
196 return node.getAugmentation(NetconfNode.class) != null;
199 @Override public void close() {
200 listenerRegistration.close();
201 for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
204 registrationMap.clear();