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 "oransc.org/nonrtric/capifcore/internal/common29122"
27 discoverapi "oransc.org/nonrtric/capifcore/internal/discoverserviceapi"
28 "oransc.org/nonrtric/capifcore/internal/invokermanagement"
30 "github.com/labstack/echo/v4"
32 publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
35 type DiscoverService struct {
36 invokerRegister invokermanagement.InvokerRegister
39 func NewDiscoverService(invokerRegister invokermanagement.InvokerRegister) *DiscoverService {
40 return &DiscoverService{
41 invokerRegister: invokerRegister,
45 func (ds *DiscoverService) GetAllServiceAPIs(ctx echo.Context, params discoverapi.GetAllServiceAPIsParams) error {
46 allApis := ds.invokerRegister.GetInvokerApiList(params.ApiInvokerId)
48 return sendCoreError(ctx, http.StatusNotFound, "Invoker not registered")
50 filteredApis := []publishapi.ServiceAPIDescription{}
51 gatewayDomain := "r1-expo-func-aef"
52 for _, api := range *allApis {
53 if !matchesFilter(api, params) {
56 profiles := *api.AefProfiles
57 for i, profile := range profiles {
58 profile.DomainName = &gatewayDomain // Hardcoded for now. Should be provided through some other mechanism.
61 filteredApis = append(filteredApis, api)
63 discoveredApis := discoverapi.DiscoveredAPIs{
64 ServiceAPIDescriptions: &filteredApis,
66 err := ctx.JSON(http.StatusOK, discoveredApis)
68 // Something really bad happened, tell Echo that our handler failed
75 func matchesFilter(api publishapi.ServiceAPIDescription, filter discoverapi.GetAllServiceAPIsParams) bool {
76 if filter.ApiName != nil && *filter.ApiName != api.ApiName {
79 if filter.ApiCat != nil && (api.ServiceAPICategory == nil || *filter.ApiCat != *api.ServiceAPICategory) {
82 profiles := *api.AefProfiles
83 for _, profile := range profiles {
84 if checkAefId(filter, profile) && checkVersionAndCommType(profile, filter) && checkProtocol(filter, profile) && checkDataFormat(filter, profile) {
91 func checkAefId(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool {
92 if filter.AefId != nil {
93 return *filter.AefId == profile.AefId
98 func checkVersionAndCommType(profile publishapi.AefProfile, filter discoverapi.GetAllServiceAPIsParams) bool {
100 if filter.ApiVersion != nil {
101 for _, version := range profile.Versions {
102 match = checkVersion(version, filter.ApiVersion, filter.CommType)
107 } else if filter.CommType != nil {
108 for _, version := range profile.Versions {
109 match = checkCommType(version.Resources, filter.CommType)
117 func checkProtocol(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool {
118 if filter.Protocol != nil {
119 return profile.Protocol != nil && *filter.Protocol == *profile.Protocol
124 func checkDataFormat(filter discoverapi.GetAllServiceAPIsParams, profile publishapi.AefProfile) bool {
125 if filter.DataFormat != nil {
126 return profile.DataFormat != nil && *filter.DataFormat == *profile.DataFormat
131 func checkVersion(version publishapi.Version, wantedVersion *string, commType *publishapi.CommunicationType) bool {
133 if *wantedVersion == version.ApiVersion {
135 match = checkCommType(version.Resources, commType)
143 func checkCommType(resources *[]publishapi.Resource, commType *publishapi.CommunicationType) bool {
144 for _, resource := range *resources {
145 if resource.CommType == *commType {
152 // This function wraps sending of an error in the Error format, and
153 // handling the failure to marshal that.
154 func sendCoreError(ctx echo.Context, code int, message string) error {
155 pd := common29122.ProblemDetails{
159 err := ctx.JSON(code, pd)