MEID tableupdated incrementally rather than full update
[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         "strings"
38         "routing-manager/pkg/rtmgr"
39         "routing-manager/pkg/models"
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 = []string {meiddata}
129         } else {
130             ricData.MeidMap = []string {}
131         }
132
133
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 }
144
145 func (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         }
155
156         ricData.MeidMap = []string{}
157         for _, element := range rane2tmap {
158                 xapp.Logger.Info("data received")
159                 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
166                 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
187 func (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         }
197
198         var str,meiddel,meiddisdel string
199         ricData.MeidMap = []string{}
200         for _, element := range disassranmap {
201                 xapp.Logger.Info("data received")
202                 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                 }
209                 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)
218                         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{}
225                         } 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
254 func (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
265
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
284         delete(ricData.E2Ts, *E2TInst.E2TAddress)
285
286         for _, element := range E2TInst.RanAssocList {
287                 var str,meidar string
288                 xapp.Logger.Info("data received")
289                 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)
294                 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 }