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