Skip to content

Add support for generating code to send MuleSoft Dataweave Transformations to TriggerMesh #183

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

Merged
merged 11 commits into from
Sep 23, 2022
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ spring.profiles.active=default, core
spring.application.name=spring-boot-migrator
# toggle support for git to sync and auto-commit
sbm.gitSupportEnabled=true
sbm.muleTriggerMeshTransformEnabled=true
logging.level.org=ERROR
logging.level.org.springframework.sbm.logging.MethodCallTraceInterceptor=DEBUG
logging.level.org.springframework.sbm.logging.StopWatchTraceInterceptor=DEBUG
Expand Down
9 changes: 9 additions & 0 deletions applications/spring-shell/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ Base Package: ${sbm.defaultBasePackage}

Use -Dsbm.defaultBasePackage=com.acme.packagename as VM parameter on startup to set the property.

TriggerMesh transformation support of Dataweave: ${sbm.muleTriggerMeshTransformEnabled}

- When applying the mule-to-boot recipe, use `sbm.muleTriggerMeshTransformEnabled` to
generate the code required to send the Dataweave transformation to TriggerMesh using
the TriggerMesh Dataweave transformation service (https://docs.triggermesh.io/guides/dataweavetransformation/).
- This will require a TriggerMesh transformation exist on your Kubernetes cluster to function. When running the
service, be sure to set the `K_SINK` environment variable to your exposed service URL.

Use -Dsbm.muleTriggerMeshTransformEnabled=true|false as VM parameter on startup to set property.

Get Started:
------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2021 - 2022 the original author or authors.
*
* 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
*
* https://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.springframework.sbm;

import org.junit.jupiter.api.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.*;

import static org.assertj.core.api.Assertions.assertThat;

class MigrateSimpleMuleAppDataweaveIntegrationTest extends IntegrationTestBaseClass {
private final RestTemplate restTemplate = new RestTemplate();
private static RunningNetworkedContainer tmDataweaveContainer;

@Override
protected String getTestSubDir() {

return "mule-app/spring-dw-mule";

}

@BeforeAll
public static void beforeAll() {
IntegrationTestBaseClass.beforeAll();

// Will need to ensure this is set globally for the test
System.setProperty("sbm.muleTriggerMeshTransformEnabled", "true");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I looked into overriding @SpringProperties, but this would not get picked up by the transformer that would swap out the stub code for the code that will send the transformation out to the TriggerMesh Dataweave transformer. If there's a cleaner way to do this, then would appreciate hearing it.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should rework this approach and set the flag in the action of a dedicated recipe, e.g. migrate-mule-to-boot-tm and then just call it from here.


// start TriggerMesh Dataweave Translator
HashMap<String, String> envMap = new HashMap<>();
envMap.put("NAMESPACE", "default");
envMap.put("DATAWEAVETRANSFORMATION_ALLOW_SPELL_OVERRIDE", "true");

tmDataweaveContainer = startDockerContainer(
new NetworkedContainer(
"gcr.io/triggermesh/dataweavetransformation-adapter:v1.21.0",
List.of(8080),
"dwhost"),
null,
envMap);
if (!tmDataweaveContainer.getContainer().isRunning()) {
throw new RuntimeException("TriggerMesh Dataweave Transformer container could not be started");
}
}

@AfterAll
public static void afterAll() {
if (tmDataweaveContainer != null && tmDataweaveContainer.getContainer() != null) {
tmDataweaveContainer.getContainer().stop();
}
}

@Test
@Tag("integration")
public void t2_dataweaveIntegrationWorks() {
intializeTestProject();
scanProject();
applyRecipe("initialize-spring-boot-migration");
applyRecipe("migrate-mule-to-triggermesh-boot");

executeMavenGoals(getTestDir(), "clean", "package", "spring-boot:build-image");

int dwPort = tmDataweaveContainer.getContainer().getMappedPort(8080);
HashMap<String, String> runtimeEnv = new HashMap<>();
runtimeEnv.put("K_SINK", "http://dwhost:8080");

RunningNetworkedContainer container = startDockerContainer(
new NetworkedContainer("hellomuledw-migrated:1.0-SNAPSHOT", List.of(9081), "spring"),
tmDataweaveContainer.getNetwork(),
runtimeEnv);

checkSendHttpMessage(container.getContainer().getMappedPort(9081));
checkTranslatedInboundGatewayHttpMessage(container.getContainer().getMappedPort(9081));
}

private void checkTranslatedInboundGatewayHttpMessage(int port) {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:" + port + "/dwtest", String.class);

assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(responseEntity.getBody()).isEqualTo("\n{\n \"greeting\": \"hello from SBM\"\n}");
}

private void checkSendHttpMessage(int port) {
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://localhost:" + port + "/test", String.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2021 - 2022 the original author or authors.
~
~ 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
~
~ https://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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>hellomuledw-migrated</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.javadsl;

import org.springframework.context.annotation.Configuration;

@Configuration
public class Foo {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2021 - 2022 the original author or authors.
~
~ 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
~
~ https://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.
-->
<mule xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/ee/dw">
<flow name="get:/dwtest:dwtest-config">
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 2.0
output application/json
---
greeting: "hello from SBM"
]]>
</dw:set-payload>
</dw:transform-message>
</flow>
</mule>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ Copyright 2021 - 2022 the original author or authors.
~
~ 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
~
~ https://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.
-->

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="9081" doc:name="HTTP Listener Configuration"/>
<flow name="http-flow">
<http:listener doc:name="Listener" doc:id="9f602d5c-5386-4fc9-ac8f-024d754c17e5" config-ref="HTTP_Listener_Configuration" path="/test"/>
<logger level="INFO" doc:name="Logger" doc:id="4585ec7f-2d4a-4d86-af24-b678d4a99227" />
</flow>
</mule>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@ConfigurationProperties(prefix = "sbm")
public class SbmApplicationProperties {
private boolean gitSupportEnabled;
private boolean muleTriggerMeshTransformEnabled;
private String defaultBasePackage;
private boolean writeInMavenLocal;
private boolean javaParserLoggingCompilationWarningsAndErrors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

# toggle support for git to sync and auto-commit
sbm.gitSupportEnabled=true
# toggle support to use TriggerMesh for dataweave transformations
sbm.muleTriggerMeshTransformEnabled=false
# default base package when adding classes and no base package can be calculated
sbm.defaultBasePackage=org.springframework.sbm
# default groupId when creating a new build file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.sbm.mule;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -30,23 +31,34 @@
import org.springframework.sbm.java.migration.conditions.HasTypeAnnotation;
import org.springframework.sbm.mule.actions.JavaDSLAction2;
import org.springframework.sbm.mule.conditions.MuleConfigFileExist;
import org.springframework.sbm.project.resource.SbmApplicationProperties;

import java.util.List;

@RequiredArgsConstructor
@Configuration
public class MigrateMuleToBoot {
private final SbmApplicationProperties sbmProperties;

@Autowired
private JavaDSLAction2 javaDSLAction2;


@Bean
public Recipe muleRecipe() {
String name = "migrate-mule-to-boot";
String description = "Migrate Mulesoft 3.9 to Spring Boot.";

// Flag to enable TriggerMesh ransformation mode
if (sbmProperties.isMuleTriggerMeshTransformEnabled()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should better create two bean methods here, calling one factory method that creates the recipe twice with the flag true and false. Taht would then finally allow to remove the application property :)

name = "migrate-mule-to-triggermesh-boot";
description = "Migrate Mulesoft 3.9 to Spring Boot using TriggerMesh.";
javaDSLAction2.setMuleTriggerMeshTransformEnabled(true);
}

return Recipe.builder()
.name("migrate-mule-to-boot")
.description("Migrate Mulesoft 3.9 to Spring Boot")
.name(name)
.description(description)
.order(60)
.description("Migrate Mulesoft to Spring Boot")
.condition(new MuleConfigFileExist())
.actions(List.of(
/*
Expand Down
Loading