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 java.util.HashMap;
12 import java.util.List;
14 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.config.yang.messagebus.app.impl.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 import com.google.common.base.Preconditions;
41 public final class NetconfEventSourceManager implements DataChangeListener, AutoCloseable {
43 private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSourceManager.class);
44 private static final TopologyKey NETCONF_TOPOLOGY_KEY = new TopologyKey(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)
49 private final Map<String, String> streamMap;
50 private final ConcurrentHashMap<InstanceIdentifier<?>, NetconfEventSourceRegistration> registrationMap = new ConcurrentHashMap<>();
51 private final DOMNotificationPublishService publishService;
52 private final DOMMountPointService domMounts;
53 private final MountPointService mountPointService;
54 private ListenerRegistration<DataChangeListener> listenerRegistration;
55 private final EventSourceRegistry eventSourceRegistry;
57 public static NetconfEventSourceManager create(final DataBroker dataBroker,
58 final DOMNotificationPublishService domPublish,
59 final DOMMountPointService domMount,
60 final MountPointService bindingMount,
61 final EventSourceRegistry eventSourceRegistry,
62 final List<NamespaceToStream> namespaceMapping){
64 final NetconfEventSourceManager eventSourceManager =
65 new NetconfEventSourceManager(domPublish, domMount,bindingMount, eventSourceRegistry, namespaceMapping);
67 eventSourceManager.initialize(dataBroker);
69 return eventSourceManager;
73 private NetconfEventSourceManager(final DOMNotificationPublishService domPublish,
74 final DOMMountPointService domMount,
75 final MountPointService bindingMount,
76 final EventSourceRegistry eventSourceRegistry,
77 final List<NamespaceToStream> namespaceMapping) {
79 Preconditions.checkNotNull(domPublish);
80 Preconditions.checkNotNull(domMount);
81 Preconditions.checkNotNull(bindingMount);
82 Preconditions.checkNotNull(eventSourceRegistry);
83 Preconditions.checkNotNull(namespaceMapping);
84 this.streamMap = namespaceToStreamMapping(namespaceMapping);
85 this.domMounts = domMount;
86 this.mountPointService = bindingMount;
87 this.publishService = domPublish;
88 this.eventSourceRegistry = eventSourceRegistry;
91 private void initialize(final DataBroker dataBroker){
92 Preconditions.checkNotNull(dataBroker);
93 listenerRegistration = dataBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH, this, 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());
108 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
110 LOG.info("[DataChangeEvent<InstanceIdentifier<?>, DataObject>: {}]", event);
111 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getCreatedData().entrySet()) {
112 if (changeEntry.getValue() instanceof Node) {
113 nodeCreated(changeEntry.getKey(),(Node) changeEntry.getValue());
117 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
118 if (changeEntry.getValue() instanceof Node) {
119 nodeUpdated(changeEntry.getKey(),(Node) changeEntry.getValue());
123 for(InstanceIdentifier<?> removePath : event.getRemovedPaths()){
124 DataObject removeObject = event.getOriginalData().get(removePath);
125 if(removeObject instanceof Node){
126 nodeRemoved(removePath);
132 private void nodeCreated(final InstanceIdentifier<?> key, final Node node){
133 Preconditions.checkNotNull(key);
134 if(validateNode(node) == false){
135 LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key.toString());
138 LOG.info("Netconf event source [{}] is creating...", key.toString());
139 NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
141 NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
148 private void nodeUpdated(final InstanceIdentifier<?> key, final Node node){
149 Preconditions.checkNotNull(key);
150 if(validateNode(node) == false){
151 LOG.warn("NodeUpdated event : Node [{}] is null or not valid.", key.toString());
155 LOG.info("Netconf event source [{}] is updating...", key.toString());
156 NetconfEventSourceRegistration nesr = registrationMap.get(key);
160 nodeCreated(key, node);
164 private void nodeRemoved(final InstanceIdentifier<?> key){
165 Preconditions.checkNotNull(key);
166 LOG.info("Netconf event source [{}] is removing...", key.toString());
167 NetconfEventSourceRegistration nesr = registrationMap.remove(key);
173 private boolean validateNode(final Node node){
177 return isNetconfNode(node);
180 Map<String, String> getStreamMap() {
184 DOMNotificationPublishService getPublishService() {
185 return publishService;
188 DOMMountPointService getDomMounts() {
192 EventSourceRegistry getEventSourceRegistry() {
193 return eventSourceRegistry;
196 MountPointService getMountPointService() {
197 return mountPointService;
200 private boolean isNetconfNode(final Node node) {
201 return node.getAugmentation(NetconfNode.class) != null ;
205 public void close() {
206 listenerRegistration.close();
207 for(final NetconfEventSourceRegistration reg : registrationMap.values()){
210 registrationMap.clear();