1 // ==================================================================================
3 // Copyright (c) 2022 Samsung
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
20 // ==================================================================================
30 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models"
31 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/experiment"
32 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/onboard"
33 "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms"
34 "github.com/go-openapi/runtime/middleware"
37 type IOnboarder interface {
38 Onboard(descriptor *models.Descriptor) middleware.Responder
39 CustomOnboard(reader io.Reader) middleware.Responder
42 type Onboarder struct {
45 func NewOnboarder() IOnboarder {
49 func (o *Onboarder) Onboard(descriptor *models.Descriptor) middleware.Responder {
50 ricdms.Logger.Debug("onboarder invoked : %+v", descriptor)
52 // validate if the required patameter is available
53 if descriptor.Schema == nil || descriptor.Config == nil {
54 return onboard.NewPostOnboardxAppsBadRequest()
57 body := map[string]interface{}{
58 "config-file.json": descriptor.Config,
59 "controls-schema.json": descriptor.Schema,
62 bodyBytes, _ := json.Marshal(body)
64 // resp, err := http.Post("http://172.17.0.1:8888/api/v1/onboard", "application/json", bytes.NewReader(bodyBytes))
65 ricdms.Logger.Info("config : %+v", ricdms.Config)
66 resp, err := http.Post(ricdms.Config.OnboardURL, "application/json", bytes.NewReader(bodyBytes))
69 ricdms.Logger.Info("no error response: %+v", resp)
71 ricdms.Logger.Error("err : (%v)", err)
73 return experiment.NewPostCustomOnboardOK()
76 // onboard provided helm chart
77 func (o *Onboarder) CustomOnboard(reader io.Reader) middleware.Responder {
78 ricdms.Logger.Debug("onboarder received req to onboard")
79 resp, err := http.Post(ricdms.Config.CustomOnboardURL, "application/x-www-form-urlencoded", reader)
81 ricdms.Logger.Error("err received while onboarding chart to chartmuseum: %v", err)
83 resp := experiment.NewPostCustomOnboardInternalServerError()
84 resp.SetPayload(&models.ErrorMessage{
85 ErrorMessage: &errmsg,
90 defer resp.Body.Close()
91 if resp.StatusCode < 200 || resp.StatusCode >= 300 {
92 // TODO: return error code in response
93 ricdms.Logger.Error("chartmuseum returned bad status code(%d): %+v", resp.StatusCode, resp)
94 errmsg := fmt.Sprintf("chartmuseum returns status code :%d", resp.StatusCode)
95 resp := experiment.NewPostCustomOnboardInternalServerError()
96 resp.SetPayload(&models.ErrorMessage{
97 ErrorMessage: &errmsg,
101 return &experiment.PostCustomOnboardOK{}