Add apiId filtering for eventservice 69/10069/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 12 Dec 2022 12:01:00 +0000 (13:01 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Mon, 12 Dec 2022 12:01:05 +0000 (13:01 +0100)
Issue-ID: NONRTRIC-814
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: Ie49fae90fef8fe41ac15e2fd481bdbfc23c917b4

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

index d82b63d..1bc910b 100644 (file)
@@ -124,9 +124,26 @@ func (es *EventService) sendEvent(event eventsapi.EventNotification, subscriptio
 }
 
 func (es *EventService) getMatchingSubs(event eventsapi.EventNotification) []string {
-       matchingSubs := []string{}
        es.lock.Lock()
        defer es.lock.Unlock()
+       matchingTypeSubs := es.filterOnEventType(event)
+       matchingSubs := []string{}
+       for _, subId := range matchingTypeSubs {
+               subscription := es.subscriptions[subId]
+               if subscription.EventFilters == nil || event.EventDetail == nil {
+                       matchingSubs = append(matchingSubs, subId)
+                       break
+               }
+               if matchesApiIds(*event.EventDetail.ApiIds, *subscription.EventFilters) {
+                       matchingSubs = append(matchingSubs, subId)
+               }
+       }
+
+       return matchingSubs
+}
+
+func (es *EventService) filterOnEventType(event eventsapi.EventNotification) []string {
+       matchingSubs := []string{}
        for subId, subInfo := range es.subscriptions {
                if slices.Contains(asStrings(subInfo.Events), string(event.Events)) {
                        matchingSubs = append(matchingSubs, subId)
@@ -135,6 +152,20 @@ func (es *EventService) getMatchingSubs(event eventsapi.EventNotification) []str
        return matchingSubs
 }
 
+func matchesApiIds(eventIds []string, filters []eventsapi.CAPIFEventFilter) bool {
+       if len(filters) == 0 {
+               return true
+       }
+       for _, apiId := range eventIds {
+               filter := filters[0]
+               if filter.ApiIds == nil {
+                       return true && matchesApiIds(eventIds, filters[1:])
+               }
+               return slices.Contains(*filter.ApiIds, apiId) && matchesApiIds(eventIds, filters[1:])
+       }
+       return true
+}
+
 func asStrings(events []eventsapi.CAPIFEvent) []string {
        asStrings := make([]string, len(events))
        for i, event := range events {
index cd03ba7..f2acb9c 100644 (file)
@@ -122,13 +122,12 @@ func TestSendEvent(t *testing.T) {
        })
        serviceUnderTest, _ := getEcho(clientMock)
 
-       subscription := eventsapi.EventSubscription{
+       serviceUnderTest.addSubscription(subId, eventsapi.EventSubscription{
                Events: []eventsapi.CAPIFEvent{
                        eventsapi.CAPIFEventSERVICEAPIAVAILABLE,
                },
                NotificationDestination: common29122.Uri(notificationUrl),
-       }
-       serviceUnderTest.addSubscription(subId, subscription)
+       })
        sub2 := eventsapi.EventSubscription{
                Events: []eventsapi.CAPIFEvent{
                        eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE,
@@ -152,20 +151,19 @@ func TestMatchEventType(t *testing.T) {
        notificationUrl := "url"
        subId := "sub1"
        serviceUnderTest := NewEventService(nil)
-       subscription := eventsapi.EventSubscription{
+       serviceUnderTest.addSubscription(subId, eventsapi.EventSubscription{
                Events: []eventsapi.CAPIFEvent{
                        eventsapi.CAPIFEventSERVICEAPIAVAILABLE,
                },
                NotificationDestination: common29122.Uri(notificationUrl),
-       }
-       serviceUnderTest.addSubscription(subId, subscription)
-       sub2 := eventsapi.EventSubscription{
+               EventFilters:            &[]eventsapi.CAPIFEventFilter{},
+       })
+       serviceUnderTest.addSubscription("other", eventsapi.EventSubscription{
                Events: []eventsapi.CAPIFEvent{
                        eventsapi.CAPIFEventACCESSCONTROLPOLICYUNAVAILABLE,
                },
                NotificationDestination: common29122.Uri(notificationUrl),
-       }
-       serviceUnderTest.addSubscription("other", sub2)
+       })
 
        event := eventsapi.EventNotification{
                SubscriptionId: subId,
@@ -177,6 +175,30 @@ func TestMatchEventType(t *testing.T) {
        assert.Equal(t, subId, matchingSubs[0])
 }
 
+func TestMatchesApiIds(t *testing.T) {
+       apiId := "apiId"
+       apiIds := []string{apiId, "otherApiId"}
+       eventFilters := []eventsapi.CAPIFEventFilter{
+               {},
+               {
+                       ApiIds: &apiIds,
+               },
+       }
+
+       eventApiIds := []string{apiId}
+       assert.True(t, matchesApiIds(eventApiIds, eventFilters))
+       assert.True(t, matchesApiIds(nil, eventFilters))
+
+       altApiIds := []string{"anotherApiId"}
+       unMatchingFilterAdded := append(eventFilters, eventsapi.CAPIFEventFilter{
+               ApiIds: &altApiIds,
+       })
+       assert.False(t, matchesApiIds(eventApiIds, unMatchingFilterAdded))
+
+       apiIds[0] = "anotherId"
+       assert.False(t, matchesApiIds(eventApiIds, eventFilters))
+}
+
 func getEcho(client restclient.HTTPClient) (*EventService, *echo.Echo) {
        swagger, err := eventsapi.GetSwagger()
        if err != nil {