Renamed controller.md.sal.binding.impl to mdsal.binding.dom.adapter
[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 org.opendaylight.mdsal.binding.dom.adapter.BindingDOMAdapterBuilder.Factory;
11
12 import com.google.common.collect.ClassToInstanceMap;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Set;
15 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
16 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMService;
18 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
19 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.binding.NotificationListener;
22
23 public class BindingDOMNotificationServiceAdapter implements NotificationService, AutoCloseable {
24
25     public static final Factory<NotificationService> BUILDER_FACTORY = new Factory<NotificationService>() {
26
27         @Override
28         public BindingDOMAdapterBuilder<NotificationService> newBuilder() {
29             return new Builder();
30         }
31
32     };
33     private final BindingNormalizedNodeSerializer codec;
34     private final DOMNotificationService domNotifService;
35
36     public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec, final DOMNotificationService domNotifService) {
37         this.codec = codec;
38         this.domNotifService = domNotifService;
39     }
40
41     @Override
42     public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
43         final BindingDOMNotificationListenerAdapter domListener = new BindingDOMNotificationListenerAdapter(codec, listener);
44         final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
45                 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
46         return new ListenerRegistrationImpl<>(listener, domRegistration);
47     }
48
49     @Override
50     public void close() throws Exception {
51
52     }
53
54     private static class ListenerRegistrationImpl<T extends NotificationListener> extends AbstractListenerRegistration<T> {
55         private final ListenerRegistration<?> listenerRegistration;
56
57         public ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
58             super(listener);
59             this.listenerRegistration = listenerRegistration;
60         }
61
62         @Override
63         protected void removeRegistration() {
64             listenerRegistration.close();
65         }
66     }
67
68     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
69
70         @Override
71         protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
72                 final ClassToInstanceMap<DOMService> delegates) {
73             final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
74             return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification);
75         }
76
77         @Override
78         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
79             return ImmutableSet.of(DOMNotificationService.class);
80         }
81     }
82
83     public DOMNotificationService getDomService() {
84         return domNotifService;
85     }
86 }