NONRTRIC-946: Add support for Kong routes
[nonrtric/plt/sme.git] / servicemanager / internal / discoverservice / discoverservice.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2023-2024: OpenInfra Foundation Europe
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package discoverservice
22
23 import (
24         "context"
25         "fmt"
26         "net/http"
27
28         "oransc.org/nonrtric/servicemanager/internal/common29122"
29         discoverapi "oransc.org/nonrtric/servicemanager/internal/discoverserviceapi"
30
31         echo "github.com/labstack/echo/v4"
32         log "github.com/sirupsen/logrus"
33 )
34
35 type DiscoverService struct {
36         CapifProtocol string
37         CapifIPv4     common29122.Ipv4Addr
38         CapifPort     common29122.Port
39 }
40
41 func NewDiscoverService(capifProtocol string, capifIPv4 common29122.Ipv4Addr, capifPort common29122.Port) *DiscoverService {
42         return &DiscoverService{
43                 CapifProtocol: capifProtocol,
44                 CapifIPv4:     capifIPv4,
45                 CapifPort:     capifPort,
46         }
47 }
48
49 func (ds *DiscoverService) GetAllServiceAPIs(ctx echo.Context, params discoverapi.GetAllServiceAPIsParams) error {
50         log.Trace("entering GetAllServiceAPIs")
51
52         capifcoreUrl := fmt.Sprintf("%s://%s:%d/service-apis/v1/", ds.CapifProtocol, ds.CapifIPv4, ds.CapifPort)
53         client, err := discoverapi.NewClientWithResponses(capifcoreUrl)
54         if err != nil {
55                 return err
56         }
57
58         var (
59                 ctxHandler context.Context
60                 cancel     context.CancelFunc
61         )
62         ctxHandler, cancel = context.WithCancel(context.Background())
63         defer cancel()
64
65         var rsp *discoverapi.GetAllServiceAPIsResponse
66         rsp, err = client.GetAllServiceAPIsWithResponse(ctxHandler, &params)
67
68         if err != nil {
69                 msg := err.Error()
70                 log.Errorf("error on GetAllServiceAPIsWithResponse %s", msg)
71                 return sendCoreError(ctx, http.StatusInternalServerError, msg)
72         }
73
74         if rsp.StatusCode() != http.StatusOK {
75                 msg := string(rsp.Body)
76                 log.Errorf("GetAllServiceAPIs status code %d", rsp.StatusCode())
77                 log.Errorf("GetAllServiceAPIs error %s", string(rsp.Body))
78                 return sendCoreError(ctx, rsp.StatusCode(), msg)
79         }
80
81         rspDiscoveredAPIs := *rsp.JSON200
82         err = ctx.JSON(http.StatusOK, rspDiscoveredAPIs)
83         if err != nil {
84                 return err // tell Echo that our handler failed
85         }
86         return nil
87 }
88
89
90 // This function wraps sending of an error in the Error format, and
91 // handling the failure to marshal that.
92 func sendCoreError(ctx echo.Context, code int, message string) error {
93         pd := common29122.ProblemDetails{
94                 Cause:  &message,
95                 Status: &code,
96         }
97         err := ctx.JSON(code, pd)
98         return err
99 }