2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
21 Abstract: File SDL implementation. Only for testing purpose.
32 "routing-manager/pkg/rtmgr"
36 Reads the content of the rt.json file
37 Parses the JSON content and loads each xApp entry into an xApp object
38 Returns an array os xApp object
45 func NewFile() *File {
50 func (f *File) ReadAll(file string) (*rtmgr.RicComponents, error) {
51 rtmgr.Logger.Debug("Invoked sdl.ReadAll("+ file +")")
52 var rcs *rtmgr.RicComponents
53 jsonFile, err := os.Open(file)
55 return nil, errors.New("cannot open the file due to: " + err.Error())
57 defer jsonFile.Close()
58 byteValue, err := ioutil.ReadAll(jsonFile)
60 return nil, errors.New("cannot read the file due to: " + err.Error())
62 err = json.Unmarshal(byteValue, &rcs)
64 return nil, errors.New("cannot parse data due to: " + err.Error())
66 rtmgr.Logger.Debug("file.fileReadAll returns: %v", rcs)
70 func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error {
71 rtmgr.Logger.Debug("Invoked sdl.WriteAll")
72 rtmgr.Logger.Debug("file.fileWriteAll writes into file: " + file)
73 rtmgr.Logger.Debug("file.fileWriteAll writes data: %v", (*rcs))
74 byteValue, err := json.Marshal(rcs)
76 return errors.New("cannot convert data due to: " + err.Error())
78 err = ioutil.WriteFile(file, byteValue, 0644)
80 return errors.New("cannot write file due to: " + err.Error())
85 func (f *File) WriteXapps(file string, xapps *[]rtmgr.XApp) error {
86 rtmgr.Logger.Debug("Invoked sdl.WriteXapps")
87 rtmgr.Logger.Debug("file.fileWriteXapps writes into file: " + file)
88 rtmgr.Logger.Debug("file.fileWriteXapps writes data: %v", (*xapps))
90 ricData, err := NewFile().ReadAll(file)
91 if err != nil || ricData == nil {
92 rtmgr.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
93 return errors.New("cannot read full ric data to modify xapps data, due to: " + err.Error())
96 ricData.Xapps = *xapps
98 byteValue, err := json.Marshal(ricData)
100 return errors.New("cannot convert data due to: " + err.Error())
102 err = ioutil.WriteFile(file, byteValue, 0644)
104 return errors.New("cannot write file due to: " + err.Error())