Remove DOMDataTreeProducer-related classes
[controller.git] / opendaylight / md-sal / messagebus-impl / src / main / java / org / opendaylight / controller / messagebus / app / impl / EventSourceTopic.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.controller.messagebus.app.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.util.Collection;
16 import java.util.Optional;
17 import java.util.UUID;
18 import java.util.concurrent.CopyOnWriteArraySet;
19 import java.util.concurrent.ExecutionException;
20 import java.util.regex.Pattern;
21 import org.opendaylight.mdsal.binding.api.DataObjectModification;
22 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
23 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
24 import org.opendaylight.mdsal.binding.api.DataTreeModification;
25 import org.opendaylight.mdsal.binding.api.ReadTransaction;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
28 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
29 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicInput;
30 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicOutput;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInput;
34 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.slf4j.LoggerFactory;
44
45 @Deprecated(forRemoval = true)
46 public final class EventSourceTopic implements DataTreeChangeListener<Node>, AutoCloseable {
47     private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(EventSourceTopic.class);
48
49     private final CopyOnWriteArraySet<InstanceIdentifier<?>> joinedEventSources = new CopyOnWriteArraySet<>();
50     private final NotificationPattern notificationPattern;
51     private final EventSourceService sourceService;
52     private final Pattern nodeIdPattern;
53     private final TopicId topicId;
54     private ListenerRegistration<?> listenerRegistration;
55
56     public static EventSourceTopic create(final NotificationPattern notificationPattern,
57             final String nodeIdRegexPattern, final EventSourceTopology eventSourceTopology) {
58         final EventSourceTopic est = new EventSourceTopic(notificationPattern, nodeIdRegexPattern,
59                 eventSourceTopology.getEventSourceService());
60         est.registerListner(eventSourceTopology);
61         est.notifyExistingNodes(eventSourceTopology);
62         return est;
63     }
64
65     private EventSourceTopic(final NotificationPattern notificationPattern, final String nodeIdRegexPattern,
66             final EventSourceService sourceService) {
67         this.notificationPattern = requireNonNull(notificationPattern);
68         this.sourceService = requireNonNull(sourceService);
69         this.nodeIdPattern = Pattern.compile(nodeIdRegexPattern);
70         this.topicId = new TopicId(getUUIDIdent());
71         this.listenerRegistration = null;
72         LOG.info("EventSourceTopic created - topicId {}", topicId.getValue());
73     }
74
75     public TopicId getTopicId() {
76         return topicId;
77     }
78
79     @Override
80     public void onDataTreeChanged(final Collection<DataTreeModification<Node>> changes) {
81         for (DataTreeModification<Node> change: changes) {
82             final DataObjectModification<Node> rootNode = change.getRootNode();
83             switch (rootNode.getModificationType()) {
84                 case WRITE:
85                 case SUBTREE_MODIFIED:
86                     final Node node = rootNode.getDataAfter();
87                     if (getNodeIdRegexPattern().matcher(node.getNodeId().getValue()).matches()) {
88                         notifyNode(change.getRootPath().getRootIdentifier());
89                     }
90                     break;
91                 default:
92                     break;
93             }
94         }
95     }
96
97     public void notifyNode(final InstanceIdentifier<?> nodeId) {
98         LOG.debug("Notify node: {}", nodeId);
99         try {
100             final RpcResult<JoinTopicOutput> rpcResultJoinTopic =
101                     sourceService.joinTopic(getJoinTopicInputArgument(nodeId)).get();
102             if (!rpcResultJoinTopic.isSuccessful()) {
103                 for (final RpcError err : rpcResultJoinTopic.getErrors()) {
104                     LOG.error("Can not join topic: [{}] on node: [{}]. Error: {}", getTopicId().getValue(),
105                             nodeId.toString(), err.toString());
106                 }
107             } else {
108                 joinedEventSources.add(nodeId);
109             }
110         } catch (InterruptedException | ExecutionException e) {
111             LOG.error("Could not invoke join topic for node {}", nodeId);
112         }
113     }
114
115     private void notifyExistingNodes(final EventSourceTopology eventSourceTopology) {
116         LOG.debug("Notify existing nodes");
117         final Pattern nodeRegex = this.nodeIdPattern;
118
119         final FluentFuture<Optional<Topology>> future;
120         try (ReadTransaction tx = eventSourceTopology.getDataBroker().newReadOnlyTransaction()) {
121             future = tx.read(LogicalDatastoreType.OPERATIONAL, EventSourceTopology.EVENT_SOURCE_TOPOLOGY_PATH);
122         }
123
124         future.addCallback(new FutureCallback<Optional<Topology>>() {
125             @Override
126             public void onSuccess(final Optional<Topology> data) {
127                 if (data.isPresent()) {
128                     for (final Node node : data.get().nonnullNode().values()) {
129                         if (nodeRegex.matcher(node.getNodeId().getValue()).matches()) {
130                             notifyNode(EventSourceTopology.EVENT_SOURCE_TOPOLOGY_PATH.child(Node.class, node.key()));
131                         }
132                     }
133                 }
134             }
135
136             @Override
137             public void onFailure(final Throwable ex) {
138                 LOG.error("Can not notify existing nodes", ex);
139             }
140         }, MoreExecutors.directExecutor());
141     }
142
143     private JoinTopicInput getJoinTopicInputArgument(final InstanceIdentifier<?> path) {
144         final NodeRef nodeRef = new NodeRef(path);
145         final JoinTopicInput jti =
146                 new JoinTopicInputBuilder()
147                         .setNode(nodeRef.getValue())
148                         .setTopicId(topicId)
149                         .setNotificationPattern(notificationPattern)
150                         .build();
151         return jti;
152     }
153
154     public Pattern getNodeIdRegexPattern() {
155         return nodeIdPattern;
156     }
157
158     private DisJoinTopicInput getDisJoinTopicInputArgument(final InstanceIdentifier<?> eventSourceNodeId) {
159         final NodeRef nodeRef = new NodeRef(eventSourceNodeId);
160         final DisJoinTopicInput dji = new DisJoinTopicInputBuilder()
161                 .setNode(nodeRef.getValue())
162                 .setTopicId(topicId)
163                 .build();
164         return dji;
165     }
166
167     private void registerListner(final EventSourceTopology eventSourceTopology) {
168         this.listenerRegistration = eventSourceTopology.getDataBroker().registerDataTreeChangeListener(
169             DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL,
170                 EventSourceTopology.EVENT_SOURCE_TOPOLOGY_PATH.child(Node.class)), this);
171     }
172
173     @Override
174     public void close() {
175         if (this.listenerRegistration != null) {
176             this.listenerRegistration.close();
177         }
178         for (final InstanceIdentifier<?> eventSourceNodeId : joinedEventSources) {
179             try {
180                 final RpcResult<DisJoinTopicOutput> result = sourceService
181                         .disJoinTopic(getDisJoinTopicInputArgument(eventSourceNodeId)).get();
182                 if (result.isSuccessful() == false) {
183                     for (final RpcError err : result.getErrors()) {
184                         LOG.error("Can not destroy topic: [{}] on node: [{}]. Error: {}", getTopicId().getValue(),
185                                 eventSourceNodeId, err.toString());
186                     }
187                 }
188             } catch (InterruptedException | ExecutionException ex) {
189                 LOG.error("Can not close event source topic / destroy topic {} on node {}.", this.topicId.getValue(),
190                         eventSourceNodeId, ex);
191             }
192         }
193         joinedEventSources.clear();
194     }
195
196     private static String getUUIDIdent() {
197         final UUID uuid = UUID.randomUUID();
198         return uuid.toString();
199     }
200 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.