[RIC-346] update ng-eNB and eNB node types and add tests
[ric-plt/e2mgr.git] / E2Manager / models / e2_setup_request_message.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package models
21
22 import (
23         "encoding/xml"
24         "errors"
25         "fmt"
26         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
27         "strconv"
28         "strings"
29 )
30
31 type Gnb struct {
32         Text        string `xml:",chardata"`
33         GlobalGNBID struct {
34                 Text   string `xml:",chardata"`
35                 PlmnID string `xml:"plmn-id"`
36                 GnbID  struct {
37                         Text  string `xml:",chardata"`
38                         GnbID string `xml:"gnb-ID"`
39                 } `xml:"gnb-id"`
40         } `xml:"global-gNB-ID"`
41 }
42
43 type EnGnb struct {
44         Text        string `xml:",chardata"`
45         GlobalGNBID struct {
46                 Text   string `xml:",chardata"`
47                 PlmnID string `xml:"pLMN-Identity"`
48                 GnbID  struct {
49                         Text  string `xml:",chardata"`
50                         GnbID string `xml:"gNB-ID"`
51                 } `xml:"gNB-ID"`
52         } `xml:"global-gNB-ID"`
53 }
54
55 type NgEnbId struct {
56         Text            string `xml:",chardata"`
57         EnbIdMacro      string `xml:"enb-ID-macro"`
58         EnbIdShortMacro string `xml:"enb-ID-shortmacro"`
59         EnbIdLongMacro  string `xml:"enb-ID-longmacro"`
60 }
61
62 type NgEnb struct {
63         Text          string `xml:",chardata"`
64         GlobalNgENBID struct {
65                 Text   string  `xml:",chardata"`
66                 PlmnID string  `xml:"plmn-id"`
67                 EnbID  NgEnbId `xml:"enb-id"`
68         } `xml:"global-ng-eNB-ID"`
69 }
70
71 type EnbId struct {
72         Text            string `xml:",chardata"`
73         MacroEnbId      string `xml:"macro-eNB-ID"`
74         HomeEnbId       string `xml:"home-eNB-ID"`
75         ShortMacroEnbId string `xml:"short-Macro-eNB-ID"`
76         LongMacroEnbId  string `xml:"long-Macro-eNB-ID"`
77 }
78
79 type Enb struct {
80         Text        string `xml:",chardata"`
81         GlobalENBID struct {
82                 Text   string `xml:",chardata"`
83                 PlmnID string `xml:"pLMN-Identity"`
84                 EnbID  EnbId  `xml:"eNB-ID"`
85         } `xml:"global-eNB-ID"`
86 }
87
88 type GlobalE2NodeId struct {
89         Text  string `xml:",chardata"`
90         GNB   Gnb    `xml:"gNB"`
91         EnGNB EnGnb  `xml:"en-gNB"`
92         NgENB NgEnb  `xml:"ng-eNB"`
93         ENB   Enb    `xml:"eNB"`
94 }
95
96 type E2SetupRequest struct {
97         Text        string `xml:",chardata"`
98         ProtocolIEs struct {
99                 Text              string `xml:",chardata"`
100                 E2setupRequestIEs []struct {
101                         Text        string `xml:",chardata"`
102                         ID          string `xml:"id"`
103                         Criticality struct {
104                                 Text   string `xml:",chardata"`
105                                 Reject string `xml:"reject"`
106                         } `xml:"criticality"`
107                         Value struct {
108                                 Text             string           `xml:",chardata"`
109                                 GlobalE2nodeID   GlobalE2NodeId   `xml:"GlobalE2node-ID"`
110                                 RANfunctionsList RANfunctionsList `xml:"RANfunctions-List"`
111                         } `xml:"value"`
112                 } `xml:"E2setupRequestIEs"`
113         } `xml:"protocolIEs"`
114 }
115
116 type E2SetupRequestMessage struct {
117         XMLName xml.Name `xml:"E2SetupRequestMessage"`
118         Text    string   `xml:",chardata"`
119         E2APPDU struct {
120                 Text              string `xml:",chardata"`
121                 InitiatingMessage struct {
122                         Text          string `xml:",chardata"`
123                         ProcedureCode string `xml:"procedureCode"`
124                         Criticality   struct {
125                                 Text   string `xml:",chardata"`
126                                 Reject string `xml:"reject"`
127                         } `xml:"criticality"`
128                         Value struct {
129                                 Text           string         `xml:",chardata"`
130                                 E2setupRequest E2SetupRequest `xml:"E2setupRequest"`
131                         } `xml:"value"`
132                 } `xml:"initiatingMessage"`
133         } `xml:"E2AP-PDU"`
134 }
135
136 type RanFunctionItem struct {
137         Text                  string `xml:",chardata"`
138         RanFunctionID         string `xml:"ranFunctionID"`
139         RanFunctionDefinition string `xml:"ranFunctionDefinition"`
140         RanFunctionRevision   string `xml:"ranFunctionRevision"`
141 }
142
143 type RANfunctionsList struct {
144         Text                      string `xml:",chardata"`
145         ProtocolIESingleContainer []struct {
146                 Text        string `xml:",chardata"`
147                 ID          string `xml:"id"`
148                 Criticality struct {
149                         Text   string `xml:",chardata"`
150                         Reject string `xml:"reject"`
151                 } `xml:"criticality"`
152                 Value struct {
153                         Text            string          `xml:",chardata"`
154                         RANfunctionItem RanFunctionItem `xml:"RANfunction-Item"`
155                 } `xml:"value"`
156         } `xml:"ProtocolIE-SingleContainer"`
157 }
158
159 func (m *E2SetupRequestMessage) ExtractRanFunctionsList() ([]*entities.RanFunction, error) {
160         list := m.E2APPDU.InitiatingMessage.Value.E2setupRequest.ProtocolIEs.E2setupRequestIEs[1].Value.RANfunctionsList.ProtocolIESingleContainer
161         funcs := make([]*entities.RanFunction, len(list))
162         for i := 0; i < len(funcs); i++ {
163                 funcs[i] = &entities.RanFunction{}
164                 id, err := strconv.ParseUint(list[i].Value.RANfunctionItem.RanFunctionID, 10, 32)
165                 if err != nil {
166                         return nil, errors.New(fmt.Sprintf("#e2_setup_request_message.ExtractRanFunctionsList - Failed parse uint RanFunctionID from %s", list[i].Value.RANfunctionItem.RanFunctionID))
167                 }
168                 funcs[i].RanFunctionId = uint32(id)
169                 rev, err := strconv.ParseUint(list[i].Value.RANfunctionItem.RanFunctionRevision, 10, 32)
170                 if err != nil {
171                         return nil, errors.New(fmt.Sprintf("#e2_setup_request_message.ExtractRanFunctionsList - Failed parse uint RanFunctionRevision from %s", list[i].Value.RANfunctionItem.RanFunctionRevision))
172                 }
173                 funcs[i].RanFunctionDefinition = m.trimSpaces(list[i].Value.RANfunctionItem.RanFunctionDefinition)
174                 funcs[i].RanFunctionRevision = uint32(rev)
175         }
176         return funcs, nil
177 }
178
179 func (m *E2SetupRequestMessage) getGlobalE2NodeId() GlobalE2NodeId {
180         return m.E2APPDU.InitiatingMessage.Value.E2setupRequest.ProtocolIEs.E2setupRequestIEs[0].Value.GlobalE2nodeID
181 }
182
183 //func (m *E2SetupRequestMessage) GetNodeType() entities.Node_Type {
184 //      globalE2NodeId := m.getGlobalE2NodeId()
185 //      if id := globalE2NodeId.GNB.GlobalGNBID.PlmnID; id != "" {
186 //              return entities.Node_GNB
187 //      }
188 //      if id := globalE2NodeId.EnGNB.GlobalGNBID.PlmnID; id != "" {
189 //              return entities.Node_GNB
190 //      }
191 //      if id := globalE2NodeId.ENB.GlobalENBID.PlmnID; id != "" {
192 //              return entities.Node_ENB
193 //      }
194 //      if id := globalE2NodeId.NgENB.GlobalNgENBID.PlmnID; id != "" {
195 //              return entities.Node_ENB
196 //      }
197 //      return entities.Node_UNKNOWN
198 //}
199
200 func (m *E2SetupRequestMessage) GetPlmnId() string {
201         globalE2NodeId := m.getGlobalE2NodeId()
202         if id := globalE2NodeId.GNB.GlobalGNBID.PlmnID; id != "" {
203                 return m.trimSpaces(id)
204         }
205         if id := globalE2NodeId.EnGNB.GlobalGNBID.PlmnID; id != "" {
206                 return m.trimSpaces(id)
207         }
208         if id := globalE2NodeId.ENB.GlobalENBID.PlmnID; id != "" {
209                 return m.trimSpaces(id)
210         }
211         if id := globalE2NodeId.NgENB.GlobalNgENBID.PlmnID; id != "" {
212                 return m.trimSpaces(id)
213         }
214         return ""
215 }
216
217 func (m *E2SetupRequestMessage) getInnerEnbId(enbId EnbId) string {
218
219         if id := enbId.HomeEnbId; id != "" {
220                 return id
221         }
222
223         if id := enbId.LongMacroEnbId; id != "" {
224                 return id
225         }
226
227         if id := enbId.MacroEnbId; id != "" {
228                 return id
229         }
230
231         if id := enbId.ShortMacroEnbId; id != "" {
232                 return id
233         }
234
235         return ""
236 }
237
238 func (m *E2SetupRequestMessage) getInnerNgEnbId(enbId NgEnbId) string {
239         if id := enbId.EnbIdLongMacro; id != "" {
240                 return id
241         }
242
243         if id := enbId.EnbIdMacro; id != "" {
244                 return id
245         }
246
247         if id := enbId.EnbIdShortMacro; id != "" {
248                 return id
249         }
250
251         return ""
252 }
253
254 func (m *E2SetupRequestMessage) GetNbId() string {
255         globalE2NodeId := m.getGlobalE2NodeId()
256
257         if id := globalE2NodeId.GNB.GlobalGNBID.GnbID.GnbID; id != "" {
258                 return m.trimSpaces(id)
259         }
260
261         if id := globalE2NodeId.EnGNB.GlobalGNBID.GnbID.GnbID; id != "" {
262                 return m.trimSpaces(id)
263         }
264
265         if id := m.getInnerEnbId(globalE2NodeId.ENB.GlobalENBID.EnbID); id != "" {
266                 return m.trimSpaces(id)
267         }
268
269         if id := m.getInnerNgEnbId(globalE2NodeId.NgENB.GlobalNgENBID.EnbID); id != "" {
270                 return m.trimSpaces(id)
271         }
272
273         return ""
274 }
275
276 func (m *E2SetupRequestMessage) trimSpaces(str string) string {
277         return strings.NewReplacer(" ", "", "\n", "").Replace(str)
278 }