Do not retain leaf nodes in containers by default
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / LazyLeafOperations.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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.yangtools.yang.data.impl.schema.nodes;
9
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.Collections2;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.Map.Entry;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
24 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
26 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Support utilities for dealing with Maps which would normally hold {@link DataContainerChild} values, but are modified
32  * to eliminate {@link LeafNode} instances.
33  *
34  * <p>
35  * This class holds implementation logic which controls lifecycle of {@link LeafNode}s  by providing a central policy
36  * point for how the implementation treats these nodes. There are two modes of operation:
37  * <ul>
38  *   <li>eager, which means leaf nodes are retained by their parent<li>
39  *   <li>lazy, which means leaf nodes are created whenever they are queried and no attempt is made to retain them</li>
40  * </ul>
41  *
42  * <p>
43  * Selection of the mode in effect is available through {@value #EXPENDABLE_PROP_NAME} system property.
44  */
45 @Beta
46 public final class LazyLeafOperations {
47     private static final Logger LOG = LoggerFactory.getLogger(LazyLeafOperations.class);
48     private static final String EXPENDABLE_PROP_NAME =
49             "org.opendaylight.yangtools.yang.data.impl.schema.nodes.lazy-leaves";
50
51     /**
52      * Global enabled run-time constant. If set to true, this class will treat {@link LeafNode} and
53      * {@link LeafSetEntryNode} as an expendable object. This constant is controlled by {@value #EXPENDABLE_PROP_NAME}
54      * system property.
55      */
56     private static final boolean EXPENDABLE;
57
58     static {
59         EXPENDABLE = Boolean.parseBoolean(System.getProperty(EXPENDABLE_PROP_NAME, "true"));
60         LOG.info("Leaf nodes are treated as {} nodes", EXPENDABLE ? "transient" : "regular");
61     }
62
63     private LazyLeafOperations() {
64
65     }
66
67     public static Optional<DataContainerChild<?, ?>> findChild(final Map<PathArgument, Object> map,
68             final PathArgument key) {
69         final Object value = map.get(key);
70         return value == null ? Optional.empty() : Optional.of(decodeChild(key, value));
71     }
72
73     public static @Nullable DataContainerChild<?, ?> getChild(final Map<PathArgument, Object> map,
74             final PathArgument key) {
75         final Object value = map.get(key);
76         return value == null ? null : decodeChild(key, value);
77     }
78
79     public static void putChild(final Map<PathArgument, Object> map, final DataContainerChild<?, ?> child) {
80         final DataContainerChild<?, ?> node = requireNonNull(child);
81         map.put(child.getIdentifier(), EXPENDABLE ? encodeExpendableChild(node) : node);
82     }
83
84     @SuppressWarnings({ "rawtypes", "unchecked" })
85     public static @NonNull Collection<DataContainerChild<?, ?>> getValue(final Map<PathArgument, Object> map) {
86         return EXPENDABLE ? Collections2.transform(map.entrySet(), LazyLeafOperations::decodeEntry)
87                 // This is an ugly cast, but it is accurate IFF all modifications are done through this class
88                 : (Collection) map.values();
89     }
90
91     private static @NonNull Object encodeExpendableChild(final DataContainerChild<?, ?> key) {
92         return key instanceof LeafNode ? ((LeafNode<?>) key).getValue() : requireNonNull(key);
93     }
94
95     private static @Nullable DataContainerChild<?, ?> decodeChild(final PathArgument key, final @NonNull Object value) {
96         return EXPENDABLE ? decodeExpendableChild(key, value) : verifyCast(value);
97     }
98
99     private static @NonNull DataContainerChild<?, ?> decodeExpendableChild(final PathArgument key,
100             @NonNull final Object value) {
101         return value instanceof DataContainerChild ? (DataContainerChild<?, ?>) value  : coerceLeaf(key, value);
102     }
103
104     private static @NonNull DataContainerChild<?, ?> verifyCast(final @NonNull Object value) {
105         verify(value instanceof DataContainerChild, "Unexpected child %s", value);
106         return (DataContainerChild<?, ?>)value;
107     }
108
109     private static @NonNull DataContainerChild<? extends PathArgument, ?> decodeEntry(
110             final @NonNull Entry<PathArgument, Object> entry) {
111         final Object value = entry.getValue();
112         return value instanceof DataContainerChild ? (DataContainerChild<?, ?>) value
113                 : coerceLeaf(entry.getKey(), value);
114     }
115
116     private static @NonNull LeafNode<?> coerceLeaf(final PathArgument key, final Object value) {
117         verify(key instanceof NodeIdentifier, "Unexpected value %s for child %s", value, key);
118         return ImmutableNodes.leafNode((NodeIdentifier) key, value);
119     }
120 }