BUG-4295: instantiate MERGE operations lazily
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / AugmentationModificationStrategy.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
12 import java.util.HashSet;
13 import java.util.Set;
14 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableAugmentationNodeBuilder;
19 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
20 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
23
24 final class AugmentationModificationStrategy extends AbstractDataNodeContainerModificationStrategy<AugmentationSchema> {
25     AugmentationModificationStrategy(final AugmentationSchema schema, final DataNodeContainer resolved, final TreeType treeType) {
26         super(createAugmentProxy(schema,resolved), AugmentationNode.class, treeType);
27     }
28
29     @Override
30     @SuppressWarnings("rawtypes")
31     protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
32         checkArgument(original instanceof AugmentationNode);
33         return ImmutableAugmentationNodeBuilder.create((AugmentationNode) original);
34     }
35
36     @Override
37     protected NormalizedNode<?, ?> createEmptyValue(NormalizedNode<?, ?> original) {
38         checkArgument(original instanceof AugmentationNode);
39         return ImmutableAugmentationNodeBuilder.create()
40                 .withNodeIdentifier(((AugmentationNode) original).getIdentifier()).build();
41     }
42
43     private static AugmentationSchema createAugmentProxy(final AugmentationSchema schema, final DataNodeContainer resolved) {
44         final Set<DataSchemaNode> realChildSchemas = new HashSet<>();
45         for(final DataSchemaNode augChild : schema.getChildNodes()) {
46             realChildSchemas.add(resolved.getDataChildByName(augChild.getQName()));
47         }
48         return new EffectiveAugmentationSchema(schema, realChildSchemas);
49     }
50 }