X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fsdl%2Ffile.go;h=2d49be77ec48fc10d716289b7e80e6a2ced8a3ea;hb=988d1da4f05b180cac103e59e8118e2fd9f1ddec;hp=11c3350a3e74c3440be0ea3fa1cab3841936691d;hpb=871fa393844ce1b61b8d5218d27687d9fc05803a;p=ric-plt%2Frtmgr.git diff --git a/pkg/sdl/file.go b/pkg/sdl/file.go index 11c3350..2d49be7 100644 --- a/pkg/sdl/file.go +++ b/pkg/sdl/file.go @@ -29,7 +29,7 @@ import ( "errors" "io/ioutil" "os" - "rtmgr" + "routing-manager/pkg/rtmgr" ) /* @@ -37,32 +37,65 @@ Reads the content of the rt.json file Parses the JSON content and loads each xApp entry into an xApp object Returns an array os xApp object */ -func fileReadAll(file string) (*[]rtmgr.XApp, error) { - rtmgr.Logger.Debug("Invoked file.fileReadAll") - rtmgr.Logger.Debug("file.fileReadAll opens file: " + file) - var xapps *[]rtmgr.XApp + +type File struct { + Sdl +} + +func NewFile() *File { + instance := new(File) + return instance +} + +func (f *File) ReadAll(file string) (*rtmgr.RicComponents, error) { + rtmgr.Logger.Debug("Invoked sdl.ReadAll(" + file + ")") + var rcs *rtmgr.RicComponents jsonFile, err := os.Open(file) if err != nil { return nil, errors.New("cannot open the file due to: " + err.Error()) } defer jsonFile.Close() + byteValue, err := ioutil.ReadAll(jsonFile) if err != nil { return nil, errors.New("cannot read the file due to: " + err.Error()) } - err = json.Unmarshal(byteValue, &xapps) + err = json.Unmarshal(byteValue, &rcs) if err != nil { return nil, errors.New("cannot parse data due to: " + err.Error()) } - rtmgr.Logger.Debug("file.fileReadAll returns: %v", xapps) - return xapps, nil + rtmgr.Logger.Debug("file.fileReadAll returns: %v", rcs) + return rcs, nil } -func fileWriteAll(file string, xapps *[]rtmgr.XApp) error { - rtmgr.Logger.Debug("Invoked file.fileWriteAll") +func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error { + rtmgr.Logger.Debug("Invoked sdl.WriteAll") rtmgr.Logger.Debug("file.fileWriteAll writes into file: " + file) - rtmgr.Logger.Debug("file.fileWriteAll writes data: %v", (*xapps)) - byteValue, err := json.Marshal(xapps) + rtmgr.Logger.Debug("file.fileWriteAll writes data: %v", *rcs) + byteValue, err := json.Marshal(rcs) + if err != nil { + return errors.New("cannot convert data due to: " + err.Error()) + } + err = ioutil.WriteFile(file, byteValue, 0644) + if err != nil { + return errors.New("cannot write file due to: " + err.Error()) + } + return nil +} + +func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error { + rtmgr.Logger.Debug("Invoked sdl.WriteXApps") + rtmgr.Logger.Debug("file.fileWriteXApps writes into file: " + file) + rtmgr.Logger.Debug("file.fileWriteXApps writes data: %v", *xApps) + + ricData, err := NewFile().ReadAll(file) + if err != nil { + rtmgr.Logger.Error("cannot get data from sdl interface due to: " + err.Error()) + return errors.New("cannot read full ric data to modify xApps data, due to: " + err.Error()) + } + ricData.XApps = *xApps + + byteValue, err := json.Marshal(ricData) if err != nil { return errors.New("cannot convert data due to: " + err.Error()) }