NONRTRIC-946: Servicemanager - Add Read The Docs
[nonrtric/plt/sme.git] / invoker / handler / discovery_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         "net/url"
27
28         "github.com/labstack/echo/v4"
29         log "github.com/sirupsen/logrus"
30         "oransc.org/nonrtric/capifinvoker/internal/discoverserviceapi"
31 )
32
33 func DiscoverAPIs(server string) echo.HandlerFunc {
34         return func(c echo.Context) error {
35
36                 invokerId := c.FormValue("api-invoker-id")
37                 if invokerId == "" {
38                         return c.Render(http.StatusOK, "discovery.html", map[string]interface{}{
39                                 "isResponse": false,
40                                 "isError":    false,
41                         })
42                 }
43
44                 //server format: http://localhost:8090
45                 urlStr, _ := url.Parse(server + "/service-apis/v1/allServiceAPIs")
46                 log.Infof("[Discovery API] apis to %v for invokerId: %v", urlStr, invokerId)
47
48                 //TODO check how to remove empty parameters
49                 c.Request().ParseForm()
50                 params := c.Request().Form
51                 for key, val := range params {
52                         if len(val) == 0 || val[0] == "" {
53                                 params.Del(key)
54                         }
55                 }
56                 urlStr.RawQuery = params.Encode()
57
58                 headers := map[string]string{
59                         "Content-Type": "text/plain",
60                 }
61                 resp, err := makeRequest("GET", urlStr.String(), headers, nil)
62                 if err != nil {
63                         log.Errorf("[Discovery API] %v", fmt.Sprintf("error: %v", err))
64                         return c.Render(http.StatusBadRequest, "discovery.html", map[string]interface{}{
65                                 "response":   fmt.Sprintf("error: %v", err),
66                                 "isError":    true,
67                                 "isResponse": false,
68                         })
69                 }
70                 log.Infof("[Discovery API] Response from service: %+v error: %v\n", string(resp), err)
71
72                 var resAPIs discoverserviceapi.DiscoveredAPIs
73                 err = json.Unmarshal(resp, &resAPIs)
74                 if err != nil {
75                         log.Error("[Discovery API] error unmarshaling parameter DiscoveredAPIs as JSON")
76                         return c.Render(http.StatusBadRequest, "discovery.html", map[string]interface{}{
77                                 "isResponse": false,
78                                 "isError":    true,
79                                 "response":   "error unmarshaling parameter []DiscoveredAPIs as JSON",
80                         })
81                 }
82
83                 if len(*resAPIs.ServiceAPIDescriptions) == 0 {
84                         log.Info("[Discovery API] There are no APIs availables for the specified parameters.")
85                         return c.Render(http.StatusOK, "discovery.html", map[string]interface{}{
86                                 "isResponse": false,
87                                 "isError":    false,
88                                 "isEmpty":    true,
89                                 "response":   "There are no APIs availables for the specified parameters.",
90                         })
91                 }
92
93                 bytes, _ := json.Marshal(resAPIs)
94                 return c.Render(http.StatusOK, "discovery.html", map[string]interface{}{
95                         "isResponse": true,
96                         "response":   string(bytes),
97                 })
98         }
99 }