2 * Copyright (c) 2021 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.mdsal.binding.dom.adapter;
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
14 import java.util.concurrent.Executor;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener.Component;
17 import org.opendaylight.mdsal.binding.api.NotificationService.Listener;
18 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
19 import org.opendaylight.mdsal.dom.api.DOMEvent;
20 import org.opendaylight.mdsal.dom.api.DOMNotification;
21 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23 import org.opendaylight.yangtools.yang.binding.Notification;
24 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
26 final class BindingDOMNotificationListenerAdapter<N extends Notification<N> & DataObject>
27 implements DOMNotificationListener {
28 private final AdapterContext adapterContext;
29 private final Listener<N> delegate;
30 private final Executor executor;
31 private final Class<N> type;
33 BindingDOMNotificationListenerAdapter(final AdapterContext adapterContext, final Class<N> type,
34 final Listener<N> delegate, final Executor executor) {
35 this.adapterContext = requireNonNull(adapterContext);
36 this.type = requireNonNull(type);
37 this.delegate = requireNonNull(delegate);
38 this.executor = requireNonNull(executor);
41 BindingDOMNotificationListenerAdapter(final AdapterContext adapterContext, final Component<N> component,
42 final Executor executor) {
43 this(adapterContext, component.type(), component.listener(), executor);
46 @NonNull Absolute schemaPath() {
47 return Absolute.of(BindingReflections.findQName(type));
51 public void onNotification(final DOMNotification notification) {
52 final var binding = type.cast(verifyNotNull(deserialize(notification)));
53 executor.execute(() -> delegate.onNotification(binding));
56 private Notification<?> deserialize(final DOMNotification notification) {
57 if (notification instanceof LazySerializedNotification lazy) {
58 // TODO: This is a routed-back notification, for which we may end up losing event time here, but that is
59 // okay, for now at least.
60 return lazy.getBindingData();
63 final var serializer = adapterContext.currentSerializer();
64 final var result = notification instanceof DOMEvent domEvent
65 ? serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody(),
66 domEvent.getEventInstant())
67 : serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
68 verify(result instanceof Notification, "Unexpected codec result %s", result);
69 return (Notification<?>) result;