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