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.model.api.EffectiveModelContext;
19 import org.opendaylight.yangtools.yang.model.api.SchemaTreeInference;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
26 * Default implementation of a a {@link SchemaTreeInference}. Guaranteed to be consistent with its
27 * {@link #getEffectiveModelContext()}.
31 public final class DefaultSchemaTreeInference
32 extends AbstractEffectiveStatementInference<SchemaTreeEffectiveStatement<?>> implements SchemaTreeInference {
33 private DefaultSchemaTreeInference(final EffectiveModelContext modelContext,
34 final ImmutableList<SchemaTreeEffectiveStatement<?>> path) {
35 super(modelContext, path);
39 * Create a new instance.
41 * @param modelContext Associated {@link EffectiveModelContext}
42 * @param path An absolute schema node identifier
43 * @return A new instance
45 public static DefaultSchemaTreeInference of(final EffectiveModelContext modelContext, final Absolute path) {
46 final List<QName> steps = path.getNodeIdentifiers();
47 final QName first = steps.get(0);
48 final ModuleEffectiveStatement module = modelContext.findModuleStatement(first.getModule()).orElseThrow(
49 () -> new IllegalArgumentException("No module for " + first));
51 final ImmutableList.Builder<SchemaTreeEffectiveStatement<?>> builder =
52 ImmutableList.builderWithExpectedSize(steps.size());
53 SchemaTreeAwareEffectiveStatement<?, ?> parent = module;
54 final Iterator<QName> it = steps.iterator();
56 final QName qname = it.next();
57 final SchemaTreeEffectiveStatement<?> found = parent.findSchemaTreeNode(qname).orElseThrow(
58 () -> new IllegalArgumentException("Cannot resolve step " + qname + " in " + builder.build()));
60 checkArgument(found instanceof SchemaTreeAwareEffectiveStatement, "Cannot resolve steps %s past %s",
62 parent = (SchemaTreeAwareEffectiveStatement<?, ?>) found;
68 return new DefaultSchemaTreeInference(modelContext, builder.build());