Move mdsal-binding-dom codec classes
[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, AutoCloseable {
23
24     public static final Factory<NotificationService> BUILDER_FACTORY = new Factory<NotificationService>() {
25
26         @Override
27         public BindingDOMAdapterBuilder<NotificationService> newBuilder() {
28             return new Builder();
29         }
30
31     };
32     private final BindingNormalizedNodeSerializer codec;
33     private final DOMNotificationService domNotifService;
34
35     public BindingDOMNotificationServiceAdapter(final BindingNormalizedNodeSerializer codec,
36             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
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     @Override
51     public void close() throws Exception {
52
53     }
54
55     private static class ListenerRegistrationImpl<T extends NotificationListener>
56             extends AbstractListenerRegistration<T> {
57         private final ListenerRegistration<?> listenerRegistration;
58
59         ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
60             super(listener);
61             this.listenerRegistration = listenerRegistration;
62         }
63
64         @Override
65         protected void removeRegistration() {
66             listenerRegistration.close();
67         }
68     }
69
70     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
71
72         @Override
73         protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
74                 final ClassToInstanceMap<DOMService> delegates) {
75             final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
76             return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification);
77         }
78
79         @Override
80         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
81             return ImmutableSet.of(DOMNotificationService.class);
82         }
83     }
84
85     public DOMNotificationService getDomService() {
86         return domNotifService;
87     }
88 }