4740420f3fa96ff58e21ba5e4f71105f8835fa50
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / AbstractDOMNotificationListenerAdapter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  * Copyright (c) 2021 PANTHEON.tech, s.r.o.
4  *
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
8  */
9 package org.opendaylight.mdsal.binding.dom.adapter;
10
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
13
14 import java.util.Set;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.dom.api.DOMEvent;
17 import org.opendaylight.mdsal.dom.api.DOMNotification;
18 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
19 import org.opendaylight.yangtools.yang.binding.Notification;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
21
22 abstract class AbstractDOMNotificationListenerAdapter implements DOMNotificationListener {
23     private final AdapterContext adapterContext;
24
25     AbstractDOMNotificationListenerAdapter(final AdapterContext adapterContext) {
26         this.adapterContext = requireNonNull(adapterContext);
27     }
28
29     @Override
30     public final void onNotification(final DOMNotification notification) {
31         onNotification(notification.getType(), verifyNotNull(deserialize(notification)));
32     }
33
34     abstract void onNotification(@NonNull Absolute domType, @NonNull Notification<?> notification);
35
36     abstract Set<Absolute> getSupportedNotifications();
37
38     private Notification<?> deserialize(final DOMNotification notification) {
39         if (notification instanceof LazySerializedDOMNotification) {
40             // TODO: This is a routed-back notification, for which we may end up losing event time here, but that is
41             //       okay, for now at least.
42             return ((LazySerializedDOMNotification) notification).getBindingData();
43         }
44
45         final CurrentAdapterSerializer serializer = adapterContext.currentSerializer();
46         return notification instanceof DOMEvent
47             ? serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody(),
48                 ((DOMEvent) notification).getEventInstant())
49                 : serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
50     }
51 }