Integrating ABAP-Based Processes into Smart Factories

Integrating ABAP-Based Processes into Smart Factories

How SAP NetWeaver Application Server ABAP 7.5 Supports Industry 4.0 Scenarios

Published: 04/August/2016

Reading time: 14 mins

A constantly changing business landscape presents as many challenges as it does opportunities, not least of which is adapting to the latest technology trends. To help its customers take advantage of technological innovation, SAP has integrated a number of trends — such as in-memory technology, responsive user interface technology, and business networks — into its technology platforms. These trends have had a deep impact on the evolution and transformation of SAP technology, reflected by the development of the SAP HANA platform and the SAP Fiori user experience, and the addition of the Ariba, Fieldglass, and Concur solutions, for instance (see the sidebar “Major Recent SAP Technology Trends” for more on key trends and their influence on SAP technology).

Major Recent SAP Technology Trends

  • In-memory database technology: SAP HANA has transformed SAP customer landscapes. The platform’s analytical capabilities combined with its transactional power and consistency have brought together the previously separate OLTP and OLAP worlds. For example, rather than extracting financial data from existing ERP systems into data warehouses such as SAP Business Warehouse, analytical scenarios can run directly in the finance system. SAP S/4HANA is built on this principle.
  • Responsive user interface technology: Applications based on SAPUI5 (SAP’s implementation of HTML5) have redefined user productivity and processes. SAPUI5-based applications, such as SAP Fiori apps, can run seamlessly across a wide range of smart phones, tablets, and desktop computers. A large set of tools, such as SAP Web IDE, helps developers quickly prototype new SAPUI5-based scenarios, replacing traditional UI controls programming with smart templates that use well-defined semantic protocols based on the OData standard.
  • Semantic data models: Semantically rich data models describe business data by defining the underlying entity types (such as orders, business partners, or products) and the semantic relationships between them. Data definitions are written once as part of a common data model that can be used by tools of any kind for evaluation purposes. SAP provides a SQL-based core data services (CDS) infrastructure for semantic data modeling that includes concepts such as annotations, which describe how data should be interpreted in different contexts.
  • Lifecycle management: Application lifecycle management software allows organizations to upgrade and update their installed systems based on their unique needs while minimizing downtime (near zero downtime) or eliminating downtime completely (true zero downtime). SAP brings these capabilities to customers with the SAP Solution Manager application management solution and a support package strategy for delivering enhancements that enables customers to implement new functionality without disrupting critical business processes.
  • Business networks: Business networks are integral to the way organizations communicate and collaborate with their customers, partners, and suppliers, enabling increasing integration across business processes and assets. As these connected networks continue to expand, they also drive organizations to differentiate themselves by focusing on particular strengths. Many of the recent SAP acquisitions, such as Ariba and Concur, point to this trend.

There is now another trend taking hold that is transforming modern factories in a way that rivals the changes brought on by the invention of steam power and electricity: the Internet of Things (IoT). The ability of IoT-connected devices to talk to one another enables completely new “smart factory” scenarios, where machines and humans can collaborate in ways that were never before possible, such as robots assisting humans in complex manufacturing tasks. The production process in these smart factories represents a new evolutionary step called Industry 4.0.

SAP lays the foundation for its customers to take this next evolutionary step with support for IoT built into SAP HANA Cloud Platform. As of release 7.5, this support also extends to SAP NetWeaver Application Server (SAP NetWeaver AS) ABAP to support the integration of back-end processes, such as logistics and quality assurance processes, into Industry 4.0 scenarios. This article looks at how SAP NetWeaver AS ABAP supports Industry 4.0, and how to implement this support in your own SAP landscape.

SAP and Industry 4.0

Industry 4.0 makes it possible to integrate machines and sensors on the shop floor with IT systems along the entire value chain, even beyond a single enterprise. These connected systems can interact with one another using standard Internet-based protocols, such as the well-known Transmission Control Protocol (TCP), for faster, more flexible, and more efficient business processes, which fosters industrial growth and ultimately boosts the competitiveness of the enterprise. So how does SAP provide support for Industry 4.0 scenarios?

SAP HANA Cloud Platform supports the onboarding of IoT devices, such as temperature sensors, into an Industry 4.0 scenario in a simple and efficient way — you simply subscribe to a set of IoT services included with SAP HANA Cloud Platform and use an embedded cockpit tool to register devices and specify the supported message types for communicating data. Registered IoT devices then act as message senders and receivers for sending data to and from SAP HANA Cloud Platform.1

With SAP NetWeaver AS ABAP 7.5, it is now possible to establish a direct link between IoT devices and the on-premise ABAP stack, enabling you to integrate ABAP-based back-end processes into Industry 4.0 scenarios. For example, in a farming scenario you might want to track weather conditions to optimize the fertilizer ingredients, or in an assembly scenario you might want automated reports on the quality status of defective goods. Direct connection between the ABAP server and shop-floor devices eliminates the need for middleware components, simplifying the system landscape setup and lowering total cost of ownership.

SAP NetWeaver AS ABAP 7.5 enables this integration between shop-floor devices and back-end systems via a channel infrastructure that includes ABAP push channels (where the ABAP back end propagates a change to the front end) and ABAP messaging channels (where ABAP sessions exchange messages using a publish/subscribe model). Introduced with support package stack (SPS) 05 for SAP NetWeaver AS ABAP 7.4,2 the ABAP push channel and ABAP messaging channel infrastructure includes native support for the widely used TCP standard.

Let’s take a closer look at the technical details behind the infrastructure that enables IoT communication in the ABAP stack, and how the new ABAP channel programming model included with 7.5 simplifies application development.

Enabling an ABAP-Based Industry 4.0 Scenario

With SAP NetWeaver AS ABAP 7.5, the ABAP stack can play different roles in the TCP-based communication infrastructure for an Industry 4.0 scenario. It can play the role of a TCP client when the ABAP stack sends a message to an IoT-connected device that is acting as the server, or it can play the role of a TCP server when the ABAP stack receives a message from an IoT-connected device that is acting as the client. It can also interface with all existing standard protocols, such as the WebSocket Protocol and HTTP, allowing you to build almost any kind of web application or integration scenario.

Implementing an ABAP Program as the TCP Client

Let’s first look at how the ABAP stack connects as a TCP client to an existing TCP server via an ABAP message channel, enabling the ABAP stack to send messages to an IoT-connected device (to push an update to a device, for instance).

You can use any TCP server to do this. For demonstration purposes, we’ll emulate a very simple server using the ncat program (freely available from nmap.org). Start ncat from your system’s command line interface and let it listen for incoming requests from TCP clients. On the ABAP side, a simple report creates a TCP client object and connects to the designated TCP server by specifying the TCP server’s host IP address and port number.

The code to implement the TCP client, shown in Figure 1, looks a bit verbose but is fairly straightforward. In essence, it creates a TCP client object, connects to the TCP server (in this example, the ncat program) via a host and port, upon return composes a binary message from the input field terminated by the newline character, and sends it to the TCP server. Then the program waits for an incoming message from the TCP server via an ABAP push channel (ABAP command “WAIT FOR PUSH CHANNELS”), which is then displayed on the front end in an output list (ABAP command “WRITE”). The code is well documented inline, where you can read it or simply copy it to your ABAP editor — the Eclipse-based ABAP development tools for SAP NetWeaver (known as ABAP in Eclipse) or the ABAP Workbench (transaction SE80), for instance — and then compile and run it without any changes.

REPORT ZTCPCLIENT.
PARAMETERS
: host type string,
            port type string,
            text type string DEFAULT 'Hello TCP, answer me!' LOWER CASE.
INITIALIZATION.
 host = cl_gui_frontend_services=>get_ip_address( ).
 concatenate '11' '88' into port.
DATA terminator type string.

CLASS apc_handler DEFINITION FINAL .
  PUBLIC SECTION.
    INTERFACES
if_apc_wsp_event_handler.
    DATA
message TYPE string.
ENDCLASS.

"Implement event handler to get events (on_start, on_message, on_close)
CLASS apc_handler IMPLEMENTATION.
  METHOD
if_apc_wsp_event_handler~on_open.
  ENDMETHOD.

  METHOD
if_apc_wsp_event_handler~on_message.
    TRY.
       
message = i_message->get_text( ).
       
"Cut of '0A' terminator
        data(len) = strlen( message ) - 1.
        message = message(len).
      CATCH cx_apc_error INTO DATA(apc_error).
        message = apc_error->get_text( ).
    ENDTRY.
  ENDMETHOD.

  METHOD if_apc_wsp_event_handler~on_close.
    message = 'Connection closed!'.
  ENDMETHOD.

  METHOD if_apc_wsp_event_handler~on_error.
  ENDMETHOD.
ENDCLASS.

CLASS apc_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS apc_demo IMPLEMENTATION.
  METHOD main.
    TRY.
        "Create event handler
        DATA(event_handler) = NEW apc_handler( ).

        "specify TCP frame information
        terminator = '0A'.

        DATA client type ref to if_apc_wsp_client.

        "create TCP client object and connect it to server
        client = cl_apc_tcp_client_manager=>create(
          i_host = host
          i_port = port
          i_frame = VALUE apc_tcp_frame(
            frame_type =
              if_apc_tcp_frame_types=>co_frame_type_terminator
            terminator =
              terminator )
          i_event_handler = event_handler ).

        client->connect( ).

        "Send initial 'hello' message from client
        DATA(message_manager) = CAST if_apc_wsp_message_manager(
          client->get_message_manager( ) ).
        DATA(message) = CAST if_apc_wsp_message(
          message_manager->create_message( ) ).
        DATA(binary_terminator) = CONV xstring( terminator ).
        DATA(binary_msg) = cl_abap_codepage=>convert_to( text ).
        CONCATENATE binary_msg binary_terminator
               INTO binary_msg IN BYTE MODE.
        message->set_binary( binary_msg ).
        message_manager->send( message ).

        "Wait for incoming messages from server
        CLEAR event_handler->message.
        WAIT FOR PUSH CHANNELS
             UNTIL event_handler->message IS NOT INITIAL UP TO 10 SECONDS.
        IF sy-subrc = 4.
          WRITE: 'No handler for APC messages registered!'.
        ELSEIF sy-subrc = 8.
          WRITE: 'Timeout occured!'.
        ELSE.
          WRITE: 'TCP client received: ', event_handler->message.
        ENDIF.
        client->close( i_reason = 'Application closed connection!' ).
      CATCH cx_apc_error INTO DATA(apc_error).
        WRITE: apc_error->get_text( ).
    ENDTRY.

  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  apc_demo=>main( ).

Figure 1 — The source code for the ABAP report that creates the TCP client object and connects to the TCP server

Let’s see what happens when we execute the report. First, in your ABAP editor (ABAP in Eclipse in the example), start the report with the standard report selection screen and specify the host IP address and port for the TCP server — for the example TCP server (the ncat program), this would be the local laptop or PC you are working with — along with a message to send to the TCP server (see Figure 2). In parallel, in the command line interface, start the ncat program listening on the specified host and port.

Figure 2

Figure 2 — Specify the IP address and port for the TCP server, and the message to send, in the standard report selection screen

Now execute the report in the ABAP editor. A connection is established to the ncat program, which acts as the TCP server in this scenario, and the message is transferred to the ncat program, which echoes it to the command line interface (see Figure 3). You can then type in a line in the command line interface, which is sent back to the ABAP-based TCP client and written to the output list in the standard ABAP list screen (see Figure 4).

Figure 3

Figure 3 — Enter a response for the TCP server

Figure 4

Figure 4 — The TCP server response displayed in the standard ABAP list screen

This simple scenario demonstrates the full bidirectional TCP communication channel 7.5 enables for any kind of message transfer from the ABAP stack to any outside device or process.

Implementing an ABAP Program as the TCP Server

Now let’s try it with the ncat program as the TCP client sending a message via an ABAP push channel to an ABAP program acting as the TCP server, an implementation approach that enables external IoT-connected devices and processes to send messages to the ABAP stack (so that sensors can communicate temperature changes, for example).

First, we create an ABAP push channel that acts as the TCP server endpoint. In ABAP in Eclipse — or alternatively in the ABAP Workbench — simply look for connectivity artifacts in the repository browser and select “ABAP push channels” from the navigation tree.

Then specify the name of the push channel and the usual change and transport properties. The system will create an SICF service (an Internet Communication Framework service) to enable HTTP communication and a corresponding ABAP push channel class based on the technical name.

The implementation of the ABAP push channel class (shown in Figure 5) is derived from an abstract base class named CL_APC_TCP_EXT_STATELESS_BASE. Note that you must implement the methods on_connection_setup, on_start, and most notably on_message, which is executed when a message arrives.

class ZCL_APC_WSP_EXT_ZTCP_SERV definition
  public
  inheriting from
CL_APC_TCP_EXT_STATELESS_BASE
  final
  create public .
public section.
  methods if_apc_tcp_server_config~on_connection_setup
    redefinition .
  methods if_apc_wsp_extension~on_message
    redefinition .
  methods if_apc_wsp_extension~on_start
    redefinition .
protected section.
private section.
ENDCLASS.

CLASS ZCL_APC_WSP_EXT_ZTCP_SERV IMPLEMENTATION.

  method if_apc_tcp_server_config~on_connection_setup.

  "Set the terminator to i.e. line feed, hex code 0A
  e_frame-frame_type = if_apc_tcp_frame_types=>co_frame_type_terminator.
  e_frame-terminator = '0A'.

  endmethod.

  method if_apc_wsp_extension~on_message.
    TRY.
      "Define the terminator byte, e.g. line feed as hex code
      DATA: lv_terminator TYPE xstring VALUE '0A'.

      "Retrieve the text message
      DATA(lv_text) = i_message->get_text( ).
      DATA(lo_message) = i_message_manager->create_message( ).

      "Cut off the terminator to i.e. line feed, hex code 0A
      DATA(len) = strlen( lv_text ) - 1.
      lv_text = lv_text(len).

      "Set message text to be sent back to client
      DATA(lv_text_mesage) = |{ sy-mandt }/{ sy-uname }: ON_MESSAGE has been
successfully executed with text
{ lv_text }!|.

      "Convert the text into UTF-8 byte stream
      DATA(
lv_binary_message) = cl_abap_codepage=>convert_to( source = lv_text_mesage ).
      " append the terminator to the message frame before sending
      CONCATENATE lv_binary_message lv_terminator INTO lv_binary_message IN BYTE
MODE.


      "Send the composed frame

      lo_message->set_binary( lv_binary_message ).
      i_message_manager->send( lo_message ).

    CATCH cx_apc_error INTO DATA(lx_apc_error).
      MESSAGE lx_apc_error->get_text( ) TYPE 'E'.
  ENDTRY.
  endmethod.

  method if_apc_wsp_extension~on_start.
  endmethod.
ENDCLASS.

Figure 5 — The source code for the ABAP push channel that serves as the TCP server

With the coding of the TCP server complete, you need to assign it to a port so that it can accept messages on that port from devices and processes that run outside of the ABAP stack. To do this, simply go to transaction SICF and create an external alias. Specify the external alias name and assign it to the service name generated earlier when creating the ABAP push channel (see Figure 6). Then use transaction SMICM to add a free port and associate the service URL so that the Internet Communication Manager can forward the TCP request to the appropriate handler class.

Figure 6 — Specify an external alias for the TCP server

Now you can use the ncat program to connect to your TCP server and type in a message, which the ABAP-based TCP server then returns with the prefix “ON_MESSAGE has been successfully executed with text” that was specified in the implementation of the on_message method.

Another way to play around is to use the ABAP-based TCP client we created earlier and let it talk directly to the ABAP-based TCP server. In this case, start the report using the standard report selection as described previously with the hostname instead set to LOCALHOST, and the result from the ABAP-based TCP server is then written to the output list in the standard ABAP list screen.

An Example Industry 4.0 Scenario

Now that we have seen the basic mechanics of TCP client and TCP server code, let’s look at an example scenario where the ABAP stack serves as the TCP server (see Figure 7), receiving external communications, which is the more commonly used scenario.

Figure 7

Figure 7 — ABAP-based TCP communication in an Industry 4.0 scenario

Here, we use the ABAP-based TCP server to collect temperature values. These values are collected from external IoT-connected devices and processes by the ncat TCP client and sent to the TCP server. The TCP server then sends the values to an ABAP message channel, which forwards the values to an SAP Fiori-based chart control, created with SAPUI5 (SAP’s implementation of HTML5), that constantly redraws the temperature graph in the browser.

To enable this example scenario, we can reuse all of the artifacts discussed previously, such as the TCP server code. We must also create a new ABAP message channel in ABAP in Eclipse or in the ABAP Workbench, similar to the push channel that forwards the messages to the chart control, by adding handler code in the on_message implementation.

While in a real-life scenario the temperature graph would be updated with values sent automatically by sensors, you can add new entries manually through the ncat TCP client for demonstration purposes. When new values come in, the temperature graph is immediately updated via push notifications.

This is just a simple example, but it shows how easy it is to connect the ABAP channel infrastructure to the outside world with TCP communication.

Summary

IoT is changing the way businesses operate — particularly organizations in the manufacturing industry, which are undergoing significant transformation as the next industrial revolution moves forward. With increased connectivity and sophisticated analytics driving a new age of smart factories, and with the comprehensive back-end support for IoT communication built into SAP NetWeaver AS ABAP 7.5, SAP customers are poised not only to adapt to Industry 4.0, but to fully reap its rewards.

1 For more on how to securely register IoT devices with SAP HANA Cloud Platform, see Stefanie García Laule’s article “Ensuring a Secure Internet of Things” in the January-March 2016 issue of SAPinsider (SAPinsiderOnline.com). [back]

2 Learn more about the ABAP channels infrastructure in my article “A Foundation for the Future” in the July-September 2015 issue of SAPinsider (SAPinsiderOnline.com). [back]


More Resources

See All Related Content