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.mdsal.binding.dom.adapter;
10 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
11 import org.opendaylight.mdsal.dom.api.DOMService;
13 import org.opendaylight.mdsal.binding.api.NotificationService;
14 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
15 import com.google.common.collect.ClassToInstanceMap;
16 import com.google.common.collect.ImmutableSet;
18 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
19 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.NotificationListener;
23 public class BindingDOMNotificationServiceAdapter implements NotificationService, AutoCloseable {
25 public static final Factory<NotificationService> BUILDER_FACTORY = new Factory<NotificationService>() {
28 public BindingDOMAdapterBuilder<NotificationService> newBuilder() {
33 private final BindingNormalizedNodeSerializer codec;
34 private final DOMNotificationService domNotifService;
36 public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec, final DOMNotificationService domNotifService) {
38 this.domNotifService = domNotifService;
42 public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
43 final BindingDOMNotificationListenerAdapter domListener = new BindingDOMNotificationListenerAdapter(codec, listener);
44 final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
45 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
46 return new ListenerRegistrationImpl<>(listener, domRegistration);
50 public void close() throws Exception {
54 private static class ListenerRegistrationImpl<T extends NotificationListener> extends AbstractListenerRegistration<T> {
55 private final ListenerRegistration<?> listenerRegistration;
57 public ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
59 this.listenerRegistration = listenerRegistration;
63 protected void removeRegistration() {
64 listenerRegistration.close();
68 private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
71 protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
72 final ClassToInstanceMap<DOMService> delegates) {
73 final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
74 return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification);
78 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
79 return ImmutableSet.of(DOMNotificationService.class);
83 public DOMNotificationService getDomService() {
84 return domNotifService;