From b70522851289af2045d3d0c950b053b5673705cd Mon Sep 17 00:00:00 2001 From: DenisGNoonan Date: Fri, 23 Feb 2024 11:00:18 +0000 Subject: [PATCH] NONRTRIC-946: Get Allowed Publishers Issue-ID: NONRTRIC-946 Change-Id: Ie906836a0a2f27d13e4f55ff19ba6572460b5b24 Signed-off-by: DenisGNoonan --- .../invokermanagement/invokermanagement.go | 13 +++-- .../invokermanagement/invokermanagement_test.go | 9 ++-- .../publishservice/mocks/PublishRegister.go | 16 ++++++ .../internal/publishservice/publishservice.go | 24 +++++++++ .../internal/publishservice/publishservice_test.go | 58 ++++++++++++++++++++++ 5 files changed, 112 insertions(+), 8 deletions(-) diff --git a/capifcore/internal/invokermanagement/invokermanagement.go b/capifcore/internal/invokermanagement/invokermanagement.go index f0a4155..ae90164 100644 --- a/capifcore/internal/invokermanagement/invokermanagement.go +++ b/capifcore/internal/invokermanagement/invokermanagement.go @@ -2,7 +2,8 @@ // ========================LICENSE_START================================= // O-RAN-SC // %% -// Copyright (C) 2022: Nordix Foundation +// Copyright (C) 2022-2023: Nordix Foundation +// Copyright (C) 2024: OpenInfra Foundation Europe // %% // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -143,16 +144,18 @@ func (im *InvokerManager) isInvokerOnboarded(newInvoker invokerapi.APIInvokerEnr } func (im *InvokerManager) prepareNewInvoker(newInvoker *invokerapi.APIInvokerEnrolmentDetails) { - var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices() - newInvoker.ApiList = &apiList + var apiListRequestedServices invokerapi.APIList = nil + if newInvoker.ApiList != nil { + apiListRequestedServices = *newInvoker.ApiList + } + var allowedPublishedServices invokerapi.APIList = im.publishRegister.GetAllowedPublishedServices(apiListRequestedServices) + newInvoker.ApiList = &allowedPublishedServices im.lock.Lock() defer im.lock.Unlock() newInvoker.PrepareNewInvoker() - im.addClientInKeycloak(newInvoker) - im.onboardedInvokers[*newInvoker.ApiInvokerId] = *newInvoker } diff --git a/capifcore/internal/invokermanagement/invokermanagement_test.go b/capifcore/internal/invokermanagement/invokermanagement_test.go index b46e925..ae79c90 100644 --- a/capifcore/internal/invokermanagement/invokermanagement_test.go +++ b/capifcore/internal/invokermanagement/invokermanagement_test.go @@ -3,7 +3,8 @@ // ========================LICENSE_START================================= // O-RAN-SC // %% -// Copyright (C) 2022: Nordix Foundation +// Copyright (C) 2022-2023: Nordix Foundation +// Copyright (C) 2024: OpenInfra Foundation Europe // %% // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -64,7 +65,7 @@ func TestOnboardInvoker(t *testing.T) { var client keycloak.Client client.Secret = &wantedInvokerSecret publishRegisterMock := publishmocks.PublishRegister{} - publishRegisterMock.On("GetAllPublishedServices").Return(publishedServices) + publishRegisterMock.On("GetAllowedPublishedServices", mock.AnythingOfType("[]publishserviceapi.ServiceAPIDescription")).Return(publishedServices) accessMgmMock := keycloackmocks.AccessManagement{} accessMgmMock.On("AddClient", mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(nil) @@ -90,7 +91,9 @@ func TestOnboardInvoker(t *testing.T) { assert.Equal(t, "http://example.com/onboardedInvokers/"+*resultInvoker.ApiInvokerId, result.Recorder.Header().Get(echo.HeaderLocation)) assert.True(t, invokerUnderTest.IsInvokerRegistered(wantedInvokerId)) assert.True(t, invokerUnderTest.VerifyInvokerSecret(wantedInvokerId, wantedInvokerSecret)) - publishRegisterMock.AssertCalled(t, "GetAllPublishedServices") + + publishRegisterMock.AssertCalled(t, "GetAllowedPublishedServices", mock.AnythingOfType("[]publishserviceapi.ServiceAPIDescription")) + assert.Equal(t, invokermanagementapi.APIList(publishedServices), *resultInvoker.ApiList) if invokerEvent, timeout := waitForEvent(eventChannel, 1*time.Second); timeout { assert.Fail(t, "No event sent") diff --git a/capifcore/internal/publishservice/mocks/PublishRegister.go b/capifcore/internal/publishservice/mocks/PublishRegister.go index 2efd373..97cbd43 100644 --- a/capifcore/internal/publishservice/mocks/PublishRegister.go +++ b/capifcore/internal/publishservice/mocks/PublishRegister.go @@ -29,6 +29,22 @@ func (_m *PublishRegister) GetAllPublishedServices() []publishserviceapi.Service return r0 } +// GetAllowedPublishedServices provides a mock function with given fields: invokerApiList +func (_m *PublishRegister) GetAllowedPublishedServices(invokerApiList []publishserviceapi.ServiceAPIDescription) []publishserviceapi.ServiceAPIDescription { + ret := _m.Called(invokerApiList) + + var r0 []publishserviceapi.ServiceAPIDescription + if rf, ok := ret.Get(0).(func([]publishserviceapi.ServiceAPIDescription) []publishserviceapi.ServiceAPIDescription); ok { + r0 = rf(invokerApiList) + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).([]publishserviceapi.ServiceAPIDescription) + } + } + + return r0 +} + // IsAPIPublished provides a mock function with given fields: aefId, path func (_m *PublishRegister) IsAPIPublished(aefId string, path string) bool { ret := _m.Called(aefId, path) diff --git a/capifcore/internal/publishservice/publishservice.go b/capifcore/internal/publishservice/publishservice.go index 0fa125f..372b1f8 100644 --- a/capifcore/internal/publishservice/publishservice.go +++ b/capifcore/internal/publishservice/publishservice.go @@ -49,6 +49,7 @@ type PublishRegister interface { // Gets all published APIs. // Returns a list of all APIs that has been published. GetAllPublishedServices() []publishapi.ServiceAPIDescription + GetAllowedPublishedServices(invokerApiList []publishapi.ServiceAPIDescription) []publishapi.ServiceAPIDescription } type PublishService struct { @@ -94,6 +95,29 @@ func (ps *PublishService) GetAllPublishedServices() []publishapi.ServiceAPIDescr return publishedDescriptions } +func (ps *PublishService) GetAllowedPublishedServices(apiListRequestedServices []publishapi.ServiceAPIDescription) []publishapi.ServiceAPIDescription { + apiListAllPublished := ps.GetAllPublishedServices() + if apiListRequestedServices != nil { + allowedPublishedServices := intersection(apiListAllPublished, apiListRequestedServices) + return allowedPublishedServices + } + return []publishapi.ServiceAPIDescription{} +} + +func intersection(a, b []publishapi.ServiceAPIDescription) []publishapi.ServiceAPIDescription { + var result []publishapi.ServiceAPIDescription + + for _, itemA := range a { + for _, itemB := range b { + if *itemA.ApiId == *itemB.ApiId { + result = append(result, itemA) + break + } + } + } + return result +} + // Retrieve all published APIs. func (ps *PublishService) GetApfIdServiceApis(ctx echo.Context, apfId string) error { if !ps.serviceRegister.IsPublishingFunctionRegistered(apfId) { diff --git a/capifcore/internal/publishservice/publishservice_test.go b/capifcore/internal/publishservice/publishservice_test.go index 670f273..e63243f 100644 --- a/capifcore/internal/publishservice/publishservice_test.go +++ b/capifcore/internal/publishservice/publishservice_test.go @@ -259,6 +259,64 @@ func TestGetPublishedServices(t *testing.T) { assert.Len(t, result, 2) } +func TestGetAllowedServices(t *testing.T) { + serviceUnderTest := NewPublishService(nil, nil, nil) + + aefProfiles1 := []publishapi.AefProfile{ + } + apiId1 := "apiId1" + aefProfiles2 := []publishapi.AefProfile{ + } + apiId2 := "apiId2" + aefProfiles3 := []publishapi.AefProfile{ + } + apiId3 := "apiId3" + aefProfiles4 := []publishapi.AefProfile{ + } + apiId4 := "apiId4" + + serviceUnderTest.publishedServices["publisher1"] = []publishapi.ServiceAPIDescription{ + { + ApiId: &apiId1, + AefProfiles: &aefProfiles1, + }, + { + ApiId: &apiId2, + AefProfiles: &aefProfiles2, + }, + { + ApiId: &apiId3, + AefProfiles: &aefProfiles3, + }, + { + ApiId: &apiId4, + AefProfiles: &aefProfiles4, + }, + } + + serviceDescription := publishapi.ServiceAPIDescription{ + ApiId: &apiId4, + AefProfiles: &aefProfiles4, + } + serviceUnderTest.publishedServices["publisher2"] = []publishapi.ServiceAPIDescription{ + serviceDescription, + } + + allowedApiList := []publishapi.ServiceAPIDescription{ + { + ApiId: &apiId2, + AefProfiles: &aefProfiles2, + }, + { + ApiId: &apiId3, + AefProfiles: &aefProfiles3, + }, + } + + result := serviceUnderTest.GetAllowedPublishedServices(allowedApiList) + assert.Len(t, result, 2) +} + func TestUpdateDescription(t *testing.T) { apfId := "apfId" serviceApiId := "serviceApiId" -- 2.16.6