Add InstanceNotification(Publish)ServiceAdapter
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMNotificationPublishServiceAdapter.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 com.google.common.annotations.VisibleForTesting;
11 import com.google.common.collect.ClassToInstanceMap;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Set;
15 import java.util.concurrent.TimeUnit;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
18 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
19 import org.opendaylight.mdsal.dom.api.DOMNotification;
20 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
21 import org.opendaylight.mdsal.dom.api.DOMService;
22 import org.opendaylight.yangtools.yang.binding.Notification;
23
24 @VisibleForTesting
25 // FIXME: 10.0.0: make this class final
26 public class BindingDOMNotificationPublishServiceAdapter extends AbstractBindingAdapter<DOMNotificationPublishService>
27         implements NotificationPublishService {
28
29     static final Factory<NotificationPublishService> BUILDER_FACTORY = Builder::new;
30
31     public BindingDOMNotificationPublishServiceAdapter(final AdapterContext adapterContext,
32             final DOMNotificationPublishService domPublishService) {
33         super(adapterContext, domPublishService);
34     }
35
36     @Deprecated(forRemoval = true, since = "9.0.2")
37     public DOMNotificationPublishService getDomPublishService() {
38         return getDelegate();
39     }
40
41     @Override
42     public void putNotification(final Notification<?> notification) throws InterruptedException {
43         getDelegate().putNotification(toDomNotification(notification));
44     }
45
46     @Override
47     public ListenableFuture<? extends Object> offerNotification(final Notification<?> notification) {
48         return toBindingResult(getDelegate().offerNotification(toDomNotification(notification)));
49     }
50
51     @Override
52     public ListenableFuture<? extends Object> offerNotification(final Notification<?> notification, final int timeout,
53             final TimeUnit unit) throws InterruptedException {
54         return toBindingResult(getDelegate().offerNotification(toDomNotification(notification), timeout, unit));
55     }
56
57     private @NonNull DOMNotification toDomNotification(final Notification<?> notification) {
58         return new LazySerializedNotification(currentSerializer(), notification);
59     }
60
61     private static @NonNull ListenableFuture<? extends Object> toBindingResult(
62             final @NonNull ListenableFuture<? extends Object> domResult) {
63         return DOMNotificationPublishService.REJECTED.equals(domResult) ? NotificationPublishService.REJECTED
64             : domResult;
65     }
66
67     // FIXME: 10.0.0: hide this class and make it final
68     protected static class Builder extends BindingDOMAdapterBuilder<NotificationPublishService> {
69         Builder(final AdapterContext adapterContext) {
70             super(adapterContext);
71         }
72
73         @Override
74         public Set<Class<? extends DOMService>> getRequiredDelegates() {
75             return ImmutableSet.of(DOMNotificationPublishService.class);
76         }
77
78         @Override
79         protected NotificationPublishService createInstance(final ClassToInstanceMap<DOMService> delegates) {
80             return new BindingDOMNotificationPublishServiceAdapter(adapterContext(),
81                 delegates.getInstance(DOMNotificationPublishService.class));
82         }
83     }
84 }