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