Merge "BUG 2854 : Do not add empty read write transactions to the replicable journal"
[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 javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
17 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMAdapterBuilder.Factory;
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.binding.data.codec.api.BindingNormalizedNodeSerializer;
22 import org.opendaylight.yangtools.yang.binding.Notification;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
25
26 public class BindingDOMNotificationPublishServiceAdapter implements NotificationPublishService, AutoCloseable {
27
28     static final Factory<NotificationPublishService> BUILDER_FACTORY = new BindingDOMAdapterBuilder.Factory<NotificationPublishService>() {
29
30         @Override
31         public BindingDOMAdapterBuilder<NotificationPublishService> newBuilder() {
32             return new Builder();
33         }
34
35     };
36
37     private final BindingNormalizedNodeSerializer codecRegistry;
38     private final DOMNotificationPublishService domPublishService;
39
40     public BindingDOMNotificationPublishServiceAdapter(BindingNormalizedNodeSerializer codecRegistry, DOMNotificationPublishService domPublishService) {
41         this.codecRegistry = codecRegistry;
42         this.domPublishService = domPublishService;
43     }
44
45     @Override
46     public void putNotification(final Notification notification) throws InterruptedException {
47         domPublishService.putNotification(toDomNotification(notification));
48     }
49
50     @Override
51     public boolean offerNotification(final Notification notification) {
52         final ListenableFuture<?> listenableFuture = domPublishService.offerNotification(toDomNotification(notification));
53         return !DOMNotificationPublishService.REJECTED.equals(listenableFuture);
54     }
55
56     @Override
57     public boolean offerNotification(final Notification notification, final int timeout, final TimeUnit unit) throws InterruptedException {
58         final ListenableFuture<?> listenableFuture =
59                 domPublishService.offerNotification(toDomNotification(notification), timeout, unit);
60         return !DOMNotificationPublishService.REJECTED.equals(listenableFuture);
61     }
62
63     private DOMNotification toDomNotification(final Notification notification) {
64         final ContainerNode domNotification = codecRegistry.toNormalizedNodeNotification(notification);
65         return new DOMNotificationImpl(domNotification);
66     }
67
68     @Override
69     public void close() throws Exception {
70
71     }
72
73     private static class DOMNotificationImpl implements DOMNotification {
74
75         private final SchemaPath type;
76         private final ContainerNode body;
77
78         public DOMNotificationImpl(final ContainerNode body) {
79             this.type = SchemaPath.create(true, body.getIdentifier().getNodeType());
80             this.body = body;
81         }
82
83         @Nonnull
84         @Override
85         public SchemaPath getType() {
86             return this.type;
87         }
88
89         @Nonnull
90         @Override
91         public ContainerNode getBody() {
92             return this.body;
93         }
94     }
95
96     protected static class Builder extends BindingDOMAdapterBuilder<NotificationPublishService> {
97
98         @Override
99         public Set<Class<? extends DOMService>> getRequiredDelegates() {
100             return ImmutableSet.<Class<? extends DOMService>>of(DOMNotificationPublishService.class);
101         }
102
103         @Override
104         protected NotificationPublishService createInstance(BindingToNormalizedNodeCodec codec,
105                 ClassToInstanceMap<DOMService> delegates) {
106             BindingNormalizedNodeSerializer codecReg = codec.getCodecRegistry();
107             DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class);
108             return new BindingDOMNotificationPublishServiceAdapter(codecReg, domPublish);
109         }
110
111     }
112 }