Add generated code
[nonrtric/plt/sme.git] / internal / discoverservice / discoverservice.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation
6 //   %%
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
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
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===================================
19 //
20
21 package discoverservice
22
23 import (
24         "net/http"
25
26         discoverapi "oransc.org/nonrtric/sme/internal/discoverserviceapi"
27
28         "oransc.org/nonrtric/sme/internal/publishservice"
29
30         "github.com/labstack/echo/v4"
31
32         publishapi "oransc.org/nonrtric/sme/internal/publishserviceapi"
33 )
34
35 type DiscoverService struct {
36         apiRegister publishservice.APIRegister
37 }
38
39 func NewDiscoverService(apiRegister publishservice.APIRegister) *DiscoverService {
40         return &DiscoverService{
41                 apiRegister: apiRegister,
42         }
43 }
44
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) {
51                         continue
52                 }
53                 profiles := *api.AefProfiles
54                 for i, profile := range profiles {
55                         profile.DomainName = &gatewayDomain // Hardcoded for now. Should be provided through some other mechanism.
56                         profiles[i] = profile
57                 }
58                 filteredApis = append(filteredApis, api)
59         }
60         discoveredApis := discoverapi.DiscoveredAPIs{
61                 ServiceAPIDescriptions: &filteredApis,
62         }
63         err := ctx.JSON(http.StatusOK, discoveredApis)
64         if err != nil {
65                 // Something really bad happened, tell Echo that our handler failed
66                 return err
67         }
68
69         return nil
70 }
71
72 func matchesFilter(api publishapi.ServiceAPIDescription, filter discoverapi.GetAllServiceAPIsParams) bool {
73         if filter.ApiName != nil && *filter.ApiName != api.ApiName {
74                 return false
75         }
76         profiles := *api.AefProfiles
77         match := false
78         for _, profile := range profiles {
79                 if filter.ApiVersion != nil {
80                         for _, version := range profile.Versions {
81                                 if *filter.ApiVersion == version.ApiVersion {
82                                         match = true
83                                 }
84                         }
85                 } else {
86                         match = true
87                 }
88         }
89         return match
90 }