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