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 / AbstractForwardedDataBroker.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.binding.impl;
9
10 import java.util.HashMap;
11 import java.util.HashSet;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.Optional;
15 import java.util.Set;
16 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
17 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
18 import org.opendaylight.yangtools.concepts.Delegator;
19 import org.opendaylight.yangtools.yang.binding.DataObject;
20 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 @Deprecated(forRemoval = true)
28 public abstract class AbstractForwardedDataBroker implements Delegator<DOMDataBroker>, AutoCloseable {
29
30     private static final Logger LOG = LoggerFactory.getLogger(AbstractForwardedDataBroker.class);
31     // The Broker to whom we do all forwarding
32     private final DOMDataBroker domDataBroker;
33
34     private final BindingToNormalizedNodeCodec codec;
35
36     protected AbstractForwardedDataBroker(final DOMDataBroker domDataBroker, final BindingToNormalizedNodeCodec codec,
37             final DOMSchemaService schemaService) {
38         this.domDataBroker = domDataBroker;
39         this.codec = codec;
40     }
41
42     protected AbstractForwardedDataBroker(final DOMDataBroker domDataBroker, final BindingToNormalizedNodeCodec codec) {
43         this.domDataBroker = domDataBroker;
44         this.codec = codec;
45     }
46
47     protected BindingToNormalizedNodeCodec getCodec() {
48         return codec;
49     }
50
51     @Override
52     public DOMDataBroker getDelegate() {
53         return domDataBroker;
54     }
55
56     protected Map<InstanceIdentifier<?>, DataObject> toBinding(final InstanceIdentifier<?> path,
57             final Map<YangInstanceIdentifier, ? extends NormalizedNode<?, ?>> normalized) {
58         final Map<InstanceIdentifier<?>, DataObject> newMap = new HashMap<>();
59
60         for (final Map.Entry<YangInstanceIdentifier, ? extends NormalizedNode<?, ?>> entry : normalized.entrySet()) {
61             try {
62                 final Optional<Entry<InstanceIdentifier<? extends DataObject>, DataObject>> potential =
63                         getCodec().toBinding(entry);
64                 if (potential.isPresent()) {
65                     final Entry<InstanceIdentifier<? extends DataObject>, DataObject> binding = potential.get();
66                     newMap.put(binding.getKey(), binding.getValue());
67                 }
68             } catch (final DeserializationException e) {
69                 LOG.warn("Failed to transform {}, omitting it", entry, e);
70             }
71         }
72         return newMap;
73     }
74
75     protected Set<InstanceIdentifier<?>> toBinding(final InstanceIdentifier<?> path,
76             final Set<YangInstanceIdentifier> normalized) {
77         final Set<InstanceIdentifier<?>> hashSet = new HashSet<>();
78         for (final YangInstanceIdentifier normalizedPath : normalized) {
79             try {
80                 final Optional<InstanceIdentifier<? extends DataObject>> potential =
81                         getCodec().toBinding(normalizedPath);
82                 if (potential.isPresent()) {
83                     final InstanceIdentifier<? extends DataObject> binding = potential.get();
84                     hashSet.add(binding);
85                 } else if (normalizedPath.getLastPathArgument()
86                         instanceof YangInstanceIdentifier.AugmentationIdentifier) {
87                     hashSet.add(path);
88                 }
89             } catch (final DeserializationException e) {
90                 LOG.warn("Failed to transform {}, omitting it", normalizedPath, e);
91             }
92         }
93         return hashSet;
94     }
95
96     protected Optional<DataObject> toBindingData(final InstanceIdentifier<?> path, final NormalizedNode<?, ?> data) {
97         if (path.isWildcarded()) {
98             return Optional.empty();
99         }
100         return (Optional<DataObject>) getCodec().deserializeFunction(path)
101                 .apply(Optional.<NormalizedNode<?, ?>>of(data));
102     }
103
104     @Override
105     public void close() {
106     }
107 }

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