Nowadays many companies choose to build business process extension projects for the SAP product they use and deploy on SAP Business Technology Platform, so that they could write the custom code and integrate with other SAP managed services or SaaS (software as a service) easily and quickly. At the same time, hyperscalers (AWS, GCP, Azure, etc…) are playing an increasingly significant role today as they help companies lower the capital expenses, increase scalability and elasticity to the system, and enhance the performance of the system. Under such circumstances, it would be better for us to understand how to leverage the services provided by the hyperscaler while developing the BTP based business process extension project, so that we could be benefit from the advantages provided by both SAP Business Technology Platform and hyperscaler.
In this blog, I will show you how to integrate Amazon Simple Notification Service (Amazon SNS) with SAP Cloud Application Programming Model (CAP), to build an SAP S/4HANA business process extension App and receive email notifications by leverage the Amazon SNS (Simple Notification Service) service. In this blog, we will focus on how to implement Amazon SNS Service within CAP (Cloud Application Programming) App to send out email notifications.
Background Information
The SAP Cloud Application Programming Model (CAP) is a framework of languages, libraries, and tools for building enterprise-grade BTP based services and applications. We could develop the BTP-based business applications by using Java and Node.js. Using the Cloud Application Programming model, we could significantly minimize the volume of codes we need to write and consume many services out-of-box.
The Amazon Simple Notification Service (Amazon SNS) is a fully AWS managed publish/subscribe based service for both Application-to-Application (A2A) and Application-to-Person (A2P) communication. It provides Topic for high-throughput, push-based, many-to-many messaging between distributed systems, microservices, and event-driven serverless applications. With Amazon SNS topic, we could also send out messages to subscriber at scale via email, mobile text (SMS), and even mobile push.
Business Scenario
The business scenario we chose for this POC project is SAP S/4HANA Business Partner Validation. There is a third-party firm/team responsible for validating all newly created or changed business partners data in SAP S/4HANA system for a company. This third-party firm/team will do the validation through the standalone S/4HANA business process extension App deployed on SAP business technology platform, instead of log into the SAP S/4HANA system directly. In this standalone extension App, the validator could review the business partner’s data, perform updates on the business partner address if necessary, and mark business partner’s validation status as NEW, IN PROCESS, VALID, and INVALID based on the condition. The validators could receive notifications through email whenever there is a business partner’s data that needs to be validated.
Architecture Diagram
As shown on the architecture diagram above, the entry point of the entire system would be the SAP S/4HANA admin creates a new business partner or modify the existing business partner in the SAP S/4HANA On-Premises system. Then the newly created or changed business partner data will be published to the message queue in the SAP Event Mesh within the SAP Business Technology Platform subaccount via the SAP S/4HANA messaging mechanism called Enterprise Messaging. There is a Java CAP App listening on the message queue, once there is a new business partner data reached message queue, it will consume the message, persist the business partner data into the SAP HANA Cloud database, and publish the message to the topic within Amazon SNS service. The business partner validators would subscribe themselves to the Amazon SNS topic, and once the message sent from Java CAP App reached the Amazon SNS topic, they will receive an email notification and remind them there is a business partner waiting for them to perform validation. They could easily jump to the website by clicking the Fiori Launchpad URL within the email, modify the address data for the business partner if necessary, and update the verification status code for the business partner. Initially the business partner in the SAP S/4HANA On-Premises system would be blocked. Once the validator updates the business partner verification code as VERIFIED in the standalone business partner validation application, the Java CAP app will send out an update request to the SAP S/4HANA business partner OData API, to unblock the business partner and release the business partner in S/4HANA system.
Configure Amazon SNS Service
In order to let the CAP App publish message to the Amazon SNS Topic successfully, there are few things we need to configure on the Amazon SNS Service.
1. Create a new IAM user with Programmatic Access Key ID & Secret Access Key in the AWS IAS Management Console
Make sure to note down the Access Key ID & Secret Access Key since we will need it in the business partner validation application.
2. Create the topic with proper name under the AWS SNS Service
Make sure to note down the Topic ARN since we will need it later.
3. Create a new user group in the AWS IAM, add the newly created IAM user into the group.
4. Generate an identity based IAM policy in the AWS IAM. In the policy set Effect as Allow, add sns:Publish in the Action, and then paste the Topic ARN in the Resource. In this way, we can allow AWS principles to publish message to the SNS Topic if they have this policy attached to them.
5. Attach the policy to the IAM user group we just created. So that all the users within this group have the permission to publish message to the AWS SNS Topic.
Code Implementation
Amazon provides the AWS SDK (Software Development Kit) which simplifies use of AWS services by providing a set of libraries and clients that we could use directly inside of our SAP CAP Java code. In this section, I will show you how to use AWS SDK to publish message to the AWS SNS Topic we just created.
1. Add AWS SDK Dependency in the pom.xml file in your CAP JAVA application
<!-- AWS SDK SNS -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sns</artifactId>
<version>2.17.228</version>
</dependency>
2. Configure the SnsClient in the code by providing the programmatic IAM user’s access key ID & access secret key. The following code example shows how to configure the SnsClient programmatically.
@Value("${amazonProperties.accessKey}")
private String awsAccessKey;
@Value("${amazonProperties.secretKey}")
private String awsSecretKey;
private SnsClient getAwsSnsClient() {
Region region = Region.of(bpValidationTopicRegion);
AwsBasicCredentials awsCreds = AwsBasicCredentials.create(awsAccessKey, awsSecretKey);
SnsClient snsClient = SnsClient.builder().credentialsProvider(StaticCredentialsProvider.create(awsCreds)).region(region).build();
return snsClient;
}
3. Then we are good to use this SnsClient to publish the message from our JAVA CAP App to the Amazon SNS Topic. The following code example shows how to publish messages to the Amazon SNS topic.
@Value("${launchpadProperties.business-partner-validation-ui}")
private String launchpadURL;
// Step 4. Send Email Notification To Validator Through AWS SNS
PublishResponse publishResult = null;
try {
String subject = "New S/4HANA Business Partner Validation Notification";
StringBuilder message = new StringBuilder();
message.append("Business Partner " + bpId + " Needs Validation. Please Validate ASAP");
message.append("\n");
message.append("Perform Validation By Click: " + launchpadURL);
SnsClient client = getAwsSnsClient();
PublishRequest request = PublishRequest.builder()
.topicArn(bpValidationAWSTopic)
.subject(subject)
.message(message.toString()).build();
publishResult = client.publish(request);
} catch (SnsException e) {
resp.setValidationResponse("500");
resp.setValidationMsg("Business Partner " + bpId + " Validation Completed, But Message Publish Failed");
context.setResult(resp);
context.setCompleted();
}
resp.setValidationResponse("200");
resp.setValidationMsg("Business Partner " + bpId + " Validation Completed.");
context.setResult(resp);
context.setCompleted();
With the code implementation above, the JAVA CAP App would be able to publish messages to the Amazon topic successfully. The subscriber of the Amazon topic we created in the previous step would receive email notification whenever there is a message published to the topic. The email notification within this POC project would be looks like on below.
No comments:
Post a Comment