2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3 * Copyright (c) 2021 PANTHEON.tech, s.r.o.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.mdsal.binding.dom.adapter;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.dom.api.DOMEvent;
18 import org.opendaylight.mdsal.dom.api.DOMNotification;
19 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
20 import org.opendaylight.yangtools.yang.binding.Notification;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
23 abstract class AbstractDOMNotificationListenerAdapter implements DOMNotificationListener {
24 private final AdapterContext adapterContext;
26 AbstractDOMNotificationListenerAdapter(final AdapterContext adapterContext) {
27 this.adapterContext = requireNonNull(adapterContext);
31 public final void onNotification(final DOMNotification notification) {
32 onNotification(notification.getType(), verifyNotNull(deserialize(notification)));
35 abstract void onNotification(@NonNull Absolute domType, @NonNull Notification<?> notification);
37 abstract Set<Absolute> getSupportedNotifications();
39 private Notification<?> deserialize(final DOMNotification notification) {
40 if (notification instanceof LazySerializedNotification) {
41 // TODO: This is a routed-back notification, for which we may end up losing event time here, but that is
42 // okay, for now at least.
43 return ((LazySerializedNotification) notification).getBindingData();
46 final var serializer = adapterContext.currentSerializer();
47 final var result = notification instanceof DOMEvent
48 ? serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody(),
49 ((DOMEvent) notification).getEventInstant())
50 : serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
51 verify(result instanceof Notification, "Unexpected codec result %s", result);
52 return (Notification<?>) result;