Do not provide SourceIdentifiers from extensions
[yangtools.git] / parser / yang-ir / src / main / java / org / opendaylight / yangtools / yang / ir / YangIRSchemaSource.java
1 /*
2  * Copyright (c) 2020 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.ir;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.ir.IRKeyword.Unqualified;
16 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
18
19 public final class YangIRSchemaSource implements YangSourceRepresentation {
20     private final @NonNull SourceIdentifier sourceId;
21     private final @NonNull IRStatement rootStatement;
22     private final @Nullable String symbolicName;
23
24     public YangIRSchemaSource(final @NonNull SourceIdentifier sourceId, final @NonNull IRStatement rootStatement,
25             final @Nullable String symbolicName) {
26         this.sourceId = requireNonNull(sourceId);
27         this.rootStatement = requireNonNull(rootStatement);
28         this.symbolicName = symbolicName;
29
30         final IRKeyword rootKeyword = rootStatement.keyword();
31         checkArgument(rootKeyword instanceof Unqualified, "Root statement has invalid keyword %s", rootKeyword);
32         final String rootName = rootKeyword.identifier();
33         switch (rootName) {
34             case "module":
35             case "submodule":
36                 break;
37             default:
38                 throw new IllegalArgumentException("Invalid root statement keyword " + rootName);
39         }
40
41         checkArgument(rootStatement.argument() != null, "Root statement does not have an argument");
42     }
43
44     @Override
45     public SourceIdentifier sourceId() {
46         return sourceId;
47     }
48
49     @Override
50     public String symbolicName() {
51         return symbolicName;
52     }
53
54     @Override
55     public Class<YangIRSchemaSource> getType() {
56         return YangIRSchemaSource.class;
57     }
58
59     /**
60      * Return the root statement of this source.
61      *
62      * @return Root statement.
63      */
64     public @NonNull IRStatement getRootStatement() {
65         return rootStatement;
66     }
67 }