1 //==================================================================================
2 // Copyright (c) 2022 Samsung
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 // platform project (RICP).
18 //==================================================================================
29 ch "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/charts"
30 dm "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/deploy"
31 ph "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health"
32 po "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard"
33 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi"
34 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations"
35 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts"
36 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/deploy"
37 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health"
38 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/onboard"
39 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/resthooks"
40 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
41 "github.com/go-openapi/loads"
42 "github.com/go-openapi/runtime"
43 "github.com/go-openapi/runtime/middleware"
46 func NewRestful() *Restful {
48 rh: resthooks.NewResthook(
49 ph.NewHealthChecker(),
52 dm.NewDeploymentManager(),
59 func (r *Restful) setupHandler() {
60 swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
65 api := operations.NewRICDMSAPI(swaggerSpec)
67 api.HealthGetHealthCheckHandler = health.GetHealthCheckHandlerFunc(func(ghcp health.GetHealthCheckParams) middleware.Responder {
68 ricdms.Logger.Debug("==> HealthCheck API invoked.")
69 resp := r.rh.GetDMSHealth()
73 api.HealthGetHealthcheckXAppXAppNameNamespaceNamespaceHandler = health.GetHealthcheckXAppXAppNameNamespaceNamespaceHandlerFunc(func(param health.GetHealthcheckXAppXAppNameNamespaceNamespaceParams) middleware.Responder {
74 ricdms.Logger.Debug("==> Healthcheck for xApp is invoked")
75 resp := r.rh.GetxAppHealth(param.XAppName, param.Namespace)
79 api.OnboardPostOnboardxAppsHandler = onboard.PostOnboardxAppsHandlerFunc(func(poap onboard.PostOnboardxAppsParams) middleware.Responder {
80 ricdms.Logger.Debug("==> onboard API invoked.")
81 resp := r.rh.OnBoard(poap.Body)
85 api.ChartsGetChartsListHandler = charts.GetChartsListHandlerFunc(func(param charts.GetChartsListParams) middleware.Responder {
86 ricdms.Logger.Debug("==> GetChartList")
87 resp := r.rh.GetCharts()
91 api.ChartsDownloadHelmChartHandler = charts.DownloadHelmChartHandlerFunc(func(param charts.DownloadHelmChartParams) middleware.Responder {
92 ricdms.Logger.Debug("==> Download helm chart")
93 resp := r.rh.DownloadChart(param.XAppName, param.Version)
97 api.ChartsGetChartHandler = charts.GetChartHandlerFunc(func(param charts.GetChartParams) middleware.Responder {
98 ricdms.Logger.Debug("==> Get Charts by name is invoked")
99 resp := r.rh.GetChartsByName(param.XAppName)
103 api.ChartsGetChartsFetcherHandler = charts.GetChartsFetcherHandlerFunc(func(param charts.GetChartsFetcherParams) middleware.Responder {
104 ricdms.Logger.Debug("==> Get Charts by name and version is invoked")
105 resp := r.rh.GetChartByNameAndVersion(param.XAppName, param.Version)
109 api.DeployPostDeployHandler = deploy.PostDeployHandlerFunc(func(param deploy.PostDeployParams) middleware.Responder {
110 ricdms.Logger.Debug("==> deployment of xApp")
111 resp := r.rh.DownloadAndInstallChart(param.Body.XAppname, param.Body.Version, *param.Body.Namespace)
115 api.DeployDeleteDeployHandler = deploy.DeleteDeployHandlerFunc(func(param deploy.DeleteDeployParams) middleware.Responder {
116 ricdms.Logger.Debug("==> undeploy xApp")
117 resp := r.rh.UninstallChart(*param.Body.XAppname, *param.Body.Version, param.Body.Namespace)
120 api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
121 if zp, ok := data.(io.ReadCloser); ok {
123 b, err := ioutil.ReadAll(zp)
126 ricdms.Logger.Error("error: %v", err)
132 ricdms.Logger.Error("error: %v", err)
137 return fmt.Errorf("not support")
142 func (r *Restful) Run() {
143 server := restapi.NewServer(r.api)
144 defer server.Shutdown()
146 server.Host = "0.0.0.0"
147 ricdms.Logger.Info("Starting server at : %s:%d", server.Host, server.Port)
148 if err := server.Serve(); err != nil {
149 log.Fatal(err.Error())