BUG 3121 - destroy topic implementation
[controller.git] / opendaylight / md-sal / messagebus-impl / 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 java.util.HashMap;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.concurrent.ConcurrentHashMap;
15
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;
38
39 import com.google.common.base.Preconditions;
40
41 public final class NetconfEventSourceManager implements DataChangeListener, AutoCloseable {
42
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)
47                 .child(Node.class);
48
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;
56
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){
63
64         final NetconfEventSourceManager eventSourceManager =
65                 new NetconfEventSourceManager(domPublish, domMount,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,
75                               final MountPointService bindingMount,
76                               final EventSourceRegistry eventSourceRegistry,
77                               final List<NamespaceToStream> namespaceMapping) {
78
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;
89     }
90
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.");
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
108     public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
109
110         LOG.debug("[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());
114             }
115         }
116
117         for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
118             if (changeEntry.getValue() instanceof Node) {
119                 nodeUpdated(changeEntry.getKey(),(Node) changeEntry.getValue());
120             }
121         }
122
123         for(InstanceIdentifier<?> removePath : event.getRemovedPaths()){
124             DataObject removeObject = event.getOriginalData().get(removePath);
125             if(removeObject instanceof Node){
126                 nodeRemoved(removePath);
127             }
128         }
129
130     }
131
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());
136             return;
137         }
138         LOG.info("Netconf event source [{}] is creating...", key.toString());
139         NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(key, node, this);
140         if(nesr != null){
141             NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
142             if(nesrOld != null){
143                 nesrOld.close();
144             }
145         }
146     }
147
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());
152             return;
153         }
154
155         LOG.info("Netconf event source [{}] is updating...", key.toString());
156         NetconfEventSourceRegistration nesr = registrationMap.get(key);
157         if(nesr != null){
158             nesr.updateStatus();
159         } else {
160             nodeCreated(key, node);
161         }
162     }
163
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);
168         if(nesr != null){
169             nesr.close();
170         }
171     }
172
173     private boolean validateNode(final Node node){
174         if(node == null){
175             return false;
176         }
177         return isNetconfNode(node);
178     }
179
180     Map<String, String> getStreamMap() {
181         return streamMap;
182     }
183
184     DOMNotificationPublishService getPublishService() {
185         return publishService;
186     }
187
188     DOMMountPointService getDomMounts() {
189         return domMounts;
190     }
191
192     EventSourceRegistry getEventSourceRegistry() {
193         return eventSourceRegistry;
194     }
195
196     MountPointService getMountPointService() {
197         return mountPointService;
198     }
199
200     private boolean isNetconfNode(final Node node)  {
201         return node.getAugmentation(NetconfNode.class) != null ;
202     }
203
204     @Override
205     public void close() {
206         listenerRegistration.close();
207         for(final NetconfEventSourceRegistration reg : registrationMap.values()){
208             reg.close();
209         }
210         registrationMap.clear();
211     }
212
213 }