Implement producer to handle appliation/zip
[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         ph "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health"
31         po "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard"
32         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi"
33         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations"
34         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/charts"
35         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health"
36         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/onboard"
37         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/resthooks"
38         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
39         "github.com/go-openapi/loads"
40         "github.com/go-openapi/runtime"
41         "github.com/go-openapi/runtime/middleware"
42 )
43
44 func NewRestful() *Restful {
45         r := &Restful{
46                 rh: resthooks.NewResthook(
47                         ph.NewHealthChecker(),
48                         po.NewOnboarder(),
49                         ch.NewChartmgr(),
50                 ),
51         }
52         r.setupHandler()
53         return r
54 }
55
56 func (r *Restful) setupHandler() {
57         swaggerSpec, err := loads.Embedded(restapi.SwaggerJSON, restapi.FlatSwaggerJSON)
58         if err != nil {
59                 os.Exit(1)
60         }
61
62         api := operations.NewRICDMSAPI(swaggerSpec)
63
64         api.HealthGetHealthCheckHandler = health.GetHealthCheckHandlerFunc(func(ghcp health.GetHealthCheckParams) middleware.Responder {
65                 ricdms.Logger.Debug("==> HealthCheck API invoked.")
66                 resp := r.rh.GetDMSHealth()
67                 return resp
68         })
69
70         api.OnboardPostOnboardxAppsHandler = onboard.PostOnboardxAppsHandlerFunc(func(poap onboard.PostOnboardxAppsParams) middleware.Responder {
71                 ricdms.Logger.Debug("==> onboard API invoked.")
72                 resp := r.rh.OnBoard(poap.Body)
73                 return resp
74         })
75
76         api.ChartsGetChartsListHandler = charts.GetChartsListHandlerFunc(func(param charts.GetChartsListParams) middleware.Responder {
77                 ricdms.Logger.Debug("==> GetChartList")
78                 resp := r.rh.GetCharts()
79                 return resp
80         })
81
82         api.ChartsDownloadHelmChartHandler = charts.DownloadHelmChartHandlerFunc(func(param charts.DownloadHelmChartParams) middleware.Responder {
83                 ricdms.Logger.Debug("==> Download helm chart")
84                 resp := r.rh.DownloadChart(param.XAppName, param.Version)
85                 return resp
86         })
87
88         api.ChartsGetChartHandler = charts.GetChartHandlerFunc(func(param charts.GetChartParams) middleware.Responder {
89                 ricdms.Logger.Debug("==> Get Charts by name is invoked")
90                 resp := r.rh.GetChartsByName(param.XAppName)
91                 return resp
92         })
93
94         api.ChartsGetChartsFetcherHandler = charts.GetChartsFetcherHandlerFunc(func(param charts.GetChartsFetcherParams) middleware.Responder {
95                 ricdms.Logger.Debug("==> Get Charts by name and version is invoked")
96                 resp := r.rh.GetChartByNameAndVersion(param.XAppName, param.Version)
97                 return resp
98         })
99
100         api.ApplicationZipProducer = runtime.ProducerFunc(func(w io.Writer, data interface{}) error {
101                 if zp, ok := data.(io.ReadCloser); ok {
102                         defer zp.Close()
103                         b, err := ioutil.ReadAll(zp)
104
105                         if err != nil {
106                                 ricdms.Logger.Error("error: %v", err)
107                                 return err
108                         }
109                         _, err = w.Write(b)
110
111                         if err != nil {
112                                 ricdms.Logger.Error("error: %v", err)
113                                 return err
114                         }
115                         return nil
116                 }
117                 return fmt.Errorf("not support")
118         })
119         r.api = api
120 }
121
122 func (r *Restful) Run() {
123         server := restapi.NewServer(r.api)
124         defer server.Shutdown()
125         server.Port = 8000
126         server.Host = "0.0.0.0"
127         ricdms.Logger.Info("Starting server at : %s:%d", server.Host, server.Port)
128         if err := server.Serve(); err != nil {
129                 log.Fatal(err.Error())
130         }
131 }