2 * Copyright (c) 2015 Cisco Systems, 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.mdsal.binding.dom.adapter;
10 import static java.util.Objects.requireNonNull;
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;
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);
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;
33 AbstractInstanceNotificationListenerAdapter(final AdapterContext adapterContext, final Class<N> nofiticationClass,
34 final L delegate, final Executor executor) {
35 this.adapterContext = requireNonNull(adapterContext);
36 notificationClass = requireNonNull(nofiticationClass);
37 this.delegate = requireNonNull(delegate);
38 this.executor = requireNonNull(executor);
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());
49 final N castNotification;
51 castNotification = notificationClass.cast(bindingNotification);
52 } catch (ClassCastException e) {
53 LOG.warn("Mismatched notification type {}, not notifying listener", notification.getType(), e);
57 final var bindingPath = serializer.fromYangInstanceIdentifier(path.path());
58 executor.execute(() -> onNotification(delegate, bindingPath, castNotification));
61 abstract void onNotification(@NonNull L delegate, @NonNull InstanceIdentifier<?> path, @NonNull N notification);