ac76ba09921fecfbce746b1d8c562bdc7d2f843d
[yangtools.git] / data / yang-data-tree-ri / src / main / java / org / opendaylight / yangtools / yang / data / tree / impl / DataNodeContainerModificationStrategy.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.yangtools.yang.data.tree.impl;
9
10 import static java.util.Objects.requireNonNull;
11
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;
26
27 /**
28  * Base strategy for applying changes to a ContainerNode, irrespective of its
29  * actual type.
30  *
31  * @param <T> Type of the container node
32  */
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;
36
37     static {
38         try {
39             CHILDREN = MethodHandles.lookup().findVarHandle(
40                 DataNodeContainerModificationStrategy.class, "children", ImmutableMap.class);
41         } catch (NoSuchFieldException | IllegalAccessException e) {
42             throw new ExceptionInInitializerError(e);
43         }
44     }
45
46     private final @NonNull DataTreeConfiguration treeConfig;
47
48     @SuppressWarnings("unused")
49     private volatile ImmutableMap<PathArgument, ModificationApplyOperation> children = ImmutableMap.of();
50
51     DataNodeContainerModificationStrategy(final NormalizedNodeContainerSupport<?, ?> support, final T schema,
52             final DataTreeConfiguration treeConfig) {
53         super(support, treeConfig, schema);
54         this.treeConfig = requireNonNull(treeConfig, "treeConfig");
55     }
56
57     @Override
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) {
62             return existing;
63         }
64
65         final var childOperation = resolveChild(arg);
66         return childOperation != null ? appendChild(local, arg, childOperation) : null;
67     }
68
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);
73         }
74
75         final var qname = identifier.getNodeType();
76         final var child = schema.dataChildByName(qname);
77         if (child == null) {
78             LOG.trace("Child {} not present in container schema {} children {}", identifier, this,
79                 schema.getChildNodes());
80             return null;
81         }
82
83         try {
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);
88             return null;
89         }
90     }
91
92     private @Nullable ModificationApplyOperation appendChild(
93             final ImmutableMap<PathArgument, ModificationApplyOperation> initial, final PathArgument identifier,
94             final ModificationApplyOperation computed) {
95         var previous = initial;
96         while (true) {
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)
100                 .putAll(previous)
101                 .put(identifier, computed)
102                 .build();
103
104             // Attempt to install the updated map
105             final var witness = (ImmutableMap<PathArgument, ModificationApplyOperation>)
106                 CHILDREN.compareAndExchangeRelease(this, previous, updated);
107             if (witness == previous) {
108                 return computed;
109             }
110
111             // We have raced, acquire a new snapshot, recheck presence and retry if needed
112             previous = witness;
113             final var raced = previous.get(identifier);
114             if (raced != null) {
115                 return raced;
116             }
117         }
118     }
119 }