From: subhash kumar singh Date: Wed, 28 Sep 2022 10:22:56 +0000 (+0000) Subject: Implement Onboarding of xApp X-Git-Tag: 2.0.0~16 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F24%2F9124%2F2;p=ric-plt%2Fricdms.git Implement Onboarding of xApp Implemented Onboarding by using API from onboarder service. Signed-off-by: subhash kumar singh Change-Id: Ib4c0032c4906b3a2f2b3b70c1d6c2a9f6caad540 --- diff --git a/api/ric-dms-api-2.0.yaml b/api/ric-dms-api-2.0.yaml index 7d027c6..b8c8a99 100644 --- a/api/ric-dms-api-2.0.yaml +++ b/api/ric-dms-api-2.0.yaml @@ -393,13 +393,13 @@ definitions: properties: config: description: represents content of config file for xApp onboarding. - type: string + type: object config-file_url: description: specify url of config-file. type: string schema: description: represents conent of schema file for xApp onboarding. - type: string + type: object schema-file_url: description: specify url of schema-file. type: string diff --git a/pkg/onboard/onboarder.go b/pkg/onboard/onboarder.go new file mode 100644 index 0000000..a06f043 --- /dev/null +++ b/pkg/onboard/onboarder.go @@ -0,0 +1,69 @@ +//================================================================================== +// Copyright (c) 2022 Samsung +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +// This source code is part of the near-RT RIC (RAN Intelligent Controller) +// platform project (RICP). +//================================================================================== +// +package onboard + +import ( + "bytes" + "encoding/json" + "net/http" + + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/onboard" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" + "github.com/go-openapi/runtime/middleware" +) + +type IOnboarder interface { + Onboard(descriptor *models.Descriptor) middleware.Responder +} + +type Onboarder struct { +} + +func NewOnboarder() IOnboarder { + return &Onboarder{} +} + +func (o *Onboarder) Onboard(descriptor *models.Descriptor) middleware.Responder { + ricdms.Logger.Debug("onboarder invoked : %+v", descriptor) + + // validate if the required patameter is available + if descriptor.Schema == nil || descriptor.Config == nil { + return onboard.NewPostOnboardxAppsBadRequest() + } + + body := map[string]interface{}{ + "config-file.json": descriptor.Config, + "controls-schema.json": descriptor.Schema, + } + + bodyBytes, _ := json.Marshal(body) + + // resp, err := http.Post("http://172.17.0.1:8888/api/v1/onboard", "application/json", bytes.NewReader(bodyBytes)) + ricdms.Logger.Info("config : %+v", ricdms.Config) + resp, err := http.Post(ricdms.Config.OnboardURL, "application/json", bytes.NewReader(bodyBytes)) + + if err == nil { + ricdms.Logger.Info("no error response: %+v", resp) + } else { + ricdms.Logger.Error("err : (%v)", err) + } + return onboard.NewPostOnboardxAppsCreated() +} diff --git a/pkg/restful/restful.go b/pkg/restful/restful.go index 4255e2b..2670fb6 100644 --- a/pkg/restful/restful.go +++ b/pkg/restful/restful.go @@ -24,9 +24,11 @@ import ( "os" ph "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" + po "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/onboard" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/resthooks" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" "github.com/go-openapi/loads" @@ -37,6 +39,7 @@ func NewRestful() *Restful { r := &Restful{ rh: resthooks.NewResthook( ph.NewHealthChecker(), + po.NewOnboarder(), ), } r.setupHandler() @@ -57,6 +60,12 @@ func (r *Restful) setupHandler() { return resp }) + api.OnboardPostOnboardxAppsHandler = onboard.PostOnboardxAppsHandlerFunc(func(poap onboard.PostOnboardxAppsParams) middleware.Responder { + ricdms.Logger.Debug("==> onboard API invoked.") + resp := r.rh.OnBoard(poap.Body) + return resp + }) + r.api = api } diff --git a/pkg/resthooks/resthooks.go b/pkg/resthooks/resthooks.go index 500b32e..072fa1d 100644 --- a/pkg/resthooks/resthooks.go +++ b/pkg/resthooks/resthooks.go @@ -22,14 +22,17 @@ package resthooks import ( ph "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" "github.com/go-openapi/runtime/middleware" ) -func NewResthook(h ph.IHealthChecker) *Resthook { +func NewResthook(h ph.IHealthChecker, o onboard.IOnboarder) *Resthook { return &Resthook{ HealthChecker: h, + Onboarder: o, } } @@ -37,3 +40,8 @@ func (rh *Resthook) GetDMSHealth() (resp middleware.Responder) { ricdms.Logger.Debug("healthchecker : %v\n", rh.HealthChecker) return health.NewGetHealthCheckOK().WithPayload(rh.HealthChecker.GetStatus()) } + +func (rh *Resthook) OnBoard(params *models.Descriptor) (resp middleware.Responder) { + ricdms.Logger.Debug("onboarder: invoked") + return rh.Onboarder.Onboard(params) +} diff --git a/pkg/resthooks/resthooks_test.go b/pkg/resthooks/resthooks_test.go index e7cbfbd..74ba05e 100644 --- a/pkg/resthooks/resthooks_test.go +++ b/pkg/resthooks/resthooks_test.go @@ -20,11 +20,18 @@ package resthooks import ( + "encoding/json" + "fmt" + "io/ioutil" + "net" + "net/http" + "net/http/httptest" "os" "testing" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/models" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard" h "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/restapi/operations/health" "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/ricdms" "github.com/stretchr/testify/assert" @@ -42,6 +49,7 @@ func TestMain(m *testing.M) { ricdms.Init() rh = &Resthook{ HealthChecker: HealthCheckerMock{}, + Onboarder: onboard.NewOnboarder(), } code := m.Run() os.Exit(code) @@ -61,6 +69,39 @@ func TestHealth(t *testing.T) { } } +func TestOnboard(t *testing.T) { + xApp := &models.Descriptor{ + Config: "SAMPLE_CONFIGURATION", + Schema: "SAMPLE_SCHEMA", + } + + svr := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var d map[string]interface{} + reqBytes, _ := ioutil.ReadAll(r.Body) + defer r.Body.Close() + + err := json.Unmarshal(reqBytes, &d) + + ricdms.Logger.Debug("after unmarshal : %+v body: %+v", d, string(reqBytes)) + + if err != nil { + assert.Fail(t, "Not able to parse the request body") + } + + assert.Equal(t, xApp.Config, d["config-file.json"]) + assert.Equal(t, xApp.Schema, d["controls-schema.json"]) + fmt.Fprintf(w, "SAMPLE_RESPONSE") + })) + svr.Listener.Close() + svr.Listener, _ = net.Listen("tcp", ricdms.Config.MockServer) + + svr.Start() + defer svr.Close() + + resp := rh.OnBoard(xApp) + assert.NotEqual(t, nil, resp) +} + type HealthCheckerMock struct { mock.Mock } diff --git a/pkg/resthooks/types.go b/pkg/resthooks/types.go index 40a46aa..d6f3ad4 100644 --- a/pkg/resthooks/types.go +++ b/pkg/resthooks/types.go @@ -21,8 +21,10 @@ package resthooks import ( "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/health" + "gerrit.o-ran-sc.org/r/ric-plt/ricdms/pkg/onboard" ) type Resthook struct { HealthChecker health.IHealthChecker + Onboarder onboard.IOnboarder }