RIC:1060: Change in PTL
[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:" + file + ", file.fileWriteAll writes data: %v", *rcs )
80         byteValue, err := json.Marshal(rcs)
81         if err != nil {
82                 return errors.New("Cannot convert data due to: " + err.Error())
83         }
84         err = ioutil.WriteFile(file, byteValue, 0644)
85         if err != nil {
86                 return errors.New("cannot write file due to: " + err.Error())
87         }
88         return nil
89 }
90
91 func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error {
92     xapp.Logger.Debug("Invoked sdl.WriteXApps: " + file + ", file.fileWriteXApps writes data: %v", *xApps )
93
94         ricData, err := NewFile().ReadAll(file)
95         if err != nil {
96                 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
97                 return errors.New("Cannot read full ric data to modify xApps data, due to:  " + err.Error())
98         }
99         ricData.XApps = *xApps
100
101         byteValue, err := json.Marshal(ricData)
102         if err != nil {
103                 return errors.New("cannot convert data due to: " + err.Error())
104         }
105         err = ioutil.WriteFile(file, byteValue, 0644)
106         if err != nil {
107                 return errors.New("cannot write file due to: " + err.Error())
108         }
109         return nil
110 }
111
112 func (f *File) WriteNewE2TInstance(file string, E2TInst *rtmgr.E2TInstance, meiddata string) error {
113     xapp.Logger.Debug("Invoked sdl.WriteNewE2TInstance: " + file + ", file.WriteNewE2TInstance writes data: %v", *E2TInst )
114
115         ricData, err := NewFile().ReadAll(file)
116         if err != nil {
117                 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
118                 return errors.New("cannot read full ric data to modify xApps data due to:  " + err.Error())
119         }
120         ricData.E2Ts[E2TInst.Fqdn] = *E2TInst
121         if len(meiddata) > 0 {
122                 ricData.MeidMap = append(ricData.MeidMap, meiddata)
123         }
124         /*{
125                     ricData.MeidMap = []string {meiddata}
126                 }
127                         else {
128                     ricData.MeidMap = []string {}
129                 }*/
130
131         byteValue, err := json.Marshal(ricData)
132         if err != nil {
133                 return errors.New("cannot convert data due to: " + err.Error())
134         }
135         err = ioutil.WriteFile(file, byteValue, 0644)
136         if err != nil {
137                 return errors.New("cannot write file due to: " + err.Error())
138         }
139         return nil
140 }
141
142 func (f *File) WriteAssRANToE2TInstance(file string, rane2tmap models.RanE2tMap) error {
143     xapp.Logger.Debug("Invoked sdl.WriteAssRANToE2TInstance: " + file + ", file.WriteAssRANToE2TInstance writes data: %v", rane2tmap)
144
145         ricData, err := NewFile().ReadAll(file)
146         if err != nil {
147                 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
148                 return errors.New("cannot read full ric data to modify xApps data due to:  " + err.Error())
149         }
150
151         //ricData.MeidMap = []string{}
152         for _, element := range rane2tmap {
153                 xapp.Logger.Info("Data received")
154                 var str, meidar string
155                 for _, meid := range element.RanNamelist {
156                         meidar += meid + " "
157                 }
158                 str = "mme_ar|" + *element.E2TAddress + "|" + strings.TrimSuffix(meidar, " ")
159                 ricData.MeidMap = append(ricData.MeidMap, str)
160
161                 for key, _ := range ricData.E2Ts {
162                         if key == *element.E2TAddress {
163                                 var estObj rtmgr.E2TInstance
164                                 estObj = ricData.E2Ts[key]
165                                 estObj.Ranlist = append(ricData.E2Ts[key].Ranlist, element.RanNamelist...)
166                                 ricData.E2Ts[key] = estObj
167                         }
168                 }
169         }
170
171         byteValue, err := json.Marshal(ricData)
172         if err != nil {
173                 return errors.New("cannot convert data due to: " + err.Error())
174         }
175         err = ioutil.WriteFile(file, byteValue, 0644)
176         if err != nil {
177                 return errors.New("cannot write file due to: " + err.Error())
178         }
179         return nil
180 }
181
182 func (f *File) WriteDisAssRANFromE2TInstance(file string, disassranmap models.RanE2tMap) error {
183     xapp.Logger.Debug("Invoked sdl.WriteDisAssRANFromE2TInstance: " + file + ",file.WriteDisAssRANFromE2TInstance writes data: %v", disassranmap)
184
185         ricData, err := NewFile().ReadAll(file)
186         if err != nil {
187                 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
188                 return errors.New("cannot read full ric data to modify xApps data due to:  " + err.Error())
189         }
190
191         var str, meiddel, meiddisdel string
192         //ricData.MeidMap = []string{}
193         for _, element := range disassranmap {
194                 xapp.Logger.Info("Data received")
195                 for _, meid := range element.RanNamelist {
196                         meiddisdel += meid + " "
197                 }
198                 if len(element.RanNamelist) > 0 {
199                         str = "mme_del|" + strings.TrimSuffix(meiddisdel, " ")
200                         ricData.MeidMap = append(ricData.MeidMap, str)
201                 }
202                 e2taddress_key := *element.E2TAddress
203                 //Check whether the provided E2T Address is available in SDL as a key.
204                 //If exist, proceed further to check RAN list, Otherwise move to next E2T Instance
205                 if _, exist := ricData.E2Ts[e2taddress_key]; exist {
206                         var estObj rtmgr.E2TInstance
207                         estObj = ricData.E2Ts[e2taddress_key]
208                         // If RAN list is empty, then routing manager assumes that all RANs attached associated to the particular E2T Instance to be removed.
209                         if len(element.RanNamelist) == 0 {
210                                 xapp.Logger.Debug("RAN List is empty. So disassociating all RANs from the E2T Instance: %v ", *element.E2TAddress)
211                                 for _, meid := range estObj.Ranlist {
212                                         meiddel += meid + " "
213                                 }
214                                 str = "mme_del|" + strings.TrimSuffix(meiddel, " ")
215                                 ricData.MeidMap = append(ricData.MeidMap, str)
216
217                                 estObj.Ranlist = []string{}
218                         } else {
219                                 xapp.Logger.Debug("Remove only selected rans from E2T Instance: %v and %v ", ricData.E2Ts[e2taddress_key].Ranlist, element.RanNamelist)
220                                 for _, disRanValue := range element.RanNamelist {
221                                         for ranIndex, ranValue := range ricData.E2Ts[e2taddress_key].Ranlist {
222                                                 if disRanValue == ranValue {
223                                                         estObj.Ranlist[ranIndex] = estObj.Ranlist[len(estObj.Ranlist)-1]
224                                                         estObj.Ranlist[len(estObj.Ranlist)-1] = ""
225                                                         estObj.Ranlist = estObj.Ranlist[:len(estObj.Ranlist)-1]
226                                                 }
227                                         }
228                                 }
229                         }
230                         ricData.E2Ts[e2taddress_key] = estObj
231                 }
232         }
233
234         xapp.Logger.Debug("Final data after disassociate: %v", ricData)
235
236         byteValue, err := json.Marshal(ricData)
237         if err != nil {
238                 return errors.New("cannot convert data due to: " + err.Error())
239         }
240         err = ioutil.WriteFile(file, byteValue, 0644)
241         if err != nil {
242                 return errors.New("cannot write file due to: " + err.Error())
243         }
244         return nil
245 }
246
247 func (f *File) WriteDeleteE2TInstance(file string, E2TInst *models.E2tDeleteData) error {
248     xapp.Logger.Debug("Invoked sdl.WriteDeleteE2TInstance: " + file + ",file.WriteDeleteE2TInstance writes data: %v", *E2TInst)
249
250         ricData, err := NewFile().ReadAll(file)
251         if err != nil {
252                 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
253                 return errors.New("cannot read full ric data to modify xApps data due to:  " + err.Error())
254         }
255
256         //ricData.MeidMap = []string{}
257         var delrow, meiddel string
258         if len(E2TInst.RanNamelistTobeDissociated) > 0 {
259                 for _, meid := range E2TInst.RanNamelistTobeDissociated {
260                         meiddel += meid + " "
261                 }
262                 delrow = "mme_del|" + strings.TrimSuffix(meiddel, " ")
263                 ricData.MeidMap = append(ricData.MeidMap, delrow)
264         } else {
265                 if len(ricData.E2Ts[*E2TInst.E2TAddress].Ranlist) > 0 {
266                         for _, meid := range ricData.E2Ts[*E2TInst.E2TAddress].Ranlist {
267                                 meiddel += meid + " "
268                         }
269                         delrow = "mme_del|" + strings.TrimSuffix(meiddel, " ")
270                         ricData.MeidMap = append(ricData.MeidMap, delrow)
271                 }
272         }
273
274         delete(ricData.E2Ts, *E2TInst.E2TAddress)
275
276         for _, element := range E2TInst.RanAssocList {
277                 var str, meidar string
278                 xapp.Logger.Info("Data received")
279                 for _, meid := range element.RanNamelist {
280                         meidar = meid + " "
281                 }
282                 str = "mme_ar|" + *element.E2TAddress + "|" + strings.TrimSuffix(meidar, " ")
283                 ricData.MeidMap = append(ricData.MeidMap, str)
284                 key := *element.E2TAddress
285
286                 if val, ok := ricData.E2Ts[key]; ok {
287                         var estObj rtmgr.E2TInstance
288                         estObj = val
289                         estObj.Ranlist = append(ricData.E2Ts[key].Ranlist, element.RanNamelist...)
290                         ricData.E2Ts[key] = estObj
291                 } else {
292                         xapp.Logger.Error("file.WriteDeleteE2TInstance E2T instance is not found for provided E2TAddress : %v", errors.New(key).Error())
293                 }
294
295         }
296
297         byteValue, err := json.Marshal(ricData)
298         if err != nil {
299                 return errors.New("cannot convert data due to: " + err.Error())
300         }
301         err = ioutil.WriteFile(file, byteValue, 0644)
302         if err != nil {
303                 return errors.New("cannot write file due to: " + err.Error())
304         }
305         return nil
306 }