Deprecate old MD-SAL APIs for removal
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / md / sal / binding / impl / BindingDOMDataTreeChangeServiceAdapter.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 static java.util.Objects.requireNonNull;
11
12 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
13 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
14 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeService;
15 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeIdentifier;
18 import org.opendaylight.yangtools.concepts.ListenerRegistration;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21
22 /**
23  * Adapter exposing Binding {@link DataTreeChangeService} and wrapping
24  * {@link DOMDataTreeChangeService} and is responsible for translation
25  * and instantiation of {@link BindingDOMDataTreeChangeListenerAdapter}
26  * adapters.
27  *
28  * <p>
29  * Each registered {@link DataTreeChangeListener} is wrapped using
30  * adapter and registered directly to DOM service.
31  */
32 @Deprecated(forRemoval = true)
33 final class BindingDOMDataTreeChangeServiceAdapter implements DataTreeChangeService {
34
35     private final BindingToNormalizedNodeCodec codec;
36     private final DOMDataTreeChangeService dataTreeChangeService;
37
38     private BindingDOMDataTreeChangeServiceAdapter(final BindingToNormalizedNodeCodec codec,
39             final DOMDataTreeChangeService dataTreeChangeService) {
40         this.codec = requireNonNull(codec);
41         this.dataTreeChangeService = requireNonNull(dataTreeChangeService);
42     }
43
44     static DataTreeChangeService create(final BindingToNormalizedNodeCodec codec,
45             final DOMDataTreeChangeService dataTreeChangeService) {
46         return new BindingDOMDataTreeChangeServiceAdapter(codec, dataTreeChangeService);
47     }
48
49     @Override
50     public <T extends DataObject, L extends DataTreeChangeListener<T>> ListenerRegistration<L>
51             registerDataTreeChangeListener(final DataTreeIdentifier<T> treeId, final L listener) {
52         final DOMDataTreeIdentifier domIdentifier = toDomTreeIdentifier(treeId);
53
54         @SuppressWarnings({ "rawtypes", "unchecked" })
55         final BindingDOMDataTreeChangeListenerAdapter<T> domListener =
56                 listener instanceof ClusteredDataTreeChangeListener
57                         ? new BindingClusteredDOMDataTreeChangeListenerAdapter<>(
58                                 codec, (ClusteredDataTreeChangeListener) listener, treeId.getDatastoreType())
59                         : new BindingDOMDataTreeChangeListenerAdapter<>(codec, listener, treeId.getDatastoreType());
60
61         final ListenerRegistration<BindingDOMDataTreeChangeListenerAdapter<T>> domReg =
62                 dataTreeChangeService.registerDataTreeChangeListener(domIdentifier, domListener);
63         return new BindingDataTreeChangeListenerRegistration<>(listener,domReg);
64     }
65
66     private DOMDataTreeIdentifier toDomTreeIdentifier(final DataTreeIdentifier<?> treeId) {
67         final YangInstanceIdentifier domPath = codec.toYangInstanceIdentifierBlocking(treeId.getRootIdentifier());
68         return new DOMDataTreeIdentifier(treeId.getDatastoreType(), domPath);
69     }
70 }

©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.