Add new packages to the container
[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 */
19 /*
20   Mnemonic:     file.go
21   Abstract:     File SDL implementation. Only for testing purpose.
22   Date:         16 March 2019
23 */
24
25 package sdl
26
27 import (
28         "encoding/json"
29         "errors"
30         "io/ioutil"
31         "os"
32         "routing-manager/pkg/rtmgr"
33 )
34
35 /*
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
39 */
40
41 type File struct {
42         Sdl
43 }
44
45 func NewFile() *File {
46         instance := new(File)
47         return instance
48 }
49
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)
54         if err != nil {
55                 return nil, errors.New("cannot open the file due to: " + err.Error())
56         }
57         defer jsonFile.Close()
58
59         byteValue, err := ioutil.ReadAll(jsonFile)
60         if err != nil {
61                 return nil, errors.New("cannot read the file due to: " + err.Error())
62         }
63         err = json.Unmarshal(byteValue, &rcs)
64         if err != nil {
65                 return nil, errors.New("cannot parse data due to: " + err.Error())
66         }
67         rtmgr.Logger.Debug("file.fileReadAll returns: %v", rcs)
68         return rcs, nil
69 }
70
71 func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error {
72         rtmgr.Logger.Debug("Invoked sdl.WriteAll")
73         rtmgr.Logger.Debug("file.fileWriteAll writes into file: " + file)
74         rtmgr.Logger.Debug("file.fileWriteAll writes data: %v", *rcs)
75         byteValue, err := json.Marshal(rcs)
76         if err != nil {
77                 return errors.New("cannot convert data due to: " + err.Error())
78         }
79         err = ioutil.WriteFile(file, byteValue, 0644)
80         if err != nil {
81                 return errors.New("cannot write file due to: " + err.Error())
82         }
83         return nil
84 }
85
86 func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error {
87         rtmgr.Logger.Debug("Invoked sdl.WriteXApps")
88         rtmgr.Logger.Debug("file.fileWriteXApps writes into file: " + file)
89         rtmgr.Logger.Debug("file.fileWriteXApps writes data: %v", *xApps)
90
91         ricData, err := NewFile().ReadAll(file)
92         if err != nil {
93                 rtmgr.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
94                 return errors.New("cannot read full ric data to modify xApps data, due to:  " + err.Error())
95         }
96         ricData.XApps = *xApps
97
98         byteValue, err := json.Marshal(ricData)
99         if err != nil {
100                 return errors.New("cannot convert data due to: " + err.Error())
101         }
102         err = ioutil.WriteFile(file, byteValue, 0644)
103         if err != nil {
104                 return errors.New("cannot write file due to: " + err.Error())
105         }
106         return nil
107 }