14d6713a6c9cde4a32cd9e33c1b434f6f4190f48
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / ForwardedNotificationPublishService.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.util.concurrent.ListenableFuture;
11 import java.util.concurrent.TimeUnit;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
14 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
15 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
16 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
17 import org.opendaylight.yangtools.yang.binding.Notification;
18 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
19 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
20
21 public class ForwardedNotificationPublishService implements NotificationPublishService, AutoCloseable {
22     private final BindingNormalizedNodeSerializer codecRegistry;
23     private final DOMNotificationPublishService domPublishService;
24
25     public ForwardedNotificationPublishService(BindingNormalizedNodeSerializer codecRegistry, DOMNotificationPublishService domPublishService) {
26         this.codecRegistry = codecRegistry;
27         this.domPublishService = domPublishService;
28     }
29
30     @Override
31     public void putNotification(final Notification notification) throws InterruptedException {
32         domPublishService.putNotification(toDomNotification(notification));
33     }
34
35     @Override
36     public boolean offerNotification(final Notification notification) {
37         final ListenableFuture<?> listenableFuture = domPublishService.offerNotification(toDomNotification(notification));
38         return !DOMNotificationPublishService.REJECTED.equals(listenableFuture);
39     }
40
41     @Override
42     public boolean offerNotification(final Notification notification, final int timeout, final TimeUnit unit) throws InterruptedException {
43         final ListenableFuture<?> listenableFuture =
44                 domPublishService.offerNotification(toDomNotification(notification), timeout, unit);
45         return !DOMNotificationPublishService.REJECTED.equals(listenableFuture);
46     }
47
48     private DOMNotification toDomNotification(final Notification notification) {
49         final ContainerNode domNotification = codecRegistry.toNormalizedNodeNotification(notification);
50         return new DOMNotificationImpl(domNotification);
51     }
52
53     @Override
54     public void close() throws Exception {
55
56     }
57
58     private static class DOMNotificationImpl implements DOMNotification {
59
60         private final SchemaPath type;
61         private final ContainerNode body;
62
63         public DOMNotificationImpl(final ContainerNode body) {
64             this.type = SchemaPath.create(true, body.getIdentifier().getNodeType());
65             this.body = body;
66         }
67
68         @Nonnull
69         @Override
70         public SchemaPath getType() {
71             return this.type;
72         }
73
74         @Nonnull
75         @Override
76         public ContainerNode getBody() {
77             return this.body;
78         }
79     }
80 }