Add InstanceNotification(Publish)ServiceAdapter
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / AbstractInstanceNotificationListenerAdapter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.concurrent.Executor;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
15 import org.opendaylight.mdsal.dom.api.DOMEvent;
16 import org.opendaylight.mdsal.dom.api.DOMInstanceNotificationListener;
17 import org.opendaylight.mdsal.dom.api.DOMNotification;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.binding.InstanceNotification;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 abstract class AbstractInstanceNotificationListenerAdapter<P extends DataObject, N extends InstanceNotification<N, P>,
25         L> implements DOMInstanceNotificationListener {
26     private static final Logger LOG = LoggerFactory.getLogger(AbstractInstanceNotificationListenerAdapter.class);
27
28     private final @NonNull AdapterContext adapterContext;
29     private final @NonNull Class<N> notificationClass;
30     private final @NonNull Executor executor;
31     private final @NonNull L delegate;
32
33     AbstractInstanceNotificationListenerAdapter(final AdapterContext adapterContext, final Class<N> nofiticationClass,
34             final L delegate, final Executor executor) {
35         this.adapterContext = requireNonNull(adapterContext);
36         this.notificationClass = requireNonNull(nofiticationClass);
37         this.delegate = requireNonNull(delegate);
38         this.executor = requireNonNull(executor);
39     }
40
41     @Override
42     public final void onNotification(final DOMDataTreeIdentifier path, final DOMNotification notification) {
43         final var serializer = adapterContext.currentSerializer();
44         final var bindingNotification = notification instanceof DOMEvent
45             ? serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody(),
46                 ((DOMEvent) notification).getEventInstant())
47                 : serializer.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
48
49         final N castNotification;
50         try {
51             castNotification = notificationClass.cast(bindingNotification);
52         } catch (ClassCastException e) {
53             LOG.warn("Mismatched notification type {}, not notifying listener", notification.getType(), e);
54             return;
55         }
56
57         final var bindingPath = serializer.fromYangInstanceIdentifier(path.getRootIdentifier());
58         executor.execute(() -> onNotification(delegate, bindingPath, castNotification));
59     }
60
61     abstract void onNotification(@NonNull L delegate, @NonNull InstanceIdentifier<?> path, @NonNull N notification);
62 }