Add aefId filtering for eventservice 25/10125/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 14 Dec 2022 08:02:14 +0000 (09:02 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 14 Dec 2022 08:02:19 +0000 (09:02 +0100)
Issue-ID: NONRTRIC-814
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: Id9ec6a5492193b812ab4478a6e157cf45be8fe6b

capifcore/internal/eventservice/eventservice.go
capifcore/internal/eventservice/eventservice_test.go

index a4fa079..0f7a8e8 100644 (file)
@@ -33,6 +33,7 @@ import (
        "k8s.io/utils/strings/slices"
        "oransc.org/nonrtric/capifcore/internal/common29122"
        "oransc.org/nonrtric/capifcore/internal/eventsapi"
+       "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
        "oransc.org/nonrtric/capifcore/internal/restclient"
 )
 
@@ -132,10 +133,9 @@ func (es *EventService) getMatchingSubs(event eventsapi.EventNotification) []str
                subscription := es.subscriptions[subId]
                if subscription.EventFilters == nil || event.EventDetail == nil {
                        matchingSubs = append(matchingSubs, subId)
-                       break
-               }
-               if matchesFilters(*event.EventDetail.ApiIds, *subscription.EventFilters, getApiIdsFromFilter) &&
-                       matchesFilters(*event.EventDetail.ApiInvokerIds, *subscription.EventFilters, getInvokerIdsFromFilter) {
+               } else if matchesFilters(event.EventDetail.ApiIds, *subscription.EventFilters, getApiIdsFromFilter) &&
+                       matchesFilters(event.EventDetail.ApiInvokerIds, *subscription.EventFilters, getInvokerIdsFromFilter) &&
+                       matchesFilters(getAefIdsFromEvent(event.EventDetail.ServiceAPIDescriptions), *subscription.EventFilters, getAefIdsFromFilter) {
                        matchingSubs = append(matchingSubs, subId)
                }
        }
@@ -153,13 +153,14 @@ func (es *EventService) filterOnEventType(event eventsapi.EventNotification) []s
        return matchingSubs
 }
 
-func matchesFilters(eventIds []string, filters []eventsapi.CAPIFEventFilter, getIds func(eventsapi.CAPIFEventFilter) *[]string) bool {
-       if len(filters) == 0 {
+func matchesFilters(eventIds *[]string, filters []eventsapi.CAPIFEventFilter, getIds func(eventsapi.CAPIFEventFilter) *[]string) bool {
+       if len(filters) == 0 || eventIds == nil {
                return true
        }
-       for _, id := range eventIds {
+       for _, id := range *eventIds {
                filter := filters[0]
-               if getIds(filter) == nil {
+               filterIds := getIds(filter)
+               if filterIds == nil || len(*filterIds) == 0 {
                        return matchesFilters(eventIds, filters[1:], getIds)
                }
                return slices.Contains(*getIds(filter), id) && matchesFilters(eventIds, filters[1:], getIds)
@@ -170,10 +171,31 @@ func matchesFilters(eventIds []string, filters []eventsapi.CAPIFEventFilter, get
 func getApiIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string {
        return filter.ApiIds
 }
+
 func getInvokerIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string {
        return filter.ApiInvokerIds
 }
 
+func getAefIdsFromEvent(serviceAPIDescriptions *[]publishserviceapi.ServiceAPIDescription) *[]string {
+       aefIds := []string{}
+       if serviceAPIDescriptions == nil {
+               return &aefIds
+       }
+       for _, serviceDescription := range *serviceAPIDescriptions {
+               if serviceDescription.AefProfiles == nil {
+                       return &aefIds
+               }
+               for _, profile := range *serviceDescription.AefProfiles {
+                       aefIds = append(aefIds, profile.AefId)
+               }
+       }
+       return &aefIds
+}
+
+func getAefIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string {
+       return filter.AefIds
+}
+
 func asStrings(events []eventsapi.CAPIFEvent) []string {
        asStrings := make([]string, len(events))
        for i, event := range events {
index bcc8c95..a328e57 100644 (file)
@@ -39,6 +39,7 @@ import (
        "github.com/stretchr/testify/assert"
        "oransc.org/nonrtric/capifcore/internal/common29122"
        "oransc.org/nonrtric/capifcore/internal/eventsapi"
+       "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
        "oransc.org/nonrtric/capifcore/internal/restclient"
 )
 
@@ -176,52 +177,95 @@ func TestMatchEventType(t *testing.T) {
 }
 
 func TestMatchEventTypeAndFilters(t *testing.T) {
-       notificationUrl := "url"
        subId := "sub1"
        apiIds := []string{"apiId"}
        invokerIds := []string{"invokerId"}
+       aefId := "aefId"
+       aefIds := []string{aefId}
        serviceUnderTest := NewEventService(nil)
        serviceUnderTest.addSubscription(subId, eventsapi.EventSubscription{
                Events: []eventsapi.CAPIFEvent{
                        eventsapi.CAPIFEventSERVICEAPIAVAILABLE,
                },
-               NotificationDestination: common29122.Uri(notificationUrl),
                EventFilters: &[]eventsapi.CAPIFEventFilter{
                        {
                                ApiIds:        &apiIds,
                                ApiInvokerIds: &invokerIds,
+                               AefIds:        &aefIds,
                        },
                },
        })
+       serviceUnderTest.addSubscription("otherSameType", eventsapi.EventSubscription{
+               Events: []eventsapi.CAPIFEvent{
+                       eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE,
+               },
+       })
        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,
-               },
+               Events: eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE,
        }
 
+       // Only match type
        matchingSubs := serviceUnderTest.getMatchingSubs(event)
+       assert.Len(t, matchingSubs, 2)
+
+       // Match with all filter ids
+       aefProfiles := []publishserviceapi.AefProfile{
+               {
+                       AefId: aefId,
+               },
+       }
+       serviceDescriptions := []publishserviceapi.ServiceAPIDescription{
+               {
+                       AefProfiles: &aefProfiles,
+               },
+       }
+       event.Events = eventsapi.CAPIFEventSERVICEAPIAVAILABLE
+       event.EventDetail = &eventsapi.CAPIFEventDetail{
+               ApiIds:                 &apiIds,
+               ApiInvokerIds:          &invokerIds,
+               ServiceAPIDescriptions: &serviceDescriptions,
+       }
+       matchingSubs = serviceUnderTest.getMatchingSubs(event)
        assert.Len(t, matchingSubs, 1)
        assert.Equal(t, subId, matchingSubs[0])
 
+       // Un match apiId
        otherApiIds := []string{"otherApiId"}
        (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiIds = &otherApiIds
        matchingSubs = serviceUnderTest.getMatchingSubs(event)
        assert.Len(t, matchingSubs, 0)
 
+       // Un match invokerId
        otherInvokerIds := []string{"otherInvokerId"}
-       (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiIds = &apiIds
+       (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiIds = nil
        (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiInvokerIds = &otherInvokerIds
        matchingSubs = serviceUnderTest.getMatchingSubs(event)
        assert.Len(t, matchingSubs, 0)
+
+       // Un match aefId
+       otherAefIds := []string{"otherAefId"}
+       (*serviceUnderTest.subscriptions[subId].EventFilters)[0].ApiInvokerIds = nil
+       (*serviceUnderTest.subscriptions[subId].EventFilters)[0].AefIds = &otherAefIds
+       matchingSubs = serviceUnderTest.getMatchingSubs(event)
+       assert.Len(t, matchingSubs, 0)
+
+       // Match with empty subscription filter id list
+       (*serviceUnderTest.subscriptions[subId].EventFilters)[0].AefIds = &[]string{}
+       matchingSubs = serviceUnderTest.getMatchingSubs(event)
+       assert.Len(t, matchingSubs, 1)
+
+       // Match with empty event id list
+       event.EventDetail.ApiIds = nil
+       event.EventDetail.ApiInvokerIds = nil
+       event.EventDetail.ServiceAPIDescriptions = &[]publishserviceapi.ServiceAPIDescription{}
+       matchingSubs = serviceUnderTest.getMatchingSubs(event)
+       assert.Len(t, matchingSubs, 1)
 }
 
 func getEcho(client restclient.HTTPClient) (*EventService, *echo.Echo) {