Deprecate old MD-SAL APIs for removal
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / compat / FunctionalNotificationListenerAdapter.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.compat;
9
10 import org.opendaylight.controller.md.sal.binding.impl.LazySerializedDOMNotification;
11 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
12 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
13 import org.opendaylight.controller.sal.binding.api.NotificationListener;
14 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
15 import org.opendaylight.yangtools.yang.binding.Notification;
16
17 @Deprecated(forRemoval = true)
18 final class FunctionalNotificationListenerAdapter<N extends Notification> implements DOMNotificationListener {
19
20     private final BindingNormalizedNodeSerializer codec;
21     private final NotificationListener<N> delegate;
22     private final Class<N> type;
23
24     FunctionalNotificationListenerAdapter(final BindingNormalizedNodeSerializer codec, final Class<N> type,
25             final NotificationListener<N> delegate) {
26         this.codec = codec;
27         this.type = type;
28         this.delegate = delegate;
29     }
30
31     @Override
32     public void onNotification(final DOMNotification notification) {
33         delegate.onNotification(type.cast(deserialize(notification)));
34     }
35
36     private Notification deserialize(final DOMNotification notification) {
37         if (notification instanceof LazySerializedDOMNotification) {
38             return ((LazySerializedDOMNotification) notification).getBindingData();
39         }
40         return codec.fromNormalizedNodeNotification(notification.getType(), notification.getBody());
41     }
42 }