Add apiInvokerId filtering for eventservice
[nonrtric/plt/sme.git] / capifcore / internal / eventservice / eventservice.go
index d82b63d..a4fa079 100644 (file)
@@ -124,9 +124,27 @@ 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 matchesFilters(*event.EventDetail.ApiIds, *subscription.EventFilters, getApiIdsFromFilter) &&
+                       matchesFilters(*event.EventDetail.ApiInvokerIds, *subscription.EventFilters, getInvokerIdsFromFilter) {
+                       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 +153,27 @@ func (es *EventService) getMatchingSubs(event eventsapi.EventNotification) []str
        return matchingSubs
 }
 
+func matchesFilters(eventIds []string, filters []eventsapi.CAPIFEventFilter, getIds func(eventsapi.CAPIFEventFilter) *[]string) bool {
+       if len(filters) == 0 {
+               return true
+       }
+       for _, id := range eventIds {
+               filter := filters[0]
+               if getIds(filter) == nil {
+                       return matchesFilters(eventIds, filters[1:], getIds)
+               }
+               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 {