Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 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. |
wahidw | 761934a | 2019-11-27 06:07:26 +0000 | [diff] [blame] | 17 | |
| 18 | This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | platform project (RICP). |
| 20 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 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" |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 34 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 35 | "io/ioutil" |
| 36 | "os" |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 37 | "routing-manager/pkg/rtmgr" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 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 | */ |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 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) { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 56 | xapp.Logger.Debug("Invoked sdl.ReadAll(" + file + ")") |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 57 | var rcs *rtmgr.RicComponents |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 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() |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 63 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 64 | byteValue, err := ioutil.ReadAll(jsonFile) |
| 65 | if err != nil { |
| 66 | return nil, errors.New("cannot read the file due to: " + err.Error()) |
| 67 | } |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 68 | err = json.Unmarshal(byteValue, &rcs) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 69 | if err != nil { |
| 70 | return nil, errors.New("cannot parse data due to: " + err.Error()) |
| 71 | } |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 72 | xapp.Logger.Debug("file.fileReadAll returns: %v", rcs) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 73 | return rcs, nil |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 74 | } |
| 75 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 76 | func (f *File) WriteAll(file string, rcs *rtmgr.RicComponents) error { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 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) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 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 | |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 91 | func (f *File) WriteXApps(file string, xApps *[]rtmgr.XApp) error { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 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) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 95 | |
| 96 | ricData, err := NewFile().ReadAll(file) |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 97 | if err != nil { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 98 | xapp.Logger.Error("cannot get data from sdl interface due to: " + err.Error()) |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 99 | return errors.New("cannot read full ric data to modify xApps data, due to: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 100 | } |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 101 | ricData.XApps = *xApps |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 102 | |
| 103 | byteValue, err := json.Marshal(ricData) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 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 | } |
rangajal | 749099b | 2019-12-10 09:37:08 +0000 | [diff] [blame^] | 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 | } |