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 com.google.common.collect.ClassToInstanceMap;
11 import com.google.common.collect.ImmutableSet;
13 import org.opendaylight.mdsal.binding.api.NotificationService;
14 import org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
15 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
16 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
17 import org.opendaylight.mdsal.dom.api.DOMService;
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 {
24 public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
26 private final BindingNormalizedNodeSerializer codec;
27 private final DOMNotificationService domNotifService;
29 public BindingDOMNotificationServiceAdapter(final DOMNotificationService domNotifService,
30 final BindingNormalizedNodeSerializer codec) {
32 this.domNotifService = domNotifService;
36 public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec,
37 final DOMNotificationService domNotifService) {
38 this(domNotifService, codec);
42 public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
43 final BindingDOMNotificationListenerAdapter domListener
44 = new BindingDOMNotificationListenerAdapter(codec, listener);
45 final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
46 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
47 return new ListenerRegistrationImpl<>(listener, domRegistration);
50 private static class ListenerRegistrationImpl<T extends NotificationListener>
51 extends AbstractListenerRegistration<T> {
52 private final ListenerRegistration<?> listenerRegistration;
54 ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
56 this.listenerRegistration = listenerRegistration;
60 protected void removeRegistration() {
61 listenerRegistration.close();
65 private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
68 protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
69 final ClassToInstanceMap<DOMService> delegates) {
70 final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
71 return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification);
75 public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
76 return ImmutableSet.of(DOMNotificationService.class);
80 public DOMNotificationService getDomService() {
81 return domNotifService;