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