Merge "Bug 2697: Improvement wrong response handling, missing message"
[controller.git] / opendaylight / md-sal / messagebus-impl / src / main / java / org / opendaylight / controller / messagebus / app / impl / EventAggregator.java
1 /*
2  * Copyright (c) 2013 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.List;
12 import java.util.concurrent.Future;
13 import org.opendaylight.controller.mdsal.MdSAL;
14 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicInput;
15 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicOutput;
16 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicOutputBuilder;
17 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInput;
18 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.EventAggregatorService;
19 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
20 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.Node1;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 // TODO: implement topic created notification
28 public class EventAggregator implements EventAggregatorService {
29     private static final Logger LOGGER = LoggerFactory.getLogger(EventAggregator.class);
30
31     private final MdSAL mdSAL;
32     private final EventSourceTopology eventSourceTopology;
33
34     public EventAggregator(final MdSAL mdSAL, final EventSourceTopology eventSourceTopology) {
35         this.mdSAL = mdSAL;
36         this.eventSourceTopology = eventSourceTopology;
37     }
38
39     public void mdsalReady() {
40         mdSAL.addRpcImplementation(EventAggregatorService.class, this);
41     }
42
43     @Override
44     public Future<RpcResult<CreateTopicOutput>> createTopic(final CreateTopicInput input) {
45         LOGGER.info("Received Topic creation request: NotificationPattern -> {}, NodeIdPattern -> {}",
46                 input.getNotificationPattern(),
47                 input.getNodeIdPattern());
48
49         Topic topic = new Topic(new NotificationPattern(input.getNotificationPattern()), input.getNodeIdPattern().getValue(), mdSAL);
50
51         //# Make sure we capture all nodes from now on
52         eventSourceTopology.registerDataChangeListener(topic);
53
54         //# Notify existing nodes
55         //# Code reader note: Context of Node type is NetworkTopology
56         List<Node> nodes = eventSourceTopology.snapshot();
57         for (Node node : nodes) {
58             NodeId nodeIdToNotify = node.getAugmentation(Node1.class).getEventSourceNode();
59             topic.notifyNode(nodeIdToNotify);
60         }
61
62         CreateTopicOutput cto = new CreateTopicOutputBuilder()
63                 .setTopicId(topic.getTopicId())
64                 .build();
65
66         return Util.resultFor(cto);
67     }
68
69     @Override
70     public Future<RpcResult<Void>> destroyTopic(final DestroyTopicInput input) {
71         // 1. UNREGISTER DATA CHANGE LISTENER -> ?
72         // 2. CLOSE TOPIC
73         return null;
74     }
75 }