X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=model%2Fyang-model-spi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fmodel%2Fspi%2FDefaultSchemaTreeInference.java;fp=model%2Fyang-model-spi%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fmodel%2Fspi%2FDefaultSchemaTreeInference.java;h=94003e15033603c7b686322e4c89f0aa92972d52;hb=b67b827e357ae40ae26e4bfec35b76ef5ee67967;hp=0000000000000000000000000000000000000000;hpb=9728fe497bcb7349f7e6ef9d3d984202d7ac07e7;p=yangtools.git diff --git a/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/DefaultSchemaTreeInference.java b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/DefaultSchemaTreeInference.java new file mode 100644 index 0000000000..94003e1503 --- /dev/null +++ b/model/yang-model-spi/src/main/java/org/opendaylight/yangtools/yang/model/spi/DefaultSchemaTreeInference.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.yang.model.spi; + +import static com.google.common.base.Preconditions.checkArgument; + +import com.google.common.annotations.Beta; +import com.google.common.collect.ImmutableList; +import java.util.Iterator; +import java.util.List; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.opendaylight.yangtools.yang.common.QName; +import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; +import org.opendaylight.yangtools.yang.model.api.SchemaTreeInference; +import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute; +import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement; +import org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveStatementInference.WithPath; + +/** + * Default implementation of a a {@link SchemaTreeInference}. Guaranteed to be consistent with its + * {@link #getEffectiveModelContext()}. + */ +@Beta +@NonNullByDefault +public final class DefaultSchemaTreeInference extends WithPath> + implements SchemaTreeInference { + private DefaultSchemaTreeInference(final EffectiveModelContext modelContext, + final ImmutableList> path) { + super(modelContext, path); + } + + /** + * Create a new instance. + * + * @param modelContext Associated {@link EffectiveModelContext} + * @param path An absolute schema node identifier + * @return A new instance + */ + public static DefaultSchemaTreeInference of(final EffectiveModelContext modelContext, final Absolute path) { + final List steps = path.getNodeIdentifiers(); + final QName first = steps.get(0); + final ModuleEffectiveStatement module = modelContext.findModuleStatement(first.getModule()).orElseThrow( + () -> new IllegalArgumentException("No module for " + first)); + + final ImmutableList.Builder> builder = + ImmutableList.builderWithExpectedSize(steps.size()); + SchemaTreeAwareEffectiveStatement parent = module; + final Iterator it = steps.iterator(); + while (true) { + final QName qname = it.next(); + final SchemaTreeEffectiveStatement found = parent.findSchemaTreeNode(qname).orElseThrow( + () -> new IllegalArgumentException("Cannot resolve step " + qname + " in " + builder.build())); + builder.add(found); + + if (it.hasNext()) { + checkArgument(found instanceof SchemaTreeAwareEffectiveStatement, "Cannot resolve steps %s past %s", + steps, found); + parent = (SchemaTreeAwareEffectiveStatement) found; + } else { + break; + } + } + + return new DefaultSchemaTreeInference(modelContext, builder.build()); + } +}