Add utility wrappers for instantiating builders/nodes
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / 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.impl.schema.tree;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.base.MoreObjects.ToStringHelper;
14 import com.google.common.cache.CacheBuilder;
15 import com.google.common.cache.CacheLoader;
16 import com.google.common.cache.LoadingCache;
17 import com.google.common.util.concurrent.UncheckedExecutionException;
18 import java.util.Optional;
19 import java.util.concurrent.ExecutionException;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
24 import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Base strategy for applying changes to a ContainerNode, irrespective of its
32  * actual type.
33  *
34  * @param <T> Type of the container node
35  */
36 class DataNodeContainerModificationStrategy<T extends DataNodeContainer>
37         extends AbstractNodeContainerModificationStrategy {
38     private static final Logger LOG = LoggerFactory.getLogger(DataNodeContainerModificationStrategy.class);
39
40     private final LoadingCache<PathArgument, ModificationApplyOperation> childCache = CacheBuilder.newBuilder()
41             .build(new CacheLoader<PathArgument, ModificationApplyOperation>() {
42                 @Override
43                 public ModificationApplyOperation load(@Nonnull final PathArgument key) {
44                     if (key instanceof AugmentationIdentifier && schema instanceof AugmentationTarget) {
45                         return SchemaAwareApplyOperation.from(schema, (AugmentationTarget) schema,
46                             (AugmentationIdentifier) key, treeConfig);
47                     }
48
49                     final DataSchemaNode child = schema.getDataChildByName(key.getNodeType());
50                     checkArgument(child != null, "Schema %s does not have a node for child %s", schema,
51                             key.getNodeType());
52                     return SchemaAwareApplyOperation.from(child, treeConfig);
53                 }
54             });
55
56     private final DataTreeConfiguration treeConfig;
57     private final T schema;
58
59     DataNodeContainerModificationStrategy(final NormalizedNodeContainerSupport<?, ?> support, final T schema,
60             final DataTreeConfiguration treeConfig) {
61         super(support, treeConfig);
62         this.schema = requireNonNull(schema, "schema");
63         this.treeConfig = requireNonNull(treeConfig, "treeConfig");
64     }
65
66     @Override
67     public final Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
68         try {
69             return Optional.ofNullable(childCache.get(identifier));
70         } catch (ExecutionException | UncheckedExecutionException e) {
71             LOG.trace("Child {} not present in container schema {} children {}", identifier, this,
72                 schema.getChildNodes(), e);
73             return Optional.empty();
74         }
75     }
76
77     @Override
78     ToStringHelper addToStringAttributes(final ToStringHelper helper) {
79         return super.addToStringAttributes(helper).add("schema", schema);
80     }
81 }