Add error check if arg is nil
[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         "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"
47 )
48
49 func NewRestful() *Restful {
50         r := &Restful{
51                 rh: resthooks.NewResthook(
52                         ph.NewHealthChecker(),
53                         po.NewOnboarder(),
54                         ch.NewChartmgr(),
55                         dm.NewDeploymentManager(),
56                 ),
57         }
58         r.setupHandler()
59         return r
60 }
61
62 func (r *Restful) setupHandler() {
63         swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
64         if err != nil {
65                 os.Exit(1)
66         }
67
68         api := operations.NewRICDMSAPI(swaggerSpec)
69
70         api.HealthGetHealthCheckHandler = health.GetHealthCheckHandlerFunc(func(ghcp health.GetHealthCheckParams) middleware.Responder {
71                 ricdms.Logger.Debug("==> HealthCheck API invoked.")
72                 resp := r.rh.GetDMSHealth()
73                 return resp
74         })
75
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)
79                 return resp
80         })
81
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)
85                 return resp
86         })
87
88         api.ChartsGetChartsListHandler = charts.GetChartsListHandlerFunc(func(param charts.GetChartsListParams) middleware.Responder {
89                 ricdms.Logger.Debug("==> GetChartList")
90                 resp := r.rh.GetCharts()
91                 return resp
92         })
93
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)
97                 return resp
98         })
99
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)
103                 return resp
104         })
105
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)
109                 return resp
110         })
111
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)
115                 return resp
116         })
117
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)
121                 return resp
122         })
123
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,
130                         })
131                 }
132                 defer pcop.Helmpkg.Close()
133                 return r.rh.Onboarder.CustomOnboard(pcop.Helmpkg)
134         })
135
136         api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
137                 if zp, ok := data.(io.ReadCloser); ok {
138                         defer zp.Close()
139                         b, err := ioutil.ReadAll(zp)
140
141                         if err != nil {
142                                 ricdms.Logger.Error("error: %v", err)
143                                 return err
144                         }
145                         _, err = w.Write(b)
146
147                         if err != nil {
148                                 ricdms.Logger.Error("error: %v", err)
149                                 return err
150                         }
151                         return nil
152                 }
153                 return fmt.Errorf("not support")
154         })
155         r.api = api
156 }
157
158 func (r *Restful) Run() {
159         server := restapi.NewServer(r.api)
160         defer server.Shutdown()
161         server.Port = 8000
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())
166         }
167 }