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;
13 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
14 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMAdapterBuilder.Factory;
15 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
16 import org.opendaylight.controller.md.sal.dom.api.DOMService;
17 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
18 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.binding.NotificationListener;
22 public class BindingDOMNotificationServiceAdapter implements NotificationService, AutoCloseable {
24 public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
25 private final BindingNormalizedNodeSerializer codec;
26 private final DOMNotificationService domNotifService;
28 public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec,
29 final DOMNotificationService domNotifService) {
31 this.domNotifService = domNotifService;
35 public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
36 final BindingDOMNotificationListenerAdapter domListener =
37 new BindingDOMNotificationListenerAdapter(codec, listener);
38 final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
39 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
40 return new ListenerRegistrationImpl<>(listener, domRegistration);
44 public void close() throws Exception {
48 private static class ListenerRegistrationImpl<T extends NotificationListener>
49 extends AbstractListenerRegistration<T> {
50 private final ListenerRegistration<?> listenerRegistration;
52 ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
54 this.listenerRegistration = listenerRegistration;
58 protected void removeRegistration() {
59 listenerRegistration.close();
63 private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
66 protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
67 final ClassToInstanceMap<DOMService> delegates) {
68 final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
69 return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification);
73 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
74 return ImmutableSet.of(DOMNotificationService.class);
78 public DOMNotificationService getDomService() {
79 return domNotifService;