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/restclient"
39 type EventService struct {
40 notificationChannel chan eventsapi.EventNotification
41 client restclient.HTTPClient
42 subscriptions map[string]eventsapi.EventSubscription
47 func NewEventService(c restclient.HTTPClient) *EventService {
49 notificationChannel: make(chan eventsapi.EventNotification),
51 subscriptions: make(map[string]eventsapi.EventSubscription),
57 func (es *EventService) start() {
58 go es.handleIncomingEvents()
61 func (es *EventService) handleIncomingEvents() {
62 for event := range es.notificationChannel {
66 func (es *EventService) GetNotificationChannel() chan<- eventsapi.EventNotification {
67 return es.notificationChannel
70 func (es *EventService) PostSubscriberIdSubscriptions(ctx echo.Context, subscriberId string) error {
71 newSubscription, err := getEventSubscriptionFromRequest(ctx)
72 errMsg := "Unable to register subscription due to %s."
74 return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, err))
76 uri := ctx.Request().Host + ctx.Request().URL.String()
77 subId := es.getSubscriptionId(subscriberId)
78 es.addSubscription(subId, newSubscription)
79 ctx.Response().Header().Set(echo.HeaderLocation, ctx.Scheme()+`://`+path.Join(uri, subId))
80 err = ctx.JSON(http.StatusCreated, newSubscription)
82 // Something really bad happened, tell Echo that our handler failed
89 func (es *EventService) DeleteSubscriberIdSubscriptionsSubscriptionId(ctx echo.Context, subscriberId string, subscriptionId string) error {
91 defer es.lock.Unlock()
93 log.Debug(es.subscriptions)
94 if _, ok := es.subscriptions[subscriptionId]; ok {
95 log.Debug("Deleting subscription", subscriptionId)
96 delete(es.subscriptions, subscriptionId)
99 return ctx.NoContent(http.StatusNoContent)
102 func getEventSubscriptionFromRequest(ctx echo.Context) (eventsapi.EventSubscription, error) {
103 var subscription eventsapi.EventSubscription
104 err := ctx.Bind(&subscription)
106 return eventsapi.EventSubscription{}, fmt.Errorf("invalid format for subscription")
108 return subscription, nil
111 func (es *EventService) handleEvent(event eventsapi.EventNotification) {
112 subsIds := es.getMatchingSubs(event)
113 for _, subId := range subsIds {
114 go es.sendEvent(event, subId)
118 func (es *EventService) sendEvent(event eventsapi.EventNotification, subscriptionId string) {
119 event.SubscriptionId = subscriptionId
120 e, _ := json.Marshal(event)
121 if error := restclient.Put(string(es.subscriptions[subscriptionId].NotificationDestination), []byte(e), es.client); error != nil {
122 log.Error("Unable to send event")
126 func (es *EventService) getMatchingSubs(event eventsapi.EventNotification) []string {
127 matchingSubs := []string{}
129 defer es.lock.Unlock()
130 for subId, subInfo := range es.subscriptions {
131 if slices.Contains(asStrings(subInfo.Events), string(event.Events)) {
132 matchingSubs = append(matchingSubs, subId)
138 func asStrings(events []eventsapi.CAPIFEvent) []string {
139 asStrings := make([]string, len(events))
140 for i, event := range events {
141 asStrings[i] = string(event)
146 func (es *EventService) getSubscriptionId(subscriberId string) string {
148 return subscriberId + strconv.FormatUint(uint64(es.idCounter), 10)
151 func (es *EventService) addSubscription(subId string, subscription eventsapi.EventSubscription) {
153 es.subscriptions[subId] = subscription
157 func (es *EventService) getSubscription(subId string) *eventsapi.EventSubscription {
159 defer es.lock.Unlock()
160 if sub, ok := es.subscriptions[subId]; ok {
167 // This function wraps sending of an error in the Error format, and
168 // handling the failure to marshal that.
169 func sendCoreError(ctx echo.Context, code int, message string) error {
170 pd := common29122.ProblemDetails{
174 err := ctx.JSON(code, pd)