When building DataWeave transformations for your Mule application, you will run into situations in which you will need to invoke external logic that may be encapsulated in a Java POJO, Groovy, Python, Ruby script, or really any lookup that uses a CSV file or database table as part of the transformation.
For example, you may need to perform a dynamic mapping between status codes in your input payload and corresponding statuses received by the output system, where the mapping lives in a CSV file or a database table. It could even be a real-time call out to an HTTP endpoint to validate account numbers in a separate application.
For such scenarios, DataWeave provides the capability to call any flow within Mule applications, providing you – the user – the flexibility to achieve a myriad of use cases, such as the ones described above, and more.
In this HowTo, we will look at an example of how to invoke logic that resides outside DataWeave, either within the same Mule application, another Mule application, or an entirely different application.
Before beginning the tutorial, please download Anypoint Studio and ensure that you have the Anypoint Connectors for the end-application or system where the logic resides in. For example, if it is a SaaS application like Workday or ServiceNow, you would need to install the connectors for those respective applications in Anypoint Studio.
Now, let us begin. Imagine an airline flight scheduling scenario in which the input payload has one or more flight numbers, and we need to derive the correct airline code based what number range a flight number falls in.
Step 1: Define the flow in the Mule application
- First, we need to define a flow in our Mule application, which will receive the flight number as an input. In this definition, we implement the Groovy logic to derive the corresponding airline code, which is known as the IATA Carrier code.
- After invoking Groovy component, the payload is set as the return value from the Groovy code, which holds the IATA Carrier code. Here’s the flow definition from the Mule configuration file:
<flow name="LookUpIATACarrierByFlightNumberCodeFlow" processingStrategy="synchronous" doc:description="Return IATA Carrier Code"> <scripting:component doc:name="Groovy"> <scripting:script engine="Groovy"> <![CDATA[def IATA = "UNKN" def flightnumber = payload int flightnumberInt = flightnumber.toInteger() switch (flightnumberInt) { case 1600..1999: IATA = "RV" break case 7086..7099: IATA = "RV" break case 7500..7749: IATA = "RS" break case 7570..7579: IATA = "RS" break case 7060..7079: IATA = "RS" break default: IATA = "AC" break } return ["IATA":IATA]]]> </scripting:script> </scripting:component> <logger message="IATA Carrier is : #[payload.IATA]" level="INFO" doc:name="Log the IATA Carrier Code"/> </flow>
Step 2: Invoke the flow in DataWeave
- Then, in our DataWeave transformation, we invoke the flow defined above, passing in the variable that contains the flight number from the input payload.
The following is a snippet from the DataWeave definition:
IATACarrierCode: lookup("LookUpIATACarrierByFlightNumberCodeFlow", payload.flightDetail.identifier.flight.fnNumber).IATA
Please note that we specified the name of the return parameter as “.IATA” for the lookup function. This must match with what the Groovy sets in the logic as “payload.IATA” by returning the value in the variable called “IATA” as follows: “return [“IATA”:IATA]” as seen in the flow definition above in Step 1.
And that’s how it is done! This HowTo guide used an airline flight scheduling scenario to show users how they can invoke Groovy logic in DataWeave. With this process, we have the ability to define a flow that calls virtually any endpoint, connector, or message processor available on the palette, then return that information back or, alternatively, perform a function as part of the DataWeave transformation.
With this capability, DataWeave provides users with a number of ways to enrich data dynamically––from data in a heterogeneous set of systems and applications to services within transformations.
For more information, please refer to the reference documentation for DataWeave.