X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Frestful%2Frestful.go;h=4e6f7a192241b8ce379b222998c8b8a32c035a86;hb=refs%2Fchanges%2F38%2F11238%2F1;hp=4693f56c53464b3fb37adb0a909a7fc758dbf739;hpb=8333c8b0a4fa46bcd1cf414662c602922cff8f57;p=ric-plt%2Fricdms.git diff --git a/pkg/restful/restful.go b/pkg/restful/restful.go index 4693f56..4e6f7a1 100644 --- a/pkg/restful/restful.go +++ b/pkg/restful/restful.go @@ -1,35 +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 +// Copyright (c) 2022 Samsung // -// http://www.apache.org/licenses/LICENSE-2.0 +// 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 // -// 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. +// http://www.apache.org/licenses/LICENSE-2.0 // -// This source code is part of the near-RT RIC (RAN Intelligent Controller) -// platform project (RICP). -//================================================================================== +// 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 restful import ( + "fmt" + "io" + "io/ioutil" "log" "os" + ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts" + dm "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/deploy" + ph "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models" + 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/deploy" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/experiment" + "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" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" "github.com/go-openapi/loads" + "github.com/go-openapi/runtime" + "github.com/go-openapi/runtime/middleware" ) func NewRestful() *Restful { - r := &Restful{} + r := &Restful{ + rh: resthooks.NewResthook( + ph.NewHealthChecker(), + po.NewOnboarder(), + ch.NewChartmgr(), + dm.NewDeploymentManager(), + ), + } r.setupHandler() return r } @@ -41,6 +66,92 @@ func (r *Restful) setupHandler() { } api := operations.NewRICDMSAPI(swaggerSpec) + + api.HealthGetHealthCheckHandler = health.GetHealthCheckHandlerFunc(func(ghcp health.GetHealthCheckParams) middleware.Responder { + ricdms.Logger.Debug("==> HealthCheck API invoked.") + resp := r.rh.GetDMSHealth() + return resp + }) + + api.HealthGetHealthcheckXAppXAppNameNamespaceNamespaceHandler = health.GetHealthcheckXAppXAppNameNamespaceNamespaceHandlerFunc(func(param health.GetHealthcheckXAppXAppNameNamespaceNamespaceParams) middleware.Responder { + ricdms.Logger.Debug("==> Healthcheck for xApp is invoked") + resp := r.rh.GetxAppHealth(param.XAppName, param.Namespace) + return resp + }) + + api.OnboardPostOnboardxAppsHandler = onboard.PostOnboardxAppsHandlerFunc(func(poap onboard.PostOnboardxAppsParams) middleware.Responder { + ricdms.Logger.Debug("==> onboard API invoked.") + resp := r.rh.OnBoard(poap.Body) + return resp + }) + + api.ChartsGetChartsListHandler = charts.GetChartsListHandlerFunc(func(param charts.GetChartsListParams) middleware.Responder { + ricdms.Logger.Debug("==> GetChartList") + resp := r.rh.GetCharts() + 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 + }) + + api.ChartsGetChartHandler = charts.GetChartHandlerFunc(func(param charts.GetChartParams) middleware.Responder { + ricdms.Logger.Debug("==> Get Charts by name is invoked") + resp := r.rh.GetChartsByName(param.XAppName) + return resp + }) + + api.ChartsGetChartsFetcherHandler = charts.GetChartsFetcherHandlerFunc(func(param charts.GetChartsFetcherParams) middleware.Responder { + ricdms.Logger.Debug("==> Get Charts by name and version is invoked") + resp := r.rh.GetChartByNameAndVersion(param.XAppName, param.Version) + return resp + }) + + api.DeployPostDeployHandler = deploy.PostDeployHandlerFunc(func(param deploy.PostDeployParams) middleware.Responder { + ricdms.Logger.Debug("==> deployment of xApp") + resp := r.rh.DownloadAndInstallChart(param.Body.XAppname, param.Body.Version, *param.Body.Namespace) + return resp + }) + + api.DeployDeleteDeployHandler = deploy.DeleteDeployHandlerFunc(func(param deploy.DeleteDeployParams) middleware.Responder { + ricdms.Logger.Debug("==> undeploy xApp") + resp := r.rh.UninstallChart(*param.Body.XAppname, *param.Body.Version, param.Body.Namespace) + return resp + }) + + api.ExperimentPostCustomOnboardHandler = experiment.PostCustomOnboardHandlerFunc(func(pcop experiment.PostCustomOnboardParams) middleware.Responder { + ricdms.Logger.Debug("==> invoked custom onboarding") + if pcop.Helmpkg == nil { + errString := "formdata not provided with key helmpkg" + return experiment.NewPostCustomOnboardInternalServerError().WithPayload(&models.ErrorMessage{ + ErrorMessage: &errString, + }) + } + defer pcop.Helmpkg.Close() + return r.rh.Onboarder.CustomOnboard(pcop.Helmpkg) + }) + + api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error { + if zp, ok := data.(io.ReadCloser); ok { + defer zp.Close() + b, err := ioutil.ReadAll(zp) + + if err != nil { + ricdms.Logger.Error("error: %v", err) + return err + } + _, err = w.Write(b) + + if err != nil { + ricdms.Logger.Error("error: %v", err) + return err + } + return nil + } + return fmt.Errorf("not support") + }) r.api = api } @@ -49,6 +160,7 @@ func (r *Restful) Run() { defer server.Shutdown() server.Port = 8000 server.Host = "0.0.0.0" + ricdms.Logger.Info("Starting server at : %s:%d", server.Host, server.Port) if err := server.Serve(); err != nil { log.Fatal(err.Error()) }