Add blueprint wiring for messagebus-netconf
[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.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
21 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
29 import org.opendaylight.yangtools.concepts.ListenerRegistration;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
37  * updating and removing registrations of event sources.
38  */
39 public final class NetconfEventSourceManager implements DataChangeListener, AutoCloseable {
40
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);
46
47     private 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 ListenerRegistration<DataChangeListener> listenerRegistration;
52     private final EventSourceRegistry eventSourceRegistry;
53     private final DataBroker dataBroker;
54
55     public NetconfEventSourceManager(final DataBroker dataBroker,
56                                      final DOMNotificationPublishService domPublish,
57                                      final DOMMountPointService domMount,
58                                      final EventSourceRegistry eventSourceRegistry) {
59         Preconditions.checkNotNull(dataBroker);
60         Preconditions.checkNotNull(domPublish);
61         Preconditions.checkNotNull(domMount);
62         Preconditions.checkNotNull(eventSourceRegistry);
63         this.dataBroker = dataBroker;
64         this.domMounts = domMount;
65         this.publishService = domPublish;
66         this.eventSourceRegistry = eventSourceRegistry;
67     }
68
69     /**
70      * Invoke by blueprint
71      */
72     public void initialize() {
73         Preconditions.checkNotNull(dataBroker);
74         listenerRegistration = dataBroker
75             .registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH, this,
76                 DataChangeScope.SUBTREE);
77         LOG.info("NetconfEventSourceManager initialized.");
78     }
79
80     @Override public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
81
82         LOG.debug("[DataChangeEvent<InstanceIdentifier<?>, DataObject>: {}]", event);
83         for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getCreatedData().entrySet()) {
84             if (changeEntry.getValue() instanceof Node) {
85                 nodeCreated(changeEntry.getKey(), (Node) changeEntry.getValue());
86             }
87         }
88
89         for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
90             if (changeEntry.getValue() instanceof Node) {
91                 nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
92             }
93         }
94
95         for (InstanceIdentifier<?> removePath : event.getRemovedPaths()) {
96             DataObject removeObject = event.getOriginalData().get(removePath);
97             if (removeObject instanceof Node) {
98                 nodeRemoved(removePath);
99             }
100         }
101
102     }
103
104     private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
105         Preconditions.checkNotNull(key);
106         if (!validateNode(node)) {
107             LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key.toString());
108             return;
109         }
110         LOG.info("Netconf event source [{}] is creating...", key.toString());
111         NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
112         if (nesr != null) {
113             NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
114             if (nesrOld != null) {
115                 nesrOld.close();
116             }
117         }
118     }
119
120     private void nodeUpdated(final InstanceIdentifier<?> key, final Node node) {
121         Preconditions.checkNotNull(key);
122         if (!validateNode(node)) {
123             LOG.warn("NodeUpdated event : Node [{}] is null or not valid.", key.toString());
124             return;
125         }
126
127         LOG.info("Netconf event source [{}] is updating...", key.toString());
128         NetconfEventSourceRegistration nesr = registrationMap.get(key);
129         if (nesr != null) {
130             nesr.updateStatus();
131         } else {
132             nodeCreated(key, node);
133         }
134     }
135
136     private void nodeRemoved(final InstanceIdentifier<?> key) {
137         Preconditions.checkNotNull(key);
138         LOG.info("Netconf event source [{}] is removing...", key.toString());
139         NetconfEventSourceRegistration nesr = registrationMap.remove(key);
140         if (nesr != null) {
141             nesr.close();
142         }
143     }
144
145     private boolean validateNode(final Node node) {
146         if (node == null) {
147             return false;
148         }
149         return isNetconfNode(node);
150     }
151
152     Map<String, String> getStreamMap() {
153         return streamMap;
154     }
155
156     DOMNotificationPublishService getPublishService() {
157         return publishService;
158     }
159
160     DOMMountPointService getDomMounts() {
161         return domMounts;
162     }
163
164     EventSourceRegistry getEventSourceRegistry() {
165         return eventSourceRegistry;
166     }
167
168     /**
169      * Invoke by blueprint
170      * @param streamMap
171      */
172     public void setStreamMap(Map<String, String> streamMap) {
173         this.streamMap = streamMap;
174     }
175
176     private boolean isNetconfNode(final Node node) {
177         return node.getAugmentation(NetconfNode.class) != null;
178     }
179
180     @Override public void close() {
181         listenerRegistration.close();
182         for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
183             reg.close();
184         }
185         registrationMap.clear();
186     }
187
188 }