Use the switch component to execute one of a number of sections of the assembly based on which specified condition is fulfilled.

Gateway support

Table 1. Table showing which gateways support this policy, and the corresponding policy version
Gateway Policy version
DataPower® Gateway (v5 compatible) 1.0.0
DataPower API Gateway 2.0.0

This topic describes how to configure the policy in the assembly user interface; for details on how to configure the policy in your OpenAPI source, see switch.

A switch construct provides a way to branch an assembly based on multiple conditions. Each switch component contains multiple cases, each corresponding to a section of the assembly that is only executed when the condition or operation specified by the case is met or used. Additionally, an otherwise case executes when no other case is fulfilled.

Add new cases by clicking + Case and add an otherwise case by clicking + Otherwise.

If multiple cases are fulfilled, the highest priority case will be executed. Change the priority of cases by clicking the Move up The Move up iconand Move down The Move down icon icons.

To configure a case to be executed if a specific operation is called, use the search operations field and select your operation from the list. You can refine the results of the search by typing in the search operations field.

To configure a case to be executed based on a GatewayScript condition, click edit condition and enter your script in the Condition editor window. When you have supplied a script, you can edit your script either in the Condition field or by clicking edit condition.

If you are using the DataPower Gateway (v5 compatible), you enter your condition as GatewayScript directly in a code area in the Condition editor. If you are using DataPower API Gateway, the Condition editor provides a script builder to help you construct your condition script; for more information, see Using the switch policy condition editor.

To delete a case, click the Remove case icon The Remove case icon.

If you want one or more policies or constructs to be executed when the condition of a case is fulfilled, drag the new policy or construct onto one of the dashed boxes that are displayed within the case's section of the switch construct.

Note: A switch case must contain at least one policy, otherwise the Gateway server returns an error.

Construct property details

You can configure a construct's properties in the property sheet in the assemble view.

Table 2. The properties of a switch construct
Property Required Description
Title No A custom title for your construct when it is displayed in the canvas. If a title is not specified, switch is used by default.
Description No A description of your construct, it is not displayed on the canvas.
case Yes (one or more)

Specify one or more operations or write a script for a condition.

DataPower API
GatewayUse the Condition editor to construct your condition script. See Using the switch policy condition editor.

GatewayUse GatewayScript to provide conditions. You can reference variables by using the form apim.getvariable('context.location.variable'), where context is the context that you want to reference, location is the location of the variable within that context, and variable is the name of the variable. A list of context variables that you can use to generate conditions can be found in API Connect context variables.

otherwise No Add an otherwise case if you want to execute a section of the assembly when no other cases are fulfilled.