47f1e66b4e33527fbf556c7d388b7538c6753e43
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ChoiceModificationStrategy.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 com.google.common.base.Optional;
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Map;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
17 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
18 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
19 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
20 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableChoiceNodeBuilder;
21 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24
25 final class ChoiceModificationStrategy extends AbstractNodeContainerModificationStrategy {
26     private final Map<YangInstanceIdentifier.PathArgument, ModificationApplyOperation> childNodes;
27
28     ChoiceModificationStrategy(final ChoiceSchemaNode schemaNode, final TreeType treeType) {
29         super(ChoiceNode.class, treeType);
30         final ImmutableMap.Builder<YangInstanceIdentifier.PathArgument, ModificationApplyOperation> child = ImmutableMap.builder();
31
32         for (final ChoiceCaseNode caze : schemaNode.getCases()) {
33             if (SchemaAwareApplyOperation.belongsToTree(treeType,caze)) {
34                 for (final DataSchemaNode cazeChild : caze.getChildNodes()) {
35                     if (SchemaAwareApplyOperation.belongsToTree(treeType,cazeChild)) {
36                         final ModificationApplyOperation childNode = SchemaAwareApplyOperation.from(cazeChild, treeType);
37                         child.put(NodeIdentifier.create(cazeChild.getQName()), childNode);
38                     }
39                 }
40             }
41         }
42         childNodes = child.build();
43     }
44
45     @Override
46     public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument child) {
47         return Optional.fromNullable(childNodes.get(child));
48     }
49
50     @Override
51     @SuppressWarnings("rawtypes")
52     protected DataContainerNodeBuilder createBuilder(final NormalizedNode<?, ?> original) {
53         checkArgument(original instanceof ChoiceNode);
54         return ImmutableChoiceNodeBuilder.create((ChoiceNode) original);
55     }
56 }