Merge "BUG 3045 : Use non-strict parsing in hello message."
[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.yangtools.binding.data.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 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, final DOMNotificationService domNotifService) {
36         this.codec = codec;
37         this.domNotifService = domNotifService;
38     }
39
40     @Override
41     public <T extends NotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener) {
42         final BindingDOMNotificationListenerAdapter domListener = new BindingDOMNotificationListenerAdapter(codec, listener);
43         final ListenerRegistration<BindingDOMNotificationListenerAdapter> domRegistration =
44                 domNotifService.registerNotificationListener(domListener, domListener.getSupportedNotifications());
45         return new ListenerRegistrationImpl<>(listener, domRegistration);
46     }
47
48     @Override
49     public void close() throws Exception {
50
51     }
52
53     private static class ListenerRegistrationImpl<T extends NotificationListener> extends AbstractListenerRegistration<T> {
54         private final ListenerRegistration<?> listenerRegistration;
55
56         public ListenerRegistrationImpl(final T listener, final ListenerRegistration<?> listenerRegistration) {
57             super(listener);
58             this.listenerRegistration = listenerRegistration;
59         }
60
61         @Override
62         protected void removeRegistration() {
63             listenerRegistration.close();
64         }
65     }
66
67     private static class Builder extends BindingDOMAdapterBuilder<NotificationService> {
68
69         @Override
70         protected NotificationService createInstance(final BindingToNormalizedNodeCodec codec,
71                 final ClassToInstanceMap<DOMService> delegates) {
72             final DOMNotificationService domNotification = delegates.getInstance(DOMNotificationService.class);
73             return new BindingDOMNotificationServiceAdapter(codec.getCodecRegistry(), domNotification);
74         }
75
76         @Override
77         public Set<? extends Class<? extends DOMService>> getRequiredDelegates() {
78             return ImmutableSet.of(DOMNotificationService.class);
79         }
80     }
81 }