From d615f74168ffccf8be8eda4b39b1e9bf9ba06bda Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Tue, 25 Jul 2017 14:30:45 +0200 Subject: Api docs: unify handling of writers and subtree writers Change-Id: Ib0bd217c92f39052cc24f6324b5d33aa0443d002 Signed-off-by: Marek Gradzki --- .../hc2vpp/docs/core/CollectingWriterBuilder.java | 74 ++++++++++++---------- .../io/fd/hc2vpp/docs/core/CoverageGenerator.java | 68 +++----------------- .../java/io/fd/hc2vpp/docs/core/VppApiUtils.java | 12 ++-- 3 files changed, 55 insertions(+), 99 deletions(-) (limited to 'vpp-integration/api-docs/core/src/main/java/io') diff --git a/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/CollectingWriterBuilder.java b/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/CollectingWriterBuilder.java index b8fcc8b03..6abb6a80b 100644 --- a/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/CollectingWriterBuilder.java +++ b/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/CollectingWriterBuilder.java @@ -36,39 +36,46 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; */ public class CollectingWriterBuilder implements ModifiableWriterRegistryBuilder { - private final List> singleNodeHandlers; - private final List multiNodeWriteHandlers; + private final List writeHandlers; public CollectingWriterBuilder() { - singleNodeHandlers = new LinkedList<>(); - multiNodeWriteHandlers = new LinkedList<>(); + writeHandlers = new LinkedList<>(); + } + + private void addHandler(final Writer handler) { + writeHandlers.add(new WriteHandler(handler)); + } + + private void addHandler(final Writer handler, + final Set> handledChildren) { + writeHandlers.add(new WriteHandler(handler, handledChildren)); } @Override public ModifiableSubtreeManagerRegistryBuilder> add( @Nonnull Writer handler) { - singleNodeHandlers.add(handler); + addHandler(handler); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> subtreeAdd( @Nonnull Set> handledChildren, @Nonnull Writer handler) { - multiNodeWriteHandlers.add(new MultiNodeWriteHandler(handler, handledChildren)); + addHandler(handler, handledChildren); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> addBefore( @Nonnull Writer handler, @Nonnull InstanceIdentifier relatedType) { - multiNodeWriteHandlers.add(new MultiNodeWriteHandler(handler, Collections.singleton(relatedType))); + addHandler(handler, Collections.singleton(relatedType)); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> addBefore( @Nonnull Writer handler, @Nonnull Collection> relatedTypes) { - singleNodeHandlers.add(handler); + addHandler(handler); return this; } @@ -76,7 +83,7 @@ public class CollectingWriterBuilder implements ModifiableWriterRegistryBuilder public ModifiableSubtreeManagerRegistryBuilder> subtreeAddBefore( @Nonnull Set> handledChildren, @Nonnull Writer handler, @Nonnull InstanceIdentifier relatedType) { - multiNodeWriteHandlers.add(new MultiNodeWriteHandler(handler, handledChildren)); + addHandler(handler, handledChildren); return null; } @@ -84,21 +91,21 @@ public class CollectingWriterBuilder implements ModifiableWriterRegistryBuilder public ModifiableSubtreeManagerRegistryBuilder> subtreeAddBefore( @Nonnull Set> handledChildren, @Nonnull Writer handler, @Nonnull Collection> relatedTypes) { - multiNodeWriteHandlers.add(new MultiNodeWriteHandler(handler, handledChildren)); + addHandler(handler, handledChildren); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> addAfter( @Nonnull Writer handler, @Nonnull InstanceIdentifier relatedType) { - singleNodeHandlers.add(handler); + addHandler(handler); return this; } @Override public ModifiableSubtreeManagerRegistryBuilder> addAfter( @Nonnull Writer handler, @Nonnull Collection> relatedTypes) { - singleNodeHandlers.add(handler); + addHandler(handler); return this; } @@ -106,7 +113,7 @@ public class CollectingWriterBuilder implements ModifiableWriterRegistryBuilder public ModifiableSubtreeManagerRegistryBuilder> subtreeAddAfter( @Nonnull Set> handledChildren, @Nonnull Writer handler, @Nonnull InstanceIdentifier relatedType) { - multiNodeWriteHandlers.add(new MultiNodeWriteHandler(handler, Collections.singleton(relatedType))); + addHandler(handler, handledChildren); return this; } @@ -114,40 +121,41 @@ public class CollectingWriterBuilder implements ModifiableWriterRegistryBuilder public ModifiableSubtreeManagerRegistryBuilder> subtreeAddAfter( @Nonnull Set> handledChildren, @Nonnull Writer handler, @Nonnull Collection> relatedTypes) { - multiNodeWriteHandlers.add(new MultiNodeWriteHandler(handler, handledChildren)); + addHandler(handler, handledChildren); return this; } - public List> getSingleNodeHandlers() { - return singleNodeHandlers; + public List getWriteHandlers() { + return writeHandlers; } - public List getMultiNodeWriteHandlers() { - return multiNodeWriteHandlers; - } - - public static class MultiNodeWriteHandler { + public static class WriteHandler { private final Writer writer; - private final Set handledChildren; - + private final Set handledNodes; - public MultiNodeWriteHandler(Writer writer, Set> handledChildren) { + public WriteHandler(Writer writer, Set> handledChildren) { this.writer = writer; - this.handledChildren = ImmutableSet.builder() - .add(writer.getManagedDataObjectType().getTargetType().getName()) - .addAll(handledChildren.stream() - .map(InstanceIdentifier::getTargetType) - .map(Class::getName) - .collect(Collectors.toSet())) - .build(); + this.handledNodes = ImmutableSet.builder() + // add node managed by writer + .add(writer.getManagedDataObjectType().getTargetType().getName()) + // and set of handled children (may be empty in case of non subtree writers) + .addAll(handledChildren.stream() + .map(InstanceIdentifier::getTargetType) + .map(Class::getName) + .collect(Collectors.toSet())) + .build(); + } + + public WriteHandler(final Writer writer) { + this(writer, Collections.emptySet()); } public Writer getWriter() { return writer; } - public Set getHandledChildren() { - return handledChildren; + public Set getHandledNodes() { + return handledNodes; } } } diff --git a/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/CoverageGenerator.java b/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/CoverageGenerator.java index 4b6ab776c..4a8a77827 100644 --- a/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/CoverageGenerator.java +++ b/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/CoverageGenerator.java @@ -34,7 +34,6 @@ import io.fd.hc2vpp.docs.api.PluginCoverage; import io.fd.hc2vpp.docs.api.YangType; import io.fd.honeycomb.translate.write.WriterFactory; import java.lang.reflect.Field; -import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Set; @@ -59,14 +58,13 @@ public class CoverageGenerator implements VppApiUtils { final List scannedModules, final YangTypeLinkIndex yangTypeIndex, final ClassPathTypeIndex classPathIndex) { - LOG.info("Generating config coverage for plugin {}", pluginClass); + LOG.info("Generating config VPP API to Yang mapping for plugin {}", pluginClass); getInjectedWriterFactories(scannedModules).forEach(writerFactory -> writerFactory.init(writerBuilder)); - LOG.info("Processing single node handlers"); - final Set singleNodeCoverageUnits = writerBuilder.getSingleNodeHandlers().stream() - .flatMap(writer -> { + final Set coverageUnits = writerBuilder.getWriteHandlers().stream() + .flatMap(handler -> { // extracts customizer class from handler - final Class customizerClass = getCustomizerClass(writer); + final Class customizerClass = getCustomizerClass(handler.getWriter()); // scans within write method final Set writeReferences = @@ -81,62 +79,19 @@ public class CoverageGenerator implements VppApiUtils { new CoverageScanner(customizerClass, DELETE, pluginClass).scan(); return Stream.of(writeReferences.stream(), updateReferences.stream(), deleteReferences.stream()) - .flatMap(pluginMethodReferences -> pluginMethodReferences) + .flatMap(pluginMethodReferenceStream -> pluginMethodReferenceStream) .map(reference -> { final CoverageUnit.CoverageUnitBuilder builder = new CoverageUnit.CoverageUnitBuilder(); // binds vpp api name and generateLink bind with version - builder.setVppApi(fromJvppApi(version, reference)); + builder.setVppApi(fromJvppApi(version, reference.getName())); //binds java api reference builder.setJavaApi(new JavaApiMessage(reference.getName())); - //binds Yang types with links from pre-build index - // TODO - use deserialized yii - final String typeName = writer.getManagedDataObjectType().getTargetType().getTypeName(); - builder.setYangTypes(Collections.singletonList(new YangType( - typeName, - yangTypeIndex.getLinkForType(typeName)))); - - final List supportedOperations = new LinkedList<>(); - - final String callerClassLink = classPathIndex.linkForClass(reference.getCaller()); - if (writeReferences.contains(reference)) { - supportedOperations.add(new Operation(callerClassLink, WRITE)); - } - - if (updateReferences.contains(reference)) { - supportedOperations.add(new Operation(callerClassLink, UPDATE)); - } - - if (deleteReferences.contains(reference)) { - supportedOperations.add(new Operation(callerClassLink, DELETE)); - } - return builder.setSupportedOperations(supportedOperations).build(); - }); - }).collect(Collectors.toSet()); - - LOG.info("Processing multi node handlers"); - final Set multiNodeCoverageUnits = writerBuilder.getMultiNodeWriteHandlers().stream() - .flatMap(handler -> { - final Class customizerClass = getCustomizerClass(handler.getWriter()); - final Set writeReferences = - new CoverageScanner(customizerClass, WRITE, pluginClass).scan(); - - final Set updateReferences = - new CoverageScanner(customizerClass, UPDATE, pluginClass).scan(); - - final Set deleteReferences = - new CoverageScanner(customizerClass, DELETE, pluginClass).scan(); - - return Stream.of(writeReferences.stream(), updateReferences.stream(), deleteReferences.stream()) - .flatMap(pluginMethodReferenceStream -> pluginMethodReferenceStream) - .map(reference -> { - final CoverageUnit.CoverageUnitBuilder builder = new CoverageUnit.CoverageUnitBuilder(); - builder.setVppApi(fromJvppApi(version, reference)); - builder.setJavaApi(new JavaApiMessage(reference.getName())); - - builder.setYangTypes(handler.getHandledChildren().stream() + // binds Yang types with links from pre-build index + // TODO - use deserialized yii e.g. /module:parent-node/child-node + builder.setYangTypes(handler.getHandledNodes().stream() .map(type -> new YangType(type, yangTypeIndex.getLinkForType(type))) .collect(Collectors.toList())); @@ -157,10 +112,7 @@ public class CoverageGenerator implements VppApiUtils { }); }).collect(Collectors.toSet()); - return new PluginCoverage(pluginClass.getSimpleName(), - Stream.of(singleNodeCoverageUnits.stream(), multiNodeCoverageUnits.stream()) - .flatMap(coverageUnitStream -> coverageUnitStream) - .collect(Collectors.toSet()), true); + return new PluginCoverage(pluginClass.getSimpleName(), coverageUnits, true); } private static Class getCustomizerClass(final Object handler) { diff --git a/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/VppApiUtils.java b/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/VppApiUtils.java index f21cc2660..c978abae5 100644 --- a/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/VppApiUtils.java +++ b/vpp-integration/api-docs/core/src/main/java/io/fd/hc2vpp/docs/core/VppApiUtils.java @@ -22,11 +22,7 @@ import io.fd.hc2vpp.docs.api.VppApiMessage; public interface VppApiUtils { - static String vppApiFromJavaApi(final String jvppApi) { - return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, jvppApi); - } - - static String generateVppApiDocLink(final String version, final String vppApi) { + static String generateVppApiDocLink(final String version, final String vppMessageName) { //https://docs.fd.io/vpp/17.07/d9/d1d/structvl__api__create__subif__t.html // links are using double underscore //final String doubleUnderscoreApiName = vppApi.replace("_", "__"); @@ -36,8 +32,8 @@ public interface VppApiUtils { return "https://docs.fd.io/vpp/17.07/annotated.html"; } - default VppApiMessage fromJvppApi(final String version, final PluginMethodReference jvppApi) { - final String vppApi = vppApiFromJavaApi(jvppApi.getName()); - return new VppApiMessage(vppApi, generateVppApiDocLink(version, vppApi)); + default VppApiMessage fromJvppApi(final String version, final String jvppMethodName) { + final String vppMessageName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, jvppMethodName); + return new VppApiMessage(vppMessageName, generateVppApiDocLink(version, vppMessageName)); } } -- cgit 1.2.3-korg