Performing Mathematical function assertion Policy
search cancel

Performing Mathematical function assertion Policy

book

Article ID: 42952

calendar_today

Updated On:

Products

CA API Gateway

Issue/Introduction

How do I perform mathematical calculations within a policy?

Environment

Release: API Gateway 10.x, 11.x

Resolution

There are multiple ways you can do this. 

a) Evaluate math assertion (tactical)

b) Within xpath built in assertion

Use case: Executing mathematical functions within Policy.

Description: Sometimes in policy the need arises to do some basic mathematical functions in order to facilitate a use case. While this can be facilitated through custom assertions to manage this, the Gateway can also do it with the standard core assertion set.

Setup: In order to do XPath math, drag a Response XPath over into our Gateway's policy workspace. In the 'XPath:' window enter the mathematical function to be executed. This field supports a very flexible array of functions as demonstrated in the attached policies. To access the result of the equation, use the *.results context variable that is created by this assertion. The prefix to *.results will depend upon what is used as the variable prefix in the lower left hand corner of the Response XPath Assertion. For example, in the use case pictured below, the result would be '2.0' and accessed via the 'math.results' context variable:

 


To get a bit more complex, use context variables in the XPath field. The variables used should be saved as a string, and invoked in the 'XPath:' field in the format $variableName. Note the lack of curly brackets around the variable [ {} ]. The policy below shows how this could be utilized to ensure there is an attachment on a message by taking the total request size, subtracting the size of the main body of the message, and seeing if there is a remainder.

 


There are plenty of other ways to use this functionality as well. Attached to this article are two stripped down policies showing how we can use XPath for both simple and more complex math. The XPath*Sample policy shows very basic +/- math. Also attached is a sample policy that will do XPath Math to round up, down, to decimal place or to do the mround function.

 

 

Attachments

1558688556073000042952_sktwi1f5rjvs16fmu.jpeg get_app
1558688554225000042952_sktwi1f5rjvs16fmt.jpeg get_app
1558688552431000042952_sktwi1f5rjvs16fms.jpeg get_app
1558688549688000042952_sktwi1f5rjvs16fmr.jpeg get_app
1558534568915TEC0000001235.zip get_app