subhash kumar singh | 6e8d970 | 2022-10-06 13:08:26 +0000 | [diff] [blame] | 1 | //================================================================================== |
| 2 | // Copyright (c) 2022 Samsung |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | // This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 17 | // platform project (RICP). |
| 18 | //================================================================================== |
| 19 | // |
| 20 | |
| 21 | package charts |
| 22 | |
| 23 | import ( |
subhash kumar singh | e3ac46a | 2022-10-12 11:58:52 +0000 | [diff] [blame] | 24 | "encoding/json" |
subhash kumar singh | f8dc81b | 2022-10-10 18:21:51 +0000 | [diff] [blame] | 25 | "fmt" |
| 26 | "io" |
subhash kumar singh | 6e8d970 | 2022-10-06 13:08:26 +0000 | [diff] [blame] | 27 | "io/ioutil" |
| 28 | "net/http" |
| 29 | |
| 30 | "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" |
| 31 | ) |
| 32 | |
| 33 | type ChartMgr struct { |
| 34 | } |
| 35 | |
| 36 | type IChartMgr interface { |
subhash kumar singh | 9960403 | 2022-10-13 09:24:47 +0000 | [diff] [blame^] | 37 | GetCharts() (map[string]interface{}, error) |
subhash kumar singh | f8dc81b | 2022-10-10 18:21:51 +0000 | [diff] [blame] | 38 | DownloadChart(string, string) (io.ReadCloser, error) |
subhash kumar singh | e3ac46a | 2022-10-12 11:58:52 +0000 | [diff] [blame] | 39 | GetChartsByName(name string) ([]map[string]interface{}, error) |
subhash kumar singh | d8d204f | 2022-10-12 18:07:44 +0000 | [diff] [blame] | 40 | GetChartsByNameAndVersion(name, version string) (map[string]interface{}, error) |
subhash kumar singh | 6e8d970 | 2022-10-06 13:08:26 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | func NewChartmgr() IChartMgr { |
| 44 | return &ChartMgr{} |
| 45 | } |
| 46 | |
subhash kumar singh | 9960403 | 2022-10-13 09:24:47 +0000 | [diff] [blame^] | 47 | func (c *ChartMgr) GetCharts() (map[string]interface{}, error) { |
subhash kumar singh | 6e8d970 | 2022-10-06 13:08:26 +0000 | [diff] [blame] | 48 | ricdms.Logger.Debug("GetCharts invoked") |
| 49 | |
| 50 | resp, err := http.Get(ricdms.Config.GetChartsURL) |
| 51 | if err != nil { |
| 52 | ricdms.Logger.Debug("Error in getting charts : %+v", err) |
subhash kumar singh | 9960403 | 2022-10-13 09:24:47 +0000 | [diff] [blame^] | 53 | return make(map[string]interface{}, 0), err |
subhash kumar singh | 6e8d970 | 2022-10-06 13:08:26 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | defer resp.Body.Close() |
| 57 | respByte, err := ioutil.ReadAll(resp.Body) |
| 58 | |
| 59 | if err != nil { |
| 60 | ricdms.Logger.Debug("error in response: %+v", respByte) |
subhash kumar singh | 9960403 | 2022-10-13 09:24:47 +0000 | [diff] [blame^] | 61 | return make(map[string]interface{}, 0), err |
subhash kumar singh | 6e8d970 | 2022-10-06 13:08:26 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | ricdms.Logger.Debug("response : %+v", string(respByte)) |
subhash kumar singh | 9960403 | 2022-10-13 09:24:47 +0000 | [diff] [blame^] | 65 | |
| 66 | v := make(map[string]interface{}, 0) |
| 67 | json.Unmarshal(respByte, &v) |
| 68 | return v, nil |
subhash kumar singh | 6e8d970 | 2022-10-06 13:08:26 +0000 | [diff] [blame] | 69 | } |
subhash kumar singh | f8dc81b | 2022-10-10 18:21:51 +0000 | [diff] [blame] | 70 | |
| 71 | func (c *ChartMgr) DownloadChart(chartName string, version string) (io.ReadCloser, error) { |
| 72 | ricdms.Logger.Debug("Download Charts invoked") |
| 73 | |
| 74 | if chartName == "" || version == "" { |
| 75 | return nil, fmt.Errorf("chartname or version is empty") |
| 76 | } |
| 77 | |
| 78 | ChartURL := fmt.Sprintf(ricdms.Config.DownloadChartURLFormat, chartName, version) |
| 79 | |
| 80 | resp, err := http.Get(ChartURL) |
| 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | |
| 85 | return resp.Request.Body, nil |
| 86 | } |
subhash kumar singh | e3ac46a | 2022-10-12 11:58:52 +0000 | [diff] [blame] | 87 | |
| 88 | func (c *ChartMgr) GetChartsByName(name string) ([]map[string]interface{}, error) { |
| 89 | ricdms.Logger.Debug("Get Chart by xApp name is invoked") |
| 90 | |
| 91 | if name == "" { |
| 92 | return make([]map[string]interface{}, 0), fmt.Errorf("xAppname is empty") |
| 93 | } |
| 94 | |
| 95 | URL := fmt.Sprintf(ricdms.Config.GetChartsByxAppNameURL, name) |
| 96 | |
| 97 | response, err := http.Get(URL) |
| 98 | if err != nil { |
| 99 | ricdms.Logger.Error("error: %v", err) |
| 100 | return make([]map[string]interface{}, 0), err |
| 101 | } |
| 102 | |
| 103 | defer response.Body.Close() |
| 104 | data, err := ioutil.ReadAll(response.Body) |
| 105 | |
| 106 | if err != nil { |
| 107 | ricdms.Logger.Debug("Reading response failed with err : %v", err) |
| 108 | return make([]map[string]interface{}, 0), err |
| 109 | } |
| 110 | |
| 111 | v := make([]map[string]interface{}, 0) |
| 112 | err = json.Unmarshal(data, &v) |
| 113 | if err != nil { |
| 114 | ricdms.Logger.Debug("Error while parsing res: %v", err) |
| 115 | return make([]map[string]interface{}, 0), err |
| 116 | } |
| 117 | return v, nil |
| 118 | } |
subhash kumar singh | d8d204f | 2022-10-12 18:07:44 +0000 | [diff] [blame] | 119 | |
| 120 | func (c *ChartMgr) GetChartsByNameAndVersion(name, version string) (map[string]interface{}, error) { |
| 121 | ricdms.Logger.Debug("Get Charts by name and version is invoked") |
| 122 | |
| 123 | if name == "" || version == "" { |
| 124 | return make(map[string]interface{}, 0), fmt.Errorf("name or version is not provided") |
| 125 | } |
| 126 | |
| 127 | URL := fmt.Sprintf(ricdms.Config.GetChartsByNameAndVersionURL, name, version) |
| 128 | |
| 129 | response, err := http.Get(URL) |
| 130 | if err != nil { |
| 131 | ricdms.Logger.Debug("error in retrieving chart: %v", err) |
| 132 | return make(map[string]interface{}, 0), err |
| 133 | } |
| 134 | |
| 135 | defer response.Body.Close() |
| 136 | data, err := ioutil.ReadAll(response.Body) |
| 137 | |
| 138 | if err != nil { |
| 139 | ricdms.Logger.Debug("error in reading response: %v", err) |
| 140 | return make(map[string]interface{}, 0), err |
| 141 | } |
| 142 | |
| 143 | v := make(map[string]interface{}, 0) |
| 144 | err = json.Unmarshal(data, &v) |
| 145 | if err != nil { |
| 146 | ricdms.Logger.Debug("error in parsing response: %v", err) |
| 147 | return make(map[string]interface{}, 0), err |
| 148 | } |
| 149 | |
| 150 | return v, nil |
| 151 | } |