X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=provider%2Fhandler%2Fregistration_handler.go;fp=provider%2Fhandler%2Fregistration_handler.go;h=0259999c0a69ca08eecede82b3ee48608dd9bbeb;hb=e71305f32cddb7933da76dc5ce60193a866c48e7;hp=0000000000000000000000000000000000000000;hpb=7ae570afcc8119b1563cb786b2a34bf190f105da;p=nonrtric%2Fplt%2Fsme.git diff --git a/provider/handler/registration_handler.go b/provider/handler/registration_handler.go new file mode 100644 index 0000000..0259999 --- /dev/null +++ b/provider/handler/registration_handler.go @@ -0,0 +1,88 @@ +// - +// +// ========================LICENSE_START================================= +// O-RAN-SC +// %% +// Copyright (C) 2023: Nordix Foundation +// %% +// 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. +// ========================LICENSE_END=================================== +package handler + +import ( + "encoding/json" + "fmt" + "net/http" + + "github.com/labstack/echo/v4" + log "github.com/sirupsen/logrus" + "oransc.org/nonrtric/capifprov/internal/providermanagementapi" +) + +func RegistrationHandler(c echo.Context) error { + return c.Render(http.StatusOK, "registration.html", map[string]interface{}{ + "isError": false, + "isResponse": false, + }) +} + +func RegistrationFormHandler(server string) echo.HandlerFunc { + return func(c echo.Context) error { + + url := server + "/api-provider-management/v1/registrations" + log.Infof("[Register provider] url to capif core %v\n", url) + + var newProvider providermanagementapi.APIProviderEnrolmentDetails + err := json.Unmarshal([]byte(c.FormValue("enrolmentDetails")), &newProvider) + if err != nil { + log.Error("[Register provider] error unmarshaling parameter enrolmentDetails as JSON") + return c.Render(http.StatusBadRequest, "registration.html", map[string]interface{}{ + "isResponse": false, + "isError": true, + "response": "Error unmarshaling parameter enrolmentDetails as JSON", + }) + } + + headers := map[string]string{ + "Content-Type": "application/json", + } + resp, err := makeRequest("POST", url, headers, newProvider) + if err != nil { + log.Errorf("[Register provider] %v", fmt.Sprintf("error: %v", err)) + return c.Render(http.StatusBadRequest, "registration.html", map[string]interface{}{ + "isResponse": false, + "isError": true, + "response": fmt.Sprintf("error: %v", err), + }) + } + + var resProvider providermanagementapi.APIProviderEnrolmentDetails + err = json.Unmarshal(resp, &resProvider) + if err != nil { + log.Error("[Register provider] error unmarshaling parameter enrolmentDetails as JSON") + return c.Render(http.StatusBadRequest, "registration.html", map[string]interface{}{ + "isResponse": false, + "isError": true, + "response": "error unmarshaling parameter enrolmentDetails as JSON", + }) + } + + bytes, _ := json.Marshal(resProvider) + log.Infof("[Register provider] Api Provider domain %v has been register\n", resProvider.ApiProvDomId) + return c.Render(http.StatusOK, "registration.html", map[string]interface{}{ + "isResponse": true, + "isError": false, + "response": string(bytes), + }) + } +}