2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
6 // Copyright (C) 2023: OpenInfra Foundation Europe.
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
12 // http://www.apache.org/licenses/LICENSE-2.0
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===================================
22 package discoverservice
28 "oransc.org/nonrtric/capifcore/internal/common29122"
29 discoverapi "oransc.org/nonrtric/capifcore/internal/discoverserviceapi"
30 "oransc.org/nonrtric/capifcore/internal/invokermanagement"
32 "github.com/labstack/echo/v4"
34 publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
37 type DiscoverService struct {
38 invokerRegister invokermanagement.InvokerRegister
41 func NewDiscoverService(invokerRegister invokermanagement.InvokerRegister) *DiscoverService {
42 return &DiscoverService{
43 invokerRegister: invokerRegister,
47 func (ds *DiscoverService) GetAllServiceAPIs(ctx echo.Context, params discoverapi.GetAllServiceAPIsParams) error {
48 allApis := ds.invokerRegister.GetInvokerApiList(params.ApiInvokerId)
50 return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf("Invoker %s not registered", params.ApiInvokerId))
53 filteredApis := []publishapi.ServiceAPIDescription{}
54 for _, api := range *allApis {
55 if matchesFilter(api, params) {
56 filteredApis = append(filteredApis, api)
59 discoveredApis := discoverapi.DiscoveredAPIs{
60 ServiceAPIDescriptions: &filteredApis,
62 err := ctx.JSON(http.StatusOK, discoveredApis)
64 // Something really bad happened, tell Echo that our handler failed
71 func matchesFilter(api publishapi.ServiceAPIDescription, filter discoverapi.GetAllServiceAPIsParams) bool {
72 if filter.ApiName != nil && *filter.ApiName != api.ApiName {
75 if filter.ApiCat != nil && (api.ServiceAPICategory == nil || *filter.ApiCat != *api.ServiceAPICategory) {
78 profiles := *api.AefProfiles
79 for _, profile := range profiles {
80 if checkAefId(filter, profile) && checkVersionAndCommType(profile, filter) && checkProtocol(filter, profile) && checkDataFormat(filter, profile) {
87 func checkAefId(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool {
88 if filter.AefId != nil {
89 return *filter.AefId == profile.AefId
94 func checkVersionAndCommType(profile publishapi.AefProfile, filter discoverapi.GetAllServiceAPIsParams) bool {
96 if filter.ApiVersion != nil {
97 for _, version := range profile.Versions {
98 match = checkVersion(version, filter.ApiVersion, filter.CommType)
103 } else if filter.CommType != nil {
104 for _, version := range profile.Versions {
105 match = checkCommType(version.Resources, filter.CommType)
113 func checkProtocol(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool {
114 if filter.Protocol != nil {
115 return profile.Protocol != nil && *filter.Protocol == *profile.Protocol
120 func checkDataFormat(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool {
121 if filter.DataFormat != nil {
122 return profile.DataFormat != nil && *filter.DataFormat == *profile.DataFormat
127 func checkVersion(version publishapi.Version, wantedVersion *string, commType *publishapi.CommunicationType) bool {
129 if *wantedVersion == version.ApiVersion {
131 match = checkCommType(version.Resources, commType)
139 func checkCommType(resources *[]publishapi.Resource, commType *publishapi.CommunicationType) bool {
140 for _, resource := range *resources {
141 if resource.CommType == *commType {
148 // This function wraps sending of an error in the Error format, and
149 // handling the failure to marshal that.
150 func sendCoreError(ctx echo.Context, code int, message string) error {
151 pd := common29122.ProblemDetails{
155 err := ctx.JSON(code, pd)