Enrich YangParserTestUtils with parseYang(String)
[yangtools.git] / parser / yang-test-util / src / main / java / org / opendaylight / yangtools / yang / test / util / LiteralYangTextSchemaSource.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech s.r.o. 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.test.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.io.CharSource;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.nio.charset.StandardCharsets;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.common.UnresolvedQName;
20 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
21 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
22
23 /**
24  * A {@link YangTextSchemaSource} backed by a string literal.
25  */
26 final class LiteralYangTextSchemaSource extends YangTextSchemaSource {
27     private final @NonNull String sourceString;
28
29     private LiteralYangTextSchemaSource(final SourceIdentifier identifier, final String sourceString,
30             final String symbolicName) {
31         super(identifier);
32         this.sourceString = requireNonNull(sourceString);
33     }
34
35     /**
36      * Create a new {@link YangTextSchemaSource} backed by a String input.
37      *
38      * @param sourceString YANG file as a String
39      * @return A new instance.
40      * @throws NullPointerException if {@code sourceString} is {@code null}
41      * @throws IllegalArgumentException if {@code sourceString} does not a valid YANG body, given a rather restrictive
42      *         view of what is valid.
43      */
44     static @NonNull LiteralYangTextSchemaSource ofLiteral(final String sourceString) {
45         // First line of a YANG file looks as follows:
46         //   `module module-name {`
47         // therefore in order to extract the name of the module from a plain string, we are interested in the second
48         // word of the first line
49         final String[] firstLine = sourceString.substring(0, sourceString.indexOf("{")).strip().split(" ");
50         final String moduleOrSubmoduleString = firstLine[0].strip();
51         checkArgument(moduleOrSubmoduleString.equals("module") || moduleOrSubmoduleString.equals("submodule"));
52
53         final String arg = firstLine[1].strip();
54         final var localName = UnresolvedQName.tryLocalName(arg);
55         checkArgument(localName != null);
56         return new LiteralYangTextSchemaSource(new SourceIdentifier(localName), sourceString, arg);
57     }
58
59     @Override
60     public InputStream openStream() throws IOException {
61         return CharSource.wrap(sourceString).asByteSource(StandardCharsets.UTF_8).openStream();
62     }
63
64     @Override
65     public Optional<String> getSymbolicName() {
66         return Optional.of(getIdentifier().name().getLocalName());
67     }
68 }