Apply modernizations
[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.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
24 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
29 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
30 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
31 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
32 import org.opendaylight.yangtools.concepts.ListenerRegistration;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * NetconfEventSourceManager implements DataChangeListener. On topology changes, it manages creation,
39  * updating and removing registrations of event sources.
40  */
41 public final class NetconfEventSourceManager implements DataTreeChangeListener<Node>, AutoCloseable {
42
43     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSourceManager.class);
44     private static final TopologyKey NETCONF_TOPOLOGY_KEY = new TopologyKey(
45             new TopologyId(TopologyNetconf.QNAME.getLocalName()));
46     private static final InstanceIdentifier<Node> NETCONF_DEVICE_PATH = InstanceIdentifier.create(NetworkTopology.class)
47             .child(Topology.class, NETCONF_TOPOLOGY_KEY).child(Node.class);
48
49     private Map<String, String> streamMap;
50     private final ConcurrentHashMap<InstanceIdentifier<?>, NetconfEventSourceRegistration> registrationMap =
51             new ConcurrentHashMap<>();
52     private final DOMNotificationPublishService publishService;
53     private final DOMMountPointService domMounts;
54     private ListenerRegistration<NetconfEventSourceManager> listenerRegistration;
55     private final EventSourceRegistry eventSourceRegistry;
56     private final DataBroker dataBroker;
57
58     public NetconfEventSourceManager(final DataBroker dataBroker,
59                                      final DOMNotificationPublishService domPublish,
60                                      final DOMMountPointService domMount,
61                                      final EventSourceRegistry eventSourceRegistry) {
62         this.dataBroker = requireNonNull(dataBroker);
63         this.domMounts = requireNonNull(domMount);
64         this.publishService = requireNonNull(domPublish);
65         this.eventSourceRegistry = requireNonNull(eventSourceRegistry);
66     }
67
68     /**
69      * Invoked by blueprint.
70      */
71     public void initialize() {
72         listenerRegistration = verifyNotNull(dataBroker).registerDataTreeChangeListener(DataTreeIdentifier.create(
73                 LogicalDatastoreType.OPERATIONAL, NETCONF_DEVICE_PATH), this);
74         LOG.info("NetconfEventSourceManager initialized.");
75     }
76
77     @Override
78     public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
79         for (DataTreeModification<Node> change: changes) {
80             LOG.debug("DataTreeModification: {}", change);
81             final DataObjectModification<Node> rootNode = change.getRootNode();
82             final InstanceIdentifier<Node> identifier = change.getRootPath().getRootIdentifier();
83             switch (rootNode.getModificationType()) {
84                 case WRITE:
85                 case SUBTREE_MODIFIED:
86                     nodeCreated(identifier, rootNode.getDataAfter());
87                     break;
88                 case DELETE:
89                     nodeRemoved(identifier);
90                     break;
91                 default:
92                     break;
93             }
94         }
95     }
96
97     private void nodeCreated(final InstanceIdentifier<?> key, final Node node) {
98         if (!validateNode(node)) {
99             LOG.warn("NodeCreated event : Node [{}] is null or not valid.", key);
100             return;
101         }
102         LOG.info("Netconf event source [{}] is creating...", key);
103         NetconfEventSourceRegistration nesr = NetconfEventSourceRegistration.create(requireNonNull(key), node, this);
104         if (nesr != null) {
105             NetconfEventSourceRegistration nesrOld = registrationMap.put(key, nesr);
106             if (nesrOld != null) {
107                 nesrOld.close();
108             }
109         }
110     }
111
112     private void nodeRemoved(final InstanceIdentifier<?> key) {
113         LOG.info("Netconf event source [{}] is removing...", key);
114         NetconfEventSourceRegistration nesr = registrationMap.remove(requireNonNull(key));
115         if (nesr != null) {
116             nesr.close();
117         }
118     }
119
120     private static boolean validateNode(final Node node) {
121         return node == null ? false : isNetconfNode(node);
122     }
123
124     Map<String, String> getStreamMap() {
125         return streamMap;
126     }
127
128     DOMNotificationPublishService getPublishService() {
129         return publishService;
130     }
131
132     DOMMountPointService getDomMounts() {
133         return domMounts;
134     }
135
136     EventSourceRegistry getEventSourceRegistry() {
137         return eventSourceRegistry;
138     }
139
140     /**
141      * Invoked by blueprint.
142      *
143      * @param streamMap Stream map
144      */
145     public void setStreamMap(final Map<String, String> streamMap) {
146         this.streamMap = streamMap;
147     }
148
149     private static boolean isNetconfNode(final Node node) {
150         return node.augmentation(NetconfNode.class) != null;
151     }
152
153     @Override
154     public void close() {
155         listenerRegistration.close();
156         for (final NetconfEventSourceRegistration reg : registrationMap.values()) {
157             reg.close();
158         }
159         registrationMap.clear();
160     }
161 }