| // ================================================================================== |
| // Copyright (c) 2000-2019 Ericsson Telecom AB AB |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v2.0 |
| // which accompanies this distribution, and is available at |
| // https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html |
| // ================================================================================== |
| // Contributors: |
| // Krisztian Gulyas - initial implementation and initial documentation |
| // |
| // File: MongoDBProtocolHelper.ttcn |
| // Rev: R1A |
| // Prodnr: CNL 0 |
| // ================================================================================== |
| module MongoDB_Functions |
| { |
| // ============================================================================== |
| // |
| // mongoDB wire protocol helper definitions |
| // please refer to MongoDBProtocol.ttcn for MongoDBProtocol defitions |
| // |
| // ============================================================================== |
| |
| import from MongoDB_Types all; // mongoDB protocol description |
| |
| // ------------------------------------------------------------------------------ |
| // Helper function for TCP protocol, returns with the length of message |
| // |
| // - first 4 octets (32 bits) (of any MongoDB message) shows the length of |
| // the message (please refer to the MsgHeader definition in MongoDBProtocol) |
| // |
| // - definition of int32 (raw encoding/decoding) provides a simple decoding |
| // function (please refer to the definition of int32 in MongoDBProtocol) |
| // |
| // parameters: |
| // - stream octets |
| // return: |
| // - length of the message (read it from the message header) |
| // |
| // ------------------------------------------------------------------------------ |
| function MsgLen(in octetstring stream) return integer { |
| return decInt32(substr(stream, 0, 4)); |
| } |
| |
| |
| // ------------------------------------------------------------------------------ |
| // Helper function to serialize a BSON stream. |
| // |
| // Finds and converts each BSON documents (of the given stream) to a JSON string. |
| // parameters: |
| // - json array of the converted JSON string |
| // return: |
| // - 0 no error |
| // - 1 BSON to JSON conversion error (error type logged) |
| // - 2 buffer cut error (error type logged) |
| // |
| // ------------------------------------------------------------------------------ |
| function bsonStream2json(in octetstring stream, inout JSONRecords json) return integer { |
| |
| var integer streamLength := lengthof(stream), |
| noDocs := 0, |
| docLength; |
| |
| // initalize records |
| json := {""}; |
| |
| while (streamLength > 0) { |
| // length of BSON document |
| docLength := decInt32(substr(stream, 0, 4)); |
| |
| // convert bson octects to json string |
| @try { |
| json[noDocs] := bson2json(substr(stream, 0, docLength)); |
| } |
| @catch(err) { |
| log("[!!] Unable to encode bson message | error: ", err); |
| return 1; |
| } |
| |
| // cut current octects, update number of docs and stream length |
| @try { |
| stream := substr(stream, docLength, streamLength - docLength); |
| } |
| @catch(err) { |
| log("[!!] Unable to cut buffer properly | error: ", err); |
| return 2; |
| } |
| |
| noDocs := noDocs + 1; |
| streamLength := lengthof(stream); |
| } |
| |
| log ("[::] " & int2str(noDocs) & " documents serialized from the incomming BSON stream"); |
| return 0; |
| } |
| |
| } with { encode "RAW" } |