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