Fix ENDC_X2_SETUP related routes
[ric-plt/rtmgr.git] / pkg / sdl / file.go
index 11c3350..2d49be7 100644 (file)
@@ -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())
        }