SAP Cloud Platform together with Cloud Foundry is offering the flexible cloud native enterprise platform to build, deploy and manage applications. SAP is key member and contributor in Cloud Foundry community. There have been great advantages of Cloud Foundry in SAP’s platform-as-a-service strategy, due to which SAP Cloud Platform services in Neo environment have already started being provisioned in Cloud Foundry. Business rules is one of them
SAP Cloud Platform Business Rules Service in now available in Cloud Foundry Trial and Enterprise environment – for both platform-as-a-service (PaaS) and software-as-a-service (SaaS) engagement model. It has been released currently for CF/AWS environment in Europe(Frankfurt) data centre (eu10).
I have put together this quick guide series that introduces you to the business rules service in Cloud Foundry where I have divided the learnings into 2 parts:
- In first part, you will learn how to subscribe to business rules application, use rules editor to model, manage and deploy rules and execute/test business rules service in Cloud Foundry
- In the second part, you will learn on how to consume the service from custom application deployed in Cloud Foundry.
In this first blog, I will concentrate on how you can use business rules application in Cloud Foundry in just four steps: (a) Business Rules Application Subscription, (b) User Role Assignment, (c) Accessing Business Rules Editor application to model rules and (d) Creating service instance and service keys to execute the business rules
I. Enable Business Rules Application Subscription
- Login to SAP Cloud Platform trial
[Note: If you are not a registered user then first sign-up for the trial account and then login with the set username and password.]
- Click on Cloud Foundry Trial to create (if you are login for the first time) and open the trial account. Cloud Foundry is a limited period trial for first 90 days and then you can choose to extend it for another 30 days.
- You will see that a global account and subaccount is already created for you with name <your-user-name>trial and trial respectively, as shown. Click on Go to Space
As a concept in Cloud Foundry accounts management, Global account represents the scope of the functionality and operations you are exposed to. A global account can have one or more sub-accounts that allows you model, deploy and manage your services and applications. For more information, you can read here
- Click on the trial subaccount tile to navigate into the account. It is from this subaccount that we are going to subscribe and configure business rules service.
In the Overview page you will see all the quota assigned for various services and memory allocations for applications in your trial account. This quota or resource allocation defines which and to-what-extend you can use specified set of resources.
II. Access Business Rules Editor Application via HTML Application Repository (new & recommended)
Before I start this section, I would like to highlight certain things about business rules editor. To those who are aware of business rules editor in Neo environment – there is absolutely no change. Infact, the editor in Neo and Cloud Foundry is EXACTLY the same. No learning experience required.
For those who have not seen/used business rules editor before, follow the blogs to know how-to model, manage and deploy rules. This section would not cover modelling of the rules. I would recommend you to go-through the blogs to familiarize yourself with modelling notations and operations for business rules.
Assuming that you know how-to work with business rules editor, let us understand how to get access to the editor. This can be done in two ways (a) via HTML Application Repository – which is the newer way to get access to the business rules application and (b) via Subscription – which is the old way of enabling the subscription (as in section III below).
In this section, we talk about how to enable the business rules editor via HTML Application Repository. There are 2 ways how you can achieve this (a) one is using the command-line tool and (b) other is using the SAP WebIDE. In this blog, I will show using the command-line tool (which is always my personal preference) but you can replicate the same using SAP WebIDE. Both are the pretty simple and straight forward to perform.
- Create the folder structure as shown in your eclipse environment (or anywhere in your local file system)
- Copy and paste the code mentioned in this documentation (step 2, 3 and 4) for package.json, xs-app.json and mta.yaml respectively.
- Execute the following commands one-after-another to create the MTAR file. This MTAR file will be deployed in the CF environment to create required approuter and service instance to access business rules editornote: Open the command-line tool and ensure that you are in businessrules_editor/approuter folder and then run these commands
npm config set @sap:registry https://npm.sap.com npm install
⇒ Once you complete the installation, you will notice the node_modules folder created inside your businessrules_editor/approuter folder
- Navigate to the parent folder but before you run the nest set of commands ensure that you have download the mta jar file from cloud tools under section Multi-Target Application Archive Builder
cd .. java -jar <path to mta jar downloaded from cloud tools> --build-target=CF build
⇒ Once the command is successfully completed, you will see a businessrules_editor.mtar generated in your folder as shown in the screenshot
- Now we need to deploy the generated MTAR file into the cf environment. For that you need to login the to CF using the following commands and enter the username, password of the account and org where you want to deploy the application
cf api <api-endpoint> cf login
⇒ before you deploy you need to ensure that you have the right set of commands installed. For this you run the command cf help and you must see deploy command listed as shown:
⇒ If you do not see this listed, then you need download and install CF MTA Plugin from cloud tools
(goto the readme text of the highlighted link and install the plugins are mentioned there)
⇒ Once you are ready, run the deploy cf command in command-line
cf deploy <location of the generated mtar file>
If you go to the Applications of your space (where you have deployed the MTAR), you will see bpmruleconsumerapprouter created and started
you can also click the application link to see the Service Bindings
III. Access Business Rules Editor Application via Subscription
(old & deprecated)
- Click on the Subscriptions (from left vertical panel) and click on tile to open the business rules standard application configuration page. In this page, click on the Subscribe button to enable the business rules application to be used.
… wait for the subscription to complete and once done successfully, you will see the business rules is shown as Subscribed
- Open Manage Roles to see various roles provided with the subscription. RulesRepositorySuperUser is to get access to rules editor and repository APIs to model and manage rules. RulesRuntimeSuperUser is to get access to deploy and execute rules.
IV. Use Business Rules Service Broker to invoke rules
In the previous sections, you learnt how you can enable/subscribe business rules application in Cloud Foundry and access the rules editor to model, manage and deploy rules. In this section, you will learn about the service broker of business rules – which will be used to create service instance and execute rules in the platform. I will also show you how you can test your rules from any available rest client using service keys of a service instance.
- Open your Global Account in Cloud Foundry trial and go to Entitlements
- Check if you can see Business Rules Service Broker and then navigate to the trial subaccount
- While working on trial, a dev space would already have been created for you. Go into the dev space to create service instance and bindings
- Open Business Rules service from Service Marketplace and create a new instance with name of your choice. For more information on services in Cloud Foundry, read here
While creating the service instance keep all the options as they appear by default and give the Instance Name of your choice.
- Now lets us create service keys that will generate credentials which will be used to communicate with the service instance. To do so, navigate into the service instance you have created above
- Switch to Service Keys and create a new service key with name of your choice
Here you have to note down certain values because we use them while testing our business rules from rest client and also when you want to consume this service from your custom application:
- clientid: used as username for any grant-type option
- clientsecret: used as password for any grant-type option
- url: used to generate the authorization token to access APIs
- runtime_url: host url to execute the rules runtime APIs
- repository_url: host url to execute rules design time APIs
V. Configure Trust and Assign Authorizations
Now you have to assign roles (to your user) associated with the service instance of this business rules service broker. You can either create a new role collection or update the existing one.
- Open your trial subaccount and go to Members. Check if your username is already added. As this is a trial account, you should be already a member, by-default.
- To assign business rules roles, first you need to create the role collection. For that, open Security –> Role Collection and add a new role collection with name of your choice let’s say “Business Rules”. If you already have a role collection where you want to add business rules roles, then you don’t need to create a new collection.
- Go into the role collection to add 2 roles (RuleRepositorySuperUser and RuleRuntimeSuperUser of bpmrulebroker) to the collection. These roles are required to access the business rules editor to model, manage and deploy rules.
- Open Security –> Trust Configuration to assign the role collection to your IDP service (which is SAP IDP is case of trial)
Enter your user id or email address to see what role collections are assigned to you. To assign the newly created role collection to your user, use Add Assignment option. If you already have a role collection where you just want to assign role associated with business rules, then you do not need do this step.
VI. Access Business Rules Editor Application
(new & recommended)
- Bind the service instance and application by
- going into the space and open Services –> Service Instances
- select the service instance for business-rules service as shown
- Click on the “link” icon
- In the pop-up, select the bpmruleconsumerapprouter application to bind this application with the approuter and Save the configuration.
- Due to new bindings, the given application need to be restarted. Go into the Applications and select the bpmruleconsumerapprouter application.
- From Overview section and click on the Restart button.
- Wait for the application to the restart
Now, click on the link under Application Routes (as highlighted in green in above image) – and this is the business rules editor available for you to model and manage rules in CF environment
(old & deprecated)
This is the old way (not recommended) of enabling business rules editor. Use the above mentioned steps to enable business rules editor.
warning: The business rules subscription will soon be removed from the list and you will loose the editor URL. However, all the content will still be there and will revive back once you enable the business rules application via HTML application repository
- Open the your trial sub account,
- click on Subscriptions and got into Business Rules subscription
- click on Go to Application link to open the business rules editor
… this logins you to the business rules editor – which is your one-stop to model, manage and deploy your business rules in Cloud Foundry.
So, now you have setup to the business rules application, assigned all desired roles to your user and opened the editor for modelling.
You can choose to model your own rules or as a sample you can import Shopping Cart Promotion Rules from GitHub. If you opt for latter, then after you have imported the rules, you need to activate the artefacts (in this given order: Data Objects, Rule Services, Rules and Rulesets) and deploy the rule services. As you deploy the rule service, a REST service is created which can be accessed via business rules runtime public APIs.
VII. Test Business Rules
As final step, let us now test the rule service from any rest-client. I will use Postman here, but you can use any available client you have.
- Open Postman and get the access token first. Here are the details:
where https://mytrial.authentication.eu10.hana.ondemand.com is the url from service key
Authentication: Basic Auth
Username: < clientid>
username: <your cloud foundry trial username>
password: < your cloud foundry trial password>
when you make the call, you will get the access token in response body. This is the bearer access token that you will use to execute the rules.
- Create a new tab in Postman and enter following details according to your trial. As an example, I am executing the sample Shopping Cart Promotion Rules that I referred in the previous section. If you have your own rules, then you need to change the URL and body accordingly.
URL: https://bpmruleruntime.cfapps.eu10.hana.ondemand.com/rules-service/rest/ v1/rule-services/java/ShoppingCartPromotionRules/DiscountRuleservice
where https://bpmruleruntime.cfapps.eu10.hana.ondemand.com is the runtime_url from service key
Authorization: Bearer <access_token>
“Category”:”Flat Screen TVs”,
“Soldby”: “Very Best Screens”
“Name”: “Very Best Screens”
when you execute the business rules, you can see the discount output from the rules in the response body as shown. [Note: I am also showing you the snippets of the decision table rule so that you can understand the response we got after execution of the business rules]
You can also run repository APIs (in similar fashion) to model and manage the business rules (in case you do not want to use the editor and work on purely PaaS model). In the above example, I showed you how to authenticate business rules runtime APIs with password-grant type, but you can also use other grant-types to call the service.
This concludes the first part of the blog where I focussed mainly on business rules application subscription and using business rules service broker instance to model, manage, deploy and invoke rules. I will soon publish the second part of the blog, until then try and use the business rules service in Cloud Foundry and connect back to me if you have any queries or feedback.
♦ Second part of the blog is published now!