Fix checkstyle in mdsal-binding-dom-adapter
[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, AutoCloseable {
23
24     static final Factory<NotificationPublishService> BUILDER_FACTORY
25             = 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,
38             final DOMNotificationPublishService domPublishService) {
39         this.codecRegistry = codec;
40         this.domPublishService = domPublishService;
41     }
42
43     public BindingToNormalizedNodeCodec getCodecRegistry() {
44         return codecRegistry;
45     }
46
47     public DOMNotificationPublishService getDomPublishService() {
48         return domPublishService;
49     }
50
51     @Override
52     public void putNotification(final Notification notification) throws InterruptedException {
53         domPublishService.putNotification(toDomNotification(notification));
54     }
55
56     @Override
57     public ListenableFuture<? extends Object> offerNotification(final Notification notification) {
58         ListenableFuture<?> offerResult = domPublishService.offerNotification(toDomNotification(notification));
59         return DOMNotificationPublishService.REJECTED.equals(offerResult)
60                 ? NotificationPublishService.REJECTED
61                 : offerResult;
62     }
63
64     @Override
65     public ListenableFuture<? extends Object> offerNotification(final Notification notification,
66             final int timeout, final TimeUnit unit) throws InterruptedException {
67         ListenableFuture<?> offerResult = domPublishService.offerNotification(
68                 toDomNotification(notification), timeout, unit);
69         return DOMNotificationPublishService.REJECTED.equals(offerResult)
70                 ? NotificationPublishService.REJECTED
71                 : offerResult;
72     }
73
74     private DOMNotification toDomNotification(final Notification notification) {
75         return LazySerializedDOMNotification.create(codecRegistry, notification);
76     }
77
78     @Override
79     public void close() throws Exception {
80
81     }
82
83     protected static class Builder extends BindingDOMAdapterBuilder<NotificationPublishService> {
84
85         @Override
86         public Set<Class<? extends DOMService>> getRequiredDelegates() {
87             return ImmutableSet.<Class<? extends DOMService>>of(DOMNotificationPublishService.class);
88         }
89
90         @Override
91         protected NotificationPublishService createInstance(final BindingToNormalizedNodeCodec codec,
92                 final ClassToInstanceMap<DOMService> delegates) {
93             final DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class);
94             return new BindingDOMNotificationPublishServiceAdapter(codec, domPublish);
95         }
96
97     }
98 }