X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fbinding%2Fimpl%2FBindingDOMNotificationPublishServiceAdapter.java;fp=opendaylight%2Fmd-sal%2Fsal-binding-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fbinding%2Fimpl%2FBindingDOMNotificationPublishServiceAdapter.java;h=0a736ffc79a11f1128b19c1110129b780c499caa;hb=0e594843543c8180c885fe228c88acc77abe4067;hp=0000000000000000000000000000000000000000;hpb=7d9f0a4667c803c7155c8964b41bdf1e6196728a;p=controller.git diff --git a/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java new file mode 100644 index 0000000000..0a736ffc79 --- /dev/null +++ b/opendaylight/md-sal/sal-binding-broker/src/main/java/org/opendaylight/controller/md/sal/binding/impl/BindingDOMNotificationPublishServiceAdapter.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.md.sal.binding.impl; + +import com.google.common.collect.ClassToInstanceMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.util.concurrent.ListenableFuture; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import javax.annotation.Nonnull; +import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; +import org.opendaylight.controller.md.sal.binding.impl.BindingDOMAdapterBuilder.Factory; +import org.opendaylight.controller.md.sal.dom.api.DOMNotification; +import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService; +import org.opendaylight.controller.md.sal.dom.api.DOMService; +import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer; +import org.opendaylight.yangtools.yang.binding.Notification; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; + +public class BindingDOMNotificationPublishServiceAdapter implements NotificationPublishService, AutoCloseable { + + static final Factory BUILDER_FACTORY = new BindingDOMAdapterBuilder.Factory() { + + @Override + public BindingDOMAdapterBuilder newBuilder() { + return new Builder(); + } + + }; + + private final BindingNormalizedNodeSerializer codecRegistry; + private final DOMNotificationPublishService domPublishService; + + public BindingDOMNotificationPublishServiceAdapter(BindingNormalizedNodeSerializer codecRegistry, DOMNotificationPublishService domPublishService) { + this.codecRegistry = codecRegistry; + this.domPublishService = domPublishService; + } + + @Override + public void putNotification(final Notification notification) throws InterruptedException { + domPublishService.putNotification(toDomNotification(notification)); + } + + @Override + public boolean offerNotification(final Notification notification) { + final ListenableFuture listenableFuture = domPublishService.offerNotification(toDomNotification(notification)); + return !DOMNotificationPublishService.REJECTED.equals(listenableFuture); + } + + @Override + public boolean offerNotification(final Notification notification, final int timeout, final TimeUnit unit) throws InterruptedException { + final ListenableFuture listenableFuture = + domPublishService.offerNotification(toDomNotification(notification), timeout, unit); + return !DOMNotificationPublishService.REJECTED.equals(listenableFuture); + } + + private DOMNotification toDomNotification(final Notification notification) { + final ContainerNode domNotification = codecRegistry.toNormalizedNodeNotification(notification); + return new DOMNotificationImpl(domNotification); + } + + @Override + public void close() throws Exception { + + } + + private static class DOMNotificationImpl implements DOMNotification { + + private final SchemaPath type; + private final ContainerNode body; + + public DOMNotificationImpl(final ContainerNode body) { + this.type = SchemaPath.create(true, body.getIdentifier().getNodeType()); + this.body = body; + } + + @Nonnull + @Override + public SchemaPath getType() { + return this.type; + } + + @Nonnull + @Override + public ContainerNode getBody() { + return this.body; + } + } + + protected static class Builder extends BindingDOMAdapterBuilder { + + @Override + public Set> getRequiredDelegates() { + return ImmutableSet.>of(DOMNotificationPublishService.class); + } + + @Override + protected NotificationPublishService createInstance(BindingToNormalizedNodeCodec codec, + ClassToInstanceMap delegates) { + BindingNormalizedNodeSerializer codecReg = codec.getCodecRegistry(); + DOMNotificationPublishService domPublish = delegates.getInstance(DOMNotificationPublishService.class); + return new BindingDOMNotificationPublishServiceAdapter(codecReg, domPublish); + } + + } +}