2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.messagebus.app.impl;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.CheckedFuture;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import java.util.List;
18 import java.util.UUID;
19 import java.util.concurrent.CopyOnWriteArraySet;
20 import java.util.concurrent.ExecutionException;
21 import java.util.regex.Pattern;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
24 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
28 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
29 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
30 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicInput;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.DisJoinTopicInputBuilder;
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.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.RpcError;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.slf4j.LoggerFactory;
46 public class EventSourceTopic implements DataChangeListener, AutoCloseable {
47 private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(EventSourceTopic.class);
48 private final NotificationPattern notificationPattern;
49 private final EventSourceService sourceService;
50 private final Pattern nodeIdPattern;
51 private final TopicId topicId;
52 private ListenerRegistration<DataChangeListener> listenerRegistration;
53 private final CopyOnWriteArraySet<InstanceIdentifier<?>> joinedEventSources = new CopyOnWriteArraySet<>();
55 public static EventSourceTopic create(final NotificationPattern notificationPattern, final String nodeIdRegexPattern, final EventSourceTopology eventSourceTopology){
56 final EventSourceTopic est = new EventSourceTopic(notificationPattern, nodeIdRegexPattern, eventSourceTopology.getEventSourceService());
57 est.registerListner(eventSourceTopology);
58 est.notifyExistingNodes(eventSourceTopology);
62 private EventSourceTopic(final NotificationPattern notificationPattern, final String nodeIdRegexPattern, final EventSourceService sourceService) {
63 this.notificationPattern = Preconditions.checkNotNull(notificationPattern);
64 this.sourceService = Preconditions.checkNotNull(sourceService);
65 this.nodeIdPattern = Pattern.compile(nodeIdRegexPattern);
66 this.topicId = new TopicId(getUUIDIdent());
67 this.listenerRegistration = null;
68 LOG.info("EventSourceTopic created - topicId {}", topicId.getValue());
71 public TopicId getTopicId() {
76 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
78 for (final Map.Entry<InstanceIdentifier<?>, DataObject> createdEntry : event.getCreatedData().entrySet()) {
79 if (createdEntry.getValue() instanceof Node) {
80 final Node node = (Node) createdEntry.getValue();
81 LOG.debug("Create node...");
82 if (getNodeIdRegexPattern().matcher(node.getNodeId().getValue()).matches()) {
83 LOG.debug("Matched...");
84 notifyNode(EventSourceTopology.EVENT_SOURCE_TOPOLOGY_PATH.child(Node.class, node.getKey()));
89 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
90 if (changeEntry.getValue() instanceof Node) {
91 final Node node = (Node) changeEntry.getValue();
92 if (getNodeIdRegexPattern().matcher(node.getNodeId().getValue()).matches()) {
93 notifyNode(changeEntry.getKey());
99 public void notifyNode(final InstanceIdentifier<?> nodeId) {
100 LOG.debug("Notify node: {}", nodeId);
102 final RpcResult<JoinTopicOutput> rpcResultJoinTopic = sourceService.joinTopic(getJoinTopicInputArgument(nodeId)).get();
103 if(rpcResultJoinTopic.isSuccessful() == false){
104 for(final RpcError err : rpcResultJoinTopic.getErrors()){
105 LOG.error("Can not join topic: [{}] on node: [{}]. Error: {}",getTopicId().getValue(),nodeId.toString(),err.toString());
108 joinedEventSources.add(nodeId);
110 } catch (final Exception e) {
111 LOG.error("Could not invoke join topic for node {}", nodeId);
115 private void notifyExistingNodes(final EventSourceTopology eventSourceTopology){
116 LOG.debug("Notify existing nodes");
117 final Pattern nodeRegex = this.nodeIdPattern;
119 final ReadOnlyTransaction tx = eventSourceTopology.getDataBroker().newReadOnlyTransaction();
120 final CheckedFuture<Optional<Topology>, ReadFailedException> future =
121 tx.read(LogicalDatastoreType.OPERATIONAL, EventSourceTopology.EVENT_SOURCE_TOPOLOGY_PATH);
123 Futures.addCallback(future, new FutureCallback<Optional<Topology>>(){
126 public void onSuccess(final Optional<Topology> data) {
127 if(data.isPresent()) {
128 final List<Node> nodes = data.get().getNode();
130 for (final Node node : nodes) {
131 if (nodeRegex.matcher(node.getNodeId().getValue()).matches()) {
132 notifyNode(EventSourceTopology.EVENT_SOURCE_TOPOLOGY_PATH.child(Node.class, node.getKey()));
141 public void onFailure(final Throwable t) {
142 LOG.error("Can not notify existing nodes", t);
150 private JoinTopicInput getJoinTopicInputArgument(final InstanceIdentifier<?> path) {
151 final NodeRef nodeRef = new NodeRef(path);
152 final JoinTopicInput jti =
153 new JoinTopicInputBuilder()
154 .setNode(nodeRef.getValue())
156 .setNotificationPattern(notificationPattern)
161 public Pattern getNodeIdRegexPattern() {
162 return nodeIdPattern;
165 private DisJoinTopicInput getDisJoinTopicInputArgument(final InstanceIdentifier<?> eventSourceNodeId){
166 final NodeRef nodeRef = new NodeRef(eventSourceNodeId);
167 final DisJoinTopicInput dji = new DisJoinTopicInputBuilder()
168 .setNode(nodeRef.getValue())
174 private void registerListner(final EventSourceTopology eventSourceTopology) {
175 this.listenerRegistration =
176 eventSourceTopology.getDataBroker().registerDataChangeListener(
177 LogicalDatastoreType.OPERATIONAL,
178 EventSourceTopology.EVENT_SOURCE_TOPOLOGY_PATH,
180 DataBroker.DataChangeScope.SUBTREE);
184 public void close() {
185 if(this.listenerRegistration != null){
186 this.listenerRegistration.close();
188 for(final InstanceIdentifier<?> eventSourceNodeId : joinedEventSources){
190 final RpcResult<Void> result = sourceService.disJoinTopic(getDisJoinTopicInputArgument(eventSourceNodeId)).get();
191 if(result.isSuccessful() == false){
192 for(final RpcError err : result.getErrors()){
193 LOG.error("Can not destroy topic: [{}] on node: [{}]. Error: {}",getTopicId().getValue(),eventSourceNodeId,err.toString());
196 } catch (InterruptedException | ExecutionException ex) {
197 LOG.error("Can not close event source topic / destroy topic {} on node {}.", this.topicId.getValue(), eventSourceNodeId, ex);
200 joinedEventSources.clear();
203 private static String getUUIDIdent(){
204 final UUID uuid = UUID.randomUUID();
205 return uuid.toString();