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