2 * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.mdsal.binding.javav2.dom.adapter.impl.notification;
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ClassToInstanceMap;
12 import com.google.common.collect.ImmutableSet;
14 import org.opendaylight.mdsal.binding.javav2.api.NotificationService;
15 import org.opendaylight.mdsal.binding.javav2.dom.adapter.spi.builder.BindingDOMAdapterBuilder;
16 import org.opendaylight.mdsal.binding.javav2.dom.adapter.spi.builder.BindingDOMAdapterBuilder.Factory;
17 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.serializer.BindingNormalizedNodeSerializer;
18 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.BindingToNormalizedNodeCodec;
19 import org.opendaylight.mdsal.binding.javav2.spec.runtime.NotificationListener;
20 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
21 import org.opendaylight.mdsal.dom.api.DOMService;
22 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 * Notification service adapter.
29 public class BindingDOMNotificationServiceAdapter implements NotificationService, AutoCloseable {
31 public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
33 private final BindingNormalizedNodeSerializer codec;
34 private final DOMNotificationService domNotifService;
36 public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec,
37 final DOMNotificationService domNotifService) {
39 this.domNotifService = domNotifService;
43 public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
44 final BindingDOMNotificationListenerAdapter domListener =
45 new BindingDOMNotificationListenerAdapter(codec, listener);
46 final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
47 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
48 return new ListenerRegistrationImpl<>(listener, domRegistration);
52 public void close() throws Exception {
56 private static class ListenerRegistrationImpl<T extends NotificationListener>
57 extends AbstractListenerRegistration<T> {
58 private final ListenerRegistration<?> listenerRegistration;
60 ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
62 this.listenerRegistration = listenerRegistration;
66 protected void removeRegistration() {
67 listenerRegistration.close();
71 private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
74 protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
75 final ClassToInstanceMap<DOMService> delegates) {
76 final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
77 return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification);
81 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
82 return ImmutableSet.of(DOMNotificationService.class);
87 * Get DOM notification service.
89 * @return DOM notification service
91 public DOMNotificationService getDomService() {
92 return domNotifService;