2 * Copyright (c) 2022 Opendaylight, Inc. 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;
10 import static java.util.Objects.requireNonNull;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
15 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
16 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
17 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
26 * {@link DeviceNotificationListenerAdaptor} is responsible to track events on notifications.
28 public final class DeviceNotificationListenerAdaptor extends AbstractNotificationListenerAdaptor
29 implements DOMMountPointListener {
30 private static final Logger LOG = LoggerFactory.getLogger(DeviceNotificationListenerAdaptor.class);
32 private final @NonNull EffectiveModelContext effectiveModel;
33 private final @NonNull DOMMountPointService mountPointService;
34 private final @NonNull YangInstanceIdentifier instanceIdentifier;
36 private ListenerRegistration<DOMMountPointListener> reg;
38 DeviceNotificationListenerAdaptor(final String streamName, final NotificationOutputType outputType,
39 final ListenersBroker listenersBroker, final EffectiveModelContext effectiveModel,
40 final DOMMountPointService mountPointService, final YangInstanceIdentifier instanceIdentifier) {
41 super(streamName, outputType, listenersBroker);
42 this.effectiveModel = requireNonNull(effectiveModel);
43 this.mountPointService = requireNonNull(mountPointService);
44 this.instanceIdentifier = requireNonNull(instanceIdentifier);
47 public synchronized void listen(final DOMNotificationService notificationService, final Set<Absolute> paths) {
49 setRegistration(notificationService.registerNotificationListener(this, paths));
50 reg = mountPointService.registerProvisionListener(this);
54 private synchronized void resetListenerRegistration() {
62 EffectiveModelContext effectiveModel() {
63 return effectiveModel;
67 public void onMountPointCreated(final YangInstanceIdentifier path) {
72 public void onMountPointRemoved(final YangInstanceIdentifier path) {
73 if (instanceIdentifier.equals(path)) {
74 getSubscribers().forEach(subscriber -> {
75 if (subscriber.isConnected()) {
76 subscriber.sendDataMessage("Device disconnected");
78 if (subscriber instanceof SSESessionHandler sseSessionHandler) {
80 sseSessionHandler.close();
81 } catch (IllegalStateException e) {
82 LOG.warn("Ignoring exception while closing sse session");
86 listenersBroker.removeAndCloseDeviceNotificationListener(this);
87 resetListenerRegistration();