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