blob: c0308dd740f5198984892720dcc43241a4d722dd [file] [log] [blame]
subhash kumar singh6e8d9702022-10-06 13:08:26 +00001//==================================================================================
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
21package charts
22
23import (
subhash kumar singhe3ac46a2022-10-12 11:58:52 +000024 "encoding/json"
subhash kumar singhf8dc81b2022-10-10 18:21:51 +000025 "fmt"
26 "io"
subhash kumar singh6e8d9702022-10-06 13:08:26 +000027 "io/ioutil"
28 "net/http"
29
30 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
31)
32
33type ChartMgr struct {
34}
35
36type IChartMgr interface {
37 GetCharts() (string, error)
subhash kumar singhf8dc81b2022-10-10 18:21:51 +000038 DownloadChart(string, string) (io.ReadCloser, error)
subhash kumar singhe3ac46a2022-10-12 11:58:52 +000039 GetChartsByName(name string) ([]map[string]interface{}, error)
subhash kumar singhd8d204f2022-10-12 18:07:44 +000040 GetChartsByNameAndVersion(name, version string) (map[string]interface{}, error)
subhash kumar singh6e8d9702022-10-06 13:08:26 +000041}
42
43func NewChartmgr() IChartMgr {
44 return &ChartMgr{}
45}
46
47func (c *ChartMgr) GetCharts() (string, error) {
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)
53 return "", err
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)
61 return "", err
62 }
63
64 ricdms.Logger.Debug("response : %+v", string(respByte))
65 return string(respByte), nil
66}
subhash kumar singhf8dc81b2022-10-10 18:21:51 +000067
68func (c *ChartMgr) DownloadChart(chartName string, version string) (io.ReadCloser, error) {
69 ricdms.Logger.Debug("Download Charts invoked")
70
71 if chartName == "" || version == "" {
72 return nil, fmt.Errorf("chartname or version is empty")
73 }
74
75 ChartURL := fmt.Sprintf(ricdms.Config.DownloadChartURLFormat, chartName, version)
76
77 resp, err := http.Get(ChartURL)
78 if err != nil {
79 return nil, err
80 }
81
82 return resp.Request.Body, nil
83}
subhash kumar singhe3ac46a2022-10-12 11:58:52 +000084
85func (c *ChartMgr) GetChartsByName(name string) ([]map[string]interface{}, error) {
86 ricdms.Logger.Debug("Get Chart by xApp name is invoked")
87
88 if name == "" {
89 return make([]map[string]interface{}, 0), fmt.Errorf("xAppname is empty")
90 }
91
92 URL := fmt.Sprintf(ricdms.Config.GetChartsByxAppNameURL, name)
93
94 response, err := http.Get(URL)
95 if err != nil {
96 ricdms.Logger.Error("error: %v", err)
97 return make([]map[string]interface{}, 0), err
98 }
99
100 defer response.Body.Close()
101 data, err := ioutil.ReadAll(response.Body)
102
103 if err != nil {
104 ricdms.Logger.Debug("Reading response failed with err : %v", err)
105 return make([]map[string]interface{}, 0), err
106 }
107
108 v := make([]map[string]interface{}, 0)
109 err = json.Unmarshal(data, &v)
110 if err != nil {
111 ricdms.Logger.Debug("Error while parsing res: %v", err)
112 return make([]map[string]interface{}, 0), err
113 }
114 return v, nil
115}
subhash kumar singhd8d204f2022-10-12 18:07:44 +0000116
117func (c *ChartMgr) GetChartsByNameAndVersion(name, version string) (map[string]interface{}, error) {
118 ricdms.Logger.Debug("Get Charts by name and version is invoked")
119
120 if name == "" || version == "" {
121 return make(map[string]interface{}, 0), fmt.Errorf("name or version is not provided")
122 }
123
124 URL := fmt.Sprintf(ricdms.Config.GetChartsByNameAndVersionURL, name, version)
125
126 response, err := http.Get(URL)
127 if err != nil {
128 ricdms.Logger.Debug("error in retrieving chart: %v", err)
129 return make(map[string]interface{}, 0), err
130 }
131
132 defer response.Body.Close()
133 data, err := ioutil.ReadAll(response.Body)
134
135 if err != nil {
136 ricdms.Logger.Debug("error in reading response: %v", err)
137 return make(map[string]interface{}, 0), err
138 }
139
140 v := make(map[string]interface{}, 0)
141 err = json.Unmarshal(data, &v)
142 if err != nil {
143 ricdms.Logger.Debug("error in parsing response: %v", err)
144 return make(map[string]interface{}, 0), err
145 }
146
147 return v, nil
148}