Upgrade RMR version to 3.6.2
[ric-plt/e2.git] / RIC-E2-TERMINATION / BuildRunName.h
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
19 #ifndef E2_BUILDRUNNAME_H
20 #define E2_BUILDRUNNAME_H
21
22 #include <3rdparty/oranE2/ProtocolIE-Field.h>
23 #include "oranE2/ProtocolIE-Container.h"
24 #include "oranE2/ProtocolIE-Field.h"
25 #include "oranE2/GlobalE2node-gNB-ID.h"
26 #include "oranE2/GlobalE2node-en-gNB-ID.h"
27 #include "oranE2/GlobalE2node-ng-eNB-ID.h"
28 #include "oranE2/GlobalE2node-eNB-ID.h"
29
30 /**
31  * return the size of the string
32  */
33 static int translatePlmnId(char * plmnId, const unsigned char *data, const char* type) {
34     auto mcc1 = (unsigned char)((unsigned char)data[0] & (unsigned char)0x0F);
35     auto mcc2 = (unsigned char)(((unsigned char)((unsigned char)data[0] & (unsigned char)0xF0)) >> (unsigned char)4);
36     auto mcc3 = (unsigned char)((data[1] & (unsigned char)0xF0) >> (unsigned char)4);
37
38     auto mnc1 = (unsigned char)(data[2] & (unsigned char)0x0F);
39     auto mnc2 =  (unsigned char)(((unsigned char)(data[2] & (unsigned char)0xF0) >> (unsigned char)4));
40     auto mnc3 = (unsigned char)(((unsigned char)(data[1] & (unsigned char)0x0F) >> (unsigned char)4) );
41
42     int j = 0;
43     if (mnc3 != 15) {
44         j = snprintf(plmnId, 20, "%s%1d%1d%1d-%1d%1d%1d", type, mcc1, mcc2, mcc3, mnc1, mnc2, mnc3);
45     }
46     else {
47         j = snprintf(plmnId, 20, "%s%1d%1d%1d-0%1d%1d", type, mcc1, mcc2, mcc3, mnc1, mnc2);
48     }
49
50     return j;
51 }
52
53 static int translateBitStringToChar(char *ranName, BIT_STRING_t &data) {
54     // dont care of last unused bits
55     char buffer[256] {};
56     auto j = snprintf(buffer, 256, "%s-", ranName);
57     memcpy(ranName, buffer, j);
58
59     unsigned b1 = 0;
60     unsigned b2 = 0;
61     for (auto i = 0; i < (int)data.size; i++) {
62         b1 = data.buf[i] & (unsigned)0xF0;
63         b1 = b1 >> (unsigned)4;
64         j = snprintf(buffer, 256, "%s%1x", ranName, b1);
65         memcpy(ranName, buffer, j);
66         b2 = data.buf[i] & (unsigned)0x0F;
67         j = snprintf(buffer, 256, "%s%1x", ranName, b2);
68         memcpy(ranName, buffer, j);
69     }
70     return j;
71 }
72
73
74 int buildRanName(char *ranName, E2setupRequestIEs_t *ie) {
75     switch (ie->value.choice.GlobalE2node_ID.present) {
76         case GlobalE2node_ID_PR_gNB: {
77             auto *gnb = ie->value.choice.GlobalE2node_ID.choice.gNB;
78             translatePlmnId(ranName, (const unsigned char *)gnb->global_gNB_ID.plmn_id.buf, (const char *)"gnb:");
79             if (gnb->global_gNB_ID.gnb_id.present == GNB_ID_Choice_PR_gnb_ID) {
80                 translateBitStringToChar(ranName, gnb->global_gNB_ID.gnb_id.choice.gnb_ID);
81             }
82             break;
83         }
84         case GlobalE2node_ID_PR_en_gNB: {
85             auto *enGnb = ie->value.choice.GlobalE2node_ID.choice.en_gNB;
86             translatePlmnId(ranName,
87                             (const unsigned char *)enGnb->global_gNB_ID.pLMN_Identity.buf,
88                             (const char *)"en-gnb:");
89             if (enGnb->global_gNB_ID.gNB_ID.present == ENGNB_ID_PR_gNB_ID) {
90                 translateBitStringToChar(ranName, enGnb->global_gNB_ID.gNB_ID.choice.gNB_ID);
91             }
92             break;
93         }
94         case GlobalE2node_ID_PR_ng_eNB: {
95             auto *ngEnb = ie->value.choice.GlobalE2node_ID.choice.ng_eNB;
96             char *buf = (char *)ngEnb->global_ng_eNB_ID.plmn_id.buf;
97             char str[20] = {};
98             BIT_STRING_t *data = nullptr;
99             switch (ngEnb->global_ng_eNB_ID.enb_id.present) {
100                 case ENB_ID_Choice_PR_enb_ID_macro: {
101                     strncpy(str, (const char *)"ng-enB-macro:", 13);
102                     data = &ngEnb->global_ng_eNB_ID.enb_id.choice.enb_ID_macro;
103                     break;
104                 }
105                 case ENB_ID_Choice_PR_enb_ID_shortmacro: {
106                     strncpy(str, (const char *)"ng-enB-shortmacro:", 18);
107                     data = &ngEnb->global_ng_eNB_ID.enb_id.choice.enb_ID_shortmacro;
108                     break;
109                 }
110                 case ENB_ID_Choice_PR_enb_ID_longmacro: {
111                     strncpy(str, (const char *)"ng-enB-longmacro:", 17);
112                     data = &ngEnb->global_ng_eNB_ID.enb_id.choice.enb_ID_longmacro;
113                 }
114                 case ENB_ID_Choice_PR_NOTHING: {
115                     break;
116                 }
117                 default:
118                     break;
119             }
120             translatePlmnId(ranName, (const unsigned char *)buf, (const char *)str);
121             translateBitStringToChar(ranName, *data);
122             break;
123         }
124         case GlobalE2node_ID_PR_eNB: {
125             auto *enb = ie->value.choice.GlobalE2node_ID.choice.eNB;
126             char *buf = (char *)enb->global_eNB_ID.pLMN_Identity.buf;
127             char str[20] = {};
128             BIT_STRING_t *data = nullptr;
129
130             switch (enb->global_eNB_ID.eNB_ID.present) {
131                 case ENB_ID_PR_macro_eNB_ID: {
132                     strncpy(str, (const char *)"enB-macro:", 10);
133                     data = &enb->global_eNB_ID.eNB_ID.choice.macro_eNB_ID;
134                     break;
135                 }
136                 case ENB_ID_PR_home_eNB_ID: {
137                     strncpy(str, (const char *)"enB-home:", 9);
138                     data = &enb->global_eNB_ID.eNB_ID.choice.home_eNB_ID;
139                     break;
140                 }
141                 case ENB_ID_PR_short_Macro_eNB_ID: {
142                     strncpy(str, (const char *)"enB-shortmacro:", 15);
143                     data = &enb->global_eNB_ID.eNB_ID.choice.short_Macro_eNB_ID;
144                     break;
145                 }
146                 case ENB_ID_PR_long_Macro_eNB_ID: {
147                     strncpy(str, (const char *)"enB-longmacro:", 14);
148                     data = &enb->global_eNB_ID.eNB_ID.choice.long_Macro_eNB_ID;
149                     break;
150                 }
151                 case ENB_ID_PR_NOTHING:
152                 default: {
153                     break;
154                 }
155             }
156             translatePlmnId(ranName, (const unsigned char *)buf, (const char *)str);
157             translateBitStringToChar(ranName, *data);
158             break;
159         }
160         case GlobalE2node_ID_PR_NOTHING:
161         default:
162             return -1;
163     }
164     return 0;
165 }
166
167
168 #endif //E2_BUILDRUNNAME_H