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 po "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard"
34 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi"
35 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations"
36 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts"
37 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/deploy"
38 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/experiment"
39 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health"
40 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/onboard"
41 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/resthooks"
42 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
43 "github.com/go-openapi/loads"
44 "github.com/go-openapi/runtime"
45 "github.com/go-openapi/runtime/middleware"
48 func NewRestful() *Restful {
50 rh: resthooks.NewResthook(
51 ph.NewHealthChecker(),
54 dm.NewDeploymentManager(),
61 func (r *Restful) setupHandler() {
62 swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
67 api := operations.NewRICDMSAPI(swaggerSpec)
69 api.HealthGetHealthCheckHandler = health.GetHealthCheckHandlerFunc(func(ghcp health.GetHealthCheckParams) middleware.Responder {
70 ricdms.Logger.Debug("==> HealthCheck API invoked.")
71 resp := r.rh.GetDMSHealth()
75 api.HealthGetHealthcheckXAppXAppNameNamespaceNamespaceHandler = health.GetHealthcheckXAppXAppNameNamespaceNamespaceHandlerFunc(func(param health.GetHealthcheckXAppXAppNameNamespaceNamespaceParams) middleware.Responder {
76 ricdms.Logger.Debug("==> Healthcheck for xApp is invoked")
77 resp := r.rh.GetxAppHealth(param.XAppName, param.Namespace)
81 api.OnboardPostOnboardxAppsHandler = onboard.PostOnboardxAppsHandlerFunc(func(poap onboard.PostOnboardxAppsParams) middleware.Responder {
82 ricdms.Logger.Debug("==> onboard API invoked.")
83 resp := r.rh.OnBoard(poap.Body)
87 api.ChartsGetChartsListHandler = charts.GetChartsListHandlerFunc(func(param charts.GetChartsListParams) middleware.Responder {
88 ricdms.Logger.Debug("==> GetChartList")
89 resp := r.rh.GetCharts()
93 api.ChartsDownloadHelmChartHandler = charts.DownloadHelmChartHandlerFunc(func(param charts.DownloadHelmChartParams) middleware.Responder {
94 ricdms.Logger.Debug("==> Download helm chart")
95 resp := r.rh.DownloadChart(param.XAppName, param.Version)
99 api.ChartsGetChartHandler = charts.GetChartHandlerFunc(func(param charts.GetChartParams) middleware.Responder {
100 ricdms.Logger.Debug("==> Get Charts by name is invoked")
101 resp := r.rh.GetChartsByName(param.XAppName)
105 api.ChartsGetChartsFetcherHandler = charts.GetChartsFetcherHandlerFunc(func(param charts.GetChartsFetcherParams) middleware.Responder {
106 ricdms.Logger.Debug("==> Get Charts by name and version is invoked")
107 resp := r.rh.GetChartByNameAndVersion(param.XAppName, param.Version)
111 api.DeployPostDeployHandler = deploy.PostDeployHandlerFunc(func(param deploy.PostDeployParams) middleware.Responder {
112 ricdms.Logger.Debug("==> deployment of xApp")
113 resp := r.rh.DownloadAndInstallChart(param.Body.XAppname, param.Body.Version, *param.Body.Namespace)
117 api.DeployDeleteDeployHandler = deploy.DeleteDeployHandlerFunc(func(param deploy.DeleteDeployParams) middleware.Responder {
118 ricdms.Logger.Debug("==> undeploy xApp")
119 resp := r.rh.UninstallChart(*param.Body.XAppname, *param.Body.Version, param.Body.Namespace)
123 api.ExperimentPostCustomOnboardHandler = experiment.PostCustomOnboardHandlerFunc(func(pcop experiment.PostCustomOnboardParams) middleware.Responder {
124 ricdms.Logger.Debug("==> invoked custom onboarding")
125 defer pcop.Upfile.Close()
126 return r.rh.Onboarder.CustomOnboard(pcop.Upfile)
129 api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
130 if zp, ok := data.(io.ReadCloser); ok {
132 b, err := ioutil.ReadAll(zp)
135 ricdms.Logger.Error("error: %v", err)
141 ricdms.Logger.Error("error: %v", err)
146 return fmt.Errorf("not support")
151 func (r *Restful) Run() {
152 server := restapi.NewServer(r.api)
153 defer server.Shutdown()
155 server.Host = "0.0.0.0"
156 ricdms.Logger.Info("Starting server at : %s:%d", server.Host, server.Port)
157 if err := server.Serve(); err != nil {
158 log.Fatal(err.Error())