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.impl.schema.tree;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import java.util.Optional;
15 import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
21 import org.opendaylight.yangtools.yang.data.impl.schema.tree.AbstractNodeContainerModificationStrategy.Visible;
22 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Base strategy for applying changes to a ContainerNode, irrespective of its
33 * @param <T> Type of the container node
35 class DataNodeContainerModificationStrategy<T extends DataNodeContainer & WithStatus> extends Visible<T> {
36 private static final Logger LOG = LoggerFactory.getLogger(DataNodeContainerModificationStrategy.class);
38 private final @NonNull DataTreeConfiguration treeConfig;
40 @SuppressWarnings("rawtypes")
41 private static final AtomicReferenceFieldUpdater<DataNodeContainerModificationStrategy, ImmutableMap> UPDATER =
42 AtomicReferenceFieldUpdater.newUpdater(DataNodeContainerModificationStrategy.class, ImmutableMap.class,
44 private volatile ImmutableMap<PathArgument, ModificationApplyOperation> children = ImmutableMap.of();
46 DataNodeContainerModificationStrategy(final NormalizedNodeContainerSupport<?, ?> support, final T schema,
47 final DataTreeConfiguration treeConfig) {
48 super(support, treeConfig, schema);
49 this.treeConfig = requireNonNull(treeConfig, "treeConfig");
53 public final Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
54 final ImmutableMap<PathArgument, ModificationApplyOperation> local = children;
55 final ModificationApplyOperation existing = local.get(identifier);
56 if (existing != null) {
57 return Optional.of(existing);
60 final ModificationApplyOperation childOperation = resolveChild(identifier);
61 return childOperation != null ? appendChild(local, identifier, childOperation) : Optional.empty();
64 private ModificationApplyOperation resolveChild(final PathArgument identifier) {
65 final T schema = getSchema();
66 if (identifier instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
67 return SchemaAwareApplyOperation.from(schema, (AugmentationTarget) schema,
68 (AugmentationIdentifier) identifier, treeConfig);
71 final QName qname = identifier.getNodeType();
72 final Optional<DataSchemaNode> child = schema.findDataChildByName(qname);
73 if (!child.isPresent()) {
74 LOG.trace("Child {} not present in container schema {} children {}", identifier, this,
75 schema.getChildNodes());
80 return SchemaAwareApplyOperation.from(child.get(), treeConfig);
81 } catch (ExcludedDataSchemaNodeException e) {
82 LOG.trace("Failed to instantiate child {} in container schema {} children {}", identifier, this,
83 schema.getChildNodes(), e);
88 private Optional<ModificationApplyOperation> appendChild(
89 final ImmutableMap<PathArgument, ModificationApplyOperation> initial, final PathArgument identifier,
90 final ModificationApplyOperation computed) {
92 ImmutableMap<PathArgument, ModificationApplyOperation> previous = initial;
94 // Build up a new map based on observed snapshot and computed child
95 final Builder<PathArgument, ModificationApplyOperation> builder = ImmutableMap.builderWithExpectedSize(
97 builder.putAll(previous);
98 builder.put(identifier, computed);
99 final ImmutableMap<PathArgument, ModificationApplyOperation> updated = builder.build();
101 // Attempt to install the updated map
102 if (UPDATER.compareAndSet(this, previous, updated)) {
103 return Optional.of(computed);
106 // We have raced, acquire a new snapshot, recheck presence and retry if needed
108 final ModificationApplyOperation raced = previous.get(identifier);
110 return Optional.of(raced);