blob: 6a3fc8e971536a5b4605ea0476f0d6293bcf8828 [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 {
subhash kumar singh99604032022-10-13 09:24:47 +000037 GetCharts() (map[string]interface{}, 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
subhash kumar singh99604032022-10-13 09:24:47 +000047func (c *ChartMgr) GetCharts() (map[string]interface{}, error) {
subhash kumar singh6e8d9702022-10-06 13:08:26 +000048 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 singh99604032022-10-13 09:24:47 +000053 return make(map[string]interface{}, 0), err
subhash kumar singh6e8d9702022-10-06 13:08:26 +000054 }
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 singh99604032022-10-13 09:24:47 +000061 return make(map[string]interface{}, 0), err
subhash kumar singh6e8d9702022-10-06 13:08:26 +000062 }
63
64 ricdms.Logger.Debug("response : %+v", string(respByte))
subhash kumar singh99604032022-10-13 09:24:47 +000065
66 v := make(map[string]interface{}, 0)
67 json.Unmarshal(respByte, &v)
68 return v, nil
subhash kumar singh6e8d9702022-10-06 13:08:26 +000069}
subhash kumar singhf8dc81b2022-10-10 18:21:51 +000070
71func (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 singhe3ac46a2022-10-12 11:58:52 +000087
88func (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 singhd8d204f2022-10-12 18:07:44 +0000119
120func (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}