* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.yangtools.yang.model.spi.source;
-
-import static java.util.Objects.requireNonNull;
+package org.opendaylight.yangtools.yang.model.api.source;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.io.CharSource;
-import java.io.InputStream;
-import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
-import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
+import java.io.Reader;
+import org.eclipse.jdt.annotation.NonNullByDefault;
/**
- * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as an {@link InputStream}.
+ * YANG text schema source representation. Exposes an RFC6020 or RFC7950 text representation as a {@link Reader}.
*/
+@NonNullByDefault
public abstract class YangTextSource extends CharSource implements YangSourceRepresentation {
- private final @NonNull SourceIdentifier sourceId;
-
- protected YangTextSource(final SourceIdentifier sourceId) {
- this.sourceId = requireNonNull(sourceId);
- }
-
@Override
public final Class<YangTextSource> getType() {
return YangTextSource.class;
}
- @Override
- public final SourceIdentifier sourceId() {
- return sourceId;
- }
-
@Override
public final String toString() {
return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
* @param toStringHelper ToStringHelper onto the attributes can be added
* @return ToStringHelper supplied as input argument.
*/
- protected ToStringHelper addToStringAttributes(final @NonNull ToStringHelper toStringHelper) {
- return toStringHelper.add("identifier", sourceId);
+ protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+ return toStringHelper.add("identifier", sourceId());
}
}
--- /dev/null
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.spi.source;
+
+import static java.util.Objects.requireNonNull;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.concepts.Delegator;
+import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
+
+/**
+ * Abstract base class for implementing {@link YangTextSource}s with {@link Delegator}.
+ */
+@NonNullByDefault
+abstract class AbstractYangTextSource<T> extends YangTextSource implements Delegator<T> {
+ private final SourceIdentifier sourceId;
+ private final T delegate;
+
+ AbstractYangTextSource(final SourceIdentifier sourceId, final T delegate) {
+ this.sourceId = requireNonNull(sourceId);
+ this.delegate = requireNonNull(delegate);
+ }
+
+ @Override
+ public final SourceIdentifier sourceId() {
+ return sourceId;
+ }
+
+ @Override
+ public final T getDelegate() {
+ return delegate;
+ }
+}
*/
package org.opendaylight.yangtools.yang.model.spi.source;
-import static java.util.Objects.requireNonNull;
-
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.io.CharSource;
import java.io.IOException;
import java.io.Reader;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.opendaylight.yangtools.concepts.Delegator;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
/**
* A {@link YangTextSource} delegating to a {@link CharSource}.
*/
@NonNullByDefault
-public class DelegatedYangTextSource extends YangTextSource implements Delegator<CharSource> {
- private final CharSource delegate;
-
+public class DelegatedYangTextSource extends AbstractYangTextSource<CharSource> {
/**
* Default constructor.
*
* @param delegate Backing {@link CharSource} instance
*/
public DelegatedYangTextSource(final SourceIdentifier sourceId, final CharSource delegate) {
- super(sourceId);
- this.delegate = requireNonNull(delegate);
- }
-
- @Override
- public final CharSource getDelegate() {
- return delegate;
+ super(sourceId, delegate);
}
@Override
public final Reader openStream() throws IOException {
- return delegate.openStream();
+ return getDelegate().openStream();
}
@Override
public final @NonNull String symbolicName() {
- return "[" + delegate.toString() + "]";
+ return "[" + getDelegate().toString() + "]";
}
@Override
protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
- return super.addToStringAttributes(toStringHelper).add("delegate", delegate);
+ return super.addToStringAttributes(toStringHelper).add("delegate", getDelegate());
}
}
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YinTextSource;
/**
- * A {@link YangTextSource} delegating to a {@link ByteSource}.
+ * A {@link YinTextSource} delegating to a {@link ByteSource}.
*/
@NonNullByDefault
public class DelegatedYinTextSource extends AbstractYinTextSource<ByteSource> {
import java.nio.file.Path;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.opendaylight.yangtools.concepts.Delegator;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
/**
* A {@link YangTextSource} backed by a file.
*/
@NonNullByDefault
-public class FileYangTextSource extends YangTextSource implements Delegator<Path> {
- private final Path path;
+public class FileYangTextSource extends AbstractYangTextSource<Path> {
private final Charset charset;
/**
* @throws IllegalArgumentException if the supplied path is not a regular file
*/
public FileYangTextSource(final SourceIdentifier sourceId, final Path path, final Charset charset) {
- super(sourceId);
+ super(sourceId, path);
if (!Files.isRegularFile(path)) {
throw new IllegalArgumentException("Supplied path " + path + " is not a regular file");
}
- this.path = requireNonNull(path);
this.charset = requireNonNull(charset);
}
this(SourceIdentifier.ofYangFileName(path.toFile().getName()), path, StandardCharsets.UTF_8);
}
- @Override
- public final Path getDelegate() {
- return path;
- }
-
@Override
public final Reader openStream() throws IOException {
- return new InputStreamReader(Files.newInputStream(path), charset);
+ return new InputStreamReader(Files.newInputStream(getDelegate()), charset);
}
@Override
public final @NonNull String symbolicName() {
// FIXME: NEXT: this is forcing internal normalization. I think this boils down to providing Path back, which
// is essentially getDelegate() anyway. Perhaps expose it as PathAware?
- return path.toString();
+ return getDelegate().toString();
}
@Override
protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
- return super.addToStringAttributes(toStringHelper).add("path", path);
+ return super.addToStringAttributes(toStringHelper).add("path", getDelegate());
}
}
*/
package org.opendaylight.yangtools.yang.model.spi.source;
-import static java.util.Objects.requireNonNull;
-
import java.io.Reader;
import java.io.StringReader;
-import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
/**
* A {@link YangTextSource} with content readily available.
*/
-public class StringYangTextSource extends YangTextSource {
+public class StringYangTextSource extends AbstractYangTextSource<String> {
private final @Nullable String symbolicName;
- private final @NonNull String content;
public StringYangTextSource(final SourceIdentifier sourceId, final String content) {
this(sourceId, content, null);
public StringYangTextSource(final SourceIdentifier sourceId, final String content,
final @Nullable String symbolicName) {
- super(sourceId);
- this.content = requireNonNull(content);
+ super(sourceId, content);
this.symbolicName = symbolicName;
}
@Override
public final Reader openStream() {
- return new StringReader(content);
+ return new StringReader(getDelegate());
}
}
import java.nio.charset.StandardCharsets;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.opendaylight.yangtools.concepts.Delegator;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
/**
* A {@link YangTextSource} backed by a {@link URL}.
*/
@NonNullByDefault
-public class URLYangTextSource extends YangTextSource implements Delegator<URL> {
- private final URL url;
+public class URLYangTextSource extends AbstractYangTextSource<URL> {
private final Charset charset;
public URLYangTextSource(final SourceIdentifier sourceId, final URL url, final Charset charset) {
- super(sourceId);
- this.url = requireNonNull(url);
+ super(sourceId, url);
this.charset = requireNonNull(charset);
}
@Override
public final Reader openStream() throws IOException {
- return new InputStreamReader(url.openStream(), charset);
+ return new InputStreamReader(getDelegate().openStream(), charset);
}
@Override
public final @NonNull String symbolicName() {
- return url.toString();
- }
-
- @Override
- public final URL getDelegate() {
- return url;
+ return getDelegate().toString();
}
@Override
protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
- return super.addToStringAttributes(toStringHelper).add("url", url);
+ return super.addToStringAttributes(toStringHelper).add("url", getDelegate());
}
private static String extractFileName(final String path) {
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.api.source.YinTextSource;
import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.YinDomSource;
import org.opendaylight.yangtools.yang.model.spi.source.YinXmlSource;
import org.opendaylight.yangtools.yang.parser.api.YangParser;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactoryConfiguration;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
class SharedEffectiveModelContextFactoryTest {
import com.google.common.util.concurrent.Futures;
import java.io.IOException;
import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceTransformer;
import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.parser.rfc7950.antlr.IRSupport;
import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.Import;
import org.opendaylight.yangtools.yang.model.api.source.SourceDependency.Include;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.spi.meta.StatementDeclarations;
import org.opendaylight.yangtools.yang.model.spi.source.SourceInfo;
import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
/**
import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
import org.opendaylight.yangtools.yang.model.api.meta.StatementSourceReference;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.antlr.YangStatementLexer;
import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser;
import org.opendaylight.yangtools.yang.parser.antlr.YangStatementParser.FileContext;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.ModuleImport;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.api.source.YinTextSource;
import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.FileYinTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
+++ /dev/null
-/*
- * Copyright (c) 2023 PANTHEON.tech s.r.o. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.test.util;
-
-import static com.google.common.base.Preconditions.checkArgument;
-import static java.util.Objects.requireNonNull;
-
-import java.io.StringReader;
-import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.yangtools.yang.common.UnresolvedQName;
-import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
-
-/**
- * A {@link YangTextSource} backed by a string literal.
- */
-final class LiteralYangTextSource extends YangTextSource {
- private final @NonNull String sourceString;
-
- private LiteralYangTextSource(final SourceIdentifier identifier, final String sourceString,
- final String symbolicName) {
- super(identifier);
- this.sourceString = requireNonNull(sourceString);
- }
-
- /**
- * Create a new {@link YangTextSource} backed by a String input.
- *
- * @param sourceString YANG file as a String
- * @return A new instance.
- * @throws NullPointerException if {@code sourceString} is {@code null}
- * @throws IllegalArgumentException if {@code sourceString} does not a valid YANG body, given a rather restrictive
- * view of what is valid.
- */
- static @NonNull LiteralYangTextSource ofLiteral(final String sourceString) {
- // First line of a YANG file looks as follows:
- // `module module-name {`
- // therefore in order to extract the name of the module from a plain string, we are interested in the second
- // word of the first line
- final String[] firstLine = sourceString.substring(0, sourceString.indexOf("{")).strip().split(" ");
- final String moduleOrSubmoduleString = firstLine[0].strip();
- checkArgument(moduleOrSubmoduleString.equals("module") || moduleOrSubmoduleString.equals("submodule"));
-
- final String arg = firstLine[1].strip();
- final var localName = UnresolvedQName.tryLocalName(arg);
- checkArgument(localName != null);
- return new LiteralYangTextSource(new SourceIdentifier(localName), sourceString, arg);
- }
-
- @Override
- public StringReader openStream() {
- return new StringReader(sourceString);
- }
-
- @Override
- public String symbolicName() {
- return sourceId().name().getLocalName();
- }
-}
*/
package org.opendaylight.yangtools.yang.test.util;
+import static com.google.common.base.Preconditions.checkArgument;
+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.File;
import java.io.FileFilter;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.UnresolvedQName;
import org.opendaylight.yangtools.yang.common.YangConstants;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.StringYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.api.YangParser;
import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
import org.opendaylight.yangtools.yang.parser.api.YangParserException;
*/
public static EffectiveModelContext parseYang(final String... sources) {
return parseSources(YangParserConfiguration.DEFAULT, null,
- Arrays.stream(sources).map(LiteralYangTextSource::ofLiteral).toList());
+ Arrays.stream(sources).map(YangParserTestUtils::createYangTextSource).toList());
}
@SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification = "Wrong inferent on listFiles")
}
return Arrays.asList(new File(directoryPath).listFiles(YANG_FILE_FILTER));
}
+
+
+ /**
+ * Create a new {@link YangTextSource} backed by a String input.
+ *
+ * @param sourceString YANG file as a String
+ * @return A new instance.
+ * @throws NullPointerException if {@code sourceString} is {@code null}
+ * @throws IllegalArgumentException if {@code sourceString} does not a valid YANG body, given a rather restrictive
+ * view of what is valid.
+ */
+ private static @NonNull StringYangTextSource createYangTextSource(final String sourceString) {
+ // First line of a YANG file looks as follows:
+ // `module module-name {`
+ // therefore in order to extract the name of the module from a plain string, we are interested in the second
+ // word of the first line
+ final var firstLine = sourceString.substring(0, sourceString.indexOf("{")).strip().split(" ");
+ final var moduleOrSubmoduleString = firstLine[0].strip();
+ checkArgument(moduleOrSubmoduleString.equals("module") || moduleOrSubmoduleString.equals("submodule"));
+
+ final String arg = firstLine[1].strip();
+ final var localName = UnresolvedQName.tryLocalName(arg);
+ checkArgument(localName != null);
+ return new StringYangTextSource(new SourceIdentifier(localName), sourceString, arg);
+ }
}
import java.util.Optional;
import org.opendaylight.yangtools.yang.model.api.ModuleLike;
import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
/**
* An SPI-level interface to find the schema source for a particular YANG module, as packaged in the final artifact.
import org.opendaylight.yangtools.yang.model.api.ModuleLike;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
final class ContextHolder implements Immutable, ModuleResourceResolver {
private final @NonNull EffectiveModelContext context;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.Submodule;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.api.YangParser;
import org.opendaylight.yangtools.yang.parser.api.YangParserException;
import org.slf4j.Logger;
import org.apache.maven.project.MavenProject;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.List;
import org.apache.maven.project.MavenProject;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
@FunctionalInterface
@VisibleForTesting
import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException;
import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory;
import org.opendaylight.yangtools.yang.common.YangConstants;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
import org.opendaylight.yangtools.yang.parser.api.YangParserException;
import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opendaylight.yangtools.plugin.generator.api.ModuleResourceResolver;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
@ExtendWith(MockitoExtension.class)
class FilenameResolutionTest extends AbstractCodeGeneratorTest {
import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.spi.AbstractSchemaSourceCache;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource.Costs;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.collect.Lists;
import com.google.common.io.Files;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import java.io.File;
import java.io.IOException;
-import java.io.Reader;
-import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.api.source.SourceRepresentation;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.StringYangTextSource;
import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
final SourceIdentifier runningId = new SourceIdentifier("running", "2012-12-12");
sharedSchemaRepository.registerSchemaSource(sourceIdentifier -> FluentFutures.immediateFluentFuture(
- new YangTextSource(runningId) {
- @Override
- protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
- return toStringHelper;
- }
-
- @Override
- public Reader openStream() throws IOException {
- return new StringReader("running");
- }
-
- @Override
- public String symbolicName() {
- return null;
- }
- }), PotentialSchemaSource.create(runningId, YangTextSource.class,
+ new StringYangTextSource(runningId, "running", null)),
+ PotentialSchemaSource.create(runningId, YangTextSource.class,
PotentialSchemaSource.Costs.REMOTE_IO.getValue()));
final TextToIRTransformer transformer = TextToIRTransformer.create(sharedSchemaRepository,
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
-import com.google.common.base.MoreObjects;
import com.google.common.collect.Collections2;
-import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.Futures;
import java.io.File;
-import java.io.IOException;
-import java.io.Reader;
-import java.io.StringReader;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import org.mockito.quality.Strictness;
import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.StringYangTextSource;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
-public class FilesystemSchemaSourceCacheTest {
+class FilesystemSchemaSourceCacheTest {
@Mock
- public SchemaSourceRegistry registry;
+ private SchemaSourceRegistry registry;
@Mock
- public Registration registration;
+ private Registration registration;
- public File storageDir;
+ private File storageDir;
@BeforeEach
- public void setUp() throws Exception {
+ void setUp() throws Exception {
storageDir = Files.createTempDirectory(null).toFile();
doReturn(registration).when(registry).registerSchemaSource(any(SchemaSourceProvider.class),
any(PotentialSchemaSource.class));
}
@Test
- public void testCacheAndRestore() throws Exception {
- final FilesystemSchemaSourceCache<YangTextSource> cache = new FilesystemSchemaSourceCache<>(registry,
- YangTextSource.class, storageDir);
+ void testCacheAndRestore() throws Exception {
+ final var cache = new FilesystemSchemaSourceCache<>(registry, YangTextSource.class, storageDir);
- final String content = "content1";
- final YangTextSource source = new TestingYangSource("test", "2012-12-12", content);
+ final var content = "content1";
+ final var source = new StringYangTextSource(new SourceIdentifier("test", "2012-12-12"), content);
cache.offer(source);
- final String content2 = "content2";
- final YangTextSource source2 = new TestingYangSource("test2", null, content);
+ final var content2 = "content2";
+ final var source2 = new StringYangTextSource(new SourceIdentifier("test2"), content);
cache.offer(source2);
- final List<File> storedFiles = getFilesFromCache();
+ final var storedFiles = getFilesFromCache();
assertEquals(2, storedFiles.size());
- final Collection<String> fileNames = filesToFilenamesWithoutRevision(storedFiles);
+ final var fileNames = filesToFilenamesWithoutRevision(storedFiles);
assertThat(fileNames, both(hasItem("test2")).and(hasItem("test@2012-12-12")));
verify(registry, times(4)).registerSchemaSource(any(SchemaSourceProvider.class),
any(PotentialSchemaSource.class));
- final List<File> storedFilesAfterNewCache = getFilesFromCache();
+ final var storedFilesAfterNewCache = getFilesFromCache();
assertEquals(2, storedFilesAfterNewCache.size());
}
}
@Test
- public void testCacheDuplicate() throws Exception {
- final FilesystemSchemaSourceCache<YangTextSource> cache = new FilesystemSchemaSourceCache<>(registry,
- YangTextSource.class, storageDir);
+ void testCacheDuplicate() throws Exception {
+ final var cache = new FilesystemSchemaSourceCache<>(registry, YangTextSource.class, storageDir);
- final String content = "content1";
- final YangTextSource source = new TestingYangSource("test", null, content);
+ final var content = "content1";
+ final var source = new StringYangTextSource(new SourceIdentifier("test"), content);
// Double offer
cache.offer(source);
cache.offer(source);
- final List<File> storedFiles = getFilesFromCache();
+ final var storedFiles = getFilesFromCache();
assertEquals(1, storedFiles.size());
verify(registry).registerSchemaSource(any(SchemaSourceProvider.class), any(PotentialSchemaSource.class));
}
@Test
- public void testCacheMultipleRevisions() throws Exception {
- final FilesystemSchemaSourceCache<YangTextSource> cache = new FilesystemSchemaSourceCache<>(registry,
- YangTextSource.class, storageDir);
-
- final String content = "content1";
- final YangTextSource source = new TestingYangSource("test", null, content);
- final YangTextSource source2 = new TestingYangSource("test", "2012-12-12", content);
- final YangTextSource source3 = new TestingYangSource("test", "2013-12-12", content);
+ void testCacheMultipleRevisions() throws Exception {
+ final var cache = new FilesystemSchemaSourceCache<>(registry, YangTextSource.class, storageDir);
+
+ final var source = new StringYangTextSource(new SourceIdentifier("test"), "content1");
+ final var source2 = new StringYangTextSource(new SourceIdentifier("test", "2012-12-12"), "content2");
+ final var source3 = new StringYangTextSource(new SourceIdentifier("test", "2013-12-12"), "content3");
// Double offer
cache.offer(source);
cache.offer(source2);
cache.offer(source3);
- final List<File> storedFiles = getFilesFromCache();
+ final var storedFiles = getFilesFromCache();
assertEquals(3, storedFiles.size());
assertThat(filesToFilenamesWithoutRevision(storedFiles), both(hasItem("test"))
}
@Test
- public void sourceIdToFileEmptyRevWithEmptyDir() {
- final SourceIdentifier sourceIdentifier = new SourceIdentifier("test");
- final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, storageDir);
- final FilesystemSchemaSourceCache<YangTextSource> cache = new FilesystemSchemaSourceCache<>(registry,
- YangTextSource.class, sourceIdToFile);
+ void sourceIdToFileEmptyRevWithEmptyDir() {
+ final var sourceId = new SourceIdentifier("test");
+ final var sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceId, storageDir);
+ final var cache = new FilesystemSchemaSourceCache<>(registry, YangTextSource.class, sourceIdToFile);
assertNotNull(cache);
- final List<File> storedFiles = Arrays.asList(sourceIdToFile.listFiles());
+ final var storedFiles = Arrays.asList(sourceIdToFile.listFiles());
assertEquals(0, storedFiles.size());
}
@Test
- public void sourceIdToFileEmptyRevWithOneItemInDir() {
- final FilesystemSchemaSourceCache<YangTextSource> cache = new FilesystemSchemaSourceCache<>(registry,
- YangTextSource.class, storageDir);
- final String content = "content1";
- final YangTextSource source = new TestingYangSource("test", "2013-12-12", content);
+ void sourceIdToFileEmptyRevWithOneItemInDir() {
+ final var cache = new FilesystemSchemaSourceCache<>(registry, YangTextSource.class, storageDir);
+ final var source = new StringYangTextSource(new SourceIdentifier("test", "2013-12-12"), "content1");
cache.offer(source);
- final SourceIdentifier sourceIdentifier = new SourceIdentifier("test");
- final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier,
- storageDir);
+ final var sourceId = new SourceIdentifier("test");
+ final var sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceId, storageDir);
assertNotNull(sourceIdToFile);
- final List<File> storedFiles = Arrays.asList(storageDir.listFiles());
+ final var storedFiles = Arrays.asList(storageDir.listFiles());
assertEquals(1, storedFiles.size());
}
@Test
- public void sourceIdToFileEmptyRevWithMoreItemsInDir() {
- final FilesystemSchemaSourceCache<YangTextSource> cache = new FilesystemSchemaSourceCache<>(registry,
- YangTextSource.class, storageDir);
- final String content = "content1";
- final YangTextSource source = new TestingYangSource("test", "2012-12-12", content);
- final YangTextSource source2 = new TestingYangSource("test", "2013-12-12", content);
+ void sourceIdToFileEmptyRevWithMoreItemsInDir() {
+ final var cache = new FilesystemSchemaSourceCache<>(registry, YangTextSource.class, storageDir);
+ final var source = new StringYangTextSource(new SourceIdentifier("test", "2012-12-12"), "content1");
+ final var source2 = new StringYangTextSource(new SourceIdentifier("test", "2013-12-12"), "content1");
cache.offer(source);
cache.offer(source2);
- final SourceIdentifier sourceIdentifier = new SourceIdentifier("test");
- final File sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceIdentifier, storageDir);
+ final var sourceId = new SourceIdentifier("test");
+ final var sourceIdToFile = FilesystemSchemaSourceCache.sourceIdToFile(sourceId, storageDir);
assertNotNull(sourceIdToFile);
- final List<File> storedFiles = Arrays.asList(storageDir.listFiles());
+ final var storedFiles = Arrays.asList(storageDir.listFiles());
assertEquals(2, storedFiles.size());
}
@Test
- public void test() throws Exception {
- final FilesystemSchemaSourceCache<YangTextSource> cache = new FilesystemSchemaSourceCache<>(registry,
- YangTextSource.class, storageDir);
- final String content = "content1";
- final YangTextSource source = new TestingYangSource("test", "2013-12-12", content);
+ void test() throws Exception {
+ final var cache = new FilesystemSchemaSourceCache<>(registry, YangTextSource.class, storageDir);
+ final var sourceId = new SourceIdentifier("test", "2013-12-12");
+ final var source = new StringYangTextSource(sourceId, "content1", null);
cache.offer(source);
- final SourceIdentifier sourceIdentifier = new SourceIdentifier("test", "2013-12-12");
- final ListenableFuture<? extends YangTextSource> checked = cache.getSource(sourceIdentifier);
- assertNotNull(checked);
- assertTrue(checked.isDone());
- final YangTextSource checkedGet = checked.get();
- assertEquals(sourceIdentifier, checkedGet.sourceId());
+ assertEquals(sourceId, Futures.getDone(cache.getSource(sourceId)).sourceId());
}
@Test
- public void test1() throws Exception {
- final FilesystemSchemaSourceCache<YangTextSource> cache = new FilesystemSchemaSourceCache<>(registry,
- YangTextSource.class, storageDir);
- final String content = "content1";
- final YangTextSource source = new TestingYangSource("test", "2013-12-12", content);
+ void test1() throws Exception {
+ final var cache = new FilesystemSchemaSourceCache<>(registry, YangTextSource.class, storageDir);
+ final var source = new StringYangTextSource(new SourceIdentifier("test1", "2013-12-12"), "content1");
cache.offer(source);
- final SourceIdentifier sourceIdentifier = new SourceIdentifier("test1", "2012-12-12");
- final ListenableFuture<? extends YangTextSource> checked = cache.getSource(sourceIdentifier);
- assertNotNull(checked);
- assertThrows(ExecutionException.class, () -> checked.get());
+ final var sourceId = new SourceIdentifier("test1", "2012-12-12");
+ final var future = cache.getSource(sourceId);
+ final var ex = assertThrows(ExecutionException.class, () -> Futures.getDone(future));
+ final var cause = assertInstanceOf(MissingSchemaSourceException.class, ex.getCause());
+ assertEquals(sourceId, cause.sourceId());
+ assertEquals("Source not found", cause.getMessage());
}
private List<File> getFilesFromCache() {
return Arrays.asList(storageDir.listFiles());
}
-
- private static class TestingYangSource extends YangTextSource {
- private final String content;
-
- TestingYangSource(final String name, final String revision, final String content) {
- super(new SourceIdentifier(name, revision));
- this.content = content;
- }
-
- @Override
- protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
- return toStringHelper;
- }
-
- @Override
- public Reader openStream() throws IOException {
- return new StringReader(content);
- }
-
- @Override
- public String symbolicName() {
- return null;
- }
- }
}
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
-import com.google.common.base.MoreObjects.ToStringHelper;
-import java.io.StringReader;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.StringYangTextSource;
@Deprecated
@ExtendWith(MockitoExtension.class)
doReturn(registration).when(registry).registerSchemaSource(any(), any());
try (var cache = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) {
- final var content = "content";
- final var source = new TestingYangSource("test", "2012-12-12", content);
- cache.offer(source);
final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
- final var checkedSource = cache .getSource(sourceIdentifier);
+ final var source = new StringYangTextSource(sourceIdentifier, "content");
+ cache.offer(source);
+ final var checkedSource = cache.getSource(sourceIdentifier);
assertNotNull(checkedSource);
final var yangSchemaSourceRepresentation = checkedSource.get();
assertNotNull(yangSchemaSourceRepresentation);
try (var cache1 = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION)) {
try (var cache2 = GuavaSchemaSourceCache.createSoftCache(registry, REPRESENTATION, LIFETIME, UNITS)) {
final var content = "content";
- final var source = new TestingYangSource("test", "2012-12-12", content);
+ final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
+ final var source = new StringYangTextSource(sourceIdentifier, content);
cache1.offer(source);
cache2.offer(source);
- final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
final var checkedSource = cache1.getSource(sourceIdentifier);
final var checkedSource2 = cache2.getSource(sourceIdentifier);
assertNotNull(checkedSource);
}
}
}
-
- private static class TestingYangSource extends YangTextSource {
- private final String content;
-
- TestingYangSource(final String name, final String revision, final String content) {
- super(new SourceIdentifier(name, revision));
- this.content = content;
- }
-
- @Override
- public StringReader openStream() {
- return new StringReader(content);
- }
-
- @Override
- public String symbolicName() {
- return null;
- }
-
- @Override
- protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
- return toStringHelper;
- }
- }
}
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
-import com.google.common.base.MoreObjects.ToStringHelper;
-import java.io.StringReader;
import java.util.concurrent.ExecutionException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.api.source.YangSourceRepresentation;
-import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.StringYangTextSource;
@ExtendWith(MockitoExtension.class)
class SoftSchemaSourceCacheTest {
doReturn(registration).when(registry).registerSchemaSource(any(), any());
try (var cache = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
- final var content = "content";
- final var source = new TestingYangSource("test", "2012-12-12", content);
- cache.offer(source);
final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
- final var checkedSource = cache .getSource(sourceIdentifier);
+ final var source = new StringYangTextSource(sourceIdentifier, "content");
+ cache.offer(source);
+ final var checkedSource = cache.getSource(sourceIdentifier);
assertNotNull(checkedSource);
final var yangSchemaSourceRepresentation = checkedSource.get();
assertNotNull(yangSchemaSourceRepresentation);
try (var cache1 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
try (var cache2 = new SoftSchemaSourceCache<>(registry, REPRESENTATION)) {
- final var content = "content";
- final var source = new TestingYangSource("test", "2012-12-12", content);
+ final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
+ final var source = new StringYangTextSource(sourceIdentifier, "content");
cache1.offer(source);
cache2.offer(source);
- final var sourceIdentifier = new SourceIdentifier("test", "2012-12-12");
final var checkedSource = cache1.getSource(sourceIdentifier);
final var checkedSource2 = cache2.getSource(sourceIdentifier);
assertNotNull(checkedSource);
}
}
}
-
- private static class TestingYangSource extends YangTextSource {
- private final String content;
-
- TestingYangSource(final String name, final String revision, final String content) {
- super(new SourceIdentifier(name, revision));
- this.content = content;
- }
-
- @Override
- public StringReader openStream() {
- return new StringReader(content);
- }
-
- @Override
- public String symbolicName() {
- return null;
- }
-
- @Override
- protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
- return toStringHelper;
- }
- }
}