Merge "Fix EXI fidelity parsing"
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / messagebus / eventsources / netconf / NetconfEventSourceManager.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.messagebus.eventsources.netconf;
10
11 import com.google.common.base.Preconditions;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
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;
38
39 /**
40  * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
41  * updating and removing registrations of event sources.
42  */
43 public final class NetconfEventSourceManager implements DataChangeListener, AutoCloseable {
44
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);
50
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;
58
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) {
63
64         final NetconfEventSourceManager eventSourceManager = new NetconfEventSourceManager(domPublish, domMount,
65             bindingMount, eventSourceRegistry, namespaceMapping);
66
67         eventSourceManager.initialize(dataBroker);
68
69         return eventSourceManager;
70
71     }
72
73     private NetconfEventSourceManager(final DOMNotificationPublishService domPublish,
74         final DOMMountPointService domMount, final MountPointService bindingMount,
75         final EventSourceRegistry eventSourceRegistry, final List<NamespaceToStream> namespaceMapping) {
76
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;
87     }
88
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.");
95     }
96
97     private Map<String, String> namespaceToStreamMapping(final List<NamespaceToStream> namespaceMapping) {
98         final Map<String, String> streamMap = new HashMap<>(namespaceMapping.size());
99
100         for (final NamespaceToStream nToS : namespaceMapping) {
101             streamMap.put(nToS.getUrnPrefix(), nToS.getStreamName());
102         }
103
104         return streamMap;
105     }
106
107     @Override public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
108
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());
113             }
114         }
115
116         for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
117             if (changeEntry.getValue() instanceof Node) {
118                 nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
119             }
120         }
121
122         for (InstanceIdentifier<?> removePath : event.getRemovedPaths()) {
123             DataObject removeObject = event.getOriginalData().get(removePath);
124             if (removeObject instanceof Node) {
125                 nodeRemoved(removePath);
126             }
127         }
128
129     }
130
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());
135             return;
136         }
137         LOG.info("Netconf event source [{}] is creating...", key.toString());
138         NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
139         if (nesr != null) {
140             NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
141             if (nesrOld != null) {
142                 nesrOld.close();
143             }
144         }
145     }
146
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());
151             return;
152         }
153
154         LOG.info("Netconf event source [{}] is updating...", key.toString());
155         NetconfEventSourceRegistration nesr = registrationMap.get(key);
156         if (nesr != null) {
157             nesr.updateStatus();
158         } else {
159             nodeCreated(key, node);
160         }
161     }
162
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);
167         if (nesr != null) {
168             nesr.close();
169         }
170     }
171
172     private boolean validateNode(final Node node) {
173         if (node == null) {
174             return false;
175         }
176         return isNetconfNode(node);
177     }
178
179     Map<String, String> getStreamMap() {
180         return streamMap;
181     }
182
183     DOMNotificationPublishService getPublishService() {
184         return publishService;
185     }
186
187     DOMMountPointService getDomMounts() {
188         return domMounts;
189     }
190
191     EventSourceRegistry getEventSourceRegistry() {
192         return eventSourceRegistry;
193     }
194
195     MountPointService getMountPointService() {
196         return mountPointService;
197     }
198
199     private boolean isNetconfNode(final Node node) {
200         return node.getAugmentation(NetconfNode.class) != null;
201     }
202
203     @Override public void close() {
204         listenerRegistration.close();
205         for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
206             reg.close();
207         }
208         registrationMap.clear();
209     }
210
211 }