Multiple E2T instance feature - Introduced APIs Delete E2T Instance and associate...
[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/rtmgr"
38         "routing-manager/pkg/models"
39 )
40
41 /*
42 Reads the content of the rt.json file
43 Parses the JSON content and loads each xApp entry into an xApp object
44 Returns an array os xApp object
45 */
46
47 type File struct {
48         Sdl
49 }
50
51 func NewFile() *File {
52         instance := new(File)
53         return instance
54 }
55
56 func (f *File) ReadAll(file string) (*rtmgr.RicComponents, error) {
57         xapp.Logger.Debug("Invoked sdl.ReadAll(" + file + ")")
58         var rcs *rtmgr.RicComponents
59         jsonFile, err := os.Open(file)
60         if err != nil {
61                 return nil, errors.New("cannot open the file due to: " + err.Error())
62         }
63         defer jsonFile.Close()
64         byteValue, err := ioutil.ReadAll(jsonFile)
65         if err != nil {
66                 return nil, errors.New("cannot read the file due to: " + err.Error())
67         }
68
69         err = json.Unmarshal(byteValue, &rcs)
70         if err != nil {
71                 return nil, errors.New("cannot parse data due to: " + err.Error())
72         }
73         xapp.Logger.Debug("file.fileReadAll returns: %v", rcs)
74         return rcs, nil
75 }
76
77 func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error {
78         xapp.Logger.Debug("Invoked sdl.WriteAll")
79         xapp.Logger.Debug("file.fileWriteAll writes into file: " + file)
80         xapp.Logger.Debug("file.fileWriteAll writes data: %v", *rcs)
81         byteValue, err := json.Marshal(rcs)
82         if err != nil {
83                 return errors.New("cannot convert data due to: " + err.Error())
84         }
85         err = ioutil.WriteFile(file, byteValue, 0644)
86         if err != nil {
87                 return errors.New("cannot write file due to: " + err.Error())
88         }
89         return nil
90 }
91
92 func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error {
93         xapp.Logger.Debug("Invoked sdl.WriteXApps")
94         xapp.Logger.Debug("file.fileWriteXApps writes into file: " + file)
95         xapp.Logger.Debug("file.fileWriteXApps writes data: %v", *xApps)
96
97         ricData, err := NewFile().ReadAll(file)
98         if err != nil {
99                 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
100                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
101         }
102         ricData.XApps = *xApps
103
104         byteValue, err := json.Marshal(ricData)
105         if err != nil {
106                 return errors.New("cannot convert data due to: " + err.Error())
107         }
108         err = ioutil.WriteFile(file, byteValue, 0644)
109         if err != nil {
110                 return errors.New("cannot write file due to: " + err.Error())
111         }
112         return nil
113 }
114
115 func (f *File) WriteNewE2TInstance(file string, E2TInst *rtmgr.E2TInstance) error {
116         xapp.Logger.Debug("Invoked sdl.WriteNewE2TInstance")
117         xapp.Logger.Debug("file.WriteNewE2TInstance writes into file: " + file)
118         xapp.Logger.Debug("file.WriteNewE2TInstance writes data: %v", *E2TInst)
119
120         ricData, err := NewFile().ReadAll(file)
121         if err != nil {
122                 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
123                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
124         }
125         ricData.E2Ts[E2TInst.Fqdn] = *E2TInst
126
127         byteValue, err := json.Marshal(ricData)
128         if err != nil {
129                 return errors.New("cannot convert data due to: " + err.Error())
130         }
131         err = ioutil.WriteFile(file, byteValue, 0644)
132         if err != nil {
133                 return errors.New("cannot write file due to: " + err.Error())
134         }
135         return nil
136 }
137
138 func (f *File) WriteAssRANToE2TInstance(file string, rane2tmap models.RanE2tMap) error {
139         xapp.Logger.Debug("Invoked sdl.WriteAssRANToE2TInstance")
140         xapp.Logger.Debug("file.WriteAssRANToE2TInstance writes into file: " + file)
141         xapp.Logger.Debug("file.WriteAssRANToE2TInstance writes data: %v", rane2tmap)
142
143         ricData, err := NewFile().ReadAll(file)
144         if err != nil {
145                 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
146                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
147         }
148         for _, element := range rane2tmap {
149                 xapp.Logger.Info("data received")
150                 for key, _ := range ricData.E2Ts {
151                         if key == *element.E2TAddress {
152                                 var estObj rtmgr.E2TInstance
153                                 estObj = ricData.E2Ts[key]
154                                 estObj.Ranlist = append(ricData.E2Ts[key].Ranlist, element.RanNamelist...)
155                                 ricData.E2Ts[key]= estObj
156                         }
157                 }
158         }
159
160         byteValue, err := json.Marshal(ricData)
161         if err != nil {
162                 return errors.New("cannot convert data due to: " + err.Error())
163         }
164         err = ioutil.WriteFile(file, byteValue, 0644)
165         if err != nil {
166                 return errors.New("cannot write file due to: " + err.Error())
167         }
168         return nil
169 }
170
171 func (f *File) WriteDisAssRANFromE2TInstance(file string, disassranmap models.RanE2tMap) error {
172         xapp.Logger.Debug("Invoked sdl.WriteDisAssRANFromE2TInstance")
173         xapp.Logger.Debug("file.WriteDisAssRANFromE2TInstance writes into file: " + file)
174         xapp.Logger.Debug("file.WriteDisAssRANFromE2TInstance writes data: %v", disassranmap)
175
176         ricData, err := NewFile().ReadAll(file)
177         if err != nil {
178                 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
179                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
180         }
181         for _, element := range disassranmap {
182                 xapp.Logger.Info("data received")
183                 e2taddress_key := *element.E2TAddress
184                 //Check whether the provided E2T Address is available in SDL as a key. 
185                 //If exist, proceed further to check RAN list, Otherwise move to next E2T Instance
186                 if _, exist := ricData.E2Ts[e2taddress_key]; exist {
187                         var estObj rtmgr.E2TInstance
188                         estObj = ricData.E2Ts[e2taddress_key]
189                         // If RAN list is empty, then routing manager assumes that all RANs attached associated to the particular E2T Instance to be removed.
190                         if len(element.RanNamelist) == 0 {
191                                 xapp.Logger.Debug("RAN List is empty. So disassociating all RANs from the E2T Instance: %v ", *element.E2TAddress)
192                                 estObj.Ranlist = []string{}
193                         } else {
194                                 xapp.Logger.Debug("Remove only selected rans from E2T Instance: %v and %v ", ricData.E2Ts[e2taddress_key].Ranlist, element.RanNamelist)
195                                 for _, disRanValue := range element.RanNamelist {
196                                         for ranIndex, ranValue := range ricData.E2Ts[e2taddress_key].Ranlist {
197                                                 if disRanValue == ranValue {
198                                                         estObj.Ranlist[ranIndex] = estObj.Ranlist[len(estObj.Ranlist)-1]
199                                                         estObj.Ranlist[len(estObj.Ranlist)-1] = ""
200                                                         estObj.Ranlist = estObj.Ranlist[:len(estObj.Ranlist)-1]
201                                                 }
202                                         }
203                                 }
204                         }
205                         ricData.E2Ts[e2taddress_key]= estObj
206                 }
207         }
208
209         xapp.Logger.Debug("Final data after disassociate: %v", ricData)
210
211         byteValue, err := json.Marshal(ricData)
212         if err != nil {
213                 return errors.New("cannot convert data due to: " + err.Error())
214         }
215         err = ioutil.WriteFile(file, byteValue, 0644)
216         if err != nil {
217                 return errors.New("cannot write file due to: " + err.Error())
218         }
219         return nil
220 }
221
222 func (f *File) WriteDeleteE2TInstance(file string, E2TInst *models.E2tDeleteData) error {
223         xapp.Logger.Debug("Invoked sdl.WriteDeleteE2TInstance")
224         xapp.Logger.Debug("file.WriteDeleteE2TInstance writes into file: " + file)
225         xapp.Logger.Debug("file.WriteDeleteE2TInstance writes data: %v", *E2TInst)
226
227         ricData, err := NewFile().ReadAll(file)
228         if err != nil {
229                 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
230                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
231         }
232
233         delete(ricData.E2Ts, *E2TInst.E2TAddress)
234
235
236         for _, element := range E2TInst.RanAssocList {
237                 xapp.Logger.Info("data received")
238                 key := *element.E2TAddress
239
240                 if val, ok := ricData.E2Ts[key]; ok {
241                         var estObj rtmgr.E2TInstance
242                         estObj = val
243                         estObj.Ranlist = append(ricData.E2Ts[key].Ranlist, element.RanNamelist...)
244                         ricData.E2Ts[key]= estObj
245                 } else {
246                         xapp.Logger.Error("file.WriteDeleteE2TInstance E2T instance is not found for provided E2TAddress : %v", errors.New(key).Error())
247                 }
248
249         }
250
251         byteValue, err := json.Marshal(ricData)
252         if err != nil {
253                 return errors.New("cannot convert data due to: " + err.Error())
254         }
255         err = ioutil.WriteFile(file, byteValue, 0644)
256         if err != nil {
257                 return errors.New("cannot write file due to: " + err.Error())
258         }
259         return nil
260 }