Create E2T API implementation and yaml change for multiple e2t instances handling...
[ric-plt/rtmgr.git] / pkg / sdl / file.go
index 11c3350..b4f1e8e 100644 (file)
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
+
+   This source code is part of the near-RT RIC (RAN Intelligent Controller)
+   platform project (RICP).
+
 ==================================================================================
 */
 /*
@@ -27,9 +31,10 @@ package sdl
 import (
        "encoding/json"
        "errors"
+       "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
        "io/ioutil"
        "os"
-       "rtmgr"
+       "routing-manager/pkg/rtmgr"
 )
 
 /*
@@ -37,32 +42,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) {
+       xapp.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
+       xapp.Logger.Debug("file.fileReadAll returns: %v", rcs)
+       return rcs, nil
+}
+
+func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error {
+       xapp.Logger.Debug("Invoked sdl.WriteAll")
+       xapp.Logger.Debug("file.fileWriteAll writes into file: " + file)
+       xapp.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 fileWriteAll(file string, xapps *[]rtmgr.XApp) error {
-       rtmgr.Logger.Debug("Invoked file.fileWriteAll")
-       rtmgr.Logger.Debug("file.fileWriteAll writes into file: " + file)
-       rtmgr.Logger.Debug("file.fileWriteAll writes data: %v", (*xapps))
-       byteValue, err := json.Marshal(xapps)
+func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error {
+       xapp.Logger.Debug("Invoked sdl.WriteXApps")
+       xapp.Logger.Debug("file.fileWriteXApps writes into file: " + file)
+       xapp.Logger.Debug("file.fileWriteXApps writes data: %v", *xApps)
+
+       ricData, err := NewFile().ReadAll(file)
+       if err != nil {
+               xapp.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())
        }
@@ -72,3 +110,26 @@ func fileWriteAll(file string, xapps *[]rtmgr.XApp) error {
        }
        return nil
 }
+
+func (f *File) WriteNewE2TInstance(file string, E2TInst *rtmgr.E2TInstance) error {
+        xapp.Logger.Debug("Invoked sdl.WriteNewE2TInstance")
+        xapp.Logger.Debug("file.WriteNewE2TInstance writes into file: " + file)
+        xapp.Logger.Debug("file.WriteNewE2TInstance writes data: %v", *E2TInst)
+
+        ricData, err := NewFile().ReadAll(file)
+        if err != nil {
+                xapp.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.E2Ts[E2TInst.Fqdn] = *E2TInst
+
+        byteValue, err := json.Marshal(ricData)
+        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
+}