Updates for G Maintenance release
[nonrtric/plt/sme.git] / capifcore / internal / eventservice / eventservice.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation
6 //   %%
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
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
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===================================
19 //
20
21 package eventservice
22
23 import (
24         "encoding/json"
25         "fmt"
26         "net/http"
27         "path"
28         "strconv"
29         "sync"
30
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"
38 )
39
40 type EventService struct {
41         notificationChannel chan eventsapi.EventNotification
42         client              restclient.HTTPClient
43         subscriptions       map[string]eventsapi.EventSubscription
44         idCounter           uint
45         lock                sync.Mutex
46 }
47
48 func NewEventService(c restclient.HTTPClient) *EventService {
49         es := EventService{
50                 notificationChannel: make(chan eventsapi.EventNotification),
51                 client:              c,
52                 subscriptions:       make(map[string]eventsapi.EventSubscription),
53         }
54         es.start()
55         return &es
56 }
57
58 func (es *EventService) start() {
59         go es.handleIncomingEvents()
60 }
61
62 func (es *EventService) handleIncomingEvents() {
63         for event := range es.notificationChannel {
64                 es.handleEvent(event)
65         }
66 }
67 func (es *EventService) GetNotificationChannel() chan<- eventsapi.EventNotification {
68         return es.notificationChannel
69 }
70
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."
74         if err != nil {
75                 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
76         }
77
78         if err := newSubscription.Validate(); err != nil {
79                 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
80         }
81
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)
87         if err != nil {
88                 // Something really bad happened, tell Echo that our handler failed
89                 return err
90         }
91
92         return nil
93 }
94
95 func (es *EventService) DeleteSubscriberIdSubscriptionsSubscriptionId(ctx echo.Context, subscriberId string, subscriptionId string) error {
96
97         log.Debug(es.subscriptions)
98         if _, ok := es.subscriptions[subscriptionId]; ok {
99                 es.deleteSubscription(subscriptionId)
100         }
101
102         return ctx.NoContent(http.StatusNoContent)
103 }
104
105 func (es *EventService) deleteSubscription(subscriptionId string) {
106         log.Debug("Deleting subscription", subscriptionId)
107         es.lock.Lock()
108         defer es.lock.Unlock()
109         delete(es.subscriptions, subscriptionId)
110 }
111
112 func getEventSubscriptionFromRequest(ctx echo.Context) (eventsapi.EventSubscription, error) {
113         var subscription eventsapi.EventSubscription
114         err := ctx.Bind(&subscription)
115         if err != nil {
116                 return eventsapi.EventSubscription{}, fmt.Errorf("invalid format for subscription")
117         }
118         return subscription, nil
119 }
120
121 func (es *EventService) handleEvent(event eventsapi.EventNotification) {
122         subsIds := es.getMatchingSubs(event)
123         for _, subId := range subsIds {
124                 go es.sendEvent(event, subId)
125         }
126 }
127
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")
133         }
134 }
135
136 func (es *EventService) getMatchingSubs(event eventsapi.EventNotification) []string {
137         es.lock.Lock()
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)
149                 }
150         }
151
152         return matchingSubs
153 }
154
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)
160                 }
161         }
162         return matchingSubs
163 }
164
165 func matchesFilters(eventIds *[]string, filters []eventsapi.CAPIFEventFilter, getIds func(eventsapi.CAPIFEventFilter) *[]string) bool {
166         if len(filters) == 0 || eventIds == nil {
167                 return true
168         }
169         for _, id := range *eventIds {
170                 filter := filters[0]
171                 filterIds := getIds(filter)
172                 if filterIds == nil || len(*filterIds) == 0 {
173                         return matchesFilters(eventIds, filters[1:], getIds)
174                 } else {
175                         return slices.Contains(*getIds(filter), id) && matchesFilters(eventIds, filters[1:], getIds)
176                 }
177         }
178         return true
179 }
180
181 func getApiIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string {
182         return filter.ApiIds
183 }
184
185 func getInvokerIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string {
186         return filter.ApiInvokerIds
187 }
188
189 func getAefIdsFromEvent(serviceAPIDescriptions *[]publishserviceapi.ServiceAPIDescription) *[]string {
190         aefIds := []string{}
191         if serviceAPIDescriptions == nil {
192                 return &aefIds
193         }
194         for _, serviceDescription := range *serviceAPIDescriptions {
195                 if serviceDescription.AefProfiles == nil {
196                         return &aefIds
197                 }
198                 for _, profile := range *serviceDescription.AefProfiles {
199                         aefIds = append(aefIds, profile.AefId)
200                 }
201         }
202         return &aefIds
203 }
204
205 func getAefIdsFromFilter(filter eventsapi.CAPIFEventFilter) *[]string {
206         return filter.AefIds
207 }
208
209 func asStrings(events []eventsapi.CAPIFEvent) []string {
210         asStrings := make([]string, len(events))
211         for i, event := range events {
212                 asStrings[i] = string(event)
213         }
214         return asStrings
215 }
216
217 func (es *EventService) getSubscriptionId(subscriberId string) string {
218         es.idCounter++
219         return subscriberId + strconv.FormatUint(uint64(es.idCounter), 10)
220 }
221
222 func (es *EventService) addSubscription(subId string, subscription eventsapi.EventSubscription) {
223         es.lock.Lock()
224         defer es.lock.Unlock()
225         es.subscriptions[subId] = subscription
226 }
227
228 func (es *EventService) getSubscription(subId string) *eventsapi.EventSubscription {
229         es.lock.Lock()
230         defer es.lock.Unlock()
231         if sub, ok := es.subscriptions[subId]; ok {
232                 return &sub
233         } else {
234                 return nil
235         }
236 }
237
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{
242                 Cause:  &message,
243                 Status: &code,
244         }
245         err := ctx.JSON(code, pd)
246         return err
247 }