From: elinuxhenrik Date: Tue, 13 Dec 2022 15:49:52 +0000 (+0100) Subject: Add apiInvokerId filtering for eventservice X-Git-Tag: 1.0.0~2 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=0793c36cf98a3f1858c3b314718d97e094376ddf;p=nonrtric%2Fplt%2Fsme.git Add apiInvokerId filtering for eventservice Issue-ID: NONRTRIC-814 Signed-off-by: elinuxhenrik Change-Id: I121958e4c4b84beed5816acf0e93250d5d1034bc --- diff --git a/capifcore/internal/eventservice/eventservice.go b/capifcore/internal/eventservice/eventservice.go index 1bc910b..a4fa079 100644 --- a/capifcore/internal/eventservice/eventservice.go +++ b/capifcore/internal/eventservice/eventservice.go @@ -134,7 +134,8 @@ func (es *EventService) getMatchingSubs(event eventsapi.EventNotification) []str matchingSubs = append(matchingSubs, subId) break } - if matchesApiIds(*event.EventDetail.ApiIds, *subscription.EventFilters) { + if matchesFilters(*event.EventDetail.ApiIds, *subscription.EventFilters, getApiIdsFromFilter) && + matchesFilters(*event.EventDetail.ApiInvokerIds, *subscription.EventFilters, getInvokerIdsFromFilter) { matchingSubs = append(matchingSubs, subId) } } @@ -152,20 +153,27 @@ func (es *EventService) filterOnEventType(event eventsapi.EventNotification) []s return matchingSubs } -func matchesApiIds(eventIds []string, filters []eventsapi.CAPIFEventFilter) bool { +func matchesFilters(eventIds []string, filters []eventsapi.CAPIFEventFilter, getIds func(eventsapi.CAPIFEventFilter) *[]string) bool { if len(filters) == 0 { return true } - for _, apiId := range eventIds { + for _, id := range eventIds { filter := filters[0] - if filter.ApiIds == nil { - return true && matchesApiIds(eventIds, filters[1:]) + if getIds(filter) == nil { + return matchesFilters(eventIds, filters[1:], getIds) } - return slices.Contains(*filter.ApiIds, apiId) && matchesApiIds(eventIds, filters[1:]) + return slices.Contains(*getIds(filter), id) && matchesFilters(eventIds, filters[1:], getIds) } return true } +func getApiIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string { + return filter.ApiIds +} +func getInvokerIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string { + return filter.ApiInvokerIds +} + func asStrings(events []eventsapi.CAPIFEvent) []string { asStrings := make([]string, len(events)) for i, event := range events { diff --git a/capifcore/internal/eventservice/eventservice_test.go b/capifcore/internal/eventservice/eventservice_test.go index f2acb9c..bcc8c95 100644 --- a/capifcore/internal/eventservice/eventservice_test.go +++ b/capifcore/internal/eventservice/eventservice_test.go @@ -170,33 +170,58 @@ func TestMatchEventType(t *testing.T) { Events: eventsapi.CAPIFEventSERVICEAPIAVAILABLE, } - matchingSubs := serviceUnderTest.getMatchingSubs(event) + matchingSubs := serviceUnderTest.filterOnEventType(event) assert.Len(t, matchingSubs, 1) assert.Equal(t, subId, matchingSubs[0]) } -func TestMatchesApiIds(t *testing.T) { - apiId := "apiId" - apiIds := []string{apiId, "otherApiId"} - eventFilters := []eventsapi.CAPIFEventFilter{ - {}, - { - ApiIds: &apiIds, +func TestMatchEventTypeAndFilters(t *testing.T) { + notificationUrl := "url" + subId := "sub1" + apiIds := []string{"apiId"} + invokerIds := []string{"invokerId"} + serviceUnderTest := NewEventService(nil) + serviceUnderTest.addSubscription(subId, eventsapi.EventSubscription{ + Events: []eventsapi.CAPIFEvent{ + eventsapi.CAPIFEventSERVICEAPIAVAILABLE, + }, + NotificationDestination: common29122.Uri(notificationUrl), + EventFilters: &[]eventsapi.CAPIFEventFilter{ + { + ApiIds: &apiIds, + ApiInvokerIds: &invokerIds, + }, + }, + }) + serviceUnderTest.addSubscription("other", eventsapi.EventSubscription{ + Events: []eventsapi.CAPIFEvent{ + eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE, + }, + NotificationDestination: common29122.Uri(notificationUrl), + }) + + event := eventsapi.EventNotification{ + Events: eventsapi.CAPIFEventSERVICEAPIAVAILABLE, + EventDetail: &eventsapi.CAPIFEventDetail{ + ApiIds: &apiIds, + ApiInvokerIds: &invokerIds, }, } - eventApiIds := []string{apiId} - assert.True(t, matchesApiIds(eventApiIds, eventFilters)) - assert.True(t, matchesApiIds(nil, eventFilters)) + matchingSubs := serviceUnderTest.getMatchingSubs(event) + assert.Len(t, matchingSubs, 1) + assert.Equal(t, subId, matchingSubs[0]) - altApiIds := []string{"anotherApiId"} - unMatchingFilterAdded := append(eventFilters, eventsapi.CAPIFEventFilter{ - ApiIds: &altApiIds, - }) - assert.False(t, matchesApiIds(eventApiIds, unMatchingFilterAdded)) + otherApiIds := []string{"otherApiId"} + (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiIds = &otherApiIds + matchingSubs = serviceUnderTest.getMatchingSubs(event) + assert.Len(t, matchingSubs, 0) - apiIds[0] = "anotherId" - assert.False(t, matchesApiIds(eventApiIds, eventFilters)) + otherInvokerIds := []string{"otherInvokerId"} + (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiIds = &apiIds + (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiInvokerIds = &otherInvokerIds + matchingSubs = serviceUnderTest.getMatchingSubs(event) + assert.Len(t, matchingSubs, 0) } func getEcho(client restclient.HTTPClient) (*EventService, *echo.Echo) {