Calculate replicated log data size on recovery
[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
9 package org.opendaylight.controller.messagebus.app.impl;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Map;
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;
26
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;
33
34     public EventSourceTopic(final NotificationPattern notificationPattern, final String nodeIdPattern, final EventSourceService eventSource) {
35         this.notificationPattern = Preconditions.checkNotNull(notificationPattern);
36         this.sourceService = eventSource;
37
38         // FIXME: regex should be the language of nodeIdPattern
39         final String regex = Util.wildcardToRegex(nodeIdPattern);
40         this.nodeIdPattern = Pattern.compile(regex);
41
42
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));
46     }
47
48     public TopicId getTopicId() {
49         return topicId;
50     }
51
52     @Override
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());
59                 }
60             }
61         }
62     }
63
64     public void notifyNode(final InstanceIdentifier<?> nodeId) {
65         try {
66             sourceService.joinTopic(getJoinTopicInputArgument(nodeId));
67         } catch (final Exception e) {
68             LOG.error("Could not invoke join topic for node {}", nodeId);
69         }
70     }
71
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())
77                         .setTopicId(topicId)
78                         .setNotificationPattern(notificationPattern)
79                         .build();
80         return jti;
81     }
82
83
84 }