Adding Dockerfile
[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         "fmt"
27         "io"
28         "net/http"
29
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"
35 )
36
37 type IOnboarder interface {
38         Onboard(descriptor *models.Descriptor) middleware.Responder
39         CustomOnboard(reader io.Reader) middleware.Responder
40 }
41
42 type Onboarder struct {
43 }
44
45 func NewOnboarder() IOnboarder {
46         return &Onboarder{}
47 }
48
49 func (o *Onboarder) Onboard(descriptor *models.Descriptor) middleware.Responder {
50         ricdms.Logger.Debug("onboarder invoked : %+v", descriptor)
51
52         // validate if the required patameter is available
53         if descriptor.Schema == nil || descriptor.Config == nil {
54                 return onboard.NewPostOnboardxAppsBadRequest()
55         }
56
57         body := map[string]interface{}{
58                 "config-file.json":     descriptor.Config,
59                 "controls-schema.json": descriptor.Schema,
60         }
61
62         bodyBytes, _ := json.Marshal(body)
63
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))
67
68         if err == nil {
69                 ricdms.Logger.Info("no error response: %+v", resp)
70         } else {
71                 ricdms.Logger.Error("err : (%v)", err)
72         }
73         return experiment.NewPostCustomOnboardOK()
74 }
75
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)
80         if err != nil {
81                 ricdms.Logger.Error("err received while onboarding chart to chartmuseum: %v", err)
82                 errmsg := err.Error()
83                 resp := experiment.NewPostCustomOnboardInternalServerError()
84                 resp.SetPayload(&models.ErrorMessage{
85                         ErrorMessage: &errmsg,
86                 })
87                 return resp
88         }
89
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,
98                 })
99                 return resp
100         }
101         return &experiment.PostCustomOnboardOK{}
102 }