Binding v2 runtime - adapters - impl - notifications
[mdsal.git] / binding2 / mdsal-binding2-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / adapter / impl / notification / BindingDOMNotificationServiceAdapter.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.javav2.dom.adapter.impl.notification;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.ClassToInstanceMap;
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Set;
14 import org.opendaylight.mdsal.binding.javav2.api.NotificationService;
15 import org.opendaylight.mdsal.binding.javav2.dom.adapter.spi.builder.BindingDOMAdapterBuilder;
16 import org.opendaylight.mdsal.binding.javav2.dom.adapter.spi.builder.BindingDOMAdapterBuilder.Factory;
17 import org.opendaylight.mdsal.binding.javav2.dom.codec.api.serializer.BindingNormalizedNodeSerializer;
18 import org.opendaylight.mdsal.binding.javav2.dom.codec.impl.BindingToNormalizedNodeCodec;
19 import org.opendaylight.mdsal.binding.javav2.spec.runtime.NotificationListener;
20 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
21 import org.opendaylight.mdsal.dom.api.DOMService;
22 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
23 import org.opendaylight.yangtools.concepts.ListenerRegistration;
24
25 /**
26  * Notification service adapter.
27  */
28 @Beta
29 public class BindingDOMNotificationServiceAdapter implements NotificationService, AutoCloseable {
30
31     public static final Factory<NotificationService> BUILDER_FACTORY = Builder::new;
32
33     private final BindingNormalizedNodeSerializer codec;
34     private final DOMNotificationService domNotifService;
35
36     public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec,
37             final DOMNotificationService domNotifService) {
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 =
45                 new BindingDOMNotificationListenerAdapter(codec, listener);
46         final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
47                 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
48         return new ListenerRegistrationImpl<>(listener, domRegistration);
49     }
50
51     @Override
52     public void close() throws Exception {
53         // NOOP
54     }
55
56     private static class ListenerRegistrationImpl<T extends NotificationListener>
57             extends AbstractListenerRegistration<T> {
58         private final ListenerRegistration<?> listenerRegistration;
59
60         ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
61             super(listener);
62             this.listenerRegistration = listenerRegistration;
63         }
64
65         @Override
66         protected void removeRegistration() {
67             listenerRegistration.close();
68         }
69     }
70
71     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
72
73         @Override
74         protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
75                 final ClassToInstanceMap<DOMService> delegates) {
76             final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
77             return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification);
78         }
79
80         @Override
81         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
82             return ImmutableSet.of(DOMNotificationService.class);
83         }
84     }
85
86     /**
87      * Get DOM notification service.
88      *
89      * @return DOM notification service
90      */
91     public DOMNotificationService getDomService() {
92         return domNotifService;
93     }
94 }
95