2 * Copyright (c) 2015 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.util;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.collect.ImmutableMap;
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;
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;
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();
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.qnameIdentifiers()) {
41 byQNameBuilder.put(qname, childOp);
46 childToCase = childToCaseBuilder.build();
47 byQName = byQNameBuilder.build();
48 byArg = byArgBuilder.build();
52 public DataSchemaContextNode<?> getChild(final PathArgument child) {
53 return byArg.get(child);
57 public DataSchemaContextNode<?> getChild(final QName child) {
58 return byQName.get(child);
62 Set<QName> qnameIdentifiers() {
63 return byQName.keySet();
67 protected DataSchemaContextNode<?> enterChild(final QName child, final SchemaInferenceStack stack) {
68 return pushToStack(getChild(child), stack);
72 protected DataSchemaContextNode<?> enterChild(final PathArgument child, final SchemaInferenceStack stack) {
73 return pushToStack(getChild(child), stack);
77 void pushToStack(final @NonNull SchemaInferenceStack stack) {
78 stack.enterChoice(getIdentifier().getNodeType());
81 private @Nullable DataSchemaContextNode<?> pushToStack(final @Nullable DataSchemaContextNode<?> child,
82 final @NonNull SchemaInferenceStack stack) {
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);