961c381ce3bc1430379bdc3f6219828e2502332a
[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.AbstractIdentifiable;
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 AbstractIdentifiable<SourceIdentifier> implements SchemaSourceRepresentation {
24     private final @NonNull IRStatement rootStatement;
25     private final @Nullable String symbolicName;
26
27     public IRSchemaSource(final @NonNull SourceIdentifier identifier, final @NonNull IRStatement rootStatement,
28             @Nullable final String symbolicName) {
29         super(identifier);
30         this.rootStatement = requireNonNull(rootStatement);
31         this.symbolicName = symbolicName;
32
33         final IRKeyword rootKeyword = rootStatement.keyword();
34         checkArgument(rootKeyword instanceof Unqualified, "Root statement has invalid keyword %s", rootKeyword);
35         final String rootName = rootKeyword.identifier();
36         switch (rootName) {
37             case "module":
38             case "submodule":
39                 break;
40             default:
41                 throw new IllegalArgumentException("Invalid root statement keyword " + rootName);
42         }
43
44         checkArgument(rootStatement.argument() != null, "Root statement does not have an argument");
45     }
46
47     public IRSchemaSource(final @NonNull SourceIdentifier identifier, final @NonNull IRStatement rootStatement) {
48         this(identifier, rootStatement, null);
49     }
50
51     @Override
52     public Optional<String> getSymbolicName() {
53         return Optional.ofNullable(symbolicName);
54     }
55
56     @Override
57     public Class<@NonNull IRSchemaSource> getType() {
58         return IRSchemaSource.class;
59     }
60
61     /**
62      * Return the root statement of this source.
63      *
64      * @return Root statement.
65      */
66     public @NonNull IRStatement getRootStatement() {
67         return rootStatement;
68     }
69 }