2 * Copyright (c) 2014, 2016 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
8 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.MoreObjects;
15 import java.time.Instant;
16 import java.util.Collection;
17 import java.util.Optional;
18 import java.util.stream.Collectors;
19 import javax.xml.xpath.XPathExpressionException;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.dom.api.ClusteredDOMDataTreeChangeListener;
22 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
23 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
24 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
25 import org.opendaylight.restconf.common.formatters.DataTreeCandidateFormatter;
26 import org.opendaylight.restconf.common.formatters.DataTreeCandidateFormatterFactory;
27 import org.opendaylight.restconf.common.formatters.JSONDataTreeCandidateFormatter;
28 import org.opendaylight.restconf.common.formatters.XMLDataTreeCandidateFormatter;
29 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
32 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * {@link ListenerAdapter} is responsible to track events, which occurred by changing data in data source.
39 public class ListenerAdapter extends AbstractCommonSubscriber implements ClusteredDOMDataTreeChangeListener {
40 private static final Logger LOG = LoggerFactory.getLogger(ListenerAdapter.class);
41 private static final String PATH = "path";
42 private static final DataTreeCandidateFormatterFactory JSON_FORMATTER_FACTORY =
43 JSONDataTreeCandidateFormatter.createFactory(JSONCodecFactorySupplier.RFC7951);
45 private final YangInstanceIdentifier path;
46 private final String streamName;
47 private final NotificationOutputType outputType;
49 @VisibleForTesting DataTreeCandidateFormatter formatter;
52 * Creates new {@link ListenerAdapter} listener specified by path and stream name and register for subscribing.
54 * @param path Path to data in data store.
55 * @param streamName The name of the stream.
56 * @param outputType Type of output on notification (JSON, XML).
58 ListenerAdapter(final YangInstanceIdentifier path, final String streamName,
59 final NotificationOutputType outputType) {
60 setLocalNameOfPath(path.getLastPathArgument().getNodeType().getLocalName());
62 this.outputType = requireNonNull(outputType);
63 this.path = requireNonNull(path);
64 this.streamName = requireNonNull(streamName);
65 checkArgument(!streamName.isEmpty());
67 formatter = getFormatterFactory().getFormatter();
70 private DataTreeCandidateFormatterFactory getFormatterFactory() {
73 return JSON_FORMATTER_FACTORY;
75 return XMLDataTreeCandidateFormatter.FACTORY;
77 throw new IllegalArgumentException("Unsupported outputType" + outputType);
81 private DataTreeCandidateFormatter getFormatter(final String filter) throws XPathExpressionException {
82 final DataTreeCandidateFormatterFactory factory = getFormatterFactory();
83 return filter == null || filter.isEmpty() ? factory.getFormatter() : factory.getFormatter(filter);
87 public void setQueryParams(final Instant startTime, final Instant stopTime, final String filter,
88 final boolean leafNodesOnly, final boolean skipNotificationData) {
89 setQueryParams(startTime, stopTime, leafNodesOnly, skipNotificationData);
91 formatter = getFormatter(filter);
92 } catch (final XPathExpressionException e) {
93 throw new IllegalArgumentException("Failed to get filter", e);
98 public void onInitialData() {
103 @SuppressWarnings("checkstyle:IllegalCatch")
104 public void onDataTreeChanged(final Collection<DataTreeCandidate> dataTreeCandidates) {
105 final Instant now = Instant.now();
106 if (!checkStartStop(now, this)) {
110 final Optional<String> maybeData;
112 maybeData = formatter.eventData(schemaHandler.get(), dataTreeCandidates, now, getLeafNodesOnly(),
113 isSkipNotificationData());
114 } catch (final Exception e) {
115 LOG.error("Failed to process notification {}",
116 dataTreeCandidates.stream().map(Object::toString).collect(Collectors.joining(",")), e);
120 if (maybeData.isPresent()) {
121 post(maybeData.get());
126 * Gets the name of the stream.
128 * @return The name of the stream.
131 public String getStreamName() {
136 public String getOutputType() {
137 return outputType.getName();
141 * Get path pointed to data in data store.
143 * @return Path pointed to data in data store.
145 public YangInstanceIdentifier getPath() {
150 * Register data change listener in DOM data broker and set it to listener on stream.
152 * @param domDataBroker data broker for register data change listener
153 * @param datastore {@link LogicalDatastoreType}
155 public final synchronized void listen(final DOMDataBroker domDataBroker, final LogicalDatastoreType datastore) {
156 if (!isListening()) {
157 final DOMDataTreeChangeService changeService = domDataBroker.getExtensions()
158 .getInstance(DOMDataTreeChangeService.class);
159 if (changeService == null) {
160 throw new UnsupportedOperationException("DOMDataBroker does not support the DOMDataTreeChangeService");
163 setRegistration(changeService.registerDataTreeChangeListener(
164 new DOMDataTreeIdentifier(datastore, getPath()), this));
169 public String toString() {
170 return MoreObjects.toStringHelper(this)
172 .add("stream-name", streamName)
173 .add("output-type", outputType)