2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.nodes;
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import java.util.Collection;
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;
29 * Support utilities for dealing with Maps which would normally hold {@link DataContainerChild} values, but are modified
30 * to eliminate {@link LeafNode} instances.
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:
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>
41 * Selection of the mode in effect is available through {@value #EXPENDABLE_PROP_NAME} system property.
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";
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}
54 private static final boolean EXPENDABLE;
57 EXPENDABLE = Boolean.parseBoolean(System.getProperty(EXPENDABLE_PROP_NAME, "true"));
58 LOG.info("Leaf nodes are treated as {} nodes", EXPENDABLE ? "transient" : "regular");
61 private LazyLeafOperations() {
66 * A boolean flag indicating whether leaf nodes are being treated as expendable.
68 * @return True if NormalizedNode implementations in this artifact are treating leaf nodes as transient, i.e. do
71 public static boolean isEnabled() {
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));
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);
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);
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();
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);
104 private static @Nullable DataContainerChild<?, ?> decodeChild(final PathArgument key, final @NonNull Object value) {
105 return EXPENDABLE ? decodeExpendableChild(key, value) : verifyCast(value);
108 private static @NonNull DataContainerChild<?, ?> decodeExpendableChild(final PathArgument key,
109 @NonNull final Object value) {
110 return value instanceof DataContainerChild ? (DataContainerChild<?, ?>) value : coerceLeaf(key, value);
113 private static @NonNull Object encodeExpendableChild(final @NonNull DataContainerChild<?, ?> node) {
114 return node instanceof LeafNode ? verifyEncode(((LeafNode<?>) node).getValue()) : node;
117 private static @NonNull Object verifyEncode(final @NonNull Object value) {
118 verify(!(value instanceof DataContainerChild), "Unexpected leaf value %s", value);
122 private static @NonNull DataContainerChild<?, ?> verifyCast(final @NonNull Object value) {
123 verify(value instanceof DataContainerChild, "Unexpected child %s", value);
124 return (DataContainerChild<?, ?>)value;