Adding scope of RICPlatform that are under Apache License
[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         "io/ioutil"
35         "os"
36         "routing-manager/pkg/rtmgr"
37 )
38
39 /*
40 Reads the content of the rt.json file
41 Parses the JSON content and loads each xApp entry into an xApp object
42 Returns an array os xApp object
43 */
44
45 type File struct {
46         Sdl
47 }
48
49 func NewFile() *File {
50         instance := new(File)
51         return instance
52 }
53
54 func (f *File) ReadAll(file string) (*rtmgr.RicComponents, error) {
55         rtmgr.Logger.Debug("Invoked sdl.ReadAll(" + file + ")")
56         var rcs *rtmgr.RicComponents
57         jsonFile, err := os.Open(file)
58         if err != nil {
59                 return nil, errors.New("cannot open the file due to: " + err.Error())
60         }
61         defer jsonFile.Close()
62
63         byteValue, err := ioutil.ReadAll(jsonFile)
64         if err != nil {
65                 return nil, errors.New("cannot read the file due to: " + err.Error())
66         }
67         err = json.Unmarshal(byteValue, &rcs)
68         if err != nil {
69                 return nil, errors.New("cannot parse data due to: " + err.Error())
70         }
71         rtmgr.Logger.Debug("file.fileReadAll returns: %v", rcs)
72         return rcs, nil
73 }
74
75 func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error {
76         rtmgr.Logger.Debug("Invoked sdl.WriteAll")
77         rtmgr.Logger.Debug("file.fileWriteAll writes into file: " + file)
78         rtmgr.Logger.Debug("file.fileWriteAll writes data: %v", *rcs)
79         byteValue, err := json.Marshal(rcs)
80         if err != nil {
81                 return errors.New("cannot convert data due to: " + err.Error())
82         }
83         err = ioutil.WriteFile(file, byteValue, 0644)
84         if err != nil {
85                 return errors.New("cannot write file due to: " + err.Error())
86         }
87         return nil
88 }
89
90 func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error {
91         rtmgr.Logger.Debug("Invoked sdl.WriteXApps")
92         rtmgr.Logger.Debug("file.fileWriteXApps writes into file: " + file)
93         rtmgr.Logger.Debug("file.fileWriteXApps writes data: %v", *xApps)
94
95         ricData, err := NewFile().ReadAll(file)
96         if err != nil {
97                 rtmgr.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
98                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
99         }
100         ricData.XApps = *xApps
101
102         byteValue, err := json.Marshal(ricData)
103         if err != nil {
104                 return errors.New("cannot convert data due to: " + err.Error())
105         }
106         err = ioutil.WriteFile(file, byteValue, 0644)
107         if err != nil {
108                 return errors.New("cannot write file due to: " + err.Error())
109         }
110         return nil
111 }