From 6e8d970e6ec582bd4473c3fbb6772906658608ca Mon Sep 17 00:00:00 2001 From: subhash kumar singh Date: Thu, 6 Oct 2022 13:08:26 +0000 Subject: [PATCH] Implement Get operation for chart list Implemented get operation for charts list. Signed-off-by: subhash kumar singh Change-Id: If8db0e0d11d48c03270fa72837322fd2e7ae184f --- config/config-test.yaml | 3 ++- config/config.yaml | 3 ++- go.mod | 10 +++---- go.sum | 17 +++++++----- pkg/charts/chart_manager.go | 60 +++++++++++++++++++++++++++++++++++++++++ pkg/config/conf.go | 7 ++--- pkg/restful/restful.go | 9 +++++++ pkg/resthooks/resthooks.go | 15 ++++++++++- pkg/resthooks/resthooks_test.go | 22 +++++++++++++++ pkg/resthooks/types.go | 2 ++ 10 files changed, 131 insertions(+), 17 deletions(-) create mode 100644 pkg/charts/chart_manager.go diff --git a/config/config-test.yaml b/config/config-test.yaml index 5e37c1c..df5b0dc 100644 --- a/config/config-test.yaml +++ b/config/config-test.yaml @@ -14,4 +14,5 @@ # log-level: debug onborder-url: "http://127.0.0.1:9191" -mock-server: "127.0.0.1:9191" \ No newline at end of file +mock-server: "127.0.0.1:9191" +getCharts-url: "http://127.0.0.1:9191" \ No newline at end of file diff --git a/config/config.yaml b/config/config.yaml index e47eaca..a7aecaf 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -13,4 +13,5 @@ # limitations under the License. # log-level: debug -onborder-url: "http://service-ricplt-xapp-onboarder-http.ricplt:8888/api/v1/onboard" \ No newline at end of file +onborder-url: "http://service-ricplt-xapp-onboarder-http.ricplt:8888/api/v1/onboard" +getCharts-url: "http://service-ricplt-xapp-onboarder-http.ricplt:8080/api/charts" \ No newline at end of file diff --git a/go.mod b/go.mod index 346e895..53e88a4 100644 --- a/go.mod +++ b/go.mod @@ -32,8 +32,9 @@ require ( github.com/go-openapi/swag v0.21.1 github.com/go-openapi/validate v0.22.0 github.com/jessevdk/go-flags v1.5.0 - github.com/stretchr/testify v1.7.0 - golang.org/x/net v0.0.0-20220630215102-69896b714898 + github.com/stretchr/testify v1.8.0 + golang.org/x/net v0.0.0-20220722155237-a158d28d115b + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -49,11 +50,10 @@ require ( github.com/mitchellh/mapstructure v1.4.3 // indirect github.com/oklog/ulid v1.3.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/stretchr/objx v0.1.1 // indirect + github.com/stretchr/objx v0.4.0 // indirect go.mongodb.org/mongo-driver v1.8.3 // indirect - golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect + golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect k8s.io/utils v0.0.0-20220812165043-ad590609e2e5 // indirect ) diff --git a/go.sum b/go.sum index fc51efc..d7c47a7 100644 --- a/go.sum +++ b/go.sum @@ -124,14 +124,17 @@ github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTd github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= @@ -151,8 +154,8 @@ golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= -golang.org/x/net v0.0.0-20220630215102-69896b714898 h1:K7wO6V1IrczY9QOQ2WkVpw4JQSwCd52UsxVEirZUfiw= -golang.org/x/net v0.0.0-20220630215102-69896b714898/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -170,8 +173,9 @@ golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -197,8 +201,9 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE= k8s.io/utils v0.0.0-20220812165043-ad590609e2e5 h1:XmRqFcQlCy/lKRZ39j+RVpokYNroHPqV3mcBRfnhT5o= k8s.io/utils v0.0.0-20220812165043-ad590609e2e5/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA= diff --git a/pkg/charts/chart_manager.go b/pkg/charts/chart_manager.go new file mode 100644 index 0000000..252b2dc --- /dev/null +++ b/pkg/charts/chart_manager.go @@ -0,0 +1,60 @@ +//================================================================================== +// Copyright (c) 2022 Samsung +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// This source code is part of the near-RT RIC (RAN Intelligent Controller) +// platform project (RICP). +//================================================================================== +// + +package charts + +import ( + "io/ioutil" + "net/http" + + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" +) + +type ChartMgr struct { +} + +type IChartMgr interface { + GetCharts() (string, error) +} + +func NewChartmgr() IChartMgr { + return &ChartMgr{} +} + +func (c *ChartMgr) GetCharts() (string, error) { + ricdms.Logger.Debug("GetCharts invoked") + + resp, err := http.Get(ricdms.Config.GetChartsURL) + if err != nil { + ricdms.Logger.Debug("Error in getting charts : %+v", err) + return "", err + } + + defer resp.Body.Close() + respByte, err := ioutil.ReadAll(resp.Body) + + if err != nil { + ricdms.Logger.Debug("error in response: %+v", respByte) + return "", err + } + + ricdms.Logger.Debug("response : %+v", string(respByte)) + return string(respByte), nil +} diff --git a/pkg/config/conf.go b/pkg/config/conf.go index 1eff109..1ac577a 100644 --- a/pkg/config/conf.go +++ b/pkg/config/conf.go @@ -28,9 +28,10 @@ import ( ) type Conf struct { - LogLevel string `yaml:"log-level"` - OnboardURL string `yaml:"onborder-url"` - MockServer string `yaml:"mock-server"` + LogLevel string `yaml:"log-level"` + OnboardURL string `yaml:"onborder-url"` + GetChartsURL string `yaml:"getCharts-url"` + 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 2670fb6..acfd65a 100644 --- a/pkg/restful/restful.go +++ b/pkg/restful/restful.go @@ -23,10 +23,12 @@ import ( "log" "os" + ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts" ph "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" po "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/onboard" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/resthooks" @@ -40,6 +42,7 @@ func NewRestful() *Restful { rh: resthooks.NewResthook( ph.NewHealthChecker(), po.NewOnboarder(), + ch.NewChartmgr(), ), } r.setupHandler() @@ -66,6 +69,12 @@ func (r *Restful) setupHandler() { return resp }) + api.ChartsGetChartsListHandler = charts.GetChartsListHandlerFunc(func(param charts.GetChartsListParams) middleware.Responder { + ricdms.Logger.Debug("==> GetChartList") + resp := r.rh.GetCharts() + return resp + }) + r.api = api } diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go index 072fa1d..cc144d4 100644 --- a/pkg/resthooks/resthooks.go +++ b/pkg/resthooks/resthooks.go @@ -21,18 +21,21 @@ package resthooks import ( + ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts" ph "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" "github.com/go-openapi/runtime/middleware" ) -func NewResthook(h ph.IHealthChecker, o onboard.IOnboarder) *Resthook { +func NewResthook(h ph.IHealthChecker, o onboard.IOnboarder, chMgr ch.IChartMgr) *Resthook { return &Resthook{ HealthChecker: h, Onboarder: o, + ChartMgr: chMgr, } } @@ -45,3 +48,13 @@ func (rh *Resthook) OnBoard(params *models.Descriptor) (resp middleware.Responde ricdms.Logger.Debug("onboarder: invoked") return rh.Onboarder.Onboard(params) } + +func (rh *Resthook) GetCharts() (resp middleware.Responder) { + ricdms.Logger.Debug("getcharts: invoked") + chartList, err := rh.ChartMgr.GetCharts() + + if err != nil { + return charts.NewGetChartsListInternalServerError() + } + return charts.NewGetChartsListOK().WithPayload(chartList) +} diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go index 74ba05e..517fb11 100644 --- a/pkg/resthooks/resthooks_test.go +++ b/pkg/resthooks/resthooks_test.go @@ -29,9 +29,11 @@ import ( "os" "testing" + ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts" h "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" "github.com/stretchr/testify/assert" @@ -50,6 +52,7 @@ func TestMain(m *testing.M) { rh = &Resthook{ HealthChecker: HealthCheckerMock{}, Onboarder: onboard.NewOnboarder(), + ChartMgr: ch.NewChartmgr(), } code := m.Run() os.Exit(code) @@ -102,6 +105,25 @@ func TestOnboard(t *testing.T) { assert.NotEqual(t, nil, resp) } +func TestGetCharts(t *testing.T) { + + svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ricdms.Logger.Debug("Mock server running") + fmt.Fprintf(w, "SAMPLE_RESPONSE") + })) + svr.Listener.Close() + svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer) + + svr.Start() + defer svr.Close() + + resp := rh.GetCharts() + assert.NotEqual(t, nil, resp) + + successResp := resp.(*charts.GetChartsListOK) + assert.Equal(t, "SAMPLE_RESPONSE", successResp.Payload) +} + type HealthCheckerMock struct { mock.Mock } diff --git a/pkg/resthooks/types.go b/pkg/resthooks/types.go index d6f3ad4..edf3ffb 100644 --- a/pkg/resthooks/types.go +++ b/pkg/resthooks/types.go @@ -20,6 +20,7 @@ package resthooks import ( + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard" ) @@ -27,4 +28,5 @@ import ( type Resthook struct { HealthChecker health.IHealthChecker Onboarder onboard.IOnboarder + ChartMgr charts.IChartMgr } -- 2.16.6