BUG-3183: Extend notification publisher API
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / 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.controller.md.sal.binding.impl;
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.controller.md.sal.binding.api.NotificationPublishService;
16 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMAdapterBuilder.Factory;
17 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
18 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
19 import org.opendaylight.controller.md.sal.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 = new BindingDOMAdapterBuilder.Factory<NotificationPublishService>() {
25
26         @Override
27         public BindingDOMAdapterBuilder<NotificationPublishService> newBuilder() {
28             return new Builder();
29         }
30
31     };
32
33     private final BindingToNormalizedNodeCodec codecRegistry;
34     private final DOMNotificationPublishService domPublishService;
35
36     public BindingDOMNotificationPublishServiceAdapter(final BindingToNormalizedNodeCodec codec, final DOMNotificationPublishService domPublishService) {
37         this.codecRegistry = codec;
38         this.domPublishService = domPublishService;
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, final int timeout, final TimeUnit unit) throws InterruptedException {
64         ListenableFuture<?> offerResult = domPublishService.offerNotification(toDomNotification(notification), timeout, unit);
65         return DOMNotificationPublishService.REJECTED.equals(offerResult)
66                 ? NotificationPublishService.REJECTED
67                 : offerResult;
68     }
69
70     private DOMNotification toDomNotification(final Notification notification) {
71         return LazySerializedDOMNotification.create(codecRegistry, notification);
72     }
73
74     @Override
75     public void close() throws Exception {
76
77     }
78
79     protected static class Builder extends BindingDOMAdapterBuilder<NotificationPublishService> {
80
81         @Override
82         public Set<Class<? extends DOMService>> getRequiredDelegates() {
83             return ImmutableSet.<Class<? extends DOMService>>of(DOMNotificationPublishService.class);
84         }
85
86         @Override
87         protected NotificationPublishService createInstance(final BindingToNormalizedNodeCodec codec,
88                 final ClassToInstanceMap<DOMService> delegates) {
89             final DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class);
90             return new BindingDOMNotificationPublishServiceAdapter(codec, domPublish);
91         }
92
93     }
94 }