Add the config parameter for chartmuseum URL
[ric-plt/ricdms.git] / pkg / onboard / onboarder.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 onboard
22
23 import (
24         "bytes"
25         "encoding/json"
26         "io"
27         "net/http"
28
29         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models"
30         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/experiment"
31         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/onboard"
32         "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
33         "github.com/go-openapi/runtime/middleware"
34 )
35
36 type IOnboarder interface {
37         Onboard(descriptor *models.Descriptor) middleware.Responder
38         CustomOnboard(reader io.Reader) middleware.Responder
39 }
40
41 type Onboarder struct {
42 }
43
44 func NewOnboarder() IOnboarder {
45         return &Onboarder{}
46 }
47
48 func (o *Onboarder) Onboard(descriptor *models.Descriptor) middleware.Responder {
49         ricdms.Logger.Debug("onboarder invoked : %+v", descriptor)
50
51         // validate if the required patameter is available
52         if descriptor.Schema == nil || descriptor.Config == nil {
53                 return onboard.NewPostOnboardxAppsBadRequest()
54         }
55
56         body := map[string]interface{}{
57                 "config-file.json":     descriptor.Config,
58                 "controls-schema.json": descriptor.Schema,
59         }
60
61         bodyBytes, _ := json.Marshal(body)
62
63         // resp, err := http.Post("http://172.17.0.1:8888/api/v1/onboard", "application/json", bytes.NewReader(bodyBytes))
64         ricdms.Logger.Info("config : %+v", ricdms.Config)
65         resp, err := http.Post(ricdms.Config.OnboardURL, "application/json", bytes.NewReader(bodyBytes))
66
67         if err == nil {
68                 ricdms.Logger.Info("no error response: %+v", resp)
69         } else {
70                 ricdms.Logger.Error("err : (%v)", err)
71         }
72         return onboard.NewPostOnboardxAppsCreated()
73 }
74
75 // onboard provided helm chart
76 func (o *Onboarder) CustomOnboard(reader io.Reader) middleware.Responder {
77         ricdms.Logger.Debug("onboarder received req to onboard")
78         resp, err := http.Post(ricdms.Config.CustomOnboardURL, "application/x-www-form-urlencoded", reader)
79         if err != nil {
80                 ricdms.Logger.Error("err received while onboarding chart to chartmuseum: %v", err)
81                 // TODO: introcuce error in in swagger to handle the error cases.
82                 return nil
83         }
84
85         defer resp.Body.Close()
86         if resp.StatusCode < 200 || resp.StatusCode >= 300 {
87                 // TODO: return error code in response
88                 ricdms.Logger.Error("chartmuseum returned bad status code(%d): %+v", resp.StatusCode, resp)
89                 return nil
90         }
91         return &experiment.PostCustomOnboardOK{}
92 }