2 * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.streams.listeners;
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.common.QName;
18 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Abstract base class for functionality shared between {@link NotificationListenerAdapter} and
25 * {@link DeviceNotificationListenerAdaptor}.
27 abstract class AbstractNotificationListenerAdaptor extends AbstractCommonSubscriber<DOMNotification>
28 implements DOMNotificationListener {
29 private static final Logger LOG = LoggerFactory.getLogger(AbstractNotificationListenerAdaptor.class);
30 private static final NotificationFormatterFactory JSON_FORMATTER_FACTORY =
31 JSONNotificationFormatter.createFactory(JSONCodecFactorySupplier.RFC7951);
33 AbstractNotificationListenerAdaptor(final QName lastQName, final String streamName,
34 final NotificationOutputType outputType) {
35 super(lastQName, streamName, outputType, getFormatterFactory(outputType));
38 private static NotificationFormatterFactory getFormatterFactory(final NotificationOutputType outputType) {
39 return switch (outputType) {
40 case JSON -> JSON_FORMATTER_FACTORY;
41 case XML -> XMLNotificationFormatter.FACTORY;
46 @SuppressWarnings("checkstyle:IllegalCatch")
47 public final void onNotification(final DOMNotification notification) {
48 final var eventInstant = notification instanceof DOMEvent domEvent ? domEvent.getEventInstant() : Instant.now();
49 if (!checkStartStop(eventInstant)) {
53 final Optional<String> maybeOutput;
55 maybeOutput = formatter().eventData(effectiveModel(), notification, eventInstant, getLeafNodesOnly(),
56 isSkipNotificationData(), getChangedLeafNodesOnly());
57 } catch (Exception e) {
58 LOG.error("Failed to process notification {}", notification, e);
61 maybeOutput.ifPresent(this::post);
64 abstract @NonNull EffectiveModelContext effectiveModel();