[RIC-346] Refactor E2 Setup flow and update EnGnb xml
[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 NgEnb struct {
56         Text          string `xml:",chardata"`
57         GlobalNgENBID struct {
58                 Text   string `xml:",chardata"`
59                 PlmnID string `xml:"plmn-id"`
60                 GnbID  struct {
61                         Text  string `xml:",chardata"`
62                         GnbID string `xml:"gnb-ID"`
63                 } `xml:"gnb-id"`
64         } `xml:"global-ng-eNB-ID"`
65 }
66
67 type Enb struct {
68         Text        string `xml:",chardata"`
69         GlobalENBID struct {
70                 Text   string `xml:",chardata"`
71                 PlmnID string `xml:"plmn-id"`
72                 GnbID  struct {
73                         Text  string `xml:",chardata"`
74                         GnbID string `xml:"gnb-ID"`
75                 } `xml:"gnb-id"`
76         } `xml:"global-eNB-ID"`
77 }
78
79 type GlobalE2NodeId struct {
80         Text  string `xml:",chardata"`
81         GNB   Gnb    `xml:"gNB"`
82         EnGNB EnGnb  `xml:"en-gNB"`
83         NgENB NgEnb  `xml:"ng-eNB"`
84         ENB   Enb    `xml:"eNB"`
85 }
86
87 type E2SetupRequest struct {
88         Text        string `xml:",chardata"`
89         ProtocolIEs struct {
90                 Text              string `xml:",chardata"`
91                 E2setupRequestIEs []struct {
92                         Text        string `xml:",chardata"`
93                         ID          string `xml:"id"`
94                         Criticality struct {
95                                 Text   string `xml:",chardata"`
96                                 Reject string `xml:"reject"`
97                         } `xml:"criticality"`
98                         Value struct {
99                                 Text             string           `xml:",chardata"`
100                                 GlobalE2nodeID   GlobalE2NodeId   `xml:"GlobalE2node-ID"`
101                                 RANfunctionsList RANfunctionsList `xml:"RANfunctions-List"`
102                         } `xml:"value"`
103                 } `xml:"E2setupRequestIEs"`
104         } `xml:"protocolIEs"`
105 }
106
107 type E2SetupRequestMessage struct {
108         XMLName xml.Name `xml:"E2SetupRequestMessage"`
109         Text    string   `xml:",chardata"`
110         E2APPDU struct {
111                 Text              string `xml:",chardata"`
112                 InitiatingMessage struct {
113                         Text          string `xml:",chardata"`
114                         ProcedureCode string `xml:"procedureCode"`
115                         Criticality   struct {
116                                 Text   string `xml:",chardata"`
117                                 Reject string `xml:"reject"`
118                         } `xml:"criticality"`
119                         Value struct {
120                                 Text           string         `xml:",chardata"`
121                                 E2setupRequest E2SetupRequest `xml:"E2setupRequest"`
122                         } `xml:"value"`
123                 } `xml:"initiatingMessage"`
124         } `xml:"E2AP-PDU"`
125 }
126
127 type RanFunctionItem struct {
128         Text                  string `xml:",chardata"`
129         RanFunctionID         string `xml:"ranFunctionID"`
130         RanFunctionDefinition string `xml:"ranFunctionDefinition"`
131         RanFunctionRevision   string `xml:"ranFunctionRevision"`
132 }
133
134 type RANfunctionsList struct {
135         Text                      string `xml:",chardata"`
136         ProtocolIESingleContainer []struct {
137                 Text        string `xml:",chardata"`
138                 ID          string `xml:"id"`
139                 Criticality struct {
140                         Text   string `xml:",chardata"`
141                         Reject string `xml:"reject"`
142                 } `xml:"criticality"`
143                 Value struct {
144                         Text            string          `xml:",chardata"`
145                         RANfunctionItem RanFunctionItem `xml:"RANfunction-Item"`
146                 } `xml:"value"`
147         } `xml:"ProtocolIE-SingleContainer"`
148 }
149
150 func (m *E2SetupRequestMessage) ExtractRanFunctionsList() ([]*entities.RanFunction, error) {
151         list := m.E2APPDU.InitiatingMessage.Value.E2setupRequest.ProtocolIEs.E2setupRequestIEs[1].Value.RANfunctionsList.ProtocolIESingleContainer
152         funcs := make([]*entities.RanFunction, len(list))
153         for i := 0; i < len(funcs); i++ {
154                 funcs[i] = &entities.RanFunction{}
155                 id, err := strconv.ParseUint(list[i].Value.RANfunctionItem.RanFunctionID, 10, 32)
156                 if err != nil {
157                         return nil, errors.New(fmt.Sprintf("#e2_setup_request_message.ExtractRanFunctionsList - Failed parse uint RanFunctionID from %s", list[i].Value.RANfunctionItem.RanFunctionID))
158                 }
159                 funcs[i].RanFunctionId = uint32(id)
160                 rev, err := strconv.ParseUint(list[i].Value.RANfunctionItem.RanFunctionRevision, 10, 32)
161                 if err != nil {
162                         return nil, errors.New(fmt.Sprintf("#e2_setup_request_message.ExtractRanFunctionsList - Failed parse uint RanFunctionRevision from %s", list[i].Value.RANfunctionItem.RanFunctionRevision))
163                 }
164                 funcs[i].RanFunctionDefinition = m.trimSpaces(list[i].Value.RANfunctionItem.RanFunctionDefinition)
165                 funcs[i].RanFunctionRevision = uint32(rev)
166         }
167         return funcs, nil
168 }
169
170 func (m *E2SetupRequestMessage) getGlobalE2NodeId() GlobalE2NodeId {
171         return m.E2APPDU.InitiatingMessage.Value.E2setupRequest.ProtocolIEs.E2setupRequestIEs[0].Value.GlobalE2nodeID
172 }
173
174 func (m *E2SetupRequestMessage) GetNodeType() entities.Node_Type {
175         globalE2NodeId := m.getGlobalE2NodeId()
176         if id := globalE2NodeId.GNB.GlobalGNBID.PlmnID; id != "" {
177                 return entities.Node_GNB
178         }
179         if id := globalE2NodeId.EnGNB.GlobalGNBID.PlmnID; id != "" {
180                 return entities.Node_GNB
181         }
182         if id := globalE2NodeId.ENB.GlobalENBID.PlmnID; id != "" {
183                 return entities.Node_ENB
184         }
185         if id := globalE2NodeId.NgENB.GlobalNgENBID.PlmnID; id != "" {
186                 return entities.Node_GNB
187         }
188         return entities.Node_UNKNOWN
189 }
190
191 func (m *E2SetupRequestMessage) GetPlmnId() string {
192         globalE2NodeId := m.getGlobalE2NodeId()
193         if id := globalE2NodeId.GNB.GlobalGNBID.PlmnID; id != "" {
194                 return m.trimSpaces(id)
195         }
196         if id := globalE2NodeId.EnGNB.GlobalGNBID.PlmnID; id != "" {
197                 return m.trimSpaces(id)
198         }
199         if id := globalE2NodeId.ENB.GlobalENBID.PlmnID; id != "" {
200                 return m.trimSpaces(id)
201         }
202         if id := globalE2NodeId.NgENB.GlobalNgENBID.PlmnID; id != "" {
203                 return m.trimSpaces(id)
204         }
205         return ""
206 }
207
208 func (m *E2SetupRequestMessage) GetNbId() string {
209         globalE2NodeId := m.getGlobalE2NodeId()
210         if id := globalE2NodeId.GNB.GlobalGNBID.GnbID.GnbID; id != "" {
211                 return m.trimSpaces(id)
212         }
213         if id := globalE2NodeId.EnGNB.GlobalGNBID.GnbID.GnbID; id != "" {
214                 return m.trimSpaces(id)
215         }
216         if id := globalE2NodeId.ENB.GlobalENBID.GnbID.GnbID; id != "" {
217                 return m.trimSpaces(id)
218         }
219         if id := globalE2NodeId.NgENB.GlobalNgENBID.GnbID.GnbID; id != "" {
220                 return m.trimSpaces(id)
221         }
222         return ""
223 }
224
225 func (m *E2SetupRequestMessage) trimSpaces(str string) string {
226         return strings.NewReplacer(" ", "", "\n", "").Replace(str)
227 }