Use FilterParameter for communicating its value
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / listeners / NotificationListenerAdapter.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
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;
31
32 /**
33  * {@link NotificationListenerAdapter} is responsible to track events on notifications.
34  */
35 public class NotificationListenerAdapter extends AbstractCommonSubscriber implements DOMNotificationListener {
36
37     private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerAdapter.class);
38     private static final NotificationFormatterFactory JSON_FORMATTER_FACTORY = JSONNotificationFormatter.createFactory(
39             JSONCodecFactorySupplier.RFC7951);
40
41     private final String streamName;
42     private final Absolute path;
43     private final NotificationOutputType outputType;
44
45     @VisibleForTesting NotificationFormatter formatter;
46
47
48     /**
49      * Set path of listener and stream name.
50      *
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).
54      */
55     NotificationListenerAdapter(final Absolute path, final String streamName, final String outputType) {
56         setLocalNameOfPath(path.lastNodeIdentifier().getLocalName());
57
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();
63
64         LOG.debug("output type: {}, {}", outputType, this.outputType);
65     }
66
67     private NotificationFormatterFactory getFormatterFactory() {
68         switch (outputType) {
69             case JSON:
70                 return JSON_FORMATTER_FACTORY;
71             case XML:
72                 return XMLNotificationFormatter.FACTORY;
73             default:
74                 throw new IllegalArgumentException("Unsupported outputType " + outputType);
75         }
76     }
77
78     @Override
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);
82     }
83
84     /**
85      * Get output type of this listener.
86      *
87      * @return The configured output type (JSON or XML).
88      */
89     @Override
90     public String getOutputType() {
91         return outputType.getName();
92     }
93
94     @Override
95     @SuppressWarnings("checkstyle:IllegalCatch")
96     public void onNotification(final DOMNotification notification) {
97         final Instant now = Instant.now();
98         if (!checkStartStop(now, this)) {
99             return;
100         }
101
102         final Optional<String> maybeOutput;
103         try {
104             maybeOutput = formatter.eventData(schemaHandler.get(), notification, now, getLeafNodesOnly(),
105                     isSkipNotificationData());
106         } catch (Exception e) {
107             LOG.error("Failed to process notification {}", notification, e);
108             return;
109         }
110         if (maybeOutput.isPresent()) {
111             post(maybeOutput.get());
112         }
113     }
114
115     /**
116      * Get stream name of this listener.
117      *
118      * @return The configured stream name.
119      */
120     @Override
121     public String getStreamName() {
122         return streamName;
123     }
124
125     /**
126      * Get schema path of notification.
127      *
128      * @return The configured schema path that points to observing YANG notification schema node.
129      */
130     public Absolute getSchemaPath() {
131         return path;
132     }
133
134     public final synchronized void listen(final DOMNotificationService notificationService) {
135         if (!isListening()) {
136             setRegistration(notificationService.registerNotificationListener(this, getSchemaPath()));
137         }
138     }
139
140     @Override
141     public String toString() {
142         return MoreObjects.toStringHelper(this)
143                 .add("path", path)
144                 .add("stream-name", streamName)
145                 .add("output-type", outputType)
146                 .toString();
147     }
148 }