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