Integrate AbstractCommonSubscriber more tightly
[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 java.time.Instant;
11 import java.util.Optional;
12 import org.opendaylight.mdsal.dom.api.DOMNotification;
13 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
14 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
15 import org.opendaylight.restconf.common.formatters.JSONNotificationFormatter;
16 import org.opendaylight.restconf.common.formatters.NotificationFormatterFactory;
17 import org.opendaylight.restconf.common.formatters.XMLNotificationFormatter;
18 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
19 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * {@link NotificationListenerAdapter} is responsible to track events on notifications.
26  */
27 public class NotificationListenerAdapter extends AbstractCommonSubscriber<Absolute, DOMNotification>
28         implements DOMNotificationListener {
29
30     private static final Logger LOG = LoggerFactory.getLogger(NotificationListenerAdapter.class);
31     private static final NotificationFormatterFactory JSON_FORMATTER_FACTORY = JSONNotificationFormatter.createFactory(
32             JSONCodecFactorySupplier.RFC7951);
33
34     /**
35      * Set path of listener and stream name.
36      *
37      * @param path       Schema path of YANG notification.
38      * @param streamName Name of the stream.
39      * @param outputType Type of output on notification (JSON or XML).
40      */
41     NotificationListenerAdapter(final Absolute path, final String streamName, final NotificationOutputType outputType) {
42         super(path.lastNodeIdentifier(), streamName, path, outputType, getFormatterFactory(outputType));
43     }
44
45     private static NotificationFormatterFactory getFormatterFactory(final NotificationOutputType outputType) {
46         switch (outputType) {
47             case JSON:
48                 return JSON_FORMATTER_FACTORY;
49             case XML:
50                 return XMLNotificationFormatter.FACTORY;
51             default:
52                 throw new IllegalArgumentException("Unsupported outputType " + outputType);
53         }
54     }
55
56     @Override
57     @SuppressWarnings("checkstyle:IllegalCatch")
58     public void onNotification(final DOMNotification notification) {
59         final Instant now = Instant.now();
60         if (!checkStartStop(now)) {
61             return;
62         }
63
64         final Optional<String> maybeOutput;
65         try {
66             maybeOutput = formatter().eventData(schemaHandler.get(), notification, now, getLeafNodesOnly(),
67                     isSkipNotificationData());
68         } catch (Exception e) {
69             LOG.error("Failed to process notification {}", notification, e);
70             return;
71         }
72         if (maybeOutput.isPresent()) {
73             post(maybeOutput.get());
74         }
75     }
76
77     /**
78      * Get schema path of notification.
79      *
80      * @return The configured schema path that points to observing YANG notification schema node.
81      */
82     public Absolute getSchemaPath() {
83         return path();
84     }
85
86     public final synchronized void listen(final DOMNotificationService notificationService) {
87         if (!isListening()) {
88             setRegistration(notificationService.registerNotificationListener(this, getSchemaPath()));
89         }
90     }
91 }