94003e15033603c7b686322e4c89f0aa92972d52
[yangtools.git] / model / yang-model-spi / src / main / java / org / opendaylight / yangtools / yang / model / spi / DefaultSchemaTreeInference.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.model.spi;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
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;
24 import org.opendaylight.yangtools.yang.model.spi.AbstractEffectiveStatementInference.WithPath;
25
26 /**
27  * Default implementation of a a {@link SchemaTreeInference}. Guaranteed to be consistent with its
28  * {@link #getEffectiveModelContext()}.
29  */
30 @Beta
31 @NonNullByDefault
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);
37     }
38
39     /**
40      * Create a new instance.
41      *
42      * @param modelContext Associated {@link EffectiveModelContext}
43      * @param path An absolute schema node identifier
44      * @return A new instance
45      */
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));
51
52         final ImmutableList.Builder<SchemaTreeEffectiveStatement<?>> builder =
53             ImmutableList.builderWithExpectedSize(steps.size());
54         SchemaTreeAwareEffectiveStatement<?, ?> parent = module;
55         final Iterator<QName> it = steps.iterator();
56         while (true) {
57             final QName qname = it.next();
58             final SchemaTreeEffectiveStatement<?> found = parent.findSchemaTreeNode(qname).orElseThrow(
59                 () -> new IllegalArgumentException("Cannot resolve step " + qname + " in " + builder.build()));
60             builder.add(found);
61
62             if (it.hasNext()) {
63                 checkArgument(found instanceof SchemaTreeAwareEffectiveStatement, "Cannot resolve steps %s past %s",
64                     steps, found);
65                 parent = (SchemaTreeAwareEffectiveStatement<?, ?>) found;
66             } else {
67                 break;
68             }
69         }
70
71         return new DefaultSchemaTreeInference(modelContext, builder.build());
72     }
73 }