32d05ca2ad50b5d381712b2ea3cf9a6bd9a879eb
[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
161         setupRequestIes := m.E2APPDU.InitiatingMessage.Value.E2setupRequest.ProtocolIEs.E2setupRequestIEs
162
163         if len(setupRequestIes) < 2 {
164                 return nil, nil
165         }
166
167         list := setupRequestIes[1].Value.RANfunctionsList.ProtocolIESingleContainer
168         funcs := make([]*entities.RanFunction, len(list))
169         for i := 0; i < len(funcs); i++ {
170                 funcs[i] = &entities.RanFunction{}
171                 id, err := strconv.ParseUint(list[i].Value.RANfunctionItem.RanFunctionID, 10, 32)
172                 if err != nil {
173                         return nil, errors.New(fmt.Sprintf("#e2_setup_request_message.ExtractRanFunctionsList - Failed parse uint RanFunctionID from %s", list[i].Value.RANfunctionItem.RanFunctionID))
174                 }
175                 funcs[i].RanFunctionId = uint32(id)
176                 rev, err := strconv.ParseUint(list[i].Value.RANfunctionItem.RanFunctionRevision, 10, 32)
177                 if err != nil {
178                         return nil, errors.New(fmt.Sprintf("#e2_setup_request_message.ExtractRanFunctionsList - Failed parse uint RanFunctionRevision from %s", list[i].Value.RANfunctionItem.RanFunctionRevision))
179                 }
180                 funcs[i].RanFunctionDefinition = m.trimSpaces(list[i].Value.RANfunctionItem.RanFunctionDefinition)
181                 funcs[i].RanFunctionRevision = uint32(rev)
182         }
183         return funcs, nil
184 }
185
186 func (m *E2SetupRequestMessage) getGlobalE2NodeId() GlobalE2NodeId {
187         return m.E2APPDU.InitiatingMessage.Value.E2setupRequest.ProtocolIEs.E2setupRequestIEs[0].Value.GlobalE2nodeID
188 }
189
190 //func (m *E2SetupRequestMessage) GetNodeType() entities.Node_Type {
191 //      globalE2NodeId := m.getGlobalE2NodeId()
192 //      if id := globalE2NodeId.GNB.GlobalGNBID.PlmnID; id != "" {
193 //              return entities.Node_GNB
194 //      }
195 //      if id := globalE2NodeId.EnGNB.GlobalGNBID.PlmnID; id != "" {
196 //              return entities.Node_GNB
197 //      }
198 //      if id := globalE2NodeId.ENB.GlobalENBID.PlmnID; id != "" {
199 //              return entities.Node_ENB
200 //      }
201 //      if id := globalE2NodeId.NgENB.GlobalNgENBID.PlmnID; id != "" {
202 //              return entities.Node_ENB
203 //      }
204 //      return entities.Node_UNKNOWN
205 //}
206
207 func (m *E2SetupRequestMessage) GetPlmnId() string {
208         globalE2NodeId := m.getGlobalE2NodeId()
209         if id := globalE2NodeId.GNB.GlobalGNBID.PlmnID; id != "" {
210                 return m.trimSpaces(id)
211         }
212         if id := globalE2NodeId.EnGNB.GlobalGNBID.PlmnID; id != "" {
213                 return m.trimSpaces(id)
214         }
215         if id := globalE2NodeId.ENB.GlobalENBID.PlmnID; id != "" {
216                 return m.trimSpaces(id)
217         }
218         if id := globalE2NodeId.NgENB.GlobalNgENBID.PlmnID; id != "" {
219                 return m.trimSpaces(id)
220         }
221         return ""
222 }
223
224 func (m *E2SetupRequestMessage) getInnerEnbId(enbId EnbId) string {
225
226         if id := enbId.HomeEnbId; id != "" {
227                 return id
228         }
229
230         if id := enbId.LongMacroEnbId; id != "" {
231                 return id
232         }
233
234         if id := enbId.MacroEnbId; id != "" {
235                 return id
236         }
237
238         if id := enbId.ShortMacroEnbId; id != "" {
239                 return id
240         }
241
242         return ""
243 }
244
245 func (m *E2SetupRequestMessage) getInnerNgEnbId(enbId NgEnbId) string {
246         if id := enbId.EnbIdLongMacro; id != "" {
247                 return id
248         }
249
250         if id := enbId.EnbIdMacro; id != "" {
251                 return id
252         }
253
254         if id := enbId.EnbIdShortMacro; id != "" {
255                 return id
256         }
257
258         return ""
259 }
260
261 func (m *E2SetupRequestMessage) GetNbId() string {
262         globalE2NodeId := m.getGlobalE2NodeId()
263
264         if id := globalE2NodeId.GNB.GlobalGNBID.GnbID.GnbID; id != "" {
265                 return m.trimSpaces(id)
266         }
267
268         if id := globalE2NodeId.EnGNB.GlobalGNBID.GnbID.GnbID; id != "" {
269                 return m.trimSpaces(id)
270         }
271
272         if id := m.getInnerEnbId(globalE2NodeId.ENB.GlobalENBID.EnbID); id != "" {
273                 return m.trimSpaces(id)
274         }
275
276         if id := m.getInnerNgEnbId(globalE2NodeId.NgENB.GlobalNgENBID.EnbID); id != "" {
277                 return m.trimSpaces(id)
278         }
279
280         return ""
281 }
282
283 func (m *E2SetupRequestMessage) trimSpaces(str string) string {
284         return strings.NewReplacer(" ", "", "\n", "").Replace(str)
285 }