Decouple message bus from netconf connector
[controller.git] / opendaylight / netconf / messagebus-netconf / src / main / java / org / opendaylight / controller / 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.controller.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 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 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;
54
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) {
59
60         final NetconfEventSourceManager eventSourceManager = new NetconfEventSourceManager(domPublish, domMount,
61             bindingMount, eventSourceRegistry, namespaceMapping);
62
63         eventSourceManager.initialize(dataBroker);
64
65         return eventSourceManager;
66
67     }
68
69     private NetconfEventSourceManager(final DOMNotificationPublishService domPublish,
70         final DOMMountPointService domMount, final MountPointService bindingMount,
71         final EventSourceRegistry eventSourceRegistry, final List<NamespaceToStream> namespaceMapping) {
72
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;
83     }
84
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.");
91     }
92
93     private Map<String, String> namespaceToStreamMapping(final List<NamespaceToStream> namespaceMapping) {
94         final Map<String, String> streamMap = new HashMap<>(namespaceMapping.size());
95
96         for (final NamespaceToStream nToS : namespaceMapping) {
97             streamMap.put(nToS.getUrnPrefix(), nToS.getStreamName());
98         }
99
100         return streamMap;
101     }
102
103     @Override public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
104
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());
109             }
110         }
111
112         for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
113             if (changeEntry.getValue() instanceof Node) {
114                 nodeUpdated(changeEntry.getKey(), (Node) changeEntry.getValue());
115             }
116         }
117
118         for (InstanceIdentifier<?> removePath : event.getRemovedPaths()) {
119             DataObject removeObject = event.getOriginalData().get(removePath);
120             if (removeObject instanceof Node) {
121                 nodeRemoved(removePath);
122             }
123         }
124
125     }
126
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());
131             return;
132         }
133         LOG.info("Netconf event source [{}] is creating...", key.toString());
134         NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
135         if (nesr != null) {
136             NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
137             if (nesrOld != null) {
138                 nesrOld.close();
139             }
140         }
141     }
142
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());
147             return;
148         }
149
150         LOG.info("Netconf event source [{}] is updating...", key.toString());
151         NetconfEventSourceRegistration nesr = registrationMap.get(key);
152         if (nesr != null) {
153             nesr.updateStatus();
154         } else {
155             nodeCreated(key, node);
156         }
157     }
158
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);
163         if (nesr != null) {
164             nesr.close();
165         }
166     }
167
168     private boolean validateNode(final Node node) {
169         if (node == null) {
170             return false;
171         }
172         return isNetconfNode(node);
173     }
174
175     Map<String, String> getStreamMap() {
176         return streamMap;
177     }
178
179     DOMNotificationPublishService getPublishService() {
180         return publishService;
181     }
182
183     DOMMountPointService getDomMounts() {
184         return domMounts;
185     }
186
187     EventSourceRegistry getEventSourceRegistry() {
188         return eventSourceRegistry;
189     }
190
191     MountPointService getMountPointService() {
192         return mountPointService;
193     }
194
195     private boolean isNetconfNode(final Node node) {
196         return node.getAugmentation(NetconfNode.class) != null;
197     }
198
199     @Override public void close() {
200         listenerRegistration.close();
201         for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
202             reg.close();
203         }
204         registrationMap.clear();
205     }
206
207 }