Overview

Skill Level: Beginner

This tutorial goes through the setup of a router gateway using gateway script for a router service.

Ingredients

  • Access to a DataPower application domain.
  • cURL or postman to test the service.
  • Import the Mock_XMLF service into a sandbox domain on DataPower. The port used is 2000. Please update the port if this port is already used on the appliance.
    • This service will contain a mock service to emulate different system responses based on different inputs.

Step-by-step

  1. Create the Router Multi-Protocol Gateway

    This section will show case creating the MPGW for the Router service. 

    The Router service will interrogate the input body of the request, and based on the input being, “name”, “accounts”, or “beneficiaries”, will direct traffic to a back endpoint based on the input.

    1. Log into DataPower and into a sandbox application domain for this lab.
    2. Click on the File Management icon. Click on the Actions… for the local:/// directory and click on Create Subdirectory.
      02.filemanagement
    3. Create the file directory Router_MPG.
      03.createdRouter_mpg-1
    4. Upload the router.js file. 
      04.uploadfiles-1
      NOTE: This router.js script uses DataPower GatewayScript APIs, which are similar to DataPower extension function on the XSLT side. In the script, you will see that the skipBackside and setVar are used to update DataPower content variables.
      The “service.mpgw.skipBackside = true” tells the gateway that there is no backside endpoint and to loop back the request or service output to the client.
      The “service.setVar (‘var://service/routing-url’, ‘<endpoint-here>’)” tells the gateway what the backside endpoint will be.
      This script will be the primary logic where the input contains a certain key value, the javascript IF statement will trigger the extension setVar() function which directs the traffic to the backend depending on the input key value.
    5. Navigate back to the main page and click on the Multi-Protocol Gateway icon. Click on Add.
      01.mpgw_
    6. When the Configure Multi-Protocol Gateway windows opens, ensure name the service Router_MPG, then right under, select dynamic-backends type, then locate the Request Type and Response Type, and change them both to Non-XML.
      05.mpgConfig

    7. Locate the Front Side Protocol and click on the Add icon. Select the HTTP Handler.
      Once in the Configure HTTP Handler, title the name HTTP_Router_FSH. Enter in the port 2050, or an open port available for your to use (remember this port since it will be used later to test with). Check the GET method to enable and click Apply.
      06.fhs_
    8. Back in the Configure Multi-Protocol Gateway, locate the Processing Policy and click the add icon.
      07.processingpolicy
    9. When the Configuration Multi-Protocol Gateway Style Policy pops up, title the Policy Name: Router_Policy and click Apply Policy.
      08.titlepolicy
    10. Click on New Rule, drop down the Rule Direction to select Client to Server. Double-click the Match Action icon and use the “default-accept-service-providersMatching Rule. Click Done.
      09.match_
    11. Locate the GatewayScript Action icon, drag and drop after the Match Action and double click to open. Once opened, locate the GatewayScript file section, drop down the first section to select local:///Router_MPG, and drop down the section section below it and select router.js. Leave the input and output as default, it will automatically update once the configuration is saved. Select Done.
      10.gws_
    12. Once back in the Configure Multi-Protocol Gateway Style Policy window, click on the Apply Policy, then click on New Rule again to create a response rule.
      Update the Rule Name to “Router_Policy_rule_response” and drop down the Rule Direction and select Server to Client.
      Double-click on the Match Action and use the “default-accept-service-providers“.
      Click Apply Policy.
      Click the link on the upper right Close Window.
      11.response

    13. Back in the Configure Multi-Protocol Gateway, click Apply and Save Configuration
      12.applySaveConfig
  2. Testing & Conclusion

    From Postman or using cURL, invoke the Router_MPG service and you should see different responses based on the content sent.

    For example, if there is a request for “name”, the response will bring back profile details:
    13.profiletest
    If there is a request for “accounts”, the response will bring back account details:
    14.accountstest
    If there is a request for “beneficiaries”, the response will bring back beneficiary details:
    15.beneficiariestest

    As you can see, the router.js is similar to the content based routing from a stylesheet IBM tech note: https://www.ibm.com/support/pages/content-based-routing-stylesheet-using-routing-url-method-websphere-datapower-soa-appliance

    The <dp-set-target> and <dp:set-variable name=”‘var://service/routing-url'” value=”$targetURL”/> extension functions are similar to “setVar(‘var://service/routing-url’)” found in the router.js script as shown below.

    The gatewayscript apis used in the router.js script may be used to route requests to multiple backends based on different content, such as:

    • the inbound request URI:
      • serviceVars.URI
    • a certain HTTP Header: 
      • var hm = require(‘header-metadata’);
    • query paramters:
      • var query = new String(service.URI);
        var querys = query.split(“=”);

    More GatewayScript APIs may be found in the IBM DataPower Knowledge Center under the GatewayScript APIs section.

    More service variables may be found in the IBM DataPower Knowledge Center under the Service Variables section.  

     

    var hm = require('header-metadata');
    var service = require('service-metadata');

    //Read input
    session.input.readAsBuffers(function (error, input) {
    if (error) {
    // handle error
    session.output.write (error.errorMessage);
    }
    else {

    if (input == '') {
    session.output.write("Please enter name, accounts, or beneficiaries.");
    service.mpgw.skipBackside = true;
    }

    //If input contains "name", route traffic to /profile endpoint.
    if (input.indexOf('name') > -1) {
    service.setVar('var://service/routing-url', 'http://localhost:2000/profile');
    }

    //If input contains "accounts", route traffic to /accounts endpoint.
    if (input.indexOf('accounts') > -1) {
    service.setVar('var://service/routing-url', 'http://localhost:2000/accounts');
    }

    //If input contains "beneficiaries", route traffic to /beneficiaries endpoint.
    if (input.indexOf('beneficiaries') > -1) {
    service.setVar('var://service/routing-url', 'http://localhost:2000/beneficiaries');
    }

    }
    });

     

     

     

  3. Artifacts

    1. Router_MPG.zip –> Solution exportrunning on port 2050.
    2. Mock_XMLF.zip –> The mock service running on port 2000.

Join The Discussion