Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2c1fc2ee9a986017149aafb16298cbe3a478fe64 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// ==================================================================================
// 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" }

Back to the top