2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
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
26 discoverapi "oransc.org/nonrtric/capifcore/internal/discoverserviceapi"
28 "oransc.org/nonrtric/capifcore/internal/publishservice"
30 "github.com/labstack/echo/v4"
32 publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
35 type DiscoverService struct {
36 apiRegister publishservice.APIRegister
39 func NewDiscoverService(apiRegister publishservice.APIRegister) *DiscoverService {
40 return &DiscoverService{
41 apiRegister: apiRegister,
45 func (ds *DiscoverService) GetAllServiceAPIs(ctx echo.Context, params discoverapi.GetAllServiceAPIsParams) error {
46 allApis := *ds.apiRegister.GetAPIs()
47 filteredApis := []publishapi.ServiceAPIDescription{}
48 gatewayDomain := "r1-expo-func-aef"
49 for _, api := range allApis {
50 if !matchesFilter(api, params) {
53 profiles := *api.AefProfiles
54 for i, profile := range profiles {
55 profile.DomainName = &gatewayDomain // Hardcoded for now. Should be provided through some other mechanism.
58 filteredApis = append(filteredApis, api)
60 discoveredApis := discoverapi.DiscoveredAPIs{
61 ServiceAPIDescriptions: &filteredApis,
63 err := ctx.JSON(http.StatusOK, discoveredApis)
65 // Something really bad happened, tell Echo that our handler failed
72 func matchesFilter(api publishapi.ServiceAPIDescription, filter discoverapi.GetAllServiceAPIsParams) bool {
73 if filter.ApiName != nil && *filter.ApiName != api.ApiName {
76 if filter.ApiCat != nil && (api.ServiceAPICategory == nil || *filter.ApiCat != *api.ServiceAPICategory) {
79 profiles := *api.AefProfiles
82 dataFormatMatch := true
84 for _, profile := range profiles {
85 if filter.AefId != nil {
86 aefIdMatch = *filter.AefId == profile.AefId
88 if filter.ApiVersion != nil || filter.CommType != nil {
89 versionMatch = checkVersionAndCommType(profile, filter.ApiVersion, filter.CommType)
91 if filter.Protocol != nil {
92 protocolMatch = profile.Protocol != nil && *filter.Protocol == *profile.Protocol
94 if filter.DataFormat != nil {
95 dataFormatMatch = profile.DataFormat != nil && *filter.DataFormat == *profile.DataFormat
97 if aefIdMatch && versionMatch && protocolMatch && dataFormatMatch {
104 func checkVersionAndCommType(profile publishapi.AefProfile, wantedVersion *string, commType *publishapi.CommunicationType) bool {
106 if wantedVersion != nil {
107 for _, version := range profile.Versions {
108 match = checkVersion(version, wantedVersion, commType)
113 } else if commType != nil {
114 for _, version := range profile.Versions {
115 match = checkCommType(version.Resources, commType)
123 func checkVersion(version publishapi.Version, wantedVersion *string, commType *publishapi.CommunicationType) bool {
125 if *wantedVersion == version.ApiVersion {
127 match = checkCommType(version.Resources, commType)
135 func checkCommType(resources *[]publishapi.Resource, commType *publishapi.CommunicationType) bool {
138 for _, resource := range *resources {
139 if resource.CommType == *commType {