Merge branch 'mdsal-trace' from controller
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / 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.mdsal.binding.dom.adapter;
9
10 import com.google.common.collect.ClassToInstanceMap;
11 import com.google.common.collect.ImmutableSet;
12 import java.util.Set;
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;
21
22 public class BindingDOMNotificationServiceAdapter implements NotificationService {
23
24     public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
25
26     private final BindingNormalizedNodeSerializer codec;
27     private final DOMNotificationService domNotifService;
28
29     public BindingDOMNotificationServiceAdapter(final DOMNotificationService domNotifService,
30             final BindingNormalizedNodeSerializer codec) {
31         this.codec = codec;
32         this.domNotifService = domNotifService;
33     }
34
35     @Deprecated
36     public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec,
37             final DOMNotificationService domNotifService) {
38         this(domNotifService, codec);
39     }
40
41     @Override
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);
48     }
49
50     private static class ListenerRegistrationImpl<T extends NotificationListener>
51             extends AbstractListenerRegistration<T> {
52         private final ListenerRegistration<?> listenerRegistration;
53
54         ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
55             super(listener);
56             this.listenerRegistration = listenerRegistration;
57         }
58
59         @Override
60         protected void removeRegistration() {
61             listenerRegistration.close();
62         }
63     }
64
65     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
66
67         @Override
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);
72         }
73
74         @Override
75         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
76             return ImmutableSet.of(DOMNotificationService.class);
77         }
78     }
79
80     public DOMNotificationService getDomService() {
81         return domNotifService;
82     }
83 }