CI: Add silent prescan SonarCloud job
[nonrtric/plt/sme.git] / provider / handler / registration_handler.go
1 // -
2 //
3 //      ========================LICENSE_START=================================
4 //      O-RAN-SC
5 //      %%
6 //      Copyright (C) 2023: Nordix Foundation
7 //      %%
8 //      Licensed under the Apache License, Version 2.0 (the "License");
9 //      you may not use this file except in compliance with the License.
10 //      You may obtain a copy of the License at
11 //
12 //           http://www.apache.org/licenses/LICENSE-2.0
13 //
14 //      Unless required by applicable law or agreed to in writing, software
15 //      distributed under the License is distributed on an "AS IS" BASIS,
16 //      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 //      See the License for the specific language governing permissions and
18 //      limitations under the License.
19 //      ========================LICENSE_END===================================
20 package handler
21
22 import (
23         "encoding/json"
24         "fmt"
25         "net/http"
26
27         "github.com/labstack/echo/v4"
28         log "github.com/sirupsen/logrus"
29         "oransc.org/nonrtric/capifprov/internal/providermanagementapi"
30 )
31
32 func RegistrationHandler(c echo.Context) error {
33         return c.Render(http.StatusOK, "registration.html", map[string]interface{}{
34                 "isError":    false,
35                 "isResponse": false,
36         })
37 }
38
39 func RegistrationFormHandler(server string) echo.HandlerFunc {
40         return func(c echo.Context) error {
41
42                 url := server + "/api-provider-management/v1/registrations"
43                 log.Infof("[Register provider] url to capif core %v\n", url)
44
45                 var newProvider providermanagementapi.APIProviderEnrolmentDetails
46                 err := json.Unmarshal([]byte(c.FormValue("enrolmentDetails")), &newProvider)
47                 if err != nil {
48                         log.Error("[Register provider] error unmarshaling parameter enrolmentDetails as JSON")
49                         return c.Render(http.StatusBadRequest, "registration.html", map[string]interface{}{
50                                 "isResponse": false,
51                                 "isError":    true,
52                                 "response":   "Error unmarshaling parameter enrolmentDetails as JSON",
53                         })
54                 }
55
56                 headers := map[string]string{
57                         "Content-Type": "application/json",
58                 }
59                 resp, err := makeRequest("POST", url, headers, newProvider)
60                 if err != nil {
61                         log.Errorf("[Register provider] %v", fmt.Sprintf("error: %v", err))
62                         return c.Render(http.StatusBadRequest, "registration.html", map[string]interface{}{
63                                 "isResponse": false,
64                                 "isError":    true,
65                                 "response":   fmt.Sprintf("error: %v", err),
66                         })
67                 }
68
69                 var resProvider providermanagementapi.APIProviderEnrolmentDetails
70                 err = json.Unmarshal(resp, &resProvider)
71                 if err != nil {
72                         log.Error("[Register provider] error unmarshaling parameter enrolmentDetails as JSON")
73                         return c.Render(http.StatusBadRequest, "registration.html", map[string]interface{}{
74                                 "isResponse": false,
75                                 "isError":    true,
76                                 "response":   "error unmarshaling parameter enrolmentDetails as JSON",
77                         })
78                 }
79
80                 bytes, _ := json.Marshal(resProvider)
81                 log.Infof("[Register provider] Api Provider domain %v has been register\n", resProvider.ApiProvDomId)
82                 return c.Render(http.StatusOK, "registration.html", map[string]interface{}{
83                         "isResponse": true,
84                         "isError":    false,
85                         "response":   string(bytes),
86                 })
87         }
88 }