Brian O'Connor
Committed by Gerrit Code Review

ONOS Swagger Plugin

just does swagger now
to build the plugin, you need to download to following:
https://github.com/bocon13/buck/releases/download/v2016.07.29.01-wip/buck.jar

put the buck.jar in tools/build/buck-plugin/lib

Change-Id: Id1b833dd013fbc5581f8e884e755920829c4a17e
[buildfile]
includes = //buck-tools/default.defs
[plugins]
directory = //bucklets/plugins
[java]
source_level = 8
target_level = 8
......
......@@ -17,6 +17,7 @@ localDB.*
.buckd
buck-out
.buckconfig.local
.buckjavaargs*
core/store/trivial/data/
core/store/apps
......
......@@ -16,4 +16,9 @@ osgi_jar_with_tests (
deps = COMPILE_DEPS,
test_deps = TEST_DEPS,
web_context = '/onos/dhcp',
api_title = 'DHCP Server App',
api_version = '1.0',
api_description = 'REST API for DHCP Server App',
api_package = 'org.onosproject.dhcp.rest',
debug = False
)
......
......@@ -107,6 +107,10 @@ def osgi_jar(
package_name_root = 'org.onosproject',
include_resources = NONE,
web_context = NONE,
api_title = NONE,
api_version = NONE,
api_package = NONE,
api_description = NONE,
resources = NONE,
resources_root = None,
**kwargs
......@@ -128,18 +132,27 @@ def osgi_jar(
if resources and not resources_root:
resources_root = RESOURCES_ROOT
if api_title != NONE:
r = 'WEB-INF/classes/apidoc/swagger.json=bin/swagger.json'
include_resources = include_resources + ',' + r if include_resources != NONE else r
bare_jar_name = name + '-jar'
osgi_jar_name = name + '-osgi'
mvn_coords = group_id + ':' + name + ':' + version
java_library(
onos_jar(
name = bare_jar_name,
srcs = srcs,
deps = deps,
visibility = [], #intentially, not visible
resources = resources,
resources_root = resources_root,
web_context = web_context,
api_title = api_title,
api_version = api_version,
api_package = api_package,
api_description = api_description,
**kwargs
)
......@@ -167,6 +180,7 @@ def osgi_jar(
bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar
print bash
# FIXME: make sure that /swagger.json gets filtered
genrule(
name = osgi_jar_name,
bash = bash,
......
remote_jar (
name = 'buck-api',
out = 'buck.jar',
url = 'https://github.com/bocon13/buck/releases/download/v2016.07.29.01-wip/buck.jar',
sha1 = 'f89324cb869b74fdcd4db9972233065a93d890a2',
visibility = [],
)
COMPILE = [
'//lib:qdox',
#'//lib:jackson-core',
#'//lib:jackson-databind',
#'//lib:jackson-annotations',
]
RUNTIME = [
]
java_library(
name = 'lib',
srcs = glob(['src/main/java/**/*.java']),
resources = glob(['src/main/resources/**/*']),
resources_root = 'src/main/resources',
deps = COMPILE,
provided_deps = [':buck-api'],
visibility = [],
)
java_binary(
name = 'onosjar',
deps = [':lib'],
visibility = ['PUBLIC'],
)
\ No newline at end of file
#!/bin/bash
PLUGINS=$ONOS_ROOT/bucklets/plugins
# Build it first
pluginJar=$(NO_BUCKD=1 buck build //tools/build/buck-plugin:onosjar --no-cache --show-output | grep onosjar.jar | cut -d\ -f2)
# Then install it
mkdir -p $PLUGINS
cp $ONOS_ROOT/$pluginJar $PLUGINS
ls -l $PLUGINS
#!/bin/bash
NO_BUCKD=1 buck build //apps/dhcp/app:onos-apps-dhcp-app --no-cache --show-output
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.onosjar;
import com.facebook.buck.jvm.java.CompileToJarStepFactory;
import com.facebook.buck.jvm.java.DefaultJavaLibrary;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.rules.AddToRuleKey;
import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.rules.BuildRuleParams;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.SourcePathResolver;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import java.nio.file.Path;
import java.util.Set;
import java.util.regex.Pattern;
/**
* Implementation of a build rule that generates a onosjar.json file for a set
* of Java sources.
*/
public class OnosJar extends DefaultJavaLibrary {
@AddToRuleKey
final Optional<String> webContext;
@AddToRuleKey
final Optional<String> apiTitle;
@AddToRuleKey
final Optional<String> apiVersion;
@AddToRuleKey
final Optional<String> apiPackage;
@AddToRuleKey
final Optional<String> apiDescription;
public OnosJar(BuildRuleParams params,
SourcePathResolver resolver,
Set<? extends SourcePath> srcs,
Set<? extends SourcePath> resources,
Optional<Path> generatedSourceFolder,
Optional<SourcePath> proguardConfig,
ImmutableList<String> postprocessClassesCommands,
ImmutableSortedSet<BuildRule> exportedDeps,
ImmutableSortedSet<BuildRule> providedDeps,
SourcePath abiJar, boolean trackClassUsage,
ImmutableSet<Path> additionalClasspathEntries,
CompileToJarStepFactory compileStepFactory,
Optional<Path> resourcesRoot,
Optional<String> mavenCoords,
ImmutableSortedSet<BuildTarget> tests,
ImmutableSet<Pattern> classesToRemoveFromJar,
Optional<String> webContext,
Optional<String> apiTitle,
Optional<String> apiVersion,
Optional<String> apiPackage,
Optional<String> apiDescription) {
super(params, resolver, srcs, resources, generatedSourceFolder,
proguardConfig, postprocessClassesCommands, exportedDeps,
providedDeps, abiJar, trackClassUsage, additionalClasspathEntries,
compileStepFactory, resourcesRoot, mavenCoords, tests,
classesToRemoveFromJar);
this.webContext = webContext;
this.apiTitle = apiTitle;
this.apiVersion = apiVersion;
this.apiPackage = apiPackage;
this.apiDescription = apiDescription;
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.onosjar;
import com.facebook.buck.cli.BuckConfig;
import com.facebook.buck.jvm.java.CalculateAbi;
import com.facebook.buck.jvm.java.DefaultJavaLibrary;
import com.facebook.buck.jvm.java.JavaBuckConfig;
import com.facebook.buck.jvm.java.JavaLibraryDescription;
import com.facebook.buck.jvm.java.JavaOptions;
import com.facebook.buck.jvm.java.JavacOptions;
import com.facebook.buck.jvm.java.JavacOptionsAmender;
import com.facebook.buck.jvm.java.JavacOptionsFactory;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.model.Flavor;
import com.facebook.buck.parser.NoSuchBuildTargetException;
import com.facebook.buck.rules.BuildRule;
import com.facebook.buck.rules.BuildRuleParams;
import com.facebook.buck.rules.BuildRuleResolver;
import com.facebook.buck.rules.BuildRuleType;
import com.facebook.buck.rules.BuildRules;
import com.facebook.buck.rules.BuildTargetSourcePath;
import com.facebook.buck.rules.Description;
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.rules.SourcePaths;
import com.facebook.buck.rules.TargetGraph;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import java.nio.file.Path;
import static com.facebook.buck.jvm.common.ResourceValidator.validateResources;
/**
* Description for the onos_jar rules.
*
* Currently, this only does Swagger generation.
*/
public class OnosJarDescription implements Description<OnosJarDescription.Arg> {
public static final BuildRuleType TYPE = BuildRuleType.of("onos_jar");
private final JavacOptions defaultJavacOptions;
private final JavaOptions defaultJavaOptions;
public OnosJarDescription(BuckConfig config) {
JavaBuckConfig javaConfig = new JavaBuckConfig(config);
defaultJavacOptions = javaConfig.getDefaultJavacOptions();
defaultJavaOptions = javaConfig.getDefaultJavaOptions();
}
@Override
public BuildRuleType getBuildRuleType() {
return TYPE;
}
@Override
public Arg createUnpopulatedConstructorArg() {
return new Arg();
}
@Override
public <A extends Arg> BuildRule createBuildRule(TargetGraph targetGraph,
BuildRuleParams params,
BuildRuleResolver resolver,
A args)
throws NoSuchBuildTargetException {
SourcePathResolver pathResolver = new SourcePathResolver(resolver);
BuildTarget target = params.getBuildTarget();
// We know that the flavour we're being asked to create is valid, since the check is done when
// creating the action graph from the target graph.
ImmutableSortedSet<Flavor> flavors = target.getFlavors();
BuildRuleParams paramsWithMavenFlavor = null;
JavacOptions javacOptions = JavacOptionsFactory.create(
defaultJavacOptions,
params,
resolver,
pathResolver,
args
);
BuildTarget abiJarTarget = params.getBuildTarget().withAppendedFlavors(CalculateAbi.FLAVOR);
ImmutableSortedSet<BuildRule> exportedDeps = resolver.getAllRules(args.exportedDeps.get());
DefaultJavaLibrary defaultJavaLibrary =
resolver.addToIndex(
new OnosJar(
params.appendExtraDeps(
Iterables.concat(
BuildRules.getExportedRules(
Iterables.concat(
params.getDeclaredDeps().get(),
exportedDeps,
resolver.getAllRules(args.providedDeps.get()))),
pathResolver.filterBuildRuleInputs(
javacOptions.getInputs(pathResolver)))),
pathResolver,
args.srcs.get(),
validateResources(
pathResolver,
params.getProjectFilesystem(),
args.resources.get()),
javacOptions.getGeneratedSourceFolderName(),
args.proguardConfig.transform(
SourcePaths.toSourcePath(params.getProjectFilesystem())),
args.postprocessClassesCommands.get(), // FIXME this should be forbidden
exportedDeps,
resolver.getAllRules(args.providedDeps.get()),
new BuildTargetSourcePath(abiJarTarget),
javacOptions.trackClassUsage(),
/* additionalClasspathEntries */ ImmutableSet.<Path>of(),
new OnosJarStepFactory(javacOptions, JavacOptionsAmender.IDENTITY,
args.webContext, args.apiTitle, args.apiVersion,
args.apiPackage, args.apiDescription, args.resources),
args.resourcesRoot,
args.mavenCoords,
args.tests.get(),
javacOptions.getClassesToRemoveFromJar(),
args.webContext,
args.apiTitle,
args.apiVersion,
args.apiPackage,
args.apiDescription));
resolver.addToIndex(
CalculateAbi.of(
abiJarTarget,
pathResolver,
params,
new BuildTargetSourcePath(defaultJavaLibrary.getBuildTarget())));
return defaultJavaLibrary;
}
public static class Arg extends JavaLibraryDescription.Arg {
public Optional<String> webContext;
public Optional<String> apiTitle;
public Optional<String> apiVersion;
public Optional<String> apiPackage;
public Optional<String> apiDescription;
}
}
\ No newline at end of file
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.onosjar;
import com.facebook.buck.io.ProjectFilesystem;
import com.facebook.buck.jvm.core.SuggestBuildRules;
import com.facebook.buck.jvm.java.ClassUsageFileWriter;
import com.facebook.buck.jvm.java.JarDirectoryStep;
import com.facebook.buck.jvm.java.JavacOptions;
import com.facebook.buck.jvm.java.JavacOptionsAmender;
import com.facebook.buck.jvm.java.JavacToJarStepFactory;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.rules.BuildContext;
import com.facebook.buck.rules.BuildableContext;
import com.facebook.buck.rules.SourcePath;
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.step.Step;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import java.nio.file.Path;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Creates the list of build steps for the onos_jar rules.
*/
public class OnosJarStepFactory extends JavacToJarStepFactory {
private static final String DEFINITIONS = "/definitions/";
private final String webContext;
private final String apiTitle;
private final String apiVersion;
private final String apiPackage;
private final String apiDescription;
private final Optional<ImmutableSortedSet<SourcePath>> resources;
public OnosJarStepFactory(JavacOptions javacOptions,
JavacOptionsAmender amender,
Optional<String> webContext,
Optional<String> apiTitle,
Optional<String> apiVersion,
Optional<String> apiPackage,
Optional<String> apiDescription,
Optional<ImmutableSortedSet<SourcePath>> resources) {
super(javacOptions, amender);
this.webContext = processParameter(webContext);
this.apiTitle = processParameter(apiTitle);
this.apiVersion = processParameter(apiVersion);
this.apiPackage = processParameter(apiPackage);
this.apiDescription = processParameter(apiDescription);
this.resources = resources;
}
private String processParameter(Optional<String> p) {
return !p.isPresent() || p.get().equals("NONE") ? null : p.get();
}
@Override
public void createCompileToJarStep(BuildContext context,
ImmutableSortedSet<Path> sourceFilePaths,
BuildTarget invokingRule,
SourcePathResolver resolver,
ProjectFilesystem filesystem,
ImmutableSortedSet<Path> declaredClasspathEntries,
Path outputDirectory,
Optional<Path> workingDirectory,
Path pathToSrcsList,
Optional<SuggestBuildRules> suggestBuildRules,
ImmutableList<String> postprocessClassesCommands,
ImmutableSortedSet<Path> entriesToJar,
Optional<String> mainClass,
Optional<Path> manifestFile,
Path outputJar,
ClassUsageFileWriter usedClassesFileWriter,
ImmutableList.Builder<Step> steps,
BuildableContext buildableContext,
ImmutableSet<Pattern> classesToRemoveFromJar) {
ImmutableSet.Builder<Path> sourceFilePathBuilder = ImmutableSet.builder();
sourceFilePathBuilder.addAll(sourceFilePaths);
ImmutableSet.Builder<Pattern> blacklistBuilder = ImmutableSet.builder();
blacklistBuilder.addAll(classesToRemoveFromJar);
// precompilation steps
// - generate sources
// add all generated sources ot pathToSrcsList
if (webContext != null && apiTitle != null && resources.isPresent()) {
ImmutableSortedSet<Path> resourceFilePaths = findSwaggerModelDefs(resolver, resources.get());
blacklistBuilder.addAll(resourceFilePaths.stream()
.map(rp -> Pattern.compile(rp.getFileName().toString(), Pattern.LITERAL))
.collect(Collectors.toSet()));
Path genSourcesOutput = workingDirectory.get();
SwaggerStep swaggerStep = new SwaggerStep(filesystem, sourceFilePaths, resourceFilePaths,
genSourcesOutput, outputDirectory,
webContext, apiTitle, apiVersion,
apiPackage, apiDescription);
sourceFilePathBuilder.add(swaggerStep.apiRegistratorPath());
steps.add(swaggerStep);
}
createCompileStep(context,
ImmutableSortedSet.copyOf(sourceFilePathBuilder.build()),
invokingRule,
resolver,
filesystem,
declaredClasspathEntries,
outputDirectory,
workingDirectory,
pathToSrcsList,
suggestBuildRules,
usedClassesFileWriter,
steps,
buildableContext);
// post compilation steps
// FIXME BOC: add mechanism to inject new Steps
//context.additionalStepFactory(JavaStep.class);
// build the jar
steps.add(new JarDirectoryStep(filesystem,
outputJar,
ImmutableSortedSet.of(outputDirectory),
mainClass.orNull(),
manifestFile.orNull(),
true,
blacklistBuilder.build()));
}
private ImmutableSortedSet<Path> findSwaggerModelDefs(SourcePathResolver resolver,
ImmutableSortedSet<SourcePath> resourcePaths) {
if (resourcePaths == null) {
return ImmutableSortedSet.of();
}
return ImmutableSortedSet.copyOf(resourcePaths.stream()
.filter(sp -> sp.toString().contains(DEFINITIONS))
.map(resolver::getRelativePath)
.collect(Collectors.toList()));
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.onosjar;
import com.facebook.buck.io.ProjectFilesystem;
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.step.AbstractExecutionStep;
import com.facebook.buck.step.ExecutionContext;
import com.facebook.buck.step.StepExecutionResult;
import com.google.common.collect.ImmutableSortedSet;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
/**
* Buck build step to trigger SwaggerGenerator.
*/
public class SwaggerStep extends AbstractExecutionStep {
private final ProjectFilesystem filesystem;
private final ImmutableSortedSet<Path> srcs;
private final ImmutableSortedSet<Path> resources;
private final Path genSourcesOutput;
private final Path genResourcesOutput;
private final String webContext;
private final String apiTitle;
private final String apiVersion;
private final String apiPackage;
private final String apiDescription;
public SwaggerStep(ProjectFilesystem filesystem,
ImmutableSortedSet<Path> srcs,
ImmutableSortedSet<Path> resources,
Path genSourcesOutput, Path genResourcesOutput,
String webContext, String apiTitle, String apiVersion,
String apiPackage, String apiDescription) {
super("swagger");
this.filesystem = filesystem;
this.srcs = srcs;
this.resources = resources;
this.genSourcesOutput = genSourcesOutput;
this.genResourcesOutput = genResourcesOutput;
this.webContext = webContext;
this.apiTitle = apiTitle;
this.apiVersion = apiVersion;
this.apiPackage = apiPackage;
this.apiDescription = apiDescription;
}
@Override
public StepExecutionResult execute(ExecutionContext executionContext)
throws IOException, InterruptedException {
try {
List<File> srcFiles = srcs.stream()
.map(src -> filesystem.resolve(src).toFile())
.collect(Collectors.toList());
List<File> resourceFiles = resources.stream()
.map(rsrc -> filesystem.resolve(rsrc).toFile())
.collect(Collectors.toList());
new SwaggerGenerator(srcFiles, resourceFiles, null, null,
filesystem.resolve(genSourcesOutput).toFile(),
filesystem.resolve(genResourcesOutput).toFile(),
webContext,
apiTitle,
apiVersion,
apiPackage,
apiDescription).execute();
return StepExecutionResult.SUCCESS;
} catch (Exception e) {
e.printStackTrace();
// FIXME print the exception
return StepExecutionResult.ERROR;
}
}
Path apiRegistratorPath() {
return genSourcesOutput.resolve(SwaggerGenerator.apiRegistratorPath(apiPackage));
}
}
/*
* Auto-generated by OnosSwaggerMojo.
*
* Copyright 2015-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ${api.package};
import org.apache.felix.scr.annotations.Component;
import org.onosproject.rest.AbstractApiDocRegistrator;
import org.onosproject.rest.ApiDocProvider;
@Component(immediate = true)
public class ApiDocRegistrator extends AbstractApiDocRegistrator {
public ApiDocRegistrator() {
super(new ApiDocProvider("${web.context}",
"${api.title}",
ApiDocRegistrator.class.getClassLoader()));
}
}
......@@ -34,5 +34,29 @@ if [ -n "$FORCE_UPDATE" ] || [ ! -f "buck" ] || [ "$REQUIRED_VERSION" != "$(cat
fi
popd > /dev/null
BUCK=$ONOS_ROOT/bin/buck
PLUGINS=$ONOS_ROOT/bucklets/plugins
ONOS_PLUGIN=$PLUGINS/onosjar.jar
if [ ! -f "$ONOS_PLUGIN" -o -n "$ONOS_BUILD_PLUGIN" ]; then
echo "Building ONOS Buck plugins..."
# Build it first
pluginJar=$(NO_BUCKD=1 $BUCK build //tools/build/buck-plugin:onosjar --show-output 2>/dev/null | grep onosjar.jar | cut -d\ -f2)
CHK_NEW=$(cksum $pluginJar | cut -d' ' -f1-2)
CHK_OLD=$(cksum $ONOS_PLUGIN 2>/dev/null | cut -d' ' -f1-2)
if [ "$CHK_NEW" != "$CHK_OLD" ]; then
# diff plugins... if different, copy and restart buckd
# Then install it
mkdir -p $PLUGINS
cp $ONOS_ROOT/$pluginJar $PLUGINS
echo "Updated to the latest plugin."
$BUCK clean 2>/dev/null
else
echo "Plugin was already up to date."
fi
fi
# Finally, run the Buck command...
$ONOS_ROOT/bin/buck "$@"
$BUCK "$@"
......
......@@ -14,4 +14,8 @@ osgi_jar_with_tests (
deps = COMPILE_DEPS,
test_deps = TEST_DEPS,
web_context = '/onos/v1',
api_title = 'ONOS Core REST API',
api_version = '1.0',
api_description = 'ONOS Core REST API',
api_package = 'org.onosproject.rest.impl',
)
......