blob: b4f1e8ea3ea541d4ee049b52ad0d5c9d96f864d9 [file] [log] [blame]
Balint Uveges871fa392019-04-02 20:31:11 +00001/*
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.
wahidw761934a2019-11-27 06:07:26 +000017
18 This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 platform project (RICP).
20
Balint Uveges871fa392019-04-02 20:31:11 +000021==================================================================================
22*/
23/*
24 Mnemonic: file.go
25 Abstract: File SDL implementation. Only for testing purpose.
26 Date: 16 March 2019
27*/
28
29package sdl
30
31import (
32 "encoding/json"
33 "errors"
wahidwa8596ec2019-12-05 06:30:42 +000034 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Balint Uveges871fa392019-04-02 20:31:11 +000035 "io/ioutil"
36 "os"
kalnagy92162652019-07-02 15:15:49 +020037 "routing-manager/pkg/rtmgr"
Balint Uveges871fa392019-04-02 20:31:11 +000038)
39
40/*
41Reads the content of the rt.json file
42Parses the JSON content and loads each xApp entry into an xApp object
43Returns an array os xApp object
44*/
kalnagy92162652019-07-02 15:15:49 +020045
46type File struct {
47 Sdl
48}
49
50func NewFile() *File {
51 instance := new(File)
52 return instance
53}
54
55func (f *File) ReadAll(file string) (*rtmgr.RicComponents, error) {
wahidwa8596ec2019-12-05 06:30:42 +000056 xapp.Logger.Debug("Invoked sdl.ReadAll(" + file + ")")
kalnagy92162652019-07-02 15:15:49 +020057 var rcs *rtmgr.RicComponents
Balint Uveges871fa392019-04-02 20:31:11 +000058 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()
zkoczkaaaf8d392019-10-02 17:16:06 +020063
Balint Uveges871fa392019-04-02 20:31:11 +000064 byteValue, err := ioutil.ReadAll(jsonFile)
65 if err != nil {
66 return nil, errors.New("cannot read the file due to: " + err.Error())
67 }
kalnagy92162652019-07-02 15:15:49 +020068 err = json.Unmarshal(byteValue, &rcs)
Balint Uveges871fa392019-04-02 20:31:11 +000069 if err != nil {
70 return nil, errors.New("cannot parse data due to: " + err.Error())
71 }
wahidwa8596ec2019-12-05 06:30:42 +000072 xapp.Logger.Debug("file.fileReadAll returns: %v", rcs)
kalnagy92162652019-07-02 15:15:49 +020073 return rcs, nil
Balint Uveges871fa392019-04-02 20:31:11 +000074}
75
kalnagy92162652019-07-02 15:15:49 +020076func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error {
wahidwa8596ec2019-12-05 06:30:42 +000077 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)
kalnagy92162652019-07-02 15:15:49 +020080 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
zkoczkaaaf8d392019-10-02 17:16:06 +020091func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error {
wahidwa8596ec2019-12-05 06:30:42 +000092 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)
kalnagy92162652019-07-02 15:15:49 +020095
96 ricData, err := NewFile().ReadAll(file)
zkoczkaaaf8d392019-10-02 17:16:06 +020097 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +000098 xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error())
zkoczkaaaf8d392019-10-02 17:16:06 +020099 return errors.New("cannot read full ric data to modify xApps data, due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +0200100 }
zkoczkaaaf8d392019-10-02 17:16:06 +0200101 ricData.XApps = *xApps
kalnagy92162652019-07-02 15:15:49 +0200102
103 byteValue, err := json.Marshal(ricData)
Balint Uveges871fa392019-04-02 20:31:11 +0000104 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}
rangajal749099b2019-12-10 09:37:08 +0000113
114func (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}