Add binding ClusteredDataTreeChangeListener
[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.collect.ClassToInstanceMap;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Set;
14 import java.util.concurrent.TimeUnit;
15 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
16 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
17 import org.opendaylight.mdsal.dom.api.DOMNotification;
18 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
19 import org.opendaylight.mdsal.dom.api.DOMService;
20 import org.opendaylight.yangtools.yang.binding.Notification;
21
22 public class BindingDOMNotificationPublishServiceAdapter implements NotificationPublishService {
23
24     static final Factory<NotificationPublishService> BUILDER_FACTORY = Builder::new;
25
26     private final BindingToNormalizedNodeCodec codecRegistry;
27     private final DOMNotificationPublishService domPublishService;
28
29     public BindingDOMNotificationPublishServiceAdapter(final DOMNotificationPublishService domPublishService,
30             final BindingToNormalizedNodeCodec codec) {
31         this.codecRegistry = codec;
32         this.domPublishService = domPublishService;
33     }
34
35     @Deprecated
36     public BindingDOMNotificationPublishServiceAdapter(final BindingToNormalizedNodeCodec codec,
37             final DOMNotificationPublishService domPublishService) {
38         this(domPublishService, codec);
39     }
40
41     public BindingToNormalizedNodeCodec getCodecRegistry() {
42         return codecRegistry;
43     }
44
45     public DOMNotificationPublishService getDomPublishService() {
46         return domPublishService;
47     }
48
49     @Override
50     public void putNotification(final Notification notification) throws InterruptedException {
51         domPublishService.putNotification(toDomNotification(notification));
52     }
53
54     @Override
55     public ListenableFuture<? extends Object> offerNotification(final Notification notification) {
56         ListenableFuture<?> offerResult = domPublishService.offerNotification(toDomNotification(notification));
57         return DOMNotificationPublishService.REJECTED.equals(offerResult)
58                 ? NotificationPublishService.REJECTED
59                 : offerResult;
60     }
61
62     @Override
63     public ListenableFuture<? extends Object> offerNotification(final Notification notification,
64                                                  final int timeout, final TimeUnit unit) throws InterruptedException {
65         ListenableFuture<?> offerResult = domPublishService.offerNotification(
66                 toDomNotification(notification), timeout, unit);
67         return DOMNotificationPublishService.REJECTED.equals(offerResult)
68                 ? NotificationPublishService.REJECTED
69                 : offerResult;
70     }
71
72     private DOMNotification toDomNotification(final Notification notification) {
73         return LazySerializedDOMNotification.create(codecRegistry, notification);
74     }
75
76     protected static class Builder extends BindingDOMAdapterBuilder<NotificationPublishService> {
77
78         @Override
79         public Set<Class<? extends DOMService>> getRequiredDelegates() {
80             return ImmutableSet.of(DOMNotificationPublishService.class);
81         }
82
83         @Override
84         protected NotificationPublishService createInstance(final BindingToNormalizedNodeCodec codec,
85                 final ClassToInstanceMap<DOMService> delegates) {
86             final DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class);
87             return new BindingDOMNotificationPublishServiceAdapter(codec, domPublish);
88         }
89
90     }
91 }