RIC:1060: Change in PTL
[ric-plt/e2.git] / RIC-E2-TERMINATION / BuildRunName.h
1 /*
2  * Copyright 2020 AT&T Intellectual Property
3  * Copyright 2020 Nokia
4  * Copyright 2023 Capgemini
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #ifndef E2_BUILDRUNNAME_H
21 #define E2_BUILDRUNNAME_H
22
23 #include <3rdparty/oranE2/ProtocolIE-Field.h>
24 #include "oranE2/ProtocolIE-Container.h"
25 #include "oranE2/ProtocolIE-Field.h"
26 #include "oranE2/GlobalE2node-gNB-ID.h"
27 #include "oranE2/GlobalE2node-en-gNB-ID.h"
28 #include "oranE2/GlobalE2node-ng-eNB-ID.h"
29 #include "oranE2/GlobalE2node-eNB-ID.h"
30
31 /**    02 F8 29
32  * return the size of the string //
33  */
34 static int translatePlmnId(char * plmnId, const unsigned char *data, const char* type) {
35     auto mcc1 = (unsigned char)((unsigned char)data[0] & (unsigned char)0x0F);
36     auto mcc2 = (unsigned char)(((unsigned char)((unsigned char)data[0] & (unsigned char)0xF0)) >> (unsigned char)4);
37     ///auto mcc3 = (unsigned char)((data[1] & (unsigned char)0xF0) >> (unsigned char)4);
38     auto mcc3 = (unsigned char)((unsigned char)(data[1] & (unsigned char)0x0F));
39
40     auto mnc1 = (unsigned char)(((unsigned char)((unsigned char)data[1] & (unsigned char)0xF0)) >> (unsigned char)4);
41     auto mnc2 =  (unsigned char)((unsigned char)data[2] & (unsigned char)0x0F) ;
42     //auto mnc3 = (unsigned char)(((unsigned char)(data[1] & (unsigned char)0x0F) >> (unsigned char)4) );
43     auto mnc3 = (unsigned char)(((unsigned char)((unsigned char)data[2] & (unsigned char)0xF0)) >> (unsigned char)4);
44
45     int j = 0;
46     if (mnc1 != 15) {
47         j = snprintf(plmnId, 20, "%s%1d%1d%1d_%1d%1d%1d", type, mcc1, mcc2, mcc3, mnc1, mnc2, mnc3);
48     }
49     else {
50         j = snprintf(plmnId, 20, "%s%1d%1d%1d_0%1d%1d", type, mcc1, mcc2, mcc3, mnc2, mnc3);
51     }
52
53     return j;
54 }
55
56 static int translateBitStringToChar(char *ranName, BIT_STRING_t &data, long cuid, long duid) {
57     // dont care of last unused bits
58     char buffer[256] {};
59     // auto j = snprintf(buffer, 256, "%s_", ranName);
60     int j = 0;
61 /*
62  // ran name decimal
63     unsigned long bitValue = 0;
64     for (auto i = 0; i < (int)data.size; i++) {
65         bitValue <<= (unsigned long)8;
66         bitValue += data.buf[i];
67     }
68
69     j = snprintf(buffer, 256, "%s%ld", ranName, bitValue);
70
71     memcpy(ranName, buffer, j);
72 */
73    
74
75     unsigned b1 = 0;
76     unsigned b2 = 0;
77     unsigned tmp_digit=0;
78     j = snprintf(buffer, 256, "%s_", ranName);
79     memcpy(ranName, buffer, j);
80     for (auto i = 0; i < (int)data.size; i++) {
81         //
82         // we need to shift trailing zeros in the bit string (from asn1c) to leading zeros
83         //
84         tmp_digit = (0==i ? (uint8_t) 0: (uint8_t) data.buf[i-1])<<(8-data.bits_unused);
85         tmp_digit = tmp_digit | ((unsigned) data.buf[i] >> data.bits_unused);
86
87         b1 = tmp_digit & (unsigned)0xF0;
88         b1 = b1 >> (unsigned)4;
89         j = snprintf(buffer, 256, "%s%1x", ranName, b1);
90         memcpy(ranName, buffer, j);
91         b2 = tmp_digit & (unsigned)0x0F;
92         j = snprintf(buffer, 256, "%s%1x", ranName, b2);
93         memcpy(ranName, buffer, j);
94     }
95
96     /*Note: Deployment where CU-UP and DU are combined
97     (but do not include the CP-CP) and have a single E2 connection
98      is not supported. The combination of CU-CP, CU-UP, and DU will be
99      treated as a single gNB and expect it to have only the
100      global gNB ID in its E2 Setup ID*/
101
102     if ( cuid >= 0 && duid >= 0){
103            j= snprintf(buffer, 256, "%s", ranName);
104        } else if ( cuid >= 0 ){
105            j= snprintf(buffer, 256, "%s_%lx", ranName,cuid);
106        }else if ( duid >= 0 ){
107            j= snprintf(buffer, 256, "%s_%lx", ranName,duid);
108        }else{
109            j= snprintf(buffer, 256, "%s", ranName);
110        }
111        printf("cuupid =%ld\n",cuid);
112        printf("duid =%ld\n",duid);
113        printf("ranName =%s\n",ranName);
114        memcpy(ranName, buffer, j);
115
116     return j;
117 }
118
119
120 int buildRanName(char *ranName, E2setupRequestIEs_t *ie) {
121     switch (ie->value.choice.GlobalE2node_ID.present) {
122         case GlobalE2node_ID_PR_gNB: {
123             auto *gnb = ie->value.choice.GlobalE2node_ID.choice.gNB;
124             long cuupid = -1;
125             long duid = -1;
126            
127             if (gnb->global_gNB_ID.gnb_id.present == GNB_ID_Choice_PR_gnb_ID) {
128                 if (gnb->gNB_CU_UP_ID != NULL && gnb->gNB_DU_ID != NULL){
129                     printf("\ngNB_CU_UP_ID and gNB_DU_ID is not null\n");
130                     asn_INTEGER2long(gnb->gNB_CU_UP_ID,&cuupid);
131                     asn_INTEGER2long(gnb->gNB_DU_ID,&duid);
132                     translatePlmnId(ranName, (const unsigned char *)gnb->global_gNB_ID.plmn_id.buf, (const char *)"gnb_");
133                 }else if (gnb->gNB_DU_ID == NULL && gnb->gNB_CU_UP_ID == NULL ){
134                     printf("\ngNB_CU_UP_ID and gNB_DU_ID is null\n");
135                     translatePlmnId(ranName, (const unsigned char *)gnb->global_gNB_ID.plmn_id.buf, (const char *)"gnb_");
136                 }else if (gnb->gNB_CU_UP_ID != NULL ){
137                     printf("\ngNB_CU_UP_ID is not null\n");
138                     asn_INTEGER2long(gnb->gNB_CU_UP_ID,&cuupid);
139                     translatePlmnId(ranName, (const unsigned char *)gnb->global_gNB_ID.plmn_id.buf, (const char *)"gnbc_");
140                 }else if (gnb->gNB_DU_ID != NULL ){
141                     printf("\ngNB_DU_ID is not null\n");
142                     asn_INTEGER2long(gnb->gNB_DU_ID,&duid);
143                     translatePlmnId(ranName, (const unsigned char *)gnb->global_gNB_ID.plmn_id.buf, (const char *)"gnbd_");
144                 }
145                 translateBitStringToChar(ranName, gnb->global_gNB_ID.gnb_id.choice.gnb_ID,cuupid,duid);
146             }else{
147                 translatePlmnId(ranName, (const unsigned char *)gnb->global_gNB_ID.plmn_id.buf, (const char *)"gnb_");
148             }
149             break;
150         }
151         case GlobalE2node_ID_PR_en_gNB: {
152             auto *enGnb = ie->value.choice.GlobalE2node_ID.choice.en_gNB;
153             translatePlmnId(ranName,
154                             (const unsigned char *)enGnb->global_en_gNB_ID.pLMN_Identity.buf,
155                             (const char *)"en_gnb_");
156             if (enGnb->global_en_gNB_ID.gNB_ID.present == ENGNB_ID_PR_gNB_ID) {
157                 translateBitStringToChar(ranName, enGnb->global_en_gNB_ID.gNB_ID.choice.gNB_ID,-1,-1);
158             }
159             break;
160         }
161         case GlobalE2node_ID_PR_ng_eNB: {
162             auto *ngEnb = ie->value.choice.GlobalE2node_ID.choice.ng_eNB;
163             switch (ngEnb->global_ng_eNB_ID.enb_id.present) {
164                 case ENB_ID_Choice_PR_enb_ID_macro: {
165                     translatePlmnId(ranName, (const unsigned char *)ngEnb->global_ng_eNB_ID.plmn_id.buf, (const char *)"ng_enB_macro_");
166                     translateBitStringToChar(ranName, ngEnb->global_ng_eNB_ID.enb_id.choice.enb_ID_macro,-1,-1);
167                     break;
168                 }
169                 case ENB_ID_Choice_PR_enb_ID_shortmacro: {
170                     translatePlmnId(ranName, (const unsigned char *)ngEnb->global_ng_eNB_ID.plmn_id.buf, (const char *)"ng_enB_shortmacro_");
171                     translateBitStringToChar(ranName, ngEnb->global_ng_eNB_ID.enb_id.choice.enb_ID_shortmacro,-1,-1);
172                     break;
173                 }
174                 case ENB_ID_Choice_PR_enb_ID_longmacro: {
175                     translatePlmnId(ranName, (const unsigned char *)ngEnb->global_ng_eNB_ID.plmn_id.buf, (const char *)"ng_enB_longmacro_");
176                     translateBitStringToChar(ranName, ngEnb->global_ng_eNB_ID.enb_id.choice.enb_ID_longmacro,-1,-1);
177                     break;
178                 }
179                 case ENB_ID_Choice_PR_NOTHING: {
180                     break;
181                 }
182                 default:
183                     break;
184             }
185             break;
186         }
187         case GlobalE2node_ID_PR_eNB: {
188             auto *enb = ie->value.choice.GlobalE2node_ID.choice.eNB;
189             switch (enb->global_eNB_ID.eNB_ID.present) {
190                 case ENB_ID_PR_macro_eNB_ID: {
191                     translatePlmnId(ranName, (const unsigned char *)enb->global_eNB_ID.pLMN_Identity.buf, (const char *)"enB_macro_");
192                     translateBitStringToChar(ranName, enb->global_eNB_ID.eNB_ID.choice.macro_eNB_ID,-1,-1);
193                     break;
194                 }
195                 case ENB_ID_PR_home_eNB_ID: {
196                     translatePlmnId(ranName, (const unsigned char *)enb->global_eNB_ID.pLMN_Identity.buf, (const char *)"enB_home_");
197                     translateBitStringToChar(ranName, enb->global_eNB_ID.eNB_ID.choice.home_eNB_ID,-1,-1);
198                     break;
199                 }
200                 case ENB_ID_PR_short_Macro_eNB_ID: {
201                     translatePlmnId(ranName, (const unsigned char *)enb->global_eNB_ID.pLMN_Identity.buf, (const char *)"enB_shortmacro_");
202                     translateBitStringToChar(ranName, enb->global_eNB_ID.eNB_ID.choice.short_Macro_eNB_ID,-1,-1);
203                     break;
204                 }
205                 case ENB_ID_PR_long_Macro_eNB_ID: {
206                     translatePlmnId(ranName, (const unsigned char *)enb->global_eNB_ID.pLMN_Identity.buf, (const char *)"enB_longmacro_");
207                     translateBitStringToChar(ranName, enb->global_eNB_ID.eNB_ID.choice.long_Macro_eNB_ID,-1,-1);
208                     break;
209                 }
210                 case ENB_ID_PR_NOTHING: {
211                     break;
212                 }
213                 default: {
214                     break;
215                 }
216             }
217             break;
218         }
219         case GlobalE2node_ID_PR_NOTHING:
220         default:
221             return -1;
222     }
223     return 0;
224 }
225
226
227 #endif //E2_BUILDRUNNAME_H