2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
31 "github.com/labstack/echo/v4"
32 log "github.com/sirupsen/logrus"
33 "k8s.io/utils/strings/slices"
34 "oransc.org/nonrtric/capifcore/internal/common29122"
35 "oransc.org/nonrtric/capifcore/internal/eventsapi"
36 "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
37 "oransc.org/nonrtric/capifcore/internal/restclient"
40 type EventService struct {
41 notificationChannel chan eventsapi.EventNotification
42 client restclient.HTTPClient
43 subscriptions map[string]eventsapi.EventSubscription
48 func NewEventService(c restclient.HTTPClient) *EventService {
50 notificationChannel: make(chan eventsapi.EventNotification),
52 subscriptions: make(map[string]eventsapi.EventSubscription),
58 func (es *EventService) start() {
59 go es.handleIncomingEvents()
62 func (es *EventService) handleIncomingEvents() {
63 for event := range es.notificationChannel {
67 func (es *EventService) GetNotificationChannel() chan<- eventsapi.EventNotification {
68 return es.notificationChannel
71 func (es *EventService) PostSubscriberIdSubscriptions(ctx echo.Context, subscriberId string) error {
72 newSubscription, err := getEventSubscriptionFromRequest(ctx)
73 errMsg := "Unable to register subscription due to %s."
75 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
78 if err := newSubscription.Validate(); err != nil {
79 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
82 uri := ctx.Request().Host + ctx.Request().URL.String()
83 subId := es.getSubscriptionId(subscriberId)
84 es.addSubscription(subId, newSubscription)
85 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, subId))
86 err = ctx.JSON(http.StatusCreated, newSubscription)
88 // Something really bad happened, tell Echo that our handler failed
95 func (es *EventService) DeleteSubscriberIdSubscriptionsSubscriptionId(ctx echo.Context, subscriberId string, subscriptionId string) error {
97 log.Debug(es.subscriptions)
98 if _, ok := es.subscriptions[subscriptionId]; ok {
99 es.deleteSubscription(subscriptionId)
102 return ctx.NoContent(http.StatusNoContent)
105 func (es *EventService) deleteSubscription(subscriptionId string) {
106 log.Debug("Deleting subscription", subscriptionId)
108 defer es.lock.Unlock()
109 delete(es.subscriptions, subscriptionId)
112 func getEventSubscriptionFromRequest(ctx echo.Context) (eventsapi.EventSubscription, error) {
113 var subscription eventsapi.EventSubscription
114 err := ctx.Bind(&subscription)
116 return eventsapi.EventSubscription{}, fmt.Errorf("invalid format for subscription")
118 return subscription, nil
121 func (es *EventService) handleEvent(event eventsapi.EventNotification) {
122 subsIds := es.getMatchingSubs(event)
123 for _, subId := range subsIds {
124 go es.sendEvent(event, subId)
128 func (es *EventService) sendEvent(event eventsapi.EventNotification, subscriptionId string) {
129 event.SubscriptionId = subscriptionId
130 e, _ := json.Marshal(event)
131 if error := restclient.Put(string(es.subscriptions[subscriptionId].NotificationDestination), []byte(e), es.client); error != nil {
132 log.Error("Unable to send event")
136 func (es *EventService) getMatchingSubs(event eventsapi.EventNotification) []string {
138 defer es.lock.Unlock()
139 matchingTypeSubs := es.filterOnEventType(event)
140 matchingSubs := []string{}
141 for _, subId := range matchingTypeSubs {
142 subscription := es.subscriptions[subId]
143 if subscription.EventFilters == nil || event.EventDetail == nil {
144 matchingSubs = append(matchingSubs, subId)
145 } else if matchesFilters(event.EventDetail.ApiIds, *subscription.EventFilters, getApiIdsFromFilter) &&
146 matchesFilters(event.EventDetail.ApiInvokerIds, *subscription.EventFilters, getInvokerIdsFromFilter) &&
147 matchesFilters(getAefIdsFromEvent(event.EventDetail.ServiceAPIDescriptions), *subscription.EventFilters, getAefIdsFromFilter) {
148 matchingSubs = append(matchingSubs, subId)
155 func (es *EventService) filterOnEventType(event eventsapi.EventNotification) []string {
156 matchingSubs := []string{}
157 for subId, subInfo := range es.subscriptions {
158 if slices.Contains(asStrings(subInfo.Events), string(event.Events)) {
159 matchingSubs = append(matchingSubs, subId)
165 func matchesFilters(eventIds *[]string, filters []eventsapi.CAPIFEventFilter, getIds func(eventsapi.CAPIFEventFilter) *[]string) bool {
166 if len(filters) == 0 || eventIds == nil {
169 for _, id := range *eventIds {
171 filterIds := getIds(filter)
172 if filterIds == nil || len(*filterIds) == 0 {
173 return matchesFilters(eventIds, filters[1:], getIds)
175 return slices.Contains(*getIds(filter), id) && matchesFilters(eventIds, filters[1:], getIds)
181 func getApiIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string {
185 func getInvokerIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string {
186 return filter.ApiInvokerIds
189 func getAefIdsFromEvent(serviceAPIDescriptions *[]publishserviceapi.ServiceAPIDescription) *[]string {
191 if serviceAPIDescriptions == nil {
194 for _, serviceDescription := range *serviceAPIDescriptions {
195 if serviceDescription.AefProfiles == nil {
198 for _, profile := range *serviceDescription.AefProfiles {
199 aefIds = append(aefIds, profile.AefId)
205 func getAefIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string {
209 func asStrings(events []eventsapi.CAPIFEvent) []string {
210 asStrings := make([]string, len(events))
211 for i, event := range events {
212 asStrings[i] = string(event)
217 func (es *EventService) getSubscriptionId(subscriberId string) string {
219 return subscriberId + strconv.FormatUint(uint64(es.idCounter), 10)
222 func (es *EventService) addSubscription(subId string, subscription eventsapi.EventSubscription) {
224 defer es.lock.Unlock()
225 es.subscriptions[subId] = subscription
228 func (es *EventService) getSubscription(subId string) *eventsapi.EventSubscription {
230 defer es.lock.Unlock()
231 if sub, ok := es.subscriptions[subId]; ok {
238 // This function wraps sending of an error in the Error format, and
239 // handling the failure to marshal that.
240 func sendCoreError(ctx echo.Context, code int, message string) error {
241 pd := common29122.ProblemDetails{
245 err := ctx.JSON(code, pd)