1ce252df98ca4e030ec4fdfc43dd5004a432d54a
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / compat / TranslatingListenerInvoker.java
1 /*
2  * Copyright (c) 2014 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.dom.broker.impl.compat;
9
10 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
11 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
12 import org.opendaylight.controller.md.sal.common.api.data.DataChangeEvent;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizer;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
17 import org.opendaylight.controller.sal.core.api.data.DataChangeListener;
18 import org.opendaylight.yangtools.concepts.Delegator;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23
24 abstract class TranslatingListenerInvoker implements AutoCloseable, DOMDataChangeListener, Delegator<DataChangeListener> {
25
26     private final DataChangeListener delegate;
27     private final DataNormalizer normalizer;
28     protected ListenerRegistration<DOMDataChangeListener> reg;
29
30     protected TranslatingListenerInvoker(final DataChangeListener listener, final DataNormalizer normalizer) {
31         this.delegate = listener;
32         this.normalizer = normalizer;
33     }
34
35     static TranslatingListenerInvoker createConfig(final DataChangeListener listener, final DataNormalizer normalizer) {
36         return new TranslatingConfigListenerInvoker(listener, normalizer);
37     }
38
39     static TranslatingListenerInvoker createOperational(final DataChangeListener listener, final DataNormalizer normalizer) {
40         return new TranslatingOperationalListenerInvoker(listener, normalizer);
41     }
42
43     @Override
44     public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> normalizedChange) {
45         delegate.onDataChanged(getLegacyEvent(normalizer, normalizedChange));
46     }
47
48     abstract DataChangeEvent<InstanceIdentifier, CompositeNode> getLegacyEvent(final DataNormalizer normalizer,
49                                                                                final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> normalizedChange);
50
51     @Override
52     public DataChangeListener getDelegate() {
53         return delegate;
54     }
55
56     abstract void register(final DOMDataBroker backingBroker, final InstanceIdentifier normalizedPath);
57
58     @Override
59     public void close() {
60         if (reg != null) {
61             reg.close();
62         }
63     }
64
65     static final class TranslatingConfigListenerInvoker extends TranslatingListenerInvoker {
66
67         public TranslatingConfigListenerInvoker(final DataChangeListener listener, final DataNormalizer normalizer) {
68             super(listener, normalizer);
69         }
70
71         DataChangeEvent<InstanceIdentifier, CompositeNode> getLegacyEvent(final DataNormalizer normalizer, final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> normalizedChange) {
72             return TranslatingDataChangeEvent.createConfiguration(normalizedChange, normalizer);
73         }
74
75         @Override
76         void register(final DOMDataBroker backingBroker, final InstanceIdentifier normalizedPath) {
77             reg = backingBroker.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION, normalizedPath, this,
78                     AsyncDataBroker.DataChangeScope.SUBTREE);
79         }
80     }
81
82     static final class TranslatingOperationalListenerInvoker extends TranslatingListenerInvoker {
83
84         public TranslatingOperationalListenerInvoker(final DataChangeListener listener, final DataNormalizer normalizer) {
85             super(listener, normalizer);
86         }
87
88         DataChangeEvent<InstanceIdentifier, CompositeNode> getLegacyEvent(final DataNormalizer normalizer, final AsyncDataChangeEvent<InstanceIdentifier, NormalizedNode<?, ?>> normalizedChange) {
89             return TranslatingDataChangeEvent.createOperational(normalizedChange, normalizer);
90         }
91
92         @Override
93         void register(final DOMDataBroker backingBroker, final InstanceIdentifier normalizedPath) {
94             reg = backingBroker.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL, normalizedPath, this,
95                     AsyncDataBroker.DataChangeScope.SUBTREE);
96         }
97     }
98
99 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.