NONRTRIC-946: Get Allowed Publishers 67/12567/1
authorDenisGNoonan <denis.noonan@est.tech>
Fri, 23 Feb 2024 11:00:18 +0000 (11:00 +0000)
committerDenisGNoonan <denis.noonan@est.tech>
Fri, 23 Feb 2024 11:15:19 +0000 (11:15 +0000)
Issue-ID: NONRTRIC-946
Change-Id: Ie906836a0a2f27d13e4f55ff19ba6572460b5b24
Signed-off-by: DenisGNoonan <denis.noonan@est.tech>
capifcore/internal/invokermanagement/invokermanagement.go
capifcore/internal/invokermanagement/invokermanagement_test.go
capifcore/internal/publishservice/mocks/PublishRegister.go
capifcore/internal/publishservice/publishservice.go
capifcore/internal/publishservice/publishservice_test.go

index f0a4155..ae90164 100644 (file)
@@ -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
 }
 
index b46e925..ae79c90 100644 (file)
@@ -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")
index 2efd373..97cbd43 100644 (file)
@@ -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)
index 0fa125f..372b1f8 100644 (file)
@@ -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) {
index 670f273..e63243f 100644 (file)
@@ -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"