*/
package org.opendaylight.yangtools.yang2sources.plugin;
-import java.util.HashSet;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.maven.project.MavenProject;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.repo.URLSchemaContextResolver;
+import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
import org.opendaylight.yangtools.yang2sources.plugin.Util.ContextHolder;
import org.opendaylight.yangtools.yang2sources.plugin.Util.YangsInZipsResult;
static final String LOG_PREFIX = "yang-to-sources:";
static final String META_INF_YANG_STRING = "META-INF" + File.separator + "yang";
static final String META_INF_YANG_STRING_JAR = "META-INF" + "/" + "yang";
+ static final String META_INF_YANG_SERVICES_STRING_JAR = "META-INF" + "/" + "services";
private final File yangFilesRootDir;
private final File[] excludedFiles;
private final boolean inspectDependencies;
private final BuildContext buildContext;
private final YangProvider yangProvider;
- private final URLSchemaContextResolver resolver;
+ private final YangTextSchemaContextResolver resolver;
@VisibleForTesting
- YangToSourcesProcessor(File yangFilesRootDir, File[] excludedFiles, List<CodeGeneratorArg> codeGenerators,
- MavenProject project, boolean inspectDependencies, YangProvider yangProvider) {
+ YangToSourcesProcessor(final File yangFilesRootDir, final File[] excludedFiles, final List<CodeGeneratorArg> codeGenerators,
+ final MavenProject project, final boolean inspectDependencies, final YangProvider yangProvider) {
this(new DefaultBuildContext(), yangFilesRootDir, excludedFiles, codeGenerators, project,
inspectDependencies, yangProvider);
}
- private YangToSourcesProcessor(BuildContext buildContext, File yangFilesRootDir, File[] excludedFiles,
- List<CodeGeneratorArg> codeGenerators, MavenProject project, boolean inspectDependencies, YangProvider
+ private YangToSourcesProcessor(final BuildContext buildContext, final File yangFilesRootDir, final File[] excludedFiles,
+ final List<CodeGeneratorArg> codeGenerators, final MavenProject project, final boolean inspectDependencies, final YangProvider
yangProvider) {
this.buildContext = Util.checkNotNull(buildContext, "buildContext");
this.yangFilesRootDir = Util.checkNotNull(yangFilesRootDir, "yangFilesRootDir");
this.project = Util.checkNotNull(project, "project");
this.inspectDependencies = inspectDependencies;
this.yangProvider = yangProvider;
- this.resolver = URLSchemaContextResolver.create("maven-plugin");
+ this.resolver = YangTextSchemaContextResolver.create("maven-plugin");
}
- YangToSourcesProcessor(BuildContext buildContext, File yangFilesRootDir, File[] excludedFiles,
- List<CodeGeneratorArg> codeGenerators, MavenProject project, boolean inspectDependencies) {
+ YangToSourcesProcessor(final BuildContext buildContext, final File yangFilesRootDir, final File[] excludedFiles,
+ final List<CodeGeneratorArg> codeGenerators, final MavenProject project, final boolean inspectDependencies) {
this(yangFilesRootDir, excludedFiles, codeGenerators, project, inspectDependencies, new YangProvider());
}
}
}
+ void conditionalExecute(final boolean skip) throws MojoExecutionException, MojoFailureException {
+ if (skip) {
+ LOG.info("Skipping YANG code generation because property yang.skip is true");
+
+ // But manually add resources
+ // add META_INF/yang
+ yangProvider.addYangsToMetaInf(project, yangFilesRootDir, excludedFiles);
+
+ // add META_INF/services
+ File generatedServicesDir = new File(project.getBasedir(), CodeGeneratorArg.YANG_SERVICES_GENERATED_DIR);
+ YangProvider.setResource(generatedServicesDir, project);
+ LOG.debug("{} Yang services files from: {} marked as resources: {}", LOG_PREFIX, generatedServicesDir,
+ META_INF_YANG_SERVICES_STRING_JAR);
+
+
+ } else {
+ execute();
+ }
+ }
+
private ContextHolder processYang() throws MojoExecutionException {
- final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
SchemaContext resolveSchemaContext;
List<Closeable> closeables = new ArrayList<>();
LOG.info("{} Inspecting {}", LOG_PREFIX, yangFilesRootDir);
closeables.addAll(yangStreams);
}
- resolveSchemaContext = reactor.buildEffective(all);
+ resolveSchemaContext = YangParserTestUtils.parseYangStreams(all);
Set<Module> parsedAllYangModules = resolveSchemaContext.getModules();
projectYangModules = new HashSet<>();
}
}
- private List<InputStream> toStreamsWithoutDuplicates(List<YangSourceFromDependency> list) throws IOException {
+ private static List<InputStream> toStreamsWithoutDuplicates(final List<YangSourceFromDependency> list) throws IOException {
ConcurrentMap<String, YangSourceFromDependency> byContent = Maps.newConcurrentMap();
for (YangSourceFromDependency yangFromDependency : list) {
try (InputStream dataStream = yangFromDependency.openStream()) {
String contents = IOUtils.toString(dataStream);
byContent.putIfAbsent(contents, yangFromDependency);
+ } catch (IOException e) {
+ throw new IOException("Exception when reading from: " + yangFromDependency.getDescription(), e);
}
}
static class YangProvider {
private static final Logger LOG = LoggerFactory.getLogger(YangProvider.class);
- void addYangsToMetaInf(MavenProject project, File yangFilesRootDir, File[] excludedFiles)
+ void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir, final File[] excludedFiles)
throws MojoFailureException {
// copy project's src/main/yang/*.yang to target/generated-sources/yang/META-INF/yang/*.yang
}
}
- private void addYangsToMetaInf(MavenProject project, File yangFilesRootDir,
- File[] excludedFiles, File generatedYangDir)
- throws MojoFailureException {
+ private static void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir,
+ final File[] excludedFiles, final File generatedYangDir) throws MojoFailureException {
File withMetaInf = new File(generatedYangDir, META_INF_YANG_STRING);
withMetaInf.mkdirs();
META_INF_YANG_STRING_JAR);
}
- private static void setResource(File targetYangDir, MavenProject project) {
+ private static void setResource(final File targetYangDir, final MavenProject project) {
Resource res = new Resource();
res.setDirectory(targetYangDir.getPath());
project.addResource(res);
/**
* Call generate on every generator from plugin configuration
*/
- private void generateSources(ContextHolder context) throws MojoFailureException {
+ private void generateSources(final ContextHolder context) throws MojoFailureException {
if (codeGenerators.size() == 0) {
LOG.warn("{} No code generators provided", LOG_PREFIX);
return;
/**
* Instantiate generator from class and call required method
*/
- private void generateSourcesWithOneGenerator(ContextHolder context, CodeGeneratorArg codeGeneratorCfg)
+ private void generateSourcesWithOneGenerator(final ContextHolder context, final CodeGeneratorArg codeGeneratorCfg)
throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
codeGeneratorCfg.check();