2 * Copyright (c) 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.Optional;
17 import javax.xml.xpath.XPathExpressionException;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.opendaylight.mdsal.dom.api.DOMNotification;
20 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
21 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
22 import org.opendaylight.restconf.common.formatters.JSONNotificationFormatter;
23 import org.opendaylight.restconf.common.formatters.NotificationFormatter;
24 import org.opendaylight.restconf.common.formatters.NotificationFormatterFactory;
25 import org.opendaylight.restconf.common.formatters.XMLNotificationFormatter;
26 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
27 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
28 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * {@link NotificationListenerAdapter} is responsible to track events on notifications.
35 public class NotificationListenerAdapter extends AbstractCommonSubscriber implements DOMNotificationListener {
37 private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerAdapter.class);
38 private static final NotificationFormatterFactory JSON_FORMATTER_FACTORY = JSONNotificationFormatter.createFactory(
39 JSONCodecFactorySupplier.RFC7951);
41 private final String streamName;
42 private final Absolute path;
43 private final NotificationOutputType outputType;
45 @VisibleForTesting NotificationFormatter formatter;
49 * Set path of listener and stream name.
51 * @param path Schema path of YANG notification.
52 * @param streamName Name of the stream.
53 * @param outputType Type of output on notification (JSON or XML).
55 NotificationListenerAdapter(final Absolute path, final String streamName, final String outputType) {
56 setLocalNameOfPath(path.lastNodeIdentifier().getLocalName());
58 this.outputType = NotificationOutputType.forName(requireNonNull(outputType)).get();
59 this.path = requireNonNull(path);
60 this.streamName = requireNonNull(streamName);
61 checkArgument(!streamName.isEmpty());
62 formatter = getFormatterFactory().getFormatter();
64 LOG.debug("output type: {}, {}", outputType, this.outputType);
67 private NotificationFormatterFactory getFormatterFactory() {
70 return JSON_FORMATTER_FACTORY;
72 return XMLNotificationFormatter.FACTORY;
74 throw new IllegalArgumentException("Unsupported outputType " + outputType);
79 final void setFilter(final @Nullable String filter) throws XPathExpressionException {
80 final NotificationFormatterFactory factory = getFormatterFactory();
81 formatter = filter == null || filter.isEmpty() ? factory.getFormatter() : factory.getFormatter(filter);
85 * Get output type of this listener.
87 * @return The configured output type (JSON or XML).
90 public String getOutputType() {
91 return outputType.getName();
95 @SuppressWarnings("checkstyle:IllegalCatch")
96 public void onNotification(final DOMNotification notification) {
97 final Instant now = Instant.now();
98 if (!checkStartStop(now, this)) {
102 final Optional<String> maybeOutput;
104 maybeOutput = formatter.eventData(schemaHandler.get(), notification, now, getLeafNodesOnly(),
105 isSkipNotificationData());
106 } catch (Exception e) {
107 LOG.error("Failed to process notification {}", notification, e);
110 if (maybeOutput.isPresent()) {
111 post(maybeOutput.get());
116 * Get stream name of this listener.
118 * @return The configured stream name.
121 public String getStreamName() {
126 * Get schema path of notification.
128 * @return The configured schema path that points to observing YANG notification schema node.
130 public Absolute getSchemaPath() {
134 public final synchronized void listen(final DOMNotificationService notificationService) {
135 if (!isListening()) {
136 setRegistration(notificationService.registerNotificationListener(this, getSchemaPath()));
141 public String toString() {
142 return MoreObjects.toStringHelper(this)
144 .add("stream-name", streamName)
145 .add("output-type", outputType)