Bump versions to 2.0.0-SNAPSHOT
[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 package org.opendaylight.netconf.messagebus.eventsources.netconf;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Collection;
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.DataObjectModification;
19 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
20 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
21 import org.opendaylight.mdsal.binding.api.DataTreeModification;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
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.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
40  * updating and removing registrations of event sources.
41  */
42 @Deprecated(forRemoval = true)
43 public final class NetconfEventSourceManager implements DataTreeChangeListener<Node>, 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 Map<String, String> streamMap;
52     private final ConcurrentHashMap<InstanceIdentifier<?>, NetconfEventSourceRegistration> registrationMap =
53             new ConcurrentHashMap<>();
54     private final DOMNotificationPublishService publishService;
55     private final DOMMountPointService domMounts;
56     private ListenerRegistration<NetconfEventSourceManager> listenerRegistration;
57     private final EventSourceRegistry eventSourceRegistry;
58     private final DataBroker dataBroker;
59     private final BindingNormalizedNodeSerializer serializer;
60
61     public NetconfEventSourceManager(final DataBroker dataBroker,
62                                      final BindingNormalizedNodeSerializer serializer,
63                                      final DOMNotificationPublishService domPublish,
64                                      final DOMMountPointService domMount,
65                                      final EventSourceRegistry eventSourceRegistry) {
66         this.dataBroker = requireNonNull(dataBroker);
67         this.serializer = requireNonNull(serializer);
68         this.domMounts = requireNonNull(domMount);
69         this.publishService = requireNonNull(domPublish);
70         this.eventSourceRegistry = requireNonNull(eventSourceRegistry);
71     }
72
73     /**
74      * Invoked by blueprint.
75      */
76     public void initialize() {
77         listenerRegistration = verifyNotNull(dataBroker).registerDataTreeChangeListener(DataTreeIdentifier.create(
78                 LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH), this);
79         LOG.info("NetconfEventSourceManager initialized.");
80     }
81
82     @Override
83     public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
84         for (DataTreeModification<Node> change: changes) {
85             LOG.debug("DataTreeModification: {}", change);
86             final DataObjectModification<Node> rootNode = change.getRootNode();
87             final InstanceIdentifier<Node> identifier = change.getRootPath().getRootIdentifier();
88             switch (rootNode.getModificationType()) {
89                 case WRITE:
90                 case SUBTREE_MODIFIED:
91                     nodeCreated(identifier, rootNode.getDataAfter());
92                     break;
93                 case DELETE:
94                     nodeRemoved(identifier);
95                     break;
96                 default:
97                     break;
98             }
99         }
100     }
101
102     private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
103         if (!validateNode(node)) {
104             LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key);
105             return;
106         }
107         LOG.info("Netconf event source [{}] is creating...", key);
108         NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(serializer, requireNonNull(key),
109             node, this);
110         if (nesr != null) {
111             NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
112             if (nesrOld != null) {
113                 nesrOld.close();
114             }
115         }
116     }
117
118     private void nodeRemoved(final InstanceIdentifier<?> key) {
119         LOG.info("Netconf event source [{}] is removing...", key);
120         NetconfEventSourceRegistration nesr = registrationMap.remove(requireNonNull(key));
121         if (nesr != null) {
122             nesr.close();
123         }
124     }
125
126     private static boolean validateNode(final Node node) {
127         return node == null ? false : isNetconfNode(node);
128     }
129
130     Map<String, String> getStreamMap() {
131         return streamMap;
132     }
133
134     DOMNotificationPublishService getPublishService() {
135         return publishService;
136     }
137
138     DOMMountPointService getDomMounts() {
139         return domMounts;
140     }
141
142     EventSourceRegistry getEventSourceRegistry() {
143         return eventSourceRegistry;
144     }
145
146     /**
147      * Invoked by blueprint.
148      *
149      * @param streamMap Stream map
150      */
151     public void setStreamMap(final Map<String, String> streamMap) {
152         this.streamMap = streamMap;
153     }
154
155     private static boolean isNetconfNode(final Node node) {
156         return node.augmentation(NetconfNode.class) != null;
157     }
158
159     @Override
160     public void close() {
161         listenerRegistration.close();
162         for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
163             reg.close();
164         }
165         registrationMap.clear();
166     }
167 }