Add DataSchemaContextNode/SchemaInferenceStack integration
[yangtools.git] / data / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / ChoiceNodeContextNode.java
1 /*
2  * Copyright (c) 2015 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.util;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.util.Set;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
23
24 final class ChoiceNodeContextNode extends AbstractMixinContextNode<NodeIdentifier> {
25     private final ImmutableMap<PathArgument, DataSchemaContextNode<?>> byArg;
26     private final ImmutableMap<QName, DataSchemaContextNode<?>> byQName;
27     private final ImmutableMap<DataSchemaContextNode<?>, QName> childToCase;
28
29     ChoiceNodeContextNode(final ChoiceSchemaNode schema) {
30         super(NodeIdentifier.create(schema.getQName()), schema);
31         ImmutableMap.Builder<DataSchemaContextNode<?>, QName> childToCaseBuilder = ImmutableMap.builder();
32         ImmutableMap.Builder<QName, DataSchemaContextNode<?>> byQNameBuilder = ImmutableMap.builder();
33         ImmutableMap.Builder<PathArgument, DataSchemaContextNode<?>> byArgBuilder = ImmutableMap.builder();
34
35         for (CaseSchemaNode caze : schema.getCases()) {
36             for (DataSchemaNode cazeChild : caze.getChildNodes()) {
37                 DataSchemaContextNode<?> childOp = DataSchemaContextNode.of(cazeChild);
38                 byArgBuilder.put(childOp.getIdentifier(), childOp);
39                 childToCaseBuilder.put(childOp, caze.getQName());
40                 for (QName qname : childOp.getQNameIdentifiers()) {
41                     byQNameBuilder.put(qname, childOp);
42                 }
43             }
44         }
45
46         childToCase = childToCaseBuilder.build();
47         byQName = byQNameBuilder.build();
48         byArg = byArgBuilder.build();
49     }
50
51     @Override
52     public DataSchemaContextNode<?> getChild(final PathArgument child) {
53         return byArg.get(child);
54     }
55
56     @Override
57     public DataSchemaContextNode<?> getChild(final QName child) {
58         return byQName.get(child);
59     }
60
61     @Override
62     protected Set<QName> getQNameIdentifiers() {
63         return byQName.keySet();
64     }
65
66     @Override
67     protected DataSchemaContextNode<?> enterChild(final QName child, final SchemaInferenceStack stack) {
68         return pushToStack(getChild(child), stack);
69     }
70
71     @Override
72     protected DataSchemaContextNode<?> enterChild(final PathArgument child, final SchemaInferenceStack stack) {
73         return pushToStack(getChild(child), stack);
74     }
75
76     @Override
77     protected void pushToStack(final @NonNull SchemaInferenceStack stack) {
78         stack.enterChoice(getIdentifier().getNodeType());
79     }
80
81     private @Nullable DataSchemaContextNode<?> pushToStack(final @Nullable DataSchemaContextNode<?> child,
82             final @NonNull SchemaInferenceStack stack) {
83         if (child != null) {
84             final var caseName = verifyNotNull(childToCase.get(child), "No case statement for %s in %s", child, this);
85             stack.enterSchemaTree(caseName);
86             child.pushToStack(stack);
87         }
88         return child;
89     }
90 }