Fix checkstyle violations in messagebus
[controller.git] / opendaylight / md-sal / messagebus-impl / src / main / java / org / opendaylight / controller / messagebus / app / impl / EventSourceTopology.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.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.concurrent.Future;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.messagebus.app.util.Util;
21 import org.opendaylight.controller.messagebus.spi.EventSource;
22 import org.opendaylight.controller.messagebus.spi.EventSourceRegistration;
23 import org.opendaylight.controller.messagebus.spi.EventSourceRegistry;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RoutedRpcRegistration;
25 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
26 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
27 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicInput;
28 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicOutput;
29 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.CreateTopicOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.DestroyTopicInput;
31 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.EventAggregatorService;
32 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.NotificationPattern;
33 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventaggregator.rev141202.TopicId;
34 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.EventSourceService;
35 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.Node1;
36 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.Node1Builder;
37 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.TopologyTypes1;
38 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.TopologyTypes1Builder;
39 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.topology.event.source.type.TopologyEventSource;
40 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.messagebus.eventsource.rev141202.topology.event.source.type.TopologyEventSourceBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeContext;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.TopologyTypes;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 public class EventSourceTopology implements EventAggregatorService, EventSourceRegistry {
58     private static final Logger LOG = LoggerFactory.getLogger(EventSourceTopology.class);
59
60     private static final String TOPOLOGY_ID = "EVENT-SOURCE-TOPOLOGY" ;
61     private static final TopologyKey EVENT_SOURCE_TOPOLOGY_KEY = new TopologyKey(new TopologyId(TOPOLOGY_ID));
62     private static final LogicalDatastoreType OPERATIONAL = LogicalDatastoreType.OPERATIONAL;
63
64     static final InstanceIdentifier<Topology> EVENT_SOURCE_TOPOLOGY_PATH =
65             InstanceIdentifier.create(NetworkTopology.class)
66                     .child(Topology.class, EVENT_SOURCE_TOPOLOGY_KEY);
67
68     private static final InstanceIdentifier<TopologyTypes1> TOPOLOGY_TYPE_PATH =
69             EVENT_SOURCE_TOPOLOGY_PATH
70                     .child(TopologyTypes.class)
71                     .augmentation(TopologyTypes1.class);
72
73     private final Map<TopicId,EventSourceTopic> eventSourceTopicMap = new ConcurrentHashMap<>();
74     private final Map<NodeKey, RoutedRpcRegistration<EventSourceService>> routedRpcRegistrations =
75             new ConcurrentHashMap<>();
76
77     private final DataBroker dataBroker;
78     private final RpcRegistration<EventAggregatorService> aggregatorRpcReg;
79     private final EventSourceService eventSourceService;
80     private final RpcProviderRegistry rpcRegistry;
81
82     public EventSourceTopology(final DataBroker dataBroker, final RpcProviderRegistry rpcRegistry) {
83
84         this.dataBroker = dataBroker;
85         this.rpcRegistry = rpcRegistry;
86         aggregatorRpcReg = rpcRegistry.addRpcImplementation(EventAggregatorService.class, this);
87         eventSourceService = rpcRegistry.getRpcService(EventSourceService.class);
88
89         final TopologyEventSource topologySource = new TopologyEventSourceBuilder().build();
90         final TopologyTypes1 topologyTypeAugment =
91                 new TopologyTypes1Builder().setTopologyEventSource(topologySource).build();
92         putData(OPERATIONAL, TOPOLOGY_TYPE_PATH, topologyTypeAugment);
93         LOG.info("EventSourceRegistry has been initialized");
94     }
95
96     private <T extends DataObject>  void putData(final LogicalDatastoreType store,
97                                                  final InstanceIdentifier<T> path,
98                                                  final T data) {
99
100         final WriteTransaction tx = getDataBroker().newWriteOnlyTransaction();
101         tx.put(store, path, data, true);
102         Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
103             @Override
104             public void onSuccess(final Void result) {
105                 LOG.trace("Data has put into datastore {} {}", store, path);
106             }
107
108             @Override
109             public void onFailure(final Throwable ex) {
110                 LOG.error("Can not put data into datastore [store: {}] [path: {}] [exception: {}]",store,path, ex);
111             }
112         }, MoreExecutors.directExecutor());
113     }
114
115     private <T extends DataObject>  void deleteData(final LogicalDatastoreType store,
116             final InstanceIdentifier<T> path) {
117         final WriteTransaction tx = getDataBroker().newWriteOnlyTransaction();
118         tx.delete(OPERATIONAL, path);
119         Futures.addCallback(tx.submit(), new FutureCallback<Void>() {
120             @Override
121             public void onSuccess(final Void result) {
122                 LOG.trace("Data has deleted from datastore {} {}", store, path);
123             }
124
125             @Override
126             public void onFailure(final Throwable ex) {
127                 LOG.error("Can not delete data from datastore [store: {}] [path: {}] [exception: {}]",store,path, ex);
128             }
129         }, MoreExecutors.directExecutor());
130     }
131
132     private void insert(final KeyedInstanceIdentifier<Node, NodeKey> sourcePath) {
133         final NodeKey nodeKey = sourcePath.getKey();
134         final InstanceIdentifier<Node1> augmentPath = sourcePath.augmentation(Node1.class);
135         final Node1 nodeAgument = new Node1Builder().setEventSourceNode(
136                 new NodeId(nodeKey.getNodeId().getValue())).build();
137         putData(OPERATIONAL, augmentPath, nodeAgument);
138     }
139
140     private void remove(final KeyedInstanceIdentifier<Node, NodeKey> sourcePath) {
141         final InstanceIdentifier<Node1> augmentPath = sourcePath.augmentation(Node1.class);
142         deleteData(OPERATIONAL, augmentPath);
143     }
144
145     @Override
146     public Future<RpcResult<CreateTopicOutput>> createTopic(final CreateTopicInput input) {
147         LOG.debug("Received Topic creation request: NotificationPattern -> {}, NodeIdPattern -> {}",
148                 input.getNotificationPattern(),
149                 input.getNodeIdPattern());
150
151         final NotificationPattern notificationPattern = new NotificationPattern(input.getNotificationPattern());
152         //FIXME: do not use Util.wildcardToRegex - NodeIdPatter should be regex
153         final String nodeIdPattern = input.getNodeIdPattern().getValue();
154         final EventSourceTopic eventSourceTopic = EventSourceTopic.create(notificationPattern, nodeIdPattern, this);
155
156         eventSourceTopicMap.put(eventSourceTopic.getTopicId(), eventSourceTopic);
157
158         final CreateTopicOutput cto = new CreateTopicOutputBuilder()
159                 .setTopicId(eventSourceTopic.getTopicId())
160                 .build();
161
162         LOG.info("Topic has been created: NotificationPattern -> {}, NodeIdPattern -> {}",
163                 input.getNotificationPattern(),
164                 input.getNodeIdPattern());
165
166         return Util.resultRpcSuccessFor(cto);
167     }
168
169     @Override
170     public Future<RpcResult<Void>> destroyTopic(final DestroyTopicInput input) {
171         final EventSourceTopic topicToDestroy = eventSourceTopicMap.remove(input.getTopicId());
172         if (topicToDestroy != null) {
173             topicToDestroy.close();
174         }
175         return Util.resultRpcSuccessFor((Void) null);
176     }
177
178     @Override
179     public void close() {
180         aggregatorRpcReg.close();
181         eventSourceTopicMap.values().forEach(EventSourceTopic::close);
182     }
183
184     public void register(final EventSource eventSource) {
185
186         final NodeKey nodeKey = eventSource.getSourceNodeKey();
187         final KeyedInstanceIdentifier<Node, NodeKey> sourcePath = EVENT_SOURCE_TOPOLOGY_PATH.child(Node.class, nodeKey);
188         final RoutedRpcRegistration<EventSourceService> reg = rpcRegistry.addRoutedRpcImplementation(
189                 EventSourceService.class, eventSource);
190         reg.registerPath(NodeContext.class, sourcePath);
191         routedRpcRegistrations.put(nodeKey,reg);
192         insert(sourcePath);
193
194     }
195
196     public void unRegister(final EventSource eventSource) {
197         final NodeKey nodeKey = eventSource.getSourceNodeKey();
198         final KeyedInstanceIdentifier<Node, NodeKey> sourcePath = EVENT_SOURCE_TOPOLOGY_PATH.child(Node.class, nodeKey);
199         final RoutedRpcRegistration<EventSourceService> removeRegistration = routedRpcRegistrations.remove(nodeKey);
200         if (removeRegistration != null) {
201             removeRegistration.close();
202             remove(sourcePath);
203         }
204     }
205
206     @Override
207     public <T extends EventSource> EventSourceRegistration<T> registerEventSource(final T eventSource) {
208         final EventSourceRegistrationImpl<T> esr = new EventSourceRegistrationImpl<>(eventSource, this);
209         register(eventSource);
210         return esr;
211     }
212
213     DataBroker getDataBroker() {
214         return dataBroker;
215     }
216
217     EventSourceService getEventSourceService() {
218         return eventSourceService;
219     }
220 }
221