8cb0ce2bafc600e7817f9f3d084a3ce54d54d60e
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / AbstractNotificationListenerAdaptor.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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;
9
10 import java.time.Instant;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.mdsal.dom.api.DOMEvent;
13 import org.opendaylight.mdsal.dom.api.DOMNotification;
14 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
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.EffectiveModelContext;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Abstract base class for functionality shared between {@link NotificationListenerAdapter} and
23  * {@link DeviceNotificationListenerAdaptor}.
24  */
25 abstract class AbstractNotificationListenerAdaptor extends AbstractCommonSubscriber<DOMNotification>
26         implements DOMNotificationListener {
27     private static final Logger LOG = LoggerFactory.getLogger(AbstractNotificationListenerAdaptor.class);
28     private static final NotificationFormatterFactory JSON_FORMATTER_FACTORY =
29         JSONNotificationFormatter.createFactory(JSONCodecFactorySupplier.RFC7951);
30
31     AbstractNotificationListenerAdaptor(final String streamName, final NotificationOutputType outputType,
32             final ListenersBroker listenersBroker) {
33         super(streamName, outputType, getFormatterFactory(outputType), listenersBroker);
34     }
35
36     private static NotificationFormatterFactory getFormatterFactory(final NotificationOutputType outputType) {
37         return switch (outputType) {
38             case JSON -> JSON_FORMATTER_FACTORY;
39             case XML -> XMLNotificationFormatter.FACTORY;
40         };
41     }
42
43     @Override
44     @SuppressWarnings("checkstyle:IllegalCatch")
45     public final void onNotification(final DOMNotification notification) {
46         final var eventInstant = notification instanceof DOMEvent domEvent ? domEvent.getEventInstant() : Instant.now();
47         final String data;
48         try {
49             data = formatter().eventData(effectiveModel(), notification, eventInstant);
50         } catch (Exception e) {
51             LOG.error("Failed to process notification {}", notification, e);
52             return;
53         }
54         if (data != null) {
55             post(data);
56         }
57     }
58
59     abstract @NonNull EffectiveModelContext effectiveModel();
60 }