Add SchemaTreeInference
[yangtools.git] / yang / 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
25 /**
26  * Default implementation of a a {@link SchemaTreeInference}. Guaranteed to be consistent with its
27  * {@link #getEffectiveModelContext()}.
28  */
29 @Beta
30 @NonNullByDefault
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);
36     }
37
38     /**
39      * Create a new instance.
40      *
41      * @param modelContext Associated {@link EffectiveModelContext}
42      * @param path An absolute schema node identifier
43      * @return A new instance
44      */
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));
50
51         final ImmutableList.Builder<SchemaTreeEffectiveStatement<?>> builder =
52             ImmutableList.builderWithExpectedSize(steps.size());
53         SchemaTreeAwareEffectiveStatement<?, ?> parent = module;
54         final Iterator<QName> it = steps.iterator();
55         while (true) {
56             final QName qname = it.next();
57             final SchemaTreeEffectiveStatement<?> found = parent.findSchemaTreeNode(qname).orElseThrow(
58                 () -> new IllegalArgumentException("Cannot resolve step " + qname + " in " + builder.build()));
59             if (it.hasNext()) {
60                 checkArgument(found instanceof SchemaTreeAwareEffectiveStatement, "Cannot resolve steps %s past %s",
61                     steps, found);
62                 parent = (SchemaTreeAwareEffectiveStatement<?, ?>) found;
63             } else {
64                 break;
65             }
66         }
67
68         return new DefaultSchemaTreeInference(modelContext, builder.build());
69     }
70 }