Add InstanceNotification(Publish)ServiceAdapter
[mdsal.git] / binding / mdsal-binding-api / src / main / java / org / opendaylight / mdsal / binding / api / InstanceNotificationService.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. 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.api;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.util.concurrent.MoreExecutors;
12 import java.util.EventListener;
13 import java.util.concurrent.Executor;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.concepts.Registration;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.Identifiable;
18 import org.opendaylight.yangtools.yang.binding.Identifier;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20 import org.opendaylight.yangtools.yang.binding.InstanceNotification;
21 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.binding.KeyedListNotification;
23
24 /**
25  * A {@link BindingService} which allows clients to subscribe to (YANG 1.1) {@link InstanceNotification}s and
26  * {@link KeyedListNotification}s.
27  */
28 @Beta
29 public interface InstanceNotificationService extends BindingService {
30
31     <P extends DataObject, N extends InstanceNotification<N, P>> @NonNull Registration registerListener(
32         InstanceNotificationSpec<N, P> spec, InstanceIdentifier<P> path, Listener<P, N> listener, Executor executor);
33
34     default <P extends DataObject, N extends InstanceNotification<N, P>> @NonNull Registration registerListener(
35             final InstanceNotificationSpec<N, P> spec, final InstanceIdentifier<P> path,
36             final Listener<P, N> listener) {
37         return registerListener(spec, path, listener, MoreExecutors.directExecutor());
38     }
39
40     <P extends DataObject & Identifiable<K>, N extends KeyedListNotification<N, P, K>, K extends Identifier<P>>
41         @NonNull Registration registerListener(InstanceNotificationSpec<N, P> spec, KeyedInstanceIdentifier<P, K> path,
42             KeyedListListener<P, N, K> listener, Executor executor);
43
44     default <P extends DataObject & Identifiable<K>, N extends KeyedListNotification<N, P, K>, K extends Identifier<P>>
45             @NonNull Registration registerListener(final InstanceNotificationSpec<N, P> spec,
46                 final KeyedInstanceIdentifier<P, K> path, final KeyedListListener<P, N, K> listener) {
47         return registerListener(spec, path, listener, MoreExecutors.directExecutor());
48     }
49
50     /*
51      * Interface for listeners on instance (YANG 1.1) notifications.
52      */
53     @FunctionalInterface
54     interface Listener<P extends DataObject, N extends InstanceNotification<N, P>> extends EventListener {
55         /**
56          * Process an instance notification.
57          *
58          * @param path Instance path
59          * @param notification Notification body
60          */
61         void onNotification(@NonNull InstanceIdentifier<P> path, @NonNull N notification);
62     }
63
64     /**
65      * Interface for listeners on instance (YANG 1.1) notifications defined in a {@code list} with a {@code key}.
66      */
67     @FunctionalInterface
68     interface KeyedListListener<P extends DataObject & Identifiable<K>, N extends KeyedListNotification<N, P, K>,
69             K extends Identifier<P>> extends EventListener {
70         /**
71          * Process an instance notification.
72          *
73          * @param path Instance path
74          * @param notification Notification body
75          */
76         void onNotification(@NonNull KeyedInstanceIdentifier<P, K> path, @NonNull N notification);
77     }
78 }