88c662866c7922d5c6abcd143207427f8f4a1ee2
[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 public class BindingDOMNotificationPublishServiceAdapter extends AbstractBindingAdapter<DOMNotificationPublishService>
28         implements NotificationPublishService {
29
30     static final Factory<NotificationPublishService> BUILDER_FACTORY = Builder::new;
31
32     public BindingDOMNotificationPublishServiceAdapter(final AdapterContext adapterContext,
33             final DOMNotificationPublishService domPublishService) {
34         super(adapterContext, domPublishService);
35     }
36
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         ListenableFuture<?> offerResult = getDelegate().offerNotification(toDomNotification(notification));
49         return DOMNotificationPublishService.REJECTED.equals(offerResult)
50                 ? NotificationPublishService.REJECTED
51                 : offerResult;
52     }
53
54     @Override
55     public ListenableFuture<? extends Object> offerNotification(final Notification<?> notification, final int timeout,
56             final TimeUnit unit) throws InterruptedException {
57         ListenableFuture<?> offerResult = getDelegate().offerNotification(toDomNotification(notification), timeout,
58             unit);
59         return DOMNotificationPublishService.REJECTED.equals(offerResult)
60                 ? NotificationPublishService.REJECTED
61                 : offerResult;
62     }
63
64     private @NonNull DOMNotification toDomNotification(final Notification<?> notification) {
65         final Instant instant = notification instanceof EventInstantAware
66                 ? ((EventInstantAware) notification).eventInstant() : Instant.now();
67         return LazySerializedDOMNotification.create(currentSerializer(), notification, instant);
68     }
69
70     protected static class Builder extends BindingDOMAdapterBuilder<NotificationPublishService> {
71         Builder(final AdapterContext adapterContext) {
72             super(adapterContext);
73         }
74
75         @Override
76         public Set<Class<? extends DOMService>> getRequiredDelegates() {
77             return ImmutableSet.of(DOMNotificationPublishService.class);
78         }
79
80         @Override
81         protected NotificationPublishService createInstance(final ClassToInstanceMap<DOMService> delegates) {
82             return new BindingDOMNotificationPublishServiceAdapter(adapterContext(),
83                 delegates.getInstance(DOMNotificationPublishService.class));
84         }
85     }
86 }