5001c9ca035b2cb196a9f88c7496ab61b21849fd
[nonrtric/plt/sme.git] / invoker / handler / onboardinvoker_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         "bytes"
24         "encoding/json"
25         "fmt"
26         "net/http"
27
28         "github.com/labstack/echo/v4"
29         log "github.com/sirupsen/logrus"
30         invokerapi "oransc.org/nonrtric/capifinvoker/internal/invokermanagementapi"
31 )
32
33 func OnboardingInvokerHandler(c echo.Context) error {
34         return c.Render(http.StatusOK, "onboardinvoker.html", map[string]interface{}{
35                 "isError":    false,
36                 "isResponse": false,
37         })
38 }
39
40 func OnboardInvoker(server string) echo.HandlerFunc {
41         return func(c echo.Context) error {
42
43                 url := server + "/api-invoker-management/v1/onboardedInvokers"
44                 log.Infof("[Register invoker] url to capif core %v\n", url)
45
46                 var newInvoker invokerapi.APIInvokerEnrolmentDetails
47                 fmt.Printf("Getting enrolment details from UI:: %+v", c.FormValue("enrolmentDetails"))
48                 err := json.Unmarshal([]byte(c.FormValue("enrolmentDetails")), &newInvoker)
49                 if err != nil {
50                         fmt.Printf("error: %+v", err)
51                         log.Error("[Register invoker] error unmarshaling parameter enrolmentDetails as JSON")
52                         return c.Render(http.StatusBadRequest, "onboardinvoker.html", map[string]interface{}{
53                                 "isResponse": false,
54                                 "isError":    true,
55                                 "response":   "Error unmarshaling parameter enrolmentDetails as JSON",
56                         })
57                 }
58
59                 headers := map[string]string{
60                         "Content-Type": "application/json",
61                 }
62                 jsonBytes, err := json.Marshal(newInvoker)
63                 if err != nil {
64                         return c.Render(http.StatusBadRequest, "onboardinvoker.html", map[string]interface{}{
65                                 "isResponse": false,
66                                 "isError":    true,
67                                 "response":   "Error marshaling parameter enrolmentDetails before doing request",
68                         })
69                 }
70
71                 resp, err := makeRequest("POST", url, headers, bytes.NewReader(jsonBytes))
72                 if err != nil {
73                         log.Errorf("[Register invoker] %v", fmt.Sprintf("error: %v", err))
74                         return c.Render(http.StatusBadRequest, "onboardinvoker.html", map[string]interface{}{
75                                 "isResponse": false,
76                                 "isError":    true,
77                                 "response":   fmt.Sprintf("error: %v", err),
78                         })
79                 }
80
81                 var resInvoker invokerapi.APIInvokerEnrolmentDetails
82                 err = json.Unmarshal(resp, &resInvoker)
83                 if err != nil {
84                         log.Error("[Register invoker] error unmarshaling response enrolmentDetails as JSON")
85                         return c.Render(http.StatusBadRequest, "onboardinvoker.html", map[string]interface{}{
86                                 "isResponse": false,
87                                 "isError":    true,
88                                 "response":   "error unmarshaling parameter enrolmentDetails as JSON",
89                         })
90                 }
91
92                 bytes, _ := json.Marshal(resInvoker)
93                 log.Infof("[Register invoker] new invoker register with id %v\n", *resInvoker.ApiInvokerId)
94                 return c.Render(http.StatusOK, "onboardinvoker.html", map[string]interface{}{
95                         "isResponse": true,
96                         "isError":    false,
97                         "response":   string(bytes),
98                 })
99         }
100 }