CI: Add silent prescan SonarCloud job
[nonrtric/plt/sme.git] / provider / handler / publishapi_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/publishserviceapi"
30 )
31
32 func PublishapiHandler(c echo.Context) error {
33         return c.Render(http.StatusOK, "publishapi.html", map[string]interface{}{
34                 "isError":    false,
35                 "isResponse": false,
36         })
37 }
38
39 func PublishApiFormHandler(server string) echo.HandlerFunc {
40         return func(c echo.Context) error {
41
42                 apfId := c.FormValue("apfId")
43                 if apfId == "" {
44                         return c.Render(http.StatusBadRequest, "publishapi.html", map[string]interface{}{
45                                 "isError":    true,
46                                 "isResponse": false,
47                                 "response":   "field apfId is needed",
48                         })
49                 }
50
51                 //server format: http://localhost:8090
52                 url := server + "/published-apis/v1/" + apfId + "/service-apis"
53
54                 log.Infof("[Publish API] url to capif core %v for aefId: %v", url, apfId)
55                 var apiDescription publishserviceapi.ServiceAPIDescription
56
57                 err := json.Unmarshal([]byte(c.FormValue("apiDescription")), &apiDescription)
58                 if err != nil {
59                         log.Error("[Publish API] error unmarshaling parameter ServiceAPIDescription as JSON")
60                         return c.Render(http.StatusBadRequest, "publishapi.html", map[string]interface{}{
61                                 "isResponse": false,
62                                 "isError":    true,
63                                 "response":   "error unmarshaling parameter ServiceAPIDescription as JSON",
64                         })
65                 }
66
67                 headers := map[string]string{
68                         "Content-Type": "application/json",
69                 }
70                 resp, err := makeRequest("POST", url, headers, apiDescription)
71                 if err != nil {
72                         log.Errorf("[Publish API] %v", fmt.Sprintf("error: %v", err))
73                         return c.Render(http.StatusBadRequest, "publishapi.html", map[string]interface{}{
74                                 "isResponse": false,
75                                 "isError":    true,
76                                 "response":   fmt.Sprintf("error: %v", err),
77                         })
78                 }
79
80                 var resAPI publishserviceapi.ServiceAPIDescription
81                 err = json.Unmarshal(resp, &resAPI)
82                 if err != nil {
83                         log.Error("[Publish API] error unmarshaling parameter ServiceAPIDescription as JSON")
84                         return c.Render(http.StatusBadRequest, "publishapi.html", map[string]interface{}{
85                                 "isResponse": false,
86                                 "isError":    true,
87                                 "response":   "Error unmarshaling parameter ServiceAPIDescription as JSON",
88                         })
89                 }
90
91                 bytes, _ := json.Marshal(resAPI)
92                 log.Infof("[Publish API] API %v with the id: %v has been register", resAPI.ApiName, *resAPI.ApiId)
93                 return c.Render(http.StatusOK, "publishapi.html", map[string]interface{}{
94                         "isResponse": true,
95                         "response":   string(bytes),
96                 })
97         }
98 }