Implement health check for xApp
[ric-plt/ricdms.git] / pkg / restful / restful.go
1 //==================================================================================
2 //  Copyright (c) 2022 Samsung
3 //
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
7 //
8 //       http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16 //   This source code is part of the near-RT RIC (RAN Intelligent Controller)
17 //   platform project (RICP).
18 //==================================================================================
19 //
20 package restful
21
22 import (
23         "fmt"
24         "io"
25         "io/ioutil"
26         "log"
27         "os"
28
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"
44 )
45
46 func NewRestful() *Restful {
47         r := &Restful{
48                 rh: resthooks.NewResthook(
49                         ph.NewHealthChecker(),
50                         po.NewOnboarder(),
51                         ch.NewChartmgr(),
52                         dm.NewDeploymentManager(),
53                 ),
54         }
55         r.setupHandler()
56         return r
57 }
58
59 func (r *Restful) setupHandler() {
60         swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
61         if err != nil {
62                 os.Exit(1)
63         }
64
65         api := operations.NewRICDMSAPI(swaggerSpec)
66
67         api.HealthGetHealthCheckHandler = health.GetHealthCheckHandlerFunc(func(ghcp health.GetHealthCheckParams) middleware.Responder {
68                 ricdms.Logger.Debug("==> HealthCheck API invoked.")
69                 resp := r.rh.GetDMSHealth()
70                 return resp
71         })
72
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)
76                 return resp
77         })
78
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)
82                 return resp
83         })
84
85         api.ChartsGetChartsListHandler = charts.GetChartsListHandlerFunc(func(param charts.GetChartsListParams) middleware.Responder {
86                 ricdms.Logger.Debug("==> GetChartList")
87                 resp := r.rh.GetCharts()
88                 return resp
89         })
90
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)
94                 return resp
95         })
96
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)
100                 return resp
101         })
102
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)
106                 return resp
107         })
108
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)
112                 return resp
113         })
114
115         api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
116                 if zp, ok := data.(io.ReadCloser); ok {
117                         defer zp.Close()
118                         b, err := ioutil.ReadAll(zp)
119
120                         if err != nil {
121                                 ricdms.Logger.Error("error: %v", err)
122                                 return err
123                         }
124                         _, err = w.Write(b)
125
126                         if err != nil {
127                                 ricdms.Logger.Error("error: %v", err)
128                                 return err
129                         }
130                         return nil
131                 }
132                 return fmt.Errorf("not support")
133         })
134         r.api = api
135 }
136
137 func (r *Restful) Run() {
138         server := restapi.NewServer(r.api)
139         defer server.Shutdown()
140         server.Port = 8000
141         server.Host = "0.0.0.0"
142         ricdms.Logger.Info("Starting server at : %s:%d", server.Host, server.Port)
143         if err := server.Serve(); err != nil {
144                 log.Fatal(err.Error())
145         }
146 }