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