2 * Copyright (c) 2014 Cisco Systems, Inc. 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.tree.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.VarHandle;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
20 import org.opendaylight.yangtools.yang.data.tree.impl.AbstractNodeContainerModificationStrategy.Visible;
21 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Base strategy for applying changes to a ContainerNode, irrespective of its
31 * @param <T> Type of the container node
33 class DataNodeContainerModificationStrategy<T extends DataNodeContainer & WithStatus> extends Visible<T> {
34 private static final Logger LOG = LoggerFactory.getLogger(DataNodeContainerModificationStrategy.class);
35 private static final VarHandle CHILDREN;
39 CHILDREN = MethodHandles.lookup().findVarHandle(
40 DataNodeContainerModificationStrategy.class, "children", ImmutableMap.class);
41 } catch (NoSuchFieldException | IllegalAccessException e) {
42 throw new ExceptionInInitializerError(e);
46 private final @NonNull DataTreeConfiguration treeConfig;
48 @SuppressWarnings("unused")
49 private volatile ImmutableMap<PathArgument, ModificationApplyOperation> children = ImmutableMap.of();
51 DataNodeContainerModificationStrategy(final NormalizedNodeContainerSupport<?, ?> support, final T schema,
52 final DataTreeConfiguration treeConfig) {
53 super(support, treeConfig, schema);
54 this.treeConfig = requireNonNull(treeConfig, "treeConfig");
58 public final ModificationApplyOperation childByArg(final PathArgument arg) {
59 final var local = (ImmutableMap<PathArgument, ModificationApplyOperation>) CHILDREN.getAcquire(this);
60 final var existing = local.get(arg);
61 if (existing != null) {
65 final var childOperation = resolveChild(arg);
66 return childOperation != null ? appendChild(local, arg, childOperation) : null;
69 private ModificationApplyOperation resolveChild(final PathArgument identifier) {
70 final T schema = getSchema();
71 if (identifier instanceof AugmentationIdentifier augId && schema instanceof AugmentationTarget augTarget) {
72 return SchemaAwareApplyOperation.from(schema, augTarget, augId, treeConfig);
75 final var qname = identifier.getNodeType();
76 final var child = schema.dataChildByName(qname);
78 LOG.trace("Child {} not present in container schema {} children {}", identifier, this,
79 schema.getChildNodes());
84 return SchemaAwareApplyOperation.from(child, treeConfig);
85 } catch (ExcludedDataSchemaNodeException e) {
86 LOG.trace("Failed to instantiate child {} in container schema {} children {}", identifier, this,
87 schema.getChildNodes(), e);
92 private @Nullable ModificationApplyOperation appendChild(
93 final ImmutableMap<PathArgument, ModificationApplyOperation> initial, final PathArgument identifier,
94 final ModificationApplyOperation computed) {
95 var previous = initial;
97 // Build up a new map based on observed snapshot and computed child
98 final var updated = ImmutableMap
99 .<PathArgument, ModificationApplyOperation>builderWithExpectedSize(previous.size() + 1)
101 .put(identifier, computed)
104 // Attempt to install the updated map
105 final var witness = (ImmutableMap<PathArgument, ModificationApplyOperation>)
106 CHILDREN.compareAndExchangeRelease(this, previous, updated);
107 if (witness == previous) {
111 // We have raced, acquire a new snapshot, recheck presence and retry if needed
113 final var raced = previous.get(identifier);