Implementation for downloading charts

Implemented API to download helm chart for specified chartname
and version.

Signed-off-by: subhash kumar singh <subh.singh@samsung.com>
Change-Id: I343dc18870c81d182a728677e3b21e5fc50d84af
diff --git a/pkg/charts/chart_manager.go b/pkg/charts/chart_manager.go
index 252b2dc..75ec338 100644
--- a/pkg/charts/chart_manager.go
+++ b/pkg/charts/chart_manager.go
@@ -21,6 +21,8 @@
 package charts
 
 import (
+	"fmt"
+	"io"
 	"io/ioutil"
 	"net/http"
 
@@ -32,6 +34,7 @@
 
 type IChartMgr interface {
 	GetCharts() (string, error)
+	DownloadChart(string, string) (io.ReadCloser, error)
 }
 
 func NewChartmgr() IChartMgr {
@@ -58,3 +61,20 @@
 	ricdms.Logger.Debug("response : %+v", string(respByte))
 	return string(respByte), nil
 }
+
+func (c *ChartMgr) DownloadChart(chartName string, version string) (io.ReadCloser, error) {
+	ricdms.Logger.Debug("Download Charts invoked")
+
+	if chartName == "" || version == "" {
+		return nil, fmt.Errorf("chartname or version is empty")
+	}
+
+	ChartURL := fmt.Sprintf(ricdms.Config.DownloadChartURLFormat, chartName, version)
+
+	resp, err := http.Get(ChartURL)
+	if err != nil {
+		return nil, err
+	}
+
+	return resp.Request.Body, nil
+}
diff --git a/pkg/config/conf.go b/pkg/config/conf.go
index 1ac577a..b241aa0 100644
--- a/pkg/config/conf.go
+++ b/pkg/config/conf.go
@@ -28,10 +28,11 @@
 )
 
 type Conf struct {
-	LogLevel     string `yaml:"log-level"`
-	OnboardURL   string `yaml:"onborder-url"`
-	GetChartsURL string `yaml:"getCharts-url"`
-	MockServer   string `yaml:"mock-server"`
+	LogLevel               string `yaml:"log-level"`
+	OnboardURL             string `yaml:"onborder-url"`
+	GetChartsURL           string `yaml:"getCharts-url"`
+	DownloadChartURLFormat string `yaml:"download-charts-url-format"`
+	MockServer             string `yaml:"mock-server"`
 }
 
 func ReadConfiguration(configFile string) (c *Conf, err error) {
diff --git a/pkg/restful/restful.go b/pkg/restful/restful.go
index acfd65a..e01d2fc 100644
--- a/pkg/restful/restful.go
+++ b/pkg/restful/restful.go
@@ -75,6 +75,12 @@
 		return resp
 	})
 
+	api.ChartsDownloadHelmChartHandler = charts.DownloadHelmChartHandlerFunc(func(param charts.DownloadHelmChartParams) middleware.Responder {
+		ricdms.Logger.Debug("==> Download helm chart")
+		resp := r.rh.DownloadChart(param.XAppName, param.Version)
+		return resp
+	})
+
 	r.api = api
 }
 
diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go
index cc144d4..622bcaf 100644
--- a/pkg/resthooks/resthooks.go
+++ b/pkg/resthooks/resthooks.go
@@ -58,3 +58,15 @@
 	}
 	return charts.NewGetChartsListOK().WithPayload(chartList)
 }
+
+func (rh *Resthook) DownloadChart(chartname, version string) (resp middleware.Responder) {
+	ricdms.Logger.Debug("DownloadCharts: invoked")
+	reader, err := rh.ChartMgr.DownloadChart(chartname, version)
+
+	if err != nil {
+		ricdms.Logger.Error("Error : %v", err)
+		return charts.NewDownloadHelmChartInternalServerError()
+	}
+
+	return charts.NewDownloadHelmChartOK().WithPayload(reader)
+}
diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go
index 517fb11..b18b164 100644
--- a/pkg/resthooks/resthooks_test.go
+++ b/pkg/resthooks/resthooks_test.go
@@ -22,11 +22,13 @@
 import (
 	"encoding/json"
 	"fmt"
+	"io"
 	"io/ioutil"
 	"net"
 	"net/http"
 	"net/http/httptest"
 	"os"
+	"strings"
 	"testing"
 
 	ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts"
@@ -124,6 +126,24 @@
 	assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload)
 }
 
+func TestDownloadChart(t *testing.T) {
+	svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		ricdms.Logger.Debug("request received by mock to download chart")
+		reader := strings.NewReader("SAMPLE_RESPONSE")
+		data, _ := io.ReadAll(reader)
+		ricdms.Logger.Debug("writing : %+v", data)
+		w.Write(data)
+	}))
+	svr.Listener.Close()
+	svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer)
+
+	svr.Start()
+	defer svr.Close()
+
+	resp := rh.DownloadChart("CHART_NAME", "VERSION")
+	assert.IsType(t, &charts.DownloadHelmChartOK{}, resp, "response did not match type")
+}
+
 type HealthCheckerMock struct {
 	mock.Mock
 }