Merge "Add missing copyright text"
[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 java.util.Map;
12 import java.util.regex.Pattern;
13
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.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.JoinTopicOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.common.RpcError;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.common.base.Preconditions;
31
32 public class EventSourceTopic implements DataChangeListener {
33     private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(EventSourceTopic.class);
34     private final NotificationPattern notificationPattern;
35     private final EventSourceService sourceService;
36     private final Pattern nodeIdPattern;
37     private final TopicId topicId;
38
39     public EventSourceTopic(final NotificationPattern notificationPattern, final String nodeIdPattern, final EventSourceService eventSource) {
40         this.notificationPattern = Preconditions.checkNotNull(notificationPattern);
41         this.sourceService = eventSource;
42         this.nodeIdPattern = Pattern.compile(nodeIdPattern);
43
44         this.topicId = new TopicId(Util.getUUIDIdent());
45     }
46
47     public TopicId getTopicId() {
48         return topicId;
49     }
50
51     @Override
52     public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> event) {
53                 for (final Map.Entry<InstanceIdentifier<?>, DataObject> changeEntry : event.getUpdatedData().entrySet()) {
54             if (changeEntry.getValue() instanceof Node) {
55                 final Node node = (Node) changeEntry.getValue();
56                 if (nodeIdPattern.matcher(node.getId().getValue()).matches()) {
57                     notifyNode(changeEntry.getKey());
58                 }
59             }
60         }
61     }
62
63     public void notifyNode(final InstanceIdentifier<?> nodeId) {
64
65         try {
66             RpcResult<JoinTopicOutput> rpcResultJoinTopic = sourceService.joinTopic(getJoinTopicInputArgument(nodeId)).get();
67             if(rpcResultJoinTopic.isSuccessful() == false){
68                 for(RpcError err : rpcResultJoinTopic.getErrors()){
69                     LOG.error("Can not join topic: [{}] on node: [{}]. Error: {}",getTopicId().getValue(),nodeId.toString(),err.toString());
70                 }
71             }
72         } catch (final Exception e) {
73             LOG.error("Could not invoke join topic for node {}", nodeId);
74         }
75     }
76
77     private JoinTopicInput getJoinTopicInputArgument(final InstanceIdentifier<?> path) {
78         final NodeRef nodeRef = new NodeRef(path);
79         final JoinTopicInput jti =
80                 new JoinTopicInputBuilder()
81                         .setNode(nodeRef.getValue())
82                         .setTopicId(topicId)
83                         .setNotificationPattern(notificationPattern)
84                         .build();
85         return jti;
86     }
87
88 }