X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=capifcore%2Finternal%2Fdiscoverservice%2Fdiscoverservice.go;h=82b59725b075211c3c617d7dcc2a5a219ce792a3;hb=refs%2Fheads%2Fmaster;hp=a0d1f472c3c6d1296d46490887708e6338aeaa07;hpb=0725130f13eba6aff175da8a7873e3f92f5be06a;p=nonrtric%2Fplt%2Fsme.git diff --git a/capifcore/internal/discoverservice/discoverservice.go b/capifcore/internal/discoverservice/discoverservice.go index a0d1f47..82b5972 100644 --- a/capifcore/internal/discoverservice/discoverservice.go +++ b/capifcore/internal/discoverservice/discoverservice.go @@ -3,6 +3,7 @@ // O-RAN-SC // %% // Copyright (C) 2022: Nordix Foundation +// Copyright (C) 2023: OpenInfra Foundation Europe. // %% // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -21,11 +22,12 @@ package discoverservice import ( + "fmt" "net/http" + "oransc.org/nonrtric/capifcore/internal/common29122" discoverapi "oransc.org/nonrtric/capifcore/internal/discoverserviceapi" - - "oransc.org/nonrtric/capifcore/internal/publishservice" + "oransc.org/nonrtric/capifcore/internal/invokermanagement" "github.com/labstack/echo/v4" @@ -33,29 +35,26 @@ import ( ) type DiscoverService struct { - apiRegister publishservice.APIRegister + invokerRegister invokermanagement.InvokerRegister } -func NewDiscoverService(apiRegister publishservice.APIRegister) *DiscoverService { +func NewDiscoverService(invokerRegister invokermanagement.InvokerRegister) *DiscoverService { return &DiscoverService{ - apiRegister: apiRegister, + invokerRegister: invokerRegister, } } func (ds *DiscoverService) GetAllServiceAPIs(ctx echo.Context, params discoverapi.GetAllServiceAPIsParams) error { - allApis := *ds.apiRegister.GetAPIs() + allApis := ds.invokerRegister.GetInvokerApiList(params.ApiInvokerId) + if allApis == nil { + return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf("Invoker %s not registered", params.ApiInvokerId)) + } + filteredApis := []publishapi.ServiceAPIDescription{} - gatewayDomain := "r1-expo-func-aef" - for _, api := range allApis { - if !matchesFilter(api, params) { - continue - } - profiles := *api.AefProfiles - for i, profile := range profiles { - profile.DomainName = &gatewayDomain // Hardcoded for now. Should be provided through some other mechanism. - profiles[i] = profile + for _, api := range *allApis { + if matchesFilter(api, params) { + filteredApis = append(filteredApis, api) } - filteredApis = append(filteredApis, api) } discoveredApis := discoverapi.DiscoveredAPIs{ ServiceAPIDescriptions: &filteredApis, @@ -77,42 +76,33 @@ func matchesFilter(api publishapi.ServiceAPIDescription, filter discoverapi.GetA return false } profiles := *api.AefProfiles - aefIdMatch := true - protocolMatch := true - dataFormatMatch := true - versionMatch := true for _, profile := range profiles { - if filter.AefId != nil { - aefIdMatch = *filter.AefId == profile.AefId - } - if filter.ApiVersion != nil || filter.CommType != nil { - versionMatch = checkVersionAndCommType(profile, filter.ApiVersion, filter.CommType) - } - if filter.Protocol != nil { - protocolMatch = profile.Protocol != nil && *filter.Protocol == *profile.Protocol - } - if filter.DataFormat != nil { - dataFormatMatch = profile.DataFormat != nil && *filter.DataFormat == *profile.DataFormat - } - if aefIdMatch && versionMatch && protocolMatch && dataFormatMatch { + if checkAefId(filter, profile) && checkVersionAndCommType(profile, filter) && checkProtocol(filter, profile) && checkDataFormat(filter, profile) { return true } } return false } -func checkVersionAndCommType(profile publishapi.AefProfile, wantedVersion *string, commType *publishapi.CommunicationType) bool { +func checkAefId(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool { + if filter.AefId != nil { + return *filter.AefId == profile.AefId + } + return true +} + +func checkVersionAndCommType(profile publishapi.AefProfile, filter discoverapi.GetAllServiceAPIsParams) bool { match := false - if wantedVersion != nil { + if filter.ApiVersion != nil { for _, version := range profile.Versions { - match = checkVersion(version, wantedVersion, commType) + match = checkVersion(version, filter.ApiVersion, filter.CommType) if match { break } } - } else if commType != nil { + } else if filter.CommType != nil { for _, version := range profile.Versions { - match = checkCommType(version.Resources, commType) + match = checkCommType(version.Resources, filter.CommType) } } else { match = true @@ -120,6 +110,20 @@ func checkVersionAndCommType(profile publishapi.AefProfile, wantedVersion *strin return match } +func checkProtocol(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool { + if filter.Protocol != nil { + return profile.Protocol != nil && *filter.Protocol == *profile.Protocol + } + return true +} + +func checkDataFormat(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool { + if filter.DataFormat != nil { + return profile.DataFormat != nil && *filter.DataFormat == *profile.DataFormat + } + return true +} + func checkVersion(version publishapi.Version, wantedVersion *string, commType *publishapi.CommunicationType) bool { match := false if *wantedVersion == version.ApiVersion { @@ -133,16 +137,21 @@ func checkVersion(version publishapi.Version, wantedVersion *string, commType *p } func checkCommType(resources *[]publishapi.Resource, commType *publishapi.CommunicationType) bool { - match := false - if commType != nil { - for _, resource := range *resources { - if resource.CommType == *commType { - match = true - break - } + for _, resource := range *resources { + if resource.CommType == *commType { + return true } - } else { - match = true } - return match + return false +} + +// This function wraps sending of an error in the Error format, and +// handling the failure to marshal that. +func sendCoreError(ctx echo.Context, code int, message string) error { + pd := common29122.ProblemDetails{ + Cause: &message, + Status: &code, + } + err := ctx.JSON(code, pd) + return err }