46a4387b11fd7fcd17a978894e1127f12427bfd9
[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.OnboardPostOnboardxAppsHandler = onboard.PostOnboardxAppsHandlerFunc(func(poap onboard.PostOnboardxAppsParams) middleware.Responder {
74                 ricdms.Logger.Debug("==> onboard API invoked.")
75                 resp := r.rh.OnBoard(poap.Body)
76                 return resp
77         })
78
79         api.ChartsGetChartsListHandler = charts.GetChartsListHandlerFunc(func(param charts.GetChartsListParams) middleware.Responder {
80                 ricdms.Logger.Debug("==> GetChartList")
81                 resp := r.rh.GetCharts()
82                 return resp
83         })
84
85         api.ChartsDownloadHelmChartHandler = charts.DownloadHelmChartHandlerFunc(func(param charts.DownloadHelmChartParams) middleware.Responder {
86                 ricdms.Logger.Debug("==> Download helm chart")
87                 resp := r.rh.DownloadChart(param.XAppName, param.Version)
88                 return resp
89         })
90
91         api.ChartsGetChartHandler = charts.GetChartHandlerFunc(func(param charts.GetChartParams) middleware.Responder {
92                 ricdms.Logger.Debug("==> Get Charts by name is invoked")
93                 resp := r.rh.GetChartsByName(param.XAppName)
94                 return resp
95         })
96
97         api.ChartsGetChartsFetcherHandler = charts.GetChartsFetcherHandlerFunc(func(param charts.GetChartsFetcherParams) middleware.Responder {
98                 ricdms.Logger.Debug("==> Get Charts by name and version is invoked")
99                 resp := r.rh.GetChartByNameAndVersion(param.XAppName, param.Version)
100                 return resp
101         })
102
103         api.DeployPostDeployHandler = deploy.PostDeployHandlerFunc(func(param deploy.PostDeployParams) middleware.Responder {
104                 ricdms.Logger.Debug("==> deployment of xApp")
105                 resp := r.rh.DownloadAndInstallChart(param.Body.XAppname, param.Body.Version, *param.Body.Namespace)
106                 return resp
107         })
108
109         api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
110                 if zp, ok := data.(io.ReadCloser); ok {
111                         defer zp.Close()
112                         b, err := ioutil.ReadAll(zp)
113
114                         if err != nil {
115                                 ricdms.Logger.Error("error: %v", err)
116                                 return err
117                         }
118                         _, err = w.Write(b)
119
120                         if err != nil {
121                                 ricdms.Logger.Error("error: %v", err)
122                                 return err
123                         }
124                         return nil
125                 }
126                 return fmt.Errorf("not support")
127         })
128         r.api = api
129 }
130
131 func (r *Restful) Run() {
132         server := restapi.NewServer(r.api)
133         defer server.Shutdown()
134         server.Port = 8000
135         server.Host = "0.0.0.0"
136         ricdms.Logger.Info("Starting server at : %s:%d", server.Host, server.Port)
137         if err := server.Serve(); err != nil {
138                 log.Fatal(err.Error())
139         }
140 }