2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.model.spi;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Iterator;
15 import java.util.List;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.SchemaNodeIdentifier.Absolute;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.model.api.SchemaTreeInference;
21 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveStatementInference.WithPath;
27 * Default implementation of a a {@link SchemaTreeInference}. Guaranteed to be consistent with its
28 * {@link #getEffectiveModelContext()}.
32 public final class DefaultSchemaTreeInference extends WithPath<SchemaTreeEffectiveStatement<?>>
33 implements SchemaTreeInference {
34 private DefaultSchemaTreeInference(final EffectiveModelContext modelContext,
35 final ImmutableList<SchemaTreeEffectiveStatement<?>> path) {
36 super(modelContext, path);
40 * Create a new instance.
42 * @param modelContext Associated {@link EffectiveModelContext}
43 * @param path An absolute schema node identifier
44 * @return A new instance
46 public static DefaultSchemaTreeInference of(final EffectiveModelContext modelContext, final Absolute path) {
47 final List<QName> steps = path.getNodeIdentifiers();
48 final QName first = steps.get(0);
49 final ModuleEffectiveStatement module = modelContext.findModuleStatement(first.getModule()).orElseThrow(
50 () -> new IllegalArgumentException("No module for " + first));
52 final ImmutableList.Builder<SchemaTreeEffectiveStatement<?>> builder =
53 ImmutableList.builderWithExpectedSize(steps.size());
54 SchemaTreeAwareEffectiveStatement<?, ?> parent = module;
55 final Iterator<QName> it = steps.iterator();
57 final QName qname = it.next();
58 final SchemaTreeEffectiveStatement<?> found = parent.findSchemaTreeNode(qname).orElseThrow(
59 () -> new IllegalArgumentException("Cannot resolve step " + qname + " in " + builder.build()));
63 checkArgument(found instanceof SchemaTreeAwareEffectiveStatement, "Cannot resolve steps %s past %s",
65 parent = (SchemaTreeAwareEffectiveStatement<?, ?>) found;
71 return new DefaultSchemaTreeInference(modelContext, builder.build());