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 java.util.Objects.requireNonNull;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
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.PathArgument;
18 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
22 abstract class DataContainerContextNode<T extends PathArgument> extends AbstractInteriorContextNode<T> {
23 private final ConcurrentMap<PathArgument, DataSchemaContextNode<?>> byArg = new ConcurrentHashMap<>();
24 private final ConcurrentMap<QName, DataSchemaContextNode<?>> byQName = new ConcurrentHashMap<>();
25 private final DataNodeContainer container;
27 DataContainerContextNode(final T identifier, final DataNodeContainer container, final DataSchemaNode schema) {
28 super(identifier, schema);
29 this.container = requireNonNull(container);
33 public DataSchemaContextNode<?> getChild(final PathArgument child) {
34 DataSchemaContextNode<?> potential = byArg.get(child);
35 if (potential != null) {
38 potential = fromLocalSchema(child);
39 return register(potential);
43 public DataSchemaContextNode<?> getChild(final QName child) {
44 DataSchemaContextNode<?> potential = byQName.get(child);
45 if (potential != null) {
48 potential = fromLocalSchemaAndQName(container, child);
49 return register(potential);
53 protected final DataSchemaContextNode<?> enterChild(final QName child, final SchemaInferenceStack stack) {
54 return pushToStack(getChild(child), stack);
59 protected final DataSchemaContextNode<?> enterChild(final PathArgument child, final SchemaInferenceStack stack) {
60 return pushToStack(getChild(child), stack);
63 private static @Nullable DataSchemaContextNode<?> pushToStack(final @Nullable DataSchemaContextNode<?> child,
64 final @NonNull SchemaInferenceStack stack) {
66 child.pushToStack(stack);
71 private DataSchemaContextNode<?> fromLocalSchema(final PathArgument child) {
72 return fromSchemaAndQNameChecked(container, child.getNodeType());
75 protected DataSchemaContextNode<?> fromLocalSchemaAndQName(final DataNodeContainer schema, final QName child) {
76 return fromSchemaAndQNameChecked(schema, child);
79 private DataSchemaContextNode<?> register(final DataSchemaContextNode<?> potential) {
80 if (potential != null) {
81 // FIXME: use putIfAbsent() to make sure we do not perform accidental overrwrites
82 byArg.put(potential.getIdentifier(), potential);
83 for (QName qname : potential.getQNameIdentifiers()) {
84 byQName.put(qname, potential);