Notification Listener Adapter uses NotificationListenerInvoker
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingDOMNotificationServiceAdapter.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.binding.impl;
9
10 import com.google.common.collect.ClassToInstanceMap;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Set;
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.controller.sal.binding.codegen.impl.SingletonHolder;
18 import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory;
19 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
20 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.binding.NotificationListener;
23
24 public class BindingDOMNotificationServiceAdapter implements NotificationService, AutoCloseable {
25
26     public static final Factory<NotificationService> BUILDER_FACTORY = new Factory<NotificationService>() {
27
28         @Override
29         public BindingDOMAdapterBuilder<NotificationService> newBuilder() {
30             return new Builder();
31         }
32
33     };
34     private final BindingNormalizedNodeSerializer codec;
35     private final DOMNotificationService domNotifService;
36
37     public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec, final DOMNotificationService domNotifService, final NotificationInvokerFactory notificationInvokerFactory) {
38         this.codec = codec;
39         this.domNotifService = domNotifService;
40     }
41
42     @Override
43     public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
44         final BindingDOMNotificationListenerAdapter domListener = new BindingDOMNotificationListenerAdapter(codec, listener);
45         final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
46                 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
47         return new ListenerRegistrationImpl<>(listener, domRegistration);
48     }
49
50     @Override
51     public void close() throws Exception {
52
53     }
54
55     private static class ListenerRegistrationImpl<T extends NotificationListener> extends AbstractListenerRegistration<T> {
56         private final ListenerRegistration<?> listenerRegistration;
57
58         public ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
59             super(listener);
60             this.listenerRegistration = listenerRegistration;
61         }
62
63         @Override
64         protected void removeRegistration() {
65             listenerRegistration.close();
66         }
67     }
68
69     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
70
71         @Override
72         protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
73                 final ClassToInstanceMap<DOMService> delegates) {
74             final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
75             final NotificationInvokerFactory invokerFactory = SingletonHolder.INVOKER_FACTORY;
76             return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification, invokerFactory);
77         }
78
79         @Override
80         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
81             return ImmutableSet.of(DOMNotificationService.class);
82         }
83     }
84 }