2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.binding.impl;
10 import com.google.common.collect.ClassToInstanceMap;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.util.concurrent.ListenableFuture;
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;
26 public class BindingDOMNotificationPublishServiceAdapter implements NotificationPublishService, AutoCloseable {
28 static final Factory<NotificationPublishService> BUILDER_FACTORY = new BindingDOMAdapterBuilder.Factory<NotificationPublishService>() {
31 public BindingDOMAdapterBuilder<NotificationPublishService> newBuilder() {
37 private final BindingNormalizedNodeSerializer codecRegistry;
38 private final DOMNotificationPublishService domPublishService;
40 public BindingDOMNotificationPublishServiceAdapter(BindingNormalizedNodeSerializer codecRegistry, DOMNotificationPublishService domPublishService) {
41 this.codecRegistry = codecRegistry;
42 this.domPublishService = domPublishService;
46 public void putNotification(final Notification notification) throws InterruptedException {
47 domPublishService.putNotification(toDomNotification(notification));
51 public boolean offerNotification(final Notification notification) {
52 final ListenableFuture<?> listenableFuture = domPublishService.offerNotification(toDomNotification(notification));
53 return !DOMNotificationPublishService.REJECTED.equals(listenableFuture);
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);
63 private DOMNotification toDomNotification(final Notification notification) {
64 final ContainerNode domNotification = codecRegistry.toNormalizedNodeNotification(notification);
65 return new DOMNotificationImpl(domNotification);
69 public void close() throws Exception {
73 private static class DOMNotificationImpl implements DOMNotification {
75 private final SchemaPath type;
76 private final ContainerNode body;
78 public DOMNotificationImpl(final ContainerNode body) {
79 this.type = SchemaPath.create(true, body.getIdentifier().getNodeType());
85 public SchemaPath getType() {
91 public ContainerNode getBody() {
96 protected static class Builder extends BindingDOMAdapterBuilder<NotificationPublishService> {
99 public Set<Class<? extends DOMService>> getRequiredDelegates() {
100 return ImmutableSet.<Class<? extends DOMService>>of(DOMNotificationPublishService.class);
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);