Merge "Cleanup: Remove passing around of DataPersistenceProvider"
[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.binding.data.codec.api.BindingNormalizedNodeSerializer;
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 BindingNormalizedNodeSerializer codecRegistry;
35     private final DOMNotificationPublishService domPublishService;
36
37     public BindingDOMNotificationPublishServiceAdapter(final BindingNormalizedNodeSerializer codecRegistry, final DOMNotificationPublishService domPublishService) {
38         this.codecRegistry = codecRegistry;
39         this.domPublishService = domPublishService;
40     }
41
42     @Override
43     public void putNotification(final Notification notification) throws InterruptedException {
44         domPublishService.putNotification(toDomNotification(notification));
45     }
46
47     @Override
48     public boolean offerNotification(final Notification notification) {
49         final ListenableFuture<?> listenableFuture = domPublishService.offerNotification(toDomNotification(notification));
50         return !DOMNotificationPublishService.REJECTED.equals(listenableFuture);
51     }
52
53     @Override
54     public boolean offerNotification(final Notification notification, final int timeout, final TimeUnit unit) throws InterruptedException {
55         final ListenableFuture<?> listenableFuture =
56                 domPublishService.offerNotification(toDomNotification(notification), timeout, unit);
57         return !DOMNotificationPublishService.REJECTED.equals(listenableFuture);
58     }
59
60     private DOMNotification toDomNotification(final Notification notification) {
61         return LazySerializedDOMNotification.create(codecRegistry, notification);
62     }
63
64     @Override
65     public void close() throws Exception {
66
67     }
68
69     protected static class Builder extends BindingDOMAdapterBuilder<NotificationPublishService> {
70
71         @Override
72         public Set<Class<? extends DOMService>> getRequiredDelegates() {
73             return ImmutableSet.<Class<? extends DOMService>>of(DOMNotificationPublishService.class);
74         }
75
76         @Override
77         protected NotificationPublishService createInstance(final BindingToNormalizedNodeCodec codec,
78                 final ClassToInstanceMap<DOMService> delegates) {
79             final BindingNormalizedNodeSerializer codecReg = codec.getCodecRegistry();
80             final DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class);
81             return new BindingDOMNotificationPublishServiceAdapter(codecReg, domPublish);
82         }
83
84     }
85 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.