Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KOGITO-9614] Kogito changes for new asyncapi extension version #3147

Merged
merged 6 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

public abstract class ValidationDecorator {

protected final Map<String, Exception> exceptions;
protected final Map<String, Throwable> exceptions;

protected ValidationDecorator(Map<String, Exception> exceptions) {
protected ValidationDecorator(Map<String, Throwable> exceptions) {
this.exceptions = exceptions;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ValidationLogDecorator extends ValidationDecorator {

private static final Logger LOGGER = LoggerFactory.getLogger(ValidationLogDecorator.class);

public ValidationLogDecorator(Map<String, Exception> exceptions) {
public ValidationLogDecorator(Map<String, Throwable> exceptions) {
super(exceptions);
}

Expand Down
2 changes: 1 addition & 1 deletion kogito-build/kogito-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<version.net.thisptr.jackson-jq>1.0.0-preview.20220705</version.net.thisptr.jackson-jq>
<version.io.quarkiverse.jackson-jq>1.1.0</version.io.quarkiverse.jackson-jq>
<version.io.quarkiverse.openapi.generator>1.3.8</version.io.quarkiverse.openapi.generator>
<version.io.quarkiverse.asyncapi>0.0.3</version.io.quarkiverse.asyncapi>
<version.io.quarkiverse.asyncapi>0.0.5</version.io.quarkiverse.asyncapi>
<version.io.quarkiverse.reactivemessaging.http>1.1.5</version.io.quarkiverse.reactivemessaging.http>
<version.io.quarkiverse.embedded.postgresql>0.0.8</version.io.quarkiverse.embedded.postgresql>
<version.com.github.haifengl.smile>1.5.2</version.com.github.haifengl.smile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public class ProcessCodegen extends AbstractGenerator {

public static ProcessCodegen ofCollectedResources(KogitoBuildContext context, Collection<CollectedResource> resources) {
Map<String, byte[]> processSVGMap = new HashMap<>();
Map<String, Exception> processesErrors = new HashMap<>();
Map<String, Throwable> processesErrors = new HashMap<>();
boolean useSvgAddon = context.getAddonsConfig().useProcessSVG();
final List<GeneratedInfo<KogitoWorkflowProcess>> processes = resources.stream()
.map(CollectedResource::resource)
Expand All @@ -123,13 +123,13 @@ public static ProcessCodegen ofCollectedResources(KogitoBuildContext context, Co
GeneratedInfo<KogitoWorkflowProcess> generatedInfo = parseWorkflowFile(resource, WorkflowFormat.fromFileName(resource.getSourcePath()), context);
notifySourceFileCodegenBindListeners(context, resource, Collections.singletonList(generatedInfo.info()));
return Stream.of(addResource(generatedInfo, resource));
} else {
return Stream.empty();
}
} catch (ValidationException | ProcessParsingException e) {
} catch (ValidationException e) {
processesErrors.put(resource.getSourcePath(), e);
return Stream.empty();
} catch (ProcessParsingException e) {
processesErrors.put(resource.getSourcePath(), e.getCause());
}
return Stream.empty();
})
//Validate parsed processes
.map(processInfo -> validate(processInfo, processesErrors))
Expand All @@ -154,7 +154,7 @@ private static void notifySourceFileCodegenBindListeners(KogitoBuildContext cont
.ifPresent(notifier -> processes.forEach(p -> notifier.notify(new SourceFileCodegenBindEvent(p.getId(), resource.getSourcePath()))));
}

private static void handleValidation(KogitoBuildContext context, Map<String, Exception> processesErrors) {
private static void handleValidation(KogitoBuildContext context, Map<String, Throwable> processesErrors) {
if (!processesErrors.isEmpty()) {
ValidationLogDecorator decorator = new ValidationLogDecorator(processesErrors);
decorator.decorate();
Expand All @@ -165,7 +165,7 @@ private static void handleValidation(KogitoBuildContext context, Map<String, Exc
}
}

private static GeneratedInfo<KogitoWorkflowProcess> validate(GeneratedInfo<KogitoWorkflowProcess> processInfo, Map<String, Exception> processesErrors) {
private static GeneratedInfo<KogitoWorkflowProcess> validate(GeneratedInfo<KogitoWorkflowProcess> processInfo, Map<String, Throwable> processesErrors) {
Process process = processInfo.info();
try {
ProcessValidatorRegistry.getInstance().getValidator(process, process.getResource()).validate(process);
Expand Down Expand Up @@ -216,7 +216,7 @@ protected static GeneratedInfo<KogitoWorkflowProcess> parseWorkflowFile(Resource
try (Reader reader = r.getReader()) {
return ServerlessWorkflowParser.of(reader, format, context).getProcessInfo();
} catch (IOException | RuntimeException e) {
throw new ProcessParsingException("Could not parse file " + r.getSourcePath(), e);
throw new ProcessParsingException(e);
}
}

Expand All @@ -227,7 +227,7 @@ protected static Collection<Process> parseProcessFile(Resource r) {
Thread.currentThread().getContextClassLoader());
return xmlReader.read(reader);
} catch (SAXException | IOException e) {
throw new ProcessParsingException("Could not parse file " + r.getSourcePath(), e);
throw new ProcessParsingException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@
package org.kie.kogito.codegen.process;

public class ProcessParsingException extends RuntimeException {
private static final long serialVersionUID = 1L;

public ProcessParsingException(Throwable cause) {
super(cause);
}

public ProcessParsingException(String s, Throwable e) {
super(s, e);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
Expand All @@ -28,7 +27,6 @@

import org.drools.codegen.common.GeneratedFile;
import org.drools.codegen.common.GeneratedFileType;
import org.eclipse.microprofile.config.ConfigProvider;
import org.kie.kogito.codegen.api.context.KogitoBuildContext;
import org.kie.kogito.internal.SupportedExtensions;
import org.kie.kogito.serverless.workflow.io.URIContentLoaderFactory;
Expand All @@ -42,12 +40,9 @@

import com.github.javaparser.ast.CompilationUnit;

import io.quarkus.deployment.CodeGenContext;
import io.serverlessworkflow.api.Workflow;
import io.serverlessworkflow.api.functions.FunctionDefinition;

import static org.kie.kogito.serverless.workflow.utils.ServerlessWorkflowUtils.FAIL_ON_ERROR_PROPERTY;

public class WorkflowCodeGenUtils {

private static final Logger logger = LoggerFactory.getLogger(WorkflowCodeGenUtils.class);
Expand All @@ -56,12 +51,8 @@ public class WorkflowCodeGenUtils {
private WorkflowCodeGenUtils() {
}

private static WorkflowOperationIdFactory operationIdFactory(CodeGenContext context) {
return WorkflowOperationIdFactoryProvider.getFactory(context.config().getOptionalValue(WorkflowOperationIdFactoryProvider.PROPERTY_NAME, String.class));
}

public static Stream<WorkflowOperationResource> operationResources(Stream<Path> files, Predicate<FunctionDefinition> predicate, CodeGenContext context) {
return getWorkflows(files).flatMap(w -> processFunction(w, predicate, operationIdFactory(context)));
public static Stream<WorkflowOperationResource> operationResources(Stream<Path> files, Predicate<FunctionDefinition> predicate, Optional<String> operationIdProp) {
return getWorkflows(files).flatMap(w -> processFunction(w, predicate, WorkflowOperationIdFactoryProvider.getFactory(operationIdProp)));
}

public static Stream<Workflow> getWorkflows(Stream<Path> files) {
Expand Down Expand Up @@ -96,20 +87,16 @@ private static WorkflowOperationResource getResource(Workflow workflow, Function
}

private static Optional<Workflow> getWorkflow(Path path) {
return workflowCache.computeIfAbsent(path, p -> SupportedExtensions.getSWFExtensions()
.stream()
.filter(e -> p.getFileName().toString().endsWith(e))
.map(e -> {
try (Reader r = Files.newBufferedReader(p)) {
return Optional.of(ServerlessWorkflowUtils.getWorkflow(r, WorkflowFormat.fromFileName(p.getFileName())));
} catch (IOException ex) {
if (ConfigProvider.getConfig().getOptionalValue(FAIL_ON_ERROR_PROPERTY, Boolean.class).orElse(true)) {
throw new UncheckedIOException(ex);
} else {
logger.error("Error reading workflow file {}", p, ex);
return Optional.<Workflow> empty();
}
}
}).flatMap(Optional::stream).findFirst());
if (SupportedExtensions.getSWFExtensions().stream().anyMatch(ext -> path.toString().endsWith(ext))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not make sense to compute files that does not finish witth current extension ( functional programming is risky ;)

return workflowCache.computeIfAbsent(path, p -> {
try (Reader r = Files.newBufferedReader(p)) {
return Optional.of(ServerlessWorkflowUtils.getWorkflow(r, WorkflowFormat.fromFileName(p.getFileName())));
} catch (IOException ex) {
logger.info("Error reading workflow file {}. Ignoring exception {}", p, ex);
return Optional.<Workflow> empty();
}
});
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

import org.kie.kogito.quarkus.serverless.workflow.WorkflowOperationResource;

import io.quarkiverse.asyncapi.generator.input.InputStreamSupplier;
import io.quarkiverse.asyncapi.config.InputStreamSupplier;

class AsyncInputStreamSupplier implements InputStreamSupplier {
class AsyncAPIInputStreamSupplier implements InputStreamSupplier {

private final WorkflowOperationResource resource;

public AsyncInputStreamSupplier(WorkflowOperationResource resource) {
public AsyncAPIInputStreamSupplier(WorkflowOperationResource resource) {
this.resource = resource;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,27 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.kie.kogito.quarkus.serverless.workflow.WorkflowCodeGenUtils;
import org.kie.kogito.serverless.workflow.operationid.WorkflowOperationIdFactoryProvider;

import io.quarkiverse.asyncapi.generator.input.AsyncAPISpecInput;
import io.quarkiverse.asyncapi.generator.input.AsyncApiSpecInputProvider;
import io.quarkus.deployment.CodeGenContext;
import io.quarkiverse.asyncapi.config.AsyncAPISpecInput;
import io.quarkiverse.asyncapi.config.AsyncAPISpecInputProvider;
import io.serverlessworkflow.api.functions.FunctionDefinition.Type;
import io.smallrye.config.ConfigSourceContext;

public class WorkflowAsyncApiSpecInputProvider implements AsyncApiSpecInputProvider {

private static final String KOGITO_PACKAGE_PREFIX = "org.kie.kogito.asyncAPI";
public class WorkflowAsyncAPISpecInputProvider implements AsyncAPISpecInputProvider {

@Override
public AsyncAPISpecInput read(CodeGenContext context) {
Path inputDir = context.inputDir();
while (!Files.exists(inputDir)) {
inputDir = inputDir.getParent();
}
try (Stream<Path> workflowFiles = Files.walk(inputDir)) {
return new AsyncAPISpecInput(WorkflowCodeGenUtils.operationResources(workflowFiles, f -> f.getType() == Type.ASYNCAPI, context)
.collect(Collectors.toMap(resource -> resource.getOperationId().getFileName(), AsyncInputStreamSupplier::new, (key1, key2) -> key1)), KOGITO_PACKAGE_PREFIX);
} catch (IOException io) {
throw new IllegalStateException(io);
public AsyncAPISpecInput read(ConfigSourceContext context) throws IOException {
try (Stream<Path> workflowFiles = Files.walk(Path.of(System.getProperty("user.dir")))) {
return new AsyncAPISpecInput(WorkflowCodeGenUtils
.operationResources(workflowFiles, f -> f.getType() == Type.ASYNCAPI,
Optional.ofNullable(context.getValue(WorkflowOperationIdFactoryProvider.PROPERTY_NAME).getValue()))
.collect(Collectors.toMap(resource -> resource.getOperationId().getFileName(), AsyncAPIInputStreamSupplier::new, (key1, key2) -> key1)));
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.kie.kogito.quarkus.serverless.workflow.WorkflowCodeGenUtils;
import org.kie.kogito.quarkus.serverless.workflow.WorkflowOperationResource;
import org.kie.kogito.serverless.workflow.operationid.WorkflowOperationIdFactoryProvider;
import org.kie.kogito.serverless.workflow.utils.OpenAPIWorkflowUtils;

import io.quarkiverse.openapi.generator.deployment.codegen.OpenApiSpecInputProvider;
Expand All @@ -41,7 +42,9 @@ public List<SpecInputModel> read(CodeGenContext context) {
inputDir = inputDir.getParent();
}
try (Stream<Path> openApiFilesPaths = Files.walk(inputDir)) {
return WorkflowCodeGenUtils.operationResources(openApiFilesPaths, OpenAPIWorkflowUtils::isOpenApiOperation, context).map(this::getSpecInput).collect(Collectors.toList());
return WorkflowCodeGenUtils
.operationResources(openApiFilesPaths, OpenAPIWorkflowUtils::isOpenApiOperation, context.config().getOptionalValue(WorkflowOperationIdFactoryProvider.PROPERTY_NAME, String.class))
.map(this::getSpecInput).collect(Collectors.toList());
} catch (IOException io) {
throw new IllegalStateException(io);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.kie.kogito.serverless.workflow.io.FileContentLoader;
import org.kie.kogito.serverless.workflow.io.URIContentLoader;
import org.kie.kogito.serverless.workflow.io.URIContentLoaderFactory;
import org.kie.kogito.serverless.workflow.operationid.WorkflowOperationIdFactoryProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -65,7 +66,8 @@ public boolean trigger(CodeGenContext context) throws CodeGenException {
Path outputPath = context.workDir().resolve("proto_temp");
Files.createDirectories(outputPath);
Collection<Path> protoFiles =
WorkflowCodeGenUtils.operationResources(rpcFilePaths, this::isRPC, context).map(r -> getPath(r, outputPath)).filter(Optional::isPresent).map(Optional::get)
WorkflowCodeGenUtils.operationResources(rpcFilePaths, this::isRPC, context.config().getOptionalValue(WorkflowOperationIdFactoryProvider.PROPERTY_NAME, String.class))
.map(r -> getPath(r, outputPath)).filter(Optional::isPresent).map(Optional::get)
.collect(Collectors.toList());
logger.debug("Collected proto paths are {}", protoFiles);
if (protoFiles.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.kie.kogito.quarkus.serverless.workflow.asyncapi.WorkflowAsyncApiSpecInputProvider
org.kie.kogito.quarkus.serverless.workflow.asyncapi.WorkflowAsyncAPISpecInputProvider
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
org.kie.kogito.quarkus.serverless.workflow.deployment.livereload.LiveReloadableAsyncApiGeneratorStreamCodeGen
org.kie.kogito.quarkus.serverless.workflow.deployment.livereload.LiveReloadableOpenApiGeneratorStreamCodeGen
org.kie.kogito.quarkus.serverless.workflow.deployment.livereload.LiveReloadableWorkflowRPCCodeGenProvider
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,15 @@
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>${version.io.grpc}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${version.io.grpc}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${version.io.grpc}</version>
</dependency>
<dependency>
<!-- necessary for Java 9+ -->
<groupId>org.apache.tomcat</groupId>
<artifactId>annotations-api</artifactId>
<version>6.0.53</version>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.kie.kogito.test.utils.SocketUtils;
Expand Down Expand Up @@ -163,7 +162,6 @@ void testGrpc() throws InterruptedException, IOException {
}

@Test
@Disabled("Disabled until https://issues.redhat.com/browse/KOGITO-9614 is resolved")
void testAsyncApi() throws IOException {
given()
.contentType(ContentType.JSON)
Expand Down
Loading