2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022-2023: Nordix Foundation
6 // Copyright (C) 2024: 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 publishservice
31 echo "github.com/labstack/echo/v4"
32 "k8s.io/utils/strings/slices"
34 "oransc.org/nonrtric/capifcore/internal/common29122"
35 "oransc.org/nonrtric/capifcore/internal/eventsapi"
36 publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
38 "oransc.org/nonrtric/capifcore/internal/helmmanagement"
39 "oransc.org/nonrtric/capifcore/internal/providermanagement"
41 log "github.com/sirupsen/logrus"
44 //go:generate mockery --name PublishRegister
45 type PublishRegister interface {
46 // Checks if the provided API is published.
47 // Returns true if the provided API has been published, false otherwise.
48 IsAPIPublished(aefId, path string) bool
49 // Gets all published APIs.
50 // Returns a list of all APIs that has been published.
51 GetAllPublishedServices() []publishapi.ServiceAPIDescription
52 GetAllowedPublishedServices(invokerApiList []publishapi.ServiceAPIDescription) []publishapi.ServiceAPIDescription
55 type PublishService struct {
56 publishedServices map[string][]publishapi.ServiceAPIDescription
57 serviceRegister providermanagement.ServiceRegister
58 helmManager helmmanagement.HelmManager
59 eventChannel chan<- eventsapi.EventNotification
63 // Creates a service that implements both the PublishRegister and the publishserviceapi.ServerInterface interfaces.
64 func NewPublishService(serviceRegister providermanagement.ServiceRegister, hm helmmanagement.HelmManager, eventChannel chan<- eventsapi.EventNotification) *PublishService {
65 return &PublishService{
67 publishedServices: make(map[string][]publishapi.ServiceAPIDescription),
68 serviceRegister: serviceRegister,
69 eventChannel: eventChannel,
73 func (ps *PublishService) getAllAefIds() []string {
75 defer ps.lock.Unlock()
78 for _, descriptions := range ps.publishedServices {
79 for _, description := range descriptions {
80 allIds = append(allIds, description.GetAefIds()...)
86 func (ps *PublishService) IsAPIPublished(aefId, path string) bool {
87 return slices.Contains(ps.getAllAefIds(), aefId)
90 func (ps *PublishService) GetAllPublishedServices() []publishapi.ServiceAPIDescription {
91 publishedDescriptions := []publishapi.ServiceAPIDescription{}
92 for _, descriptions := range ps.publishedServices {
93 publishedDescriptions = append(publishedDescriptions, descriptions...)
95 return publishedDescriptions
98 func (ps *PublishService) GetAllowedPublishedServices(apiListRequestedServices []publishapi.ServiceAPIDescription) []publishapi.ServiceAPIDescription {
99 apiListAllPublished := ps.GetAllPublishedServices()
100 allowedPublishedServices := join(apiListAllPublished, apiListRequestedServices)
101 return allowedPublishedServices
104 func join(a, b []publishapi.ServiceAPIDescription) []publishapi.ServiceAPIDescription {
105 var result []publishapi.ServiceAPIDescription
107 if (a == nil) || (b == nil) || (len(a) == 0) || (len(b) == 0) {
111 for _, itemA := range a {
112 for _, itemB := range b {
113 if itemA.ApiName == itemB.ApiName {
114 result = append(result, itemA)
122 // Retrieve all published APIs.
123 func (ps *PublishService) GetApfIdServiceApis(ctx echo.Context, apfId string) error {
124 if !ps.serviceRegister.IsPublishingFunctionRegistered(apfId) {
125 errorMsg := fmt.Sprintf("Unable to get the service due to %s api is only available for publishers", apfId)
126 return sendCoreError(ctx, http.StatusNotFound, errorMsg)
129 serviceDescriptions := ps.publishedServices[apfId]
130 err := ctx.JSON(http.StatusOK, serviceDescriptions)
132 // Something really bad happened, tell Echo that our handler failed
138 // Publish a new API.
139 func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) error {
140 var newServiceAPIDescription publishapi.ServiceAPIDescription
141 errorMsg := "Unable to publish the service due to %s "
142 err := ctx.Bind(&newServiceAPIDescription)
144 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, "invalid format for service "+apfId))
147 if !ps.serviceRegister.IsPublishingFunctionRegistered(apfId) {
148 return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errorMsg, "api is only available for publishers "+apfId))
151 if err := ps.isServicePublished(newServiceAPIDescription); err != nil {
152 return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errorMsg, err))
155 if err := newServiceAPIDescription.Validate(); err != nil {
156 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, err))
159 defer ps.lock.Unlock()
161 registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId)
162 for _, profile := range *newServiceAPIDescription.AefProfiles {
163 if !slices.Contains(registeredFuncs, profile.AefId) {
164 return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf(errorMsg, fmt.Sprintf("function %s not registered", profile.AefId)))
168 newServiceAPIDescription.PrepareNewService()
170 shouldReturn, returnValue := ps.installHelmChart(newServiceAPIDescription, ctx)
174 go ps.sendEvent(newServiceAPIDescription, eventsapi.CAPIFEventSERVICEAPIAVAILABLE)
176 _, ok := ps.publishedServices[apfId]
178 ps.publishedServices[apfId] = append(ps.publishedServices[apfId], newServiceAPIDescription)
180 ps.publishedServices[apfId] = append([]publishapi.ServiceAPIDescription{}, newServiceAPIDescription)
183 uri := ctx.Request().Host + ctx.Request().URL.String()
184 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, *newServiceAPIDescription.ApiId))
185 err = ctx.JSON(http.StatusCreated, newServiceAPIDescription)
187 // Something really bad happened, tell Echo that our handler failed
194 func (ps *PublishService) isServicePublished(newService publishapi.ServiceAPIDescription) error {
195 for _, services := range ps.publishedServices {
196 for _, service := range services {
197 if err := service.ValidateAlreadyPublished(newService); err != nil {
205 func (ps *PublishService) installHelmChart(newServiceAPIDescription publishapi.ServiceAPIDescription, ctx echo.Context) (bool, error) {
206 info := strings.Split(*newServiceAPIDescription.Description, ",")
208 err := ps.helmManager.InstallHelmChart(info[1], info[2], info[3], info[4])
210 return true, sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf("Unable to install Helm chart %s due to: %s", info[3], err.Error()))
212 log.Debug("Installed service: ", newServiceAPIDescription.ApiId)
217 // Unpublish a published service API.
218 func (ps *PublishService) DeleteApfIdServiceApisServiceApiId(ctx echo.Context, apfId string, serviceApiId string) error {
219 serviceDescriptions, ok := ps.publishedServices[string(apfId)]
221 pos, description := getServiceDescription(serviceApiId, serviceDescriptions)
222 if description != nil {
223 info := strings.Split(*description.Description, ",")
225 ps.helmManager.UninstallHelmChart(info[1], info[3])
226 log.Debug("Deleted service: ", serviceApiId)
229 ps.publishedServices[string(apfId)] = removeServiceDescription(pos, serviceDescriptions)
231 go ps.sendEvent(*description, eventsapi.CAPIFEventSERVICEAPIUNAVAILABLE)
234 return ctx.NoContent(http.StatusNoContent)
237 // Retrieve a published service API.
238 func (ps *PublishService) GetApfIdServiceApisServiceApiId(ctx echo.Context, apfId string, serviceApiId string) error {
240 serviceDescriptions, ok := ps.publishedServices[apfId]
244 _, serviceDescription := getServiceDescription(serviceApiId, serviceDescriptions)
245 if serviceDescription == nil {
246 return ctx.NoContent(http.StatusNotFound)
248 err := ctx.JSON(http.StatusOK, serviceDescription)
250 // Something really bad happened, tell Echo that our handler failed
256 return ctx.NoContent(http.StatusNotFound)
259 func getServiceDescription(serviceApiId string, descriptions []publishapi.ServiceAPIDescription) (int, *publishapi.ServiceAPIDescription) {
260 for pos, description := range descriptions {
261 // Check for nil as we had a failure here when running unit tests in parallel against a single Capifcore instance
262 if (description.ApiId != nil) && (serviceApiId == *description.ApiId) {
263 return pos, &description
269 func removeServiceDescription(i int, a []publishapi.ServiceAPIDescription) []publishapi.ServiceAPIDescription {
270 a[i] = a[len(a)-1] // Copy last element to index i.
271 a[len(a)-1] = publishapi.ServiceAPIDescription{} // Erase last element (write zero value).
272 a = a[:len(a)-1] // Truncate slice.
276 // Modify an existing published service API.
277 func (ps *PublishService) ModifyIndAPFPubAPI(ctx echo.Context, apfId string, serviceApiId string) error {
278 return ctx.NoContent(http.StatusNotImplemented)
281 // Update a published service API.
282 func (ps *PublishService) PutApfIdServiceApisServiceApiId(ctx echo.Context, apfId string, serviceApiId string) error {
284 defer ps.lock.Unlock()
285 errMsg := "Unable to update service due to %s."
287 pos, publishedService, err := ps.checkIfServiceIsPublished(apfId, serviceApiId)
289 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
292 updatedServiceDescription, err := getServiceFromRequest(ctx)
294 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
297 // Additional validation for PUT
298 if (updatedServiceDescription.ApiId == nil) || (*updatedServiceDescription.ApiId != serviceApiId) {
299 errDetail := "ServiceAPIDescription ApiId doesn't match path parameter"
300 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, errDetail))
303 err = ps.checkProfilesRegistered(apfId, *updatedServiceDescription.AefProfiles)
305 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
308 ps.updateDescription(&updatedServiceDescription, &publishedService)
310 publishedService.AefProfiles = updatedServiceDescription.AefProfiles
311 ps.publishedServices[apfId][pos] = publishedService
313 err = ctx.JSON(http.StatusOK, publishedService)
315 // Something really bad happened, tell Echo that our handler failed
321 func (ps *PublishService) checkIfServiceIsPublished(apfId string, serviceApiId string) (int, publishapi.ServiceAPIDescription, error) {
322 publishedServices, ok := ps.publishedServices[apfId]
324 return 0, publishapi.ServiceAPIDescription{}, fmt.Errorf("service must be published before updating it")
326 for pos, description := range publishedServices {
327 if *description.ApiId == serviceApiId {
328 return pos, description, nil
332 return 0, publishapi.ServiceAPIDescription{}, fmt.Errorf("service must be published before updating it")
335 func getServiceFromRequest(ctx echo.Context) (publishapi.ServiceAPIDescription, error) {
336 var updatedServiceDescription publishapi.ServiceAPIDescription
337 err := ctx.Bind(&updatedServiceDescription)
339 return publishapi.ServiceAPIDescription{}, fmt.Errorf("invalid format for service")
341 return updatedServiceDescription, nil
344 func (ps *PublishService) updateDescription(updatedServiceDescription, publishedService *publishapi.ServiceAPIDescription) {
345 if updatedServiceDescription.Description != nil {
346 publishedService.Description = updatedServiceDescription.Description
347 go ps.sendEvent(*publishedService, eventsapi.CAPIFEventSERVICEAPIUPDATE)
351 func (ps *PublishService) sendEvent(service publishapi.ServiceAPIDescription, eventType eventsapi.CAPIFEvent) {
352 apiIds := []string{*service.ApiId}
353 apis := []publishapi.ServiceAPIDescription{service}
354 event := eventsapi.EventNotification{
355 EventDetail: &eventsapi.CAPIFEventDetail{
357 ServiceAPIDescriptions: &apis,
361 ps.eventChannel <- event
364 func (ps *PublishService) checkProfilesRegistered(apfId string, updatedProfiles []publishapi.AefProfile) error {
365 registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId)
366 for _, profile := range updatedProfiles {
367 if !slices.Contains(registeredFuncs, profile.AefId) {
368 return fmt.Errorf("function %s not registered", profile.AefId)
374 // This function wraps sending of an error in the Error format, and
375 // handling the failure to marshal that.
376 func sendCoreError(ctx echo.Context, code int, message string) error {
377 pd := common29122.ProblemDetails{
381 err := ctx.JSON(code, pd)