1 // ==================================================================================
3 // Copyright (c) 2022 Samsung
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
20 // ==================================================================================
30 ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts"
31 dm "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/deploy"
32 ph "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health"
33 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models"
34 po "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard"
35 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi"
36 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations"
37 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts"
38 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/deploy"
39 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/experiment"
40 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health"
41 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/onboard"
42 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/resthooks"
43 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
44 "github.com/go-openapi/loads"
45 "github.com/go-openapi/runtime"
46 "github.com/go-openapi/runtime/middleware"
49 func NewRestful() *Restful {
51 rh: resthooks.NewResthook(
52 ph.NewHealthChecker(),
55 dm.NewDeploymentManager(),
62 func (r *Restful) setupHandler() {
63 swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
68 api := operations.NewRICDMSAPI(swaggerSpec)
70 api.HealthGetHealthCheckHandler = health.GetHealthCheckHandlerFunc(func(ghcp health.GetHealthCheckParams) middleware.Responder {
71 ricdms.Logger.Debug("==> HealthCheck API invoked.")
72 resp := r.rh.GetDMSHealth()
76 api.HealthGetHealthcheckXAppXAppNameNamespaceNamespaceHandler = health.GetHealthcheckXAppXAppNameNamespaceNamespaceHandlerFunc(func(param health.GetHealthcheckXAppXAppNameNamespaceNamespaceParams) middleware.Responder {
77 ricdms.Logger.Debug("==> Healthcheck for xApp is invoked")
78 resp := r.rh.GetxAppHealth(param.XAppName, param.Namespace)
82 api.OnboardPostOnboardxAppsHandler = onboard.PostOnboardxAppsHandlerFunc(func(poap onboard.PostOnboardxAppsParams) middleware.Responder {
83 ricdms.Logger.Debug("==> onboard API invoked.")
84 resp := r.rh.OnBoard(poap.Body)
88 api.ChartsGetChartsListHandler = charts.GetChartsListHandlerFunc(func(param charts.GetChartsListParams) middleware.Responder {
89 ricdms.Logger.Debug("==> GetChartList")
90 resp := r.rh.GetCharts()
94 api.ChartsDownloadHelmChartHandler = charts.DownloadHelmChartHandlerFunc(func(param charts.DownloadHelmChartParams) middleware.Responder {
95 ricdms.Logger.Debug("==> Download helm chart")
96 resp := r.rh.DownloadChart(param.XAppName, param.Version)
100 api.ChartsGetChartHandler = charts.GetChartHandlerFunc(func(param charts.GetChartParams) middleware.Responder {
101 ricdms.Logger.Debug("==> Get Charts by name is invoked")
102 resp := r.rh.GetChartsByName(param.XAppName)
106 api.ChartsGetChartsFetcherHandler = charts.GetChartsFetcherHandlerFunc(func(param charts.GetChartsFetcherParams) middleware.Responder {
107 ricdms.Logger.Debug("==> Get Charts by name and version is invoked")
108 resp := r.rh.GetChartByNameAndVersion(param.XAppName, param.Version)
112 api.DeployPostDeployHandler = deploy.PostDeployHandlerFunc(func(param deploy.PostDeployParams) middleware.Responder {
113 ricdms.Logger.Debug("==> deployment of xApp")
114 resp := r.rh.DownloadAndInstallChart(param.Body.XAppname, param.Body.Version, *param.Body.Namespace)
118 api.DeployDeleteDeployHandler = deploy.DeleteDeployHandlerFunc(func(param deploy.DeleteDeployParams) middleware.Responder {
119 ricdms.Logger.Debug("==> undeploy xApp")
120 resp := r.rh.UninstallChart(*param.Body.XAppname, *param.Body.Version, param.Body.Namespace)
124 api.ExperimentPostCustomOnboardHandler = experiment.PostCustomOnboardHandlerFunc(func(pcop experiment.PostCustomOnboardParams) middleware.Responder {
125 ricdms.Logger.Debug("==> invoked custom onboarding")
126 if pcop.Helmpkg == nil {
127 errString := "formdata not provided with key helmpkg"
128 return experiment.NewPostCustomOnboardInternalServerError().WithPayload(&models.ErrorMessage{
129 ErrorMessage: &errString,
132 defer pcop.Helmpkg.Close()
133 return r.rh.Onboarder.CustomOnboard(pcop.Helmpkg)
136 api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
137 if zp, ok := data.(io.ReadCloser); ok {
139 b, err := ioutil.ReadAll(zp)
142 ricdms.Logger.Error("error: %v", err)
148 ricdms.Logger.Error("error: %v", err)
153 return fmt.Errorf("not support")
158 func (r *Restful) Run() {
159 server := restapi.NewServer(r.api)
160 defer server.Shutdown()
162 server.Host = "0.0.0.0"
163 ricdms.Logger.Info("Starting server at : %s:%d", server.Host, server.Port)
164 if err := server.Serve(); err != nil {
165 log.Fatal(err.Error())