Layer7 API Management

 View Only
  • 1.  How can I create a simple arithmetic operation (a   b, for example)?

    Posted Jan 30, 2015 12:14 PM

    I have to do some logic like "if x <= a + b { //do something }", and I can't find an easy way to do that?

     

    Does anyone have a clue?



  • 2.  Re: How can I create a simple arithmetic operation (a   b, for example)?
    Best Answer

    Broadcom Employee
    Posted Jan 30, 2015 12:22 PM

    This can be done using a few assertion to calculate the 2 numbers and then compare if the result is less than another number. Below you will find a policy fragment on how to do this.

    <?xml version="1.0" encoding="UTF-8"?>

    <wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">

        <wsp:All wsp:Usage="Required">

            <L7p:SetVariable>

                <L7p:Base64Expression stringValue="PGEvPg=="/>

                <L7p:ContentType stringValue="text/xml; charset=utf-8"/>

                <L7p:DataType variableDataType="message"/>

                <L7p:VariableToSet stringValue="math"/>

            </L7p:SetVariable>

            <L7p:SetVariable>

                <L7p:Base64Expression stringValue="MTU="/>

                <L7p:VariableToSet stringValue="a"/>

            </L7p:SetVariable>

            <L7p:SetVariable>

                <L7p:Base64Expression stringValue="MTI="/>

                <L7p:VariableToSet stringValue="b"/>

            </L7p:SetVariable>

            <L7p:SetVariable>

                <L7p:Base64Expression stringValue="MzA="/>

                <L7p:VariableToSet stringValue="result"/>

            </L7p:SetVariable>

            <L7p:ResponseXpathAssertion>

                <L7p:VariablePrefix stringValue="simpleMath"/>

                <L7p:XmlMsgSrc stringValue="math"/>

                <L7p:XpathExpression xpathExpressionValue="included">

                    <L7p:Expression stringValue="$a + $b"/>

                    <L7p:Namespaces mapValue="included">

                        <L7p:entry>

                            <L7p:key stringValue="s"/>

                            <L7p:value stringValue="http://schemas.xmlsoap.org/soap/envelope/"/>

                        </L7p:entry>

                    </L7p:Namespaces>

                    <L7p:XpathVersion xpathVersion="XPATH_1_0"/>

                </L7p:XpathExpression>

            </L7p:ResponseXpathAssertion>

            <L7p:ComparisonAssertion>

                <L7p:Expression1 stringValue="${simpleMath.results}"/>

                <L7p:Expression2 stringValue="${result}"/>

                <L7p:FailIfVariableNotFound booleanValue="false"/>

                <L7p:Operator operator="LE"/>

                <L7p:Predicates predicates="included">

                    <L7p:item binary="included">

                        <L7p:Operator operator="LE"/>

                        <L7p:RightValue stringValue="${result}"/>

                    </L7p:item>

                </L7p:Predicates>

            </L7p:ComparisonAssertion>

        </wsp:All>

    </wsp:Policy>



  • 3.  Re: How can I create a simple arithmetic operation (a   b, for example)?

    Posted Feb 02, 2015 03:36 AM

    Its works perfectly!

     

    Thank you very much!

     

    Best regards.



  • 4.  Re: How can I create a simple arithmetic operation (a   b, for example)?

    Broadcom Employee
    Posted Feb 02, 2015 09:56 AM

    We do have a custom assertion that does this for us!

     

    Sent from my iPhone



  • 5.  Re: How can I create a simple arithmetic operation (a   b, for example)?

    Posted Feb 05, 2015 12:31 PM

    The XPath assertion should cover your basic math needs.

     

    If you're doing anything more sophisticated that XPath can't do - e.g. mathematical functions like SIN and LOG -  there's a Tactical "Evaluate Math Expression" assertion that might be of value. Talk to Support if you need it.



  • 6.  RE: Re: How can I create a simple arithmetic operation (a   b, for example)?

    Posted 4 days ago
    Hello,

    I tried xpath assertion as you mentioned, but it is NOT RELIABLE. 

    I need to calculate 86400 + ${gateway.time.seconds} for every request.

    Let me explain;

    Example for successful calculation;
    when a = 86400 and b =1674554585

    $a + $b = 1.674640985E9  --> the result is 1674640985, it works fine for this values
     
    Example for wrong calculation;
    when a = 86400 and b = 1674554480
    it calculates;
    $a + $b = 1.67464088E9 --> But the result is 1674640880, not 167464088. There is a missing zero.

    It is very hard to understand that why are you so insistent about not developing an basic calculator for almost ten years?



  • 7.  RE: Re: How can I create a simple arithmetic operation (a   b, for example)?

    Broadcom Employee
    Posted 4 days ago
    Did you try the Javascript assertion ? 

    https://techdocs.broadcom.com/us/en/ca-enterprise-software/layer7-api-management/api-gateway/10-1/policy-assertions/assertion-palette/service-availability-assertions/execute-javascript-assertion.html

    ------------------------------
    Aran White - [JobTitle]
    [CompanyName]
    [State]
    ------------------------------



  • 8.  RE: Re: How can I create a simple arithmetic operation (a   b, for example)?

    Posted 3 days ago
    Hello Aran,

    Yes, after the situation about xPath I tried  javascript assertion and Perform JDBC Query assertion for calculation.

    It is so simple to calculate with Perform JDBC Query Assertion.
    Query could be like;
    SELECT 360+86400 AS mathSum; 
    But I am not sure about that could affect the performance seriously or not due to using database connection. 

    Javascript assertion can be more effective.

    Thanks



  • 9.  RE: Re: How can I create a simple arithmetic operation (a   b, for example)?

    Broadcom Employee
    Posted 2 days ago
    policy might look like this

    JS would look like this

    var vara = context.getVariable('a');
    var varb = context.getVariable('b');
    var total = vara + varb;

    context.setVariable("js.total", total);



    ------------------------------
    Aran White - [JobTitle]
    [CompanyName]
    [State]
    ------------------------------