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.Preconditions;
13 import java.util.regex.Pattern;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
16 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
17 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
18 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService;
19 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInput;
20 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.LoggerFactory;
27 public class EventSourceTopic implements DataChangeListener {
28 private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(EventSourceTopic.class);
29 private final NotificationPattern notificationPattern;
30 private final EventSourceService sourceService;
31 private final Pattern nodeIdPattern;
32 private final TopicId topicId;
34 public EventSourceTopic(final NotificationPattern notificationPattern, final String nodeIdPattern, final EventSourceService eventSource) {
35 this.notificationPattern = Preconditions.checkNotNull(notificationPattern);
36 this.sourceService = eventSource;
38 // FIXME: regex should be the language of nodeIdPattern
39 final String regex = Util.wildcardToRegex(nodeIdPattern);
40 this.nodeIdPattern = Pattern.compile(regex);
43 // FIXME: We need to perform some salting in order to make
44 // the topic IDs less predictable.
45 this.topicId = new TopicId(Util.md5String(notificationPattern + nodeIdPattern));
48 public TopicId getTopicId() {
53 public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
54 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
55 if (changeEntry.getValue() instanceof Node) {
56 final Node node = (Node) changeEntry.getValue();
57 if (nodeIdPattern.matcher(node.getId().getValue()).matches()) {
58 notifyNode(changeEntry.getKey());
64 public void notifyNode(final InstanceIdentifier<?> nodeId) {
66 sourceService.joinTopic(getJoinTopicInputArgument(nodeId));
67 } catch (final Exception e) {
68 LOG.error("Could not invoke join topic for node {}", nodeId);
72 private JoinTopicInput getJoinTopicInputArgument(final InstanceIdentifier<?> path) {
73 final NodeRef nodeRef = new NodeRef(path);
74 final JoinTopicInput jti =
75 new JoinTopicInputBuilder()
76 .setNode(nodeRef.getValue())
78 .setNotificationPattern(notificationPattern)