version 4.0.0 first support for new E2 ASN
[ric-plt/e2.git] / RIC-E2-TERMINATION / TEST / testAsn / setUpMessages / SetUpMessages.cpp
1 /*
2  * Copyright 2020 AT&T Intellectual Property
3  * Copyright 2020 Nokia
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <cstring>
19 #include <cstdio>
20 #include <cerrno>
21 #include <cstdlib>
22 #include <iostream>
23
24
25 //#include <mdclog/mdclog.h>
26
27
28 #include "oranE2/E2AP-PDU.h"
29 #include "oranE2/InitiatingMessage.h"
30 #include "oranE2/SuccessfulOutcome.h"
31 #include "oranE2/UnsuccessfulOutcome.h"
32
33 #include "oranE2/ProtocolIE-Field.h"
34 #include "oranE2/ENB-ID.h"
35 #include "oranE2/GlobalENB-ID.h"
36 #include "oranE2/GlobalE2node-gNB-ID.h"
37 #include "oranE2/constr_TYPE.h"
38
39 #include "E2Builder.h"
40
41 using namespace std;
42
43
44 #include "BuildRunName.h"
45
46 void buildRanName(E2AP_PDU_t *pdu, unsigned char *buffer) {
47     for (auto i = 0; i < pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.count; i++) {
48         auto *ie = pdu->choice.initiatingMessage->value.choice.E2setupRequest.protocolIEs.list.array[i];
49         if (ie->id == ProtocolIE_ID_id_GlobalE2node_ID) {
50             if (ie->value.present == E2setupRequestIEs__value_PR_GlobalE2node_ID) {
51                 memset(buffer, 0, 128);
52                 buildRanName( (char *) buffer, ie);
53             }
54         }
55     }
56
57 }
58
59 void extractPdu(E2AP_PDU_t *pdu, unsigned char *buffer, int buffer_size) {
60     asn_enc_rval_t er;
61     er = asn_encode_to_buffer(nullptr, ATS_BASIC_XER, &asn_DEF_E2AP_PDU, pdu, buffer, buffer_size);
62     if (er.encoded == -1) {
63         cerr << "encoding of " << asn_DEF_E2AP_PDU.name << " failed, " << strerror(errno) << endl;
64         exit(-1);
65     } else if (er.encoded > (ssize_t) buffer_size) {
66         cerr << "Buffer of size " << buffer_size << " is to small for " << asn_DEF_E2AP_PDU.name << endl;
67         exit(-1);
68     } else {
69         cout << "XML result = " << buffer << endl;
70     }
71
72 }
73
74 auto main(const int argc, char **argv) -> int {
75     E2AP_PDU_t pdu;
76     char *printBuffer;
77     size_t size;
78     FILE *stream = open_memstream(&printBuffer, &size);
79     auto buffer_size =  8192;
80     unsigned char buffer[8192] = {};
81
82     buildSetupRequest(&pdu, 311, 410);
83     asn_fprint(stream, &asn_DEF_E2AP_PDU, &pdu);
84     cout << "Encoding E2AP PDU of size  " << size << endl << printBuffer << endl;
85     fseek(stream,0,SEEK_SET);
86
87     extractPdu(&pdu, buffer, buffer_size);
88     buildRanName(&pdu, buffer);
89     cout << "Ran name = " << buffer << endl;
90
91     ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, &pdu);
92     memset(buffer, 0, buffer_size);
93
94     buildSetupRequestWithFunc(&pdu, 311, 410);
95     extractPdu(&pdu, buffer, buffer_size);
96
97     buildRanName(&pdu, buffer);
98     cout << "Ran name = " << buffer << endl;
99
100     cout << "Sucessesfull outcome" << endl;
101     ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, &pdu);
102     memset(buffer, 0, buffer_size);
103     uint8_t data[4] = {0x99, 0xAA, 0xBB, 0};
104
105     buildSetupSuccsessfulResponse(&pdu, 311, 410, data);
106
107     asn_fprint(stream, &asn_DEF_E2AP_PDU, &pdu);
108     cout << "Encoding E2AP PDU of size  " << size << endl << printBuffer << endl;
109     fseek(stream,0,SEEK_SET);
110
111     extractPdu(&pdu, buffer, buffer_size);
112
113     cout << "Failure outcome" << endl;
114     ASN_STRUCT_RESET(asn_DEF_E2AP_PDU, &pdu);
115     memset(buffer, 0, buffer_size);
116
117     buildSetupUnSuccsessfulResponse(&pdu);
118     asn_fprint(stream, &asn_DEF_E2AP_PDU, &pdu);
119     cout << "Encoding E2AP PDU of size  " << size << endl << printBuffer << endl;
120     fseek(stream,0,SEEK_SET);
121
122     extractPdu(&pdu, buffer, buffer_size);
123 }