Implement custom onboard functionality
[ric-plt/ricdms.git] / pkg / restful / restful.go
1 // ==================================================================================
2 //
3 //      Copyright (c) 2022 Samsung
4 //
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
8 //
9 //           http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 //       This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //       platform project (RICP).
19 //
20 // ==================================================================================
21 package restful
22
23 import (
24         "fmt"
25         "io"
26         "io/ioutil"
27         "log"
28         "os"
29
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"
46 )
47
48 func NewRestful() *Restful {
49         r := &Restful{
50                 rh: resthooks.NewResthook(
51                         ph.NewHealthChecker(),
52                         po.NewOnboarder(),
53                         ch.NewChartmgr(),
54                         dm.NewDeploymentManager(),
55                 ),
56         }
57         r.setupHandler()
58         return r
59 }
60
61 func (r *Restful) setupHandler() {
62         swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
63         if err != nil {
64                 os.Exit(1)
65         }
66
67         api := operations.NewRICDMSAPI(swaggerSpec)
68
69         api.HealthGetHealthCheckHandler = health.GetHealthCheckHandlerFunc(func(ghcp health.GetHealthCheckParams) middleware.Responder {
70                 ricdms.Logger.Debug("==> HealthCheck API invoked.")
71                 resp := r.rh.GetDMSHealth()
72                 return resp
73         })
74
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)
78                 return resp
79         })
80
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)
84                 return resp
85         })
86
87         api.ChartsGetChartsListHandler = charts.GetChartsListHandlerFunc(func(param charts.GetChartsListParams) middleware.Responder {
88                 ricdms.Logger.Debug("==> GetChartList")
89                 resp := r.rh.GetCharts()
90                 return resp
91         })
92
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)
96                 return resp
97         })
98
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)
102                 return resp
103         })
104
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)
108                 return resp
109         })
110
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)
114                 return resp
115         })
116
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)
120                 return resp
121         })
122
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)
127         })
128
129         api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
130                 if zp, ok := data.(io.ReadCloser); ok {
131                         defer zp.Close()
132                         b, err := ioutil.ReadAll(zp)
133
134                         if err != nil {
135                                 ricdms.Logger.Error("error: %v", err)
136                                 return err
137                         }
138                         _, err = w.Write(b)
139
140                         if err != nil {
141                                 ricdms.Logger.Error("error: %v", err)
142                                 return err
143                         }
144                         return nil
145                 }
146                 return fmt.Errorf("not support")
147         })
148         r.api = api
149 }
150
151 func (r *Restful) Run() {
152         server := restapi.NewServer(r.api)
153         defer server.Shutdown()
154         server.Port = 8000
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())
159         }
160 }