2 // ========================LICENSE_START=================================
5 // Copyright (C) 2023-2024: OpenInfra Foundation Europe
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package discoverservice
28 "oransc.org/nonrtric/servicemanager/internal/common29122"
29 discoverapi "oransc.org/nonrtric/servicemanager/internal/discoverserviceapi"
31 echo "github.com/labstack/echo/v4"
32 log "github.com/sirupsen/logrus"
35 type DiscoverService struct {
37 CapifIPv4 common29122.Ipv4Addr
38 CapifPort common29122.Port
41 func NewDiscoverService(capifProtocol string, capifIPv4 common29122.Ipv4Addr, capifPort common29122.Port) *DiscoverService {
42 return &DiscoverService{
43 CapifProtocol: capifProtocol,
49 func (ds *DiscoverService) GetAllServiceAPIs(ctx echo.Context, params discoverapi.GetAllServiceAPIsParams) error {
50 log.Trace("entering GetAllServiceAPIs")
52 capifcoreUrl := fmt.Sprintf("%s://%s:%d/service-apis/v1/", ds.CapifProtocol, ds.CapifIPv4, ds.CapifPort)
53 client, err := discoverapi.NewClientWithResponses(capifcoreUrl)
59 ctxHandler context.Context
60 cancel context.CancelFunc
62 ctxHandler, cancel = context.WithCancel(context.Background())
65 var rsp *discoverapi.GetAllServiceAPIsResponse
66 rsp, err = client.GetAllServiceAPIsWithResponse(ctxHandler, ¶ms)
70 log.Errorf("error on GetAllServiceAPIsWithResponse %s", msg)
71 return sendCoreError(ctx, http.StatusInternalServerError, msg)
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)
81 rspDiscoveredAPIs := *rsp.JSON200
82 err = ctx.JSON(http.StatusOK, rspDiscoveredAPIs)
84 return err // tell Echo that our handler failed
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{
97 err := ctx.JSON(code, pd)