blob: 7ac6594c5bb2a74fd47fc65efac5f5832645762e [file] [log] [blame]
Balint Uveges871fa392019-04-02 20:31:11 +00001/*
2==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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.
wahidw761934a2019-11-27 06:07:26 +000017
18 This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 platform project (RICP).
20
Balint Uveges871fa392019-04-02 20:31:11 +000021==================================================================================
22*/
23/*
24 Mnemonic: file.go
25 Abstract: File SDL implementation. Only for testing purpose.
26 Date: 16 March 2019
27*/
28
29package sdl
30
31import (
32 "encoding/json"
33 "errors"
wahidwa8596ec2019-12-05 06:30:42 +000034 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Balint Uveges871fa392019-04-02 20:31:11 +000035 "io/ioutil"
36 "os"
wahidw51db0122020-01-12 04:24:29 +000037 "strings"
kalnagy92162652019-07-02 15:15:49 +020038 "routing-manager/pkg/rtmgr"
prabhukaliswamye110ee02019-12-23 09:51:01 +000039 "routing-manager/pkg/models"
Balint Uveges871fa392019-04-02 20:31:11 +000040)
41
42/*
43Reads the content of the rt.json file
44Parses the JSON content and loads each xApp entry into an xApp object
45Returns an array os xApp object
46*/
kalnagy92162652019-07-02 15:15:49 +020047
48type File struct {
49 Sdl
50}
51
52func NewFile() *File {
53 instance := new(File)
54 return instance
55}
56
57func (f *File) ReadAll(file string) (*rtmgr.RicComponents, error) {
wahidwa8596ec2019-12-05 06:30:42 +000058 xapp.Logger.Debug("Invoked sdl.ReadAll(" + file + ")")
kalnagy92162652019-07-02 15:15:49 +020059 var rcs *rtmgr.RicComponents
Balint Uveges871fa392019-04-02 20:31:11 +000060 jsonFile, err := os.Open(file)
61 if err != nil {
62 return nil, errors.New("cannot open the file due to: " + err.Error())
63 }
64 defer jsonFile.Close()
65 byteValue, err := ioutil.ReadAll(jsonFile)
66 if err != nil {
67 return nil, errors.New("cannot read the file due to: " + err.Error())
68 }
prabhukaliswamye110ee02019-12-23 09:51:01 +000069
kalnagy92162652019-07-02 15:15:49 +020070 err = json.Unmarshal(byteValue, &rcs)
Balint Uveges871fa392019-04-02 20:31:11 +000071 if err != nil {
72 return nil, errors.New("cannot parse data due to: " + err.Error())
73 }
wahidwa8596ec2019-12-05 06:30:42 +000074 xapp.Logger.Debug("file.fileReadAll returns: %v", rcs)
kalnagy92162652019-07-02 15:15:49 +020075 return rcs, nil
Balint Uveges871fa392019-04-02 20:31:11 +000076}
77
kalnagy92162652019-07-02 15:15:49 +020078func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error {
wahidwa8596ec2019-12-05 06:30:42 +000079 xapp.Logger.Debug("Invoked sdl.WriteAll")
80 xapp.Logger.Debug("file.fileWriteAll writes into file: " + file)
81 xapp.Logger.Debug("file.fileWriteAll writes data: %v", *rcs)
kalnagy92162652019-07-02 15:15:49 +020082 byteValue, err := json.Marshal(rcs)
83 if err != nil {
84 return errors.New("cannot convert data due to: " + err.Error())
85 }
86 err = ioutil.WriteFile(file, byteValue, 0644)
87 if err != nil {
88 return errors.New("cannot write file due to: " + err.Error())
89 }
90 return nil
91}
92
zkoczkaaaf8d392019-10-02 17:16:06 +020093func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error {
wahidwa8596ec2019-12-05 06:30:42 +000094 xapp.Logger.Debug("Invoked sdl.WriteXApps")
95 xapp.Logger.Debug("file.fileWriteXApps writes into file: " + file)
96 xapp.Logger.Debug("file.fileWriteXApps writes data: %v", *xApps)
kalnagy92162652019-07-02 15:15:49 +020097
98 ricData, err := NewFile().ReadAll(file)
zkoczkaaaf8d392019-10-02 17:16:06 +020099 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000100 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
zkoczkaaaf8d392019-10-02 17:16:06 +0200101 return errors.New("cannot read full ric data to modify xApps data, due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +0200102 }
zkoczkaaaf8d392019-10-02 17:16:06 +0200103 ricData.XApps = *xApps
kalnagy92162652019-07-02 15:15:49 +0200104
105 byteValue, err := json.Marshal(ricData)
Balint Uveges871fa392019-04-02 20:31:11 +0000106 if err != nil {
107 return errors.New("cannot convert data due to: " + err.Error())
108 }
109 err = ioutil.WriteFile(file, byteValue, 0644)
110 if err != nil {
111 return errors.New("cannot write file due to: " + err.Error())
112 }
113 return nil
114}
rangajal749099b2019-12-10 09:37:08 +0000115
wahidw51db0122020-01-12 04:24:29 +0000116func (f *File) WriteNewE2TInstance(file string, E2TInst *rtmgr.E2TInstance,meiddata string) error {
rangajal749099b2019-12-10 09:37:08 +0000117 xapp.Logger.Debug("Invoked sdl.WriteNewE2TInstance")
118 xapp.Logger.Debug("file.WriteNewE2TInstance writes into file: " + file)
119 xapp.Logger.Debug("file.WriteNewE2TInstance writes data: %v", *E2TInst)
120
121 ricData, err := NewFile().ReadAll(file)
122 if err != nil {
123 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
124 return errors.New("cannot read full ric data to modify xApps data, due to: " + err.Error())
125 }
126 ricData.E2Ts[E2TInst.Fqdn] = *E2TInst
wahidw51db0122020-01-12 04:24:29 +0000127 if (len(meiddata) > 0){
128 ricData.MeidMap = []string {meiddata}
129 } else {
130 ricData.MeidMap = []string {}
131 }
132
rangajal749099b2019-12-10 09:37:08 +0000133
134 byteValue, err := json.Marshal(ricData)
135 if err != nil {
136 return errors.New("cannot convert data due to: " + err.Error())
137 }
138 err = ioutil.WriteFile(file, byteValue, 0644)
139 if err != nil {
140 return errors.New("cannot write file due to: " + err.Error())
141 }
142 return nil
143}
prabhukaliswamye110ee02019-12-23 09:51:01 +0000144
145func (f *File) WriteAssRANToE2TInstance(file string, rane2tmap models.RanE2tMap) error {
146 xapp.Logger.Debug("Invoked sdl.WriteAssRANToE2TInstance")
147 xapp.Logger.Debug("file.WriteAssRANToE2TInstance writes into file: " + file)
148 xapp.Logger.Debug("file.WriteAssRANToE2TInstance writes data: %v", rane2tmap)
149
150 ricData, err := NewFile().ReadAll(file)
151 if err != nil {
152 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
153 return errors.New("cannot read full ric data to modify xApps data, due to: " + err.Error())
154 }
wahidw51db0122020-01-12 04:24:29 +0000155
156 ricData.MeidMap = []string{}
prabhukaliswamye110ee02019-12-23 09:51:01 +0000157 for _, element := range rane2tmap {
158 xapp.Logger.Info("data received")
wahidw51db0122020-01-12 04:24:29 +0000159 var str,meidar string
160 for _, meid := range element.RanNamelist {
161 meidar += meid + " "
162 }
163 str = "mme_ar|" + *element.E2TAddress + "|" + strings.TrimSuffix(meidar," ")
164 ricData.MeidMap = append(ricData.MeidMap,str)
165
prabhukaliswamye110ee02019-12-23 09:51:01 +0000166 for key, _ := range ricData.E2Ts {
167 if key == *element.E2TAddress {
168 var estObj rtmgr.E2TInstance
169 estObj = ricData.E2Ts[key]
170 estObj.Ranlist = append(ricData.E2Ts[key].Ranlist, element.RanNamelist...)
171 ricData.E2Ts[key]= estObj
172 }
173 }
174 }
175
176 byteValue, err := json.Marshal(ricData)
177 if err != nil {
178 return errors.New("cannot convert data due to: " + err.Error())
179 }
180 err = ioutil.WriteFile(file, byteValue, 0644)
181 if err != nil {
182 return errors.New("cannot write file due to: " + err.Error())
183 }
184 return nil
185}
186
187func (f *File) WriteDisAssRANFromE2TInstance(file string, disassranmap models.RanE2tMap) error {
188 xapp.Logger.Debug("Invoked sdl.WriteDisAssRANFromE2TInstance")
189 xapp.Logger.Debug("file.WriteDisAssRANFromE2TInstance writes into file: " + file)
190 xapp.Logger.Debug("file.WriteDisAssRANFromE2TInstance writes data: %v", disassranmap)
191
192 ricData, err := NewFile().ReadAll(file)
193 if err != nil {
194 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
195 return errors.New("cannot read full ric data to modify xApps data, due to: " + err.Error())
196 }
wahidw51db0122020-01-12 04:24:29 +0000197
198 var str,meiddel,meiddisdel string
199 ricData.MeidMap = []string{}
prabhukaliswamye110ee02019-12-23 09:51:01 +0000200 for _, element := range disassranmap {
201 xapp.Logger.Info("data received")
wahidw51db0122020-01-12 04:24:29 +0000202 for _, meid := range element.RanNamelist {
203 meiddisdel += meid + " "
204 }
205 if ( len(element.RanNamelist) > 0 ) {
206 str = "mme_del|" + strings.TrimSuffix(meiddisdel," ")
207 ricData.MeidMap = append(ricData.MeidMap,str)
208 }
prabhukaliswamye110ee02019-12-23 09:51:01 +0000209 e2taddress_key := *element.E2TAddress
210 //Check whether the provided E2T Address is available in SDL as a key.
211 //If exist, proceed further to check RAN list, Otherwise move to next E2T Instance
212 if _, exist := ricData.E2Ts[e2taddress_key]; exist {
213 var estObj rtmgr.E2TInstance
214 estObj = ricData.E2Ts[e2taddress_key]
215 // If RAN list is empty, then routing manager assumes that all RANs attached associated to the particular E2T Instance to be removed.
216 if len(element.RanNamelist) == 0 {
217 xapp.Logger.Debug("RAN List is empty. So disassociating all RANs from the E2T Instance: %v ", *element.E2TAddress)
wahidw51db0122020-01-12 04:24:29 +0000218 for _, meid := range estObj.Ranlist {
219 meiddel += meid + " "
220 }
221 str = "mme_del|" + strings.TrimSuffix(meiddel," ")
222 ricData.MeidMap = append(ricData.MeidMap,str)
223
224 estObj.Ranlist = []string{}
prabhukaliswamye110ee02019-12-23 09:51:01 +0000225 } else {
226 xapp.Logger.Debug("Remove only selected rans from E2T Instance: %v and %v ", ricData.E2Ts[e2taddress_key].Ranlist, element.RanNamelist)
227 for _, disRanValue := range element.RanNamelist {
228 for ranIndex, ranValue := range ricData.E2Ts[e2taddress_key].Ranlist {
229 if disRanValue == ranValue {
230 estObj.Ranlist[ranIndex] = estObj.Ranlist[len(estObj.Ranlist)-1]
231 estObj.Ranlist[len(estObj.Ranlist)-1] = ""
232 estObj.Ranlist = estObj.Ranlist[:len(estObj.Ranlist)-1]
233 }
234 }
235 }
236 }
237 ricData.E2Ts[e2taddress_key]= estObj
238 }
239 }
240
241 xapp.Logger.Debug("Final data after disassociate: %v", ricData)
242
243 byteValue, err := json.Marshal(ricData)
244 if err != nil {
245 return errors.New("cannot convert data due to: " + err.Error())
246 }
247 err = ioutil.WriteFile(file, byteValue, 0644)
248 if err != nil {
249 return errors.New("cannot write file due to: " + err.Error())
250 }
251 return nil
252}
253
254func (f *File) WriteDeleteE2TInstance(file string, E2TInst *models.E2tDeleteData) error {
255 xapp.Logger.Debug("Invoked sdl.WriteDeleteE2TInstance")
256 xapp.Logger.Debug("file.WriteDeleteE2TInstance writes into file: " + file)
257 xapp.Logger.Debug("file.WriteDeleteE2TInstance writes data: %v", *E2TInst)
258
259 ricData, err := NewFile().ReadAll(file)
260 if err != nil {
261 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
262 return errors.New("cannot read full ric data to modify xApps data, due to: " + err.Error())
263 }
264
wahidw51db0122020-01-12 04:24:29 +0000265
266 ricData.MeidMap = []string {}
267 var delrow,meiddel string
268 if(len(E2TInst.RanNamelistTobeDissociated)>0) {
269 for _, meid := range E2TInst.RanNamelistTobeDissociated {
270 meiddel += meid + " "
271 }
272 delrow = "mme_del|" + strings.TrimSuffix(meiddel," ")
273 ricData.MeidMap = append(ricData.MeidMap,delrow)
274 } else {
275 if(len(ricData.E2Ts[*E2TInst.E2TAddress].Ranlist) > 0) {
276 for _, meid := range ricData.E2Ts[*E2TInst.E2TAddress].Ranlist {
277 meiddel += meid + " "
278 }
279 delrow = "mme_del|" + strings.TrimSuffix(meiddel," ")
280 ricData.MeidMap = append(ricData.MeidMap,delrow)
281 }
282 }
283
prabhukaliswamye110ee02019-12-23 09:51:01 +0000284 delete(ricData.E2Ts, *E2TInst.E2TAddress)
285
prabhukaliswamye110ee02019-12-23 09:51:01 +0000286 for _, element := range E2TInst.RanAssocList {
wahidw51db0122020-01-12 04:24:29 +0000287 var str,meidar string
prabhukaliswamye110ee02019-12-23 09:51:01 +0000288 xapp.Logger.Info("data received")
wahidw51db0122020-01-12 04:24:29 +0000289 for _, meid := range element.RanNamelist {
290 meidar = meid + " "
291 }
292 str = "mme_ar|" + *element.E2TAddress + "|" + strings.TrimSuffix(meidar," ")
293 ricData.MeidMap = append(ricData.MeidMap,str)
prabhukaliswamye110ee02019-12-23 09:51:01 +0000294 key := *element.E2TAddress
295
296 if val, ok := ricData.E2Ts[key]; ok {
297 var estObj rtmgr.E2TInstance
298 estObj = val
299 estObj.Ranlist = append(ricData.E2Ts[key].Ranlist, element.RanNamelist...)
300 ricData.E2Ts[key]= estObj
301 } else {
302 xapp.Logger.Error("file.WriteDeleteE2TInstance E2T instance is not found for provided E2TAddress : %v", errors.New(key).Error())
303 }
304
305 }
306
307 byteValue, err := json.Marshal(ricData)
308 if err != nil {
309 return errors.New("cannot convert data due to: " + err.Error())
310 }
311 err = ioutil.WriteFile(file, byteValue, 0644)
312 if err != nil {
313 return errors.New("cannot write file due to: " + err.Error())
314 }
315 return nil
316}