Open RMR connection in a new thread
[ric-plt/rtmgr.git] / pkg / sdl / file.go
1 /*
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.
17
18    This source code is part of the near-RT RIC (RAN Intelligent Controller)
19    platform project (RICP).
20
21 ==================================================================================
22 */
23 /*
24   Mnemonic:     file.go
25   Abstract:     File SDL implementation. Only for testing purpose.
26   Date:         16 March 2019
27 */
28
29 package sdl
30
31 import (
32         "encoding/json"
33         "errors"
34         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
35         "io/ioutil"
36         "os"
37         "routing-manager/pkg/models"
38         "routing-manager/pkg/rtmgr"
39         "strings"
40 )
41
42 /*
43 Reads the content of the rt.json file
44 Parses the JSON content and loads each xApp entry into an xApp object
45 Returns an array os xApp object
46 */
47
48 type File struct {
49         Sdl
50 }
51
52 func NewFile() *File {
53         instance := new(File)
54         return instance
55 }
56
57 func (f *File) ReadAll(file string) (*rtmgr.RicComponents, error) {
58         xapp.Logger.Debug("Invoked sdl.ReadAll(" + file + ")")
59         var rcs *rtmgr.RicComponents
60         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         }
69
70         err = json.Unmarshal(byteValue, &rcs)
71         if err != nil {
72                 return nil, errors.New("cannot parse data due to: " + err.Error())
73         }
74         xapp.Logger.Debug("file.fileReadAll returns: %v", rcs)
75         return rcs, nil
76 }
77
78 func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error {
79         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)
82         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
93 func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error {
94         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)
97
98         ricData, err := NewFile().ReadAll(file)
99         if err != nil {
100                 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
101                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
102         }
103         ricData.XApps = *xApps
104
105         byteValue, err := json.Marshal(ricData)
106         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 }
115
116 func (f *File) WriteNewE2TInstance(file string, E2TInst *rtmgr.E2TInstance, meiddata string) error {
117         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
127         if len(meiddata) > 0 {
128                 ricData.MeidMap = append(ricData.MeidMap, meiddata)
129         }
130         /*{
131                     ricData.MeidMap = []string {meiddata}
132                 }
133                         else {
134                     ricData.MeidMap = []string {}
135                 }*/
136
137         byteValue, err := json.Marshal(ricData)
138         if err != nil {
139                 return errors.New("cannot convert data due to: " + err.Error())
140         }
141         err = ioutil.WriteFile(file, byteValue, 0644)
142         if err != nil {
143                 return errors.New("cannot write file due to: " + err.Error())
144         }
145         return nil
146 }
147
148 func (f *File) WriteAssRANToE2TInstance(file string, rane2tmap models.RanE2tMap) error {
149         xapp.Logger.Debug("Invoked sdl.WriteAssRANToE2TInstance")
150         xapp.Logger.Debug("file.WriteAssRANToE2TInstance writes into file: " + file)
151         xapp.Logger.Debug("file.WriteAssRANToE2TInstance writes data: %v", rane2tmap)
152
153         ricData, err := NewFile().ReadAll(file)
154         if err != nil {
155                 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
156                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
157         }
158
159         //ricData.MeidMap = []string{}
160         for _, element := range rane2tmap {
161                 xapp.Logger.Info("data received")
162                 var str, meidar string
163                 for _, meid := range element.RanNamelist {
164                         meidar += meid + " "
165                 }
166                 str = "mme_ar|" + *element.E2TAddress + "|" + strings.TrimSuffix(meidar, " ")
167                 ricData.MeidMap = append(ricData.MeidMap, str)
168
169                 for key, _ := range ricData.E2Ts {
170                         if key == *element.E2TAddress {
171                                 var estObj rtmgr.E2TInstance
172                                 estObj = ricData.E2Ts[key]
173                                 estObj.Ranlist = append(ricData.E2Ts[key].Ranlist, element.RanNamelist...)
174                                 ricData.E2Ts[key] = estObj
175                         }
176                 }
177         }
178
179         byteValue, err := json.Marshal(ricData)
180         if err != nil {
181                 return errors.New("cannot convert data due to: " + err.Error())
182         }
183         err = ioutil.WriteFile(file, byteValue, 0644)
184         if err != nil {
185                 return errors.New("cannot write file due to: " + err.Error())
186         }
187         return nil
188 }
189
190 func (f *File) WriteDisAssRANFromE2TInstance(file string, disassranmap models.RanE2tMap) error {
191         xapp.Logger.Debug("Invoked sdl.WriteDisAssRANFromE2TInstance")
192         xapp.Logger.Debug("file.WriteDisAssRANFromE2TInstance writes into file: " + file)
193         xapp.Logger.Debug("file.WriteDisAssRANFromE2TInstance writes data: %v", disassranmap)
194
195         ricData, err := NewFile().ReadAll(file)
196         if err != nil {
197                 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
198                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
199         }
200
201         var str, meiddel, meiddisdel string
202         //ricData.MeidMap = []string{}
203         for _, element := range disassranmap {
204                 xapp.Logger.Info("data received")
205                 for _, meid := range element.RanNamelist {
206                         meiddisdel += meid + " "
207                 }
208                 if len(element.RanNamelist) > 0 {
209                         str = "mme_del|" + strings.TrimSuffix(meiddisdel, " ")
210                         ricData.MeidMap = append(ricData.MeidMap, str)
211                 }
212                 e2taddress_key := *element.E2TAddress
213                 //Check whether the provided E2T Address is available in SDL as a key.
214                 //If exist, proceed further to check RAN list, Otherwise move to next E2T Instance
215                 if _, exist := ricData.E2Ts[e2taddress_key]; exist {
216                         var estObj rtmgr.E2TInstance
217                         estObj = ricData.E2Ts[e2taddress_key]
218                         // If RAN list is empty, then routing manager assumes that all RANs attached associated to the particular E2T Instance to be removed.
219                         if len(element.RanNamelist) == 0 {
220                                 xapp.Logger.Debug("RAN List is empty. So disassociating all RANs from the E2T Instance: %v ", *element.E2TAddress)
221                                 for _, meid := range estObj.Ranlist {
222                                         meiddel += meid + " "
223                                 }
224                                 str = "mme_del|" + strings.TrimSuffix(meiddel, " ")
225                                 ricData.MeidMap = append(ricData.MeidMap, str)
226
227                                 estObj.Ranlist = []string{}
228                         } else {
229                                 xapp.Logger.Debug("Remove only selected rans from E2T Instance: %v and %v ", ricData.E2Ts[e2taddress_key].Ranlist, element.RanNamelist)
230                                 for _, disRanValue := range element.RanNamelist {
231                                         for ranIndex, ranValue := range ricData.E2Ts[e2taddress_key].Ranlist {
232                                                 if disRanValue == ranValue {
233                                                         estObj.Ranlist[ranIndex] = estObj.Ranlist[len(estObj.Ranlist)-1]
234                                                         estObj.Ranlist[len(estObj.Ranlist)-1] = ""
235                                                         estObj.Ranlist = estObj.Ranlist[:len(estObj.Ranlist)-1]
236                                                 }
237                                         }
238                                 }
239                         }
240                         ricData.E2Ts[e2taddress_key] = estObj
241                 }
242         }
243
244         xapp.Logger.Debug("Final data after disassociate: %v", ricData)
245
246         byteValue, err := json.Marshal(ricData)
247         if err != nil {
248                 return errors.New("cannot convert data due to: " + err.Error())
249         }
250         err = ioutil.WriteFile(file, byteValue, 0644)
251         if err != nil {
252                 return errors.New("cannot write file due to: " + err.Error())
253         }
254         return nil
255 }
256
257 func (f *File) WriteDeleteE2TInstance(file string, E2TInst *models.E2tDeleteData) error {
258         xapp.Logger.Debug("Invoked sdl.WriteDeleteE2TInstance")
259         xapp.Logger.Debug("file.WriteDeleteE2TInstance writes into file: " + file)
260         xapp.Logger.Debug("file.WriteDeleteE2TInstance writes data: %v", *E2TInst)
261
262         ricData, err := NewFile().ReadAll(file)
263         if err != nil {
264                 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
265                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
266         }
267
268         //ricData.MeidMap = []string{}
269         var delrow, meiddel string
270         if len(E2TInst.RanNamelistTobeDissociated) > 0 {
271                 for _, meid := range E2TInst.RanNamelistTobeDissociated {
272                         meiddel += meid + " "
273                 }
274                 delrow = "mme_del|" + strings.TrimSuffix(meiddel, " ")
275                 ricData.MeidMap = append(ricData.MeidMap, delrow)
276         } else {
277                 if len(ricData.E2Ts[*E2TInst.E2TAddress].Ranlist) > 0 {
278                         for _, meid := range ricData.E2Ts[*E2TInst.E2TAddress].Ranlist {
279                                 meiddel += meid + " "
280                         }
281                         delrow = "mme_del|" + strings.TrimSuffix(meiddel, " ")
282                         ricData.MeidMap = append(ricData.MeidMap, delrow)
283                 }
284         }
285
286         delete(ricData.E2Ts, *E2TInst.E2TAddress)
287
288         for _, element := range E2TInst.RanAssocList {
289                 var str, meidar string
290                 xapp.Logger.Info("data received")
291                 for _, meid := range element.RanNamelist {
292                         meidar = meid + " "
293                 }
294                 str = "mme_ar|" + *element.E2TAddress + "|" + strings.TrimSuffix(meidar, " ")
295                 ricData.MeidMap = append(ricData.MeidMap, str)
296                 key := *element.E2TAddress
297
298                 if val, ok := ricData.E2Ts[key]; ok {
299                         var estObj rtmgr.E2TInstance
300                         estObj = val
301                         estObj.Ranlist = append(ricData.E2Ts[key].Ranlist, element.RanNamelist...)
302                         ricData.E2Ts[key] = estObj
303                 } else {
304                         xapp.Logger.Error("file.WriteDeleteE2TInstance E2T instance is not found for provided E2TAddress : %v", errors.New(key).Error())
305                 }
306
307         }
308
309         byteValue, err := json.Marshal(ricData)
310         if err != nil {
311                 return errors.New("cannot convert data due to: " + err.Error())
312         }
313         err = ioutil.WriteFile(file, byteValue, 0644)
314         if err != nil {
315                 return errors.New("cannot write file due to: " + err.Error())
316         }
317         return nil
318 }