Commit d4d4f074 authored by Ikram Haq's avatar Ikram Haq
Browse files

Add patch method in CAPIF API to update the event subsription

parent a0302078
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -69,6 +69,11 @@ func ApplicationsSubscriptionsPOST(w http.ResponseWriter, r *http.Request) {
func ApplicationsSubscriptionsPUT(w http.ResponseWriter, r *http.Request) {
	applicationsSubscriptionsPUT(w, r)
}

func ApplicationsSubscriptionsPATCH(w http.ResponseWriter, r *http.Request) {
	applicationsSubscriptionsPATCH(w, r)
}

func ServicesGET(w http.ResponseWriter, r *http.Request) {
	servicesGET(w, r)
}
+20 −0
Original line number Diff line number Diff line
@@ -87,6 +87,26 @@ func convertJsonToSerAvailabilityNotifSub(jsonData string) *SerAvailabilityNotif
	return &obj
}

// Convert []string back to []CapifEventFilter
func convertStringsToEventFilters(stringFilters []string) []CapifEventFilter {
	var eventFilters []CapifEventFilter
	for _, filter := range stringFilters {
		eventFilters = append(eventFilters, CapifEventFilter{
			ApiIds: []string{filter}, // Assuming each string corresponds to an ApiId in the CapifEventFilter
		})
	}
	return eventFilters
}

// Convert []string back to []CapifEvent
func convertStringsToEvents(stringEvents []string) []CapifEvent {
	var events []CapifEvent
	for _, event := range stringEvents {
		events = append(events, CapifEvent(event)) // Assuming the string directly maps to CapifEvent
	}
	return events
}

func convertEventFiltersToStrings(filters []CapifEventFilter) []string {
	var stringFilters []string
	for _, filter := range filters {
+18 −0
Original line number Diff line number Diff line
/*
 * MEC service management realized by CAPIF APIs
 *
 * The ETSI MEC ISG MEC011 MEC Service Management realized by CAPIF APIs described using OpenAPI
 *
 * API version: 3.2.1
 * Contact: cti_support@etsi.org
 * Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
 */
package server

type EventSubscriptionPatch struct {
	// The events for which the subscription is modified.
	Events       []CapifEvent       `json:"events"`
	EventFilters []CapifEventFilter `json:"eventFilters,omitempty"`
	// URI to which notifications will be sent. Shall be set to the value of the \"callbackReference\" attribute in the \"SerAvailabilityNotificationSubscription\" structure.
	NotificationDestination string `json:"notificationDestination,omitempty"`
}
+105 −2
Original line number Diff line number Diff line
@@ -882,6 +882,96 @@ func servicesGET(w http.ResponseWriter, r *http.Request) {
	}
}

func applicationsSubscriptionsPATCH(w http.ResponseWriter, r *http.Request) {
	log.Info("applicationsSubscriptionsPATCH")
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	vars := mux.Vars(r)
	subId := vars["subscriptionId"]
	appId := vars["subscriberId"]

	mutex.Lock()
	defer mutex.Unlock()

	// Get App instance
	appInfo, err := getAppInfo(appId)
	if err != nil {
		errHandlerProblemDetails(w, err.Error(), http.StatusNotFound)
		return
	}

	// Validate App info
	code, problemDetails, err := validateAppInfo(appInfo)
	if err != nil {
		log.Error(err.Error())
		if problemDetails != "" {
			w.WriteHeader(code)
			fmt.Fprint(w, problemDetails)
		} else {
			errHandlerProblemDetails(w, err.Error(), code)
		}
		return
	}

	// Retrieve the existing subscription
	sub, err := subMgr.GetSubscription(subId)
	if err != nil {
		log.Error("Subscription not found: ", err.Error())
		errHandlerProblemDetails(w, "Subscription not found", http.StatusNotFound)
		return
	}

	// Retrieve the patch request body
	if r.Body == nil {
		err := errors.New("Request body is missing")
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}

	var patchSub EventSubscriptionPatch
	decoder := json.NewDecoder(r.Body)
	err = decoder.Decode(&patchSub)
	if err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, "Failed to decode request body", http.StatusInternalServerError)
		return
	}

	// Apply partial updates
	if patchSub.NotificationDestination != "" {
		sub.Cfg.NotifyUrl = patchSub.NotificationDestination
	}

	if patchSub.EventFilters != nil {
		sub.Cfg.EventFilters = convertEventFiltersToStrings(patchSub.EventFilters)
	}

	if patchSub.Events != nil {
		sub.Cfg.CapifEvent = convertEventToStrings(patchSub.Events)
	}

	// Update the subscription in the manager
	err = subMgr.UpdateSubscription(sub)
	if err != nil {
		log.Error("Failed to update subscription: ", err.Error())
		errHandlerProblemDetails(w, "Failed to update subscription", http.StatusInternalServerError)
		return
	}

	// Convert the updated subscription to an EventSubscription struct
	updatedSub := EventSubscription{
		NotificationDestination: sub.Cfg.NotifyUrl,
		EventFilters:            convertStringsToEventFilters(sub.Cfg.EventFilters),
		Events:                  convertStringsToEvents(sub.Cfg.CapifEvent),
	}

	// Convert the updated EventSubscription to JSON format for the response
	jsonSub := convertSerAvailabilityNotifSubToJson_1(&updatedSub)

	// Send the full updated subscription in the response
	w.WriteHeader(http.StatusOK)
	fmt.Fprint(w, jsonSub)
}

func applicationsSubscriptionsPUT(w http.ResponseWriter, r *http.Request) {
	log.Info("applicationsSubscriptionsPUT")
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
@@ -932,8 +1022,21 @@ func applicationsSubscriptionsPUT(w http.ResponseWriter, r *http.Request) {
		return
	}
	if updatedSub.NotificationDestination == "" {
		log.Error("Either NotificationDestination must be set")
		errHandlerProblemDetails(w, "Either NotificationDestination must be set", http.StatusBadRequest)
		log.Error("NotificationDestination is required for PUT")
		errHandlerProblemDetails(w, "NotificationDestination is required", http.StatusBadRequest)
		return
	}

	// Assume `EventFilters` and `Events` are mandatory for `PUT` as well
	if updatedSub.EventFilters == nil || len(updatedSub.EventFilters) == 0 {
		log.Error("EventFilters are required for PUT")
		errHandlerProblemDetails(w, "EventFilters are required", http.StatusBadRequest)
		return
	}

	if updatedSub.Events == nil || len(updatedSub.Events) == 0 {
		log.Error("Events are required for PUT")
		errHandlerProblemDetails(w, "Events are required", http.StatusBadRequest)
		return
	}
	// Update the subscription object
+7 −0
Original line number Diff line number Diff line
@@ -399,6 +399,13 @@ var routes = Routes{
		capifMgmt.ApplicationsSubscriptionsPUT,
	},

	Route{
		"ApplicationsSubscriptionsPATCH",
		strings.ToUpper("Patch"),
		"/capif-events/v1/{subscriberId}/subscriptions/{subscriptionId}",
		capifMgmt.ApplicationsSubscriptionsPATCH,
	},

	Route{
		"ApplicationsSubscriptionDELETE",
		strings.ToUpper("Delete"),