Encoder

In this section the term argument is used, this is very similar to a parameter and is used to pass data between functions.

Input:

The input to the encode function is received through an argument. The input details can be retrieved by the functions provided by the argument. These functions are:
  • String getResource() - Returns the URL to the requested device resource.
  • String getValue() - Returns the request data (if any).
To obtain the requested operation from the argument, a util function needs to be called passing the input argument.
  • String getRequestType(ImpactRequest impactRequest) - Returns the request type, which can be READ, WRITE, EXECUTE, OBSERVE, DELETE
Note: To access the util function(s) you need to import 'com.nokia.transformation.script.model.TransformUtil' as shown in the following code snippet.
var utils = Java.type('com.nokia.transformation.script.model.TransformUtil');
function CodecName() {
    this.encode = function (impactRequest) {
        var type = utils.getRequestType(impactRequest);
        var url = impactRequest.getResource();
        if (impactRequest.getValue) { // get request data (if defined)
            request.resourceValue = impactRequest.getValue();
        }
        // ...

In the preceding code, the URL for the requested device resource is obtained using the getResource() function.

Output:

The encode function must return the encoded payload using one of the following util functions:
  1. createBinaryResponse(
    byte[] bytes, 
    Map<String, Object> scriptCtx,
    boolean onewaymessaging)
  2. createJsonResponse(
    String json,
    Map<String, Object> scriptCtx,
    boolean onewaymessaging)
  3. createStringResponse(
    String str,
    Map<String, Object> scriptCtx,
    boolean onewaymessaging)
Note: To access the util function(s) you need to import 'com.nokia.transformation.script.model.TransformUtil' as shown in the following code snippet.

var utils = Java.type('com.nokia.transformation.script.model.TransformUtil');
function CodecName() {
    this.encode = function (impactRequest) {
        return utils.createBinaryResponse(resBuffer, _map, false);
    }
     
    // ...

In the preceding code, the encode function returns the encoded payload as a binary byte array in the first argument to createBinaryResponse. The second argument is the script context by name _map. The third argument is a boolean flag indicating whether this downlink is a one-way messaging. For example: the READ operation is a two-way messaging for which the device will send a response. Operations, such as WRITE, EXECUTE, OBSERVE, and DELETE will be considered one-way if the device does not send the acknowledgement. The third argument (one-way messaging) equal to false indicates the device will respond to this downlink request.

You can use createJsonResponse or createStringResponse if the encoded payload is plain text or JSON.

Processing:

The processing of input request and producing the encoded payload is specific to the protocol specification of the device. Refer to Sample scripts section.

If there is any error while encoding the request, the script can throw an exception as shown in the following code snippet. In this case, the error is recorded in the - transformation gateway pod log.

function CodecName() {
    this.encode = function (impactRequest) {
        if (encoding_failure) {
            throw new Error(`request type ${request.type} not supported`);
        }
        // ...