Commit 38eeef2b authored by Ikram Haq's avatar Ikram Haq
Browse files

Implement logic to send the sotification when the subscription created

parent af69016d
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -87,6 +87,16 @@ func convertJsonToSerAvailabilityNotifSub(jsonData string) *SerAvailabilityNotif
	return &obj
}

func convertJsonToSerAvailabilityNotifSub_1(jsonData string) *EventSubscription {
	var obj EventSubscription
	err := json.Unmarshal([]byte(jsonData), &obj)
	if err != nil {
		log.Error(err.Error())
		return nil
	}
	return &obj
}

func convertServiceAvailabilityNotifToJson(obj *ServiceAvailabilityNotification) string {
	jsonInfo, err := json.Marshal(*obj)
	if err != nil {
@@ -96,6 +106,15 @@ func convertServiceAvailabilityNotifToJson(obj *ServiceAvailabilityNotification)
	return string(jsonInfo)
}

func convertServiceAvailabilityNotifToJson_1(obj *EventNotification) string {
	jsonInfo, err := json.Marshal(*obj)
	if err != nil {
		log.Error(err.Error())
		return ""
	}
	return string(jsonInfo)
}

func convertProblemDetailsToJson(obj *ProblemDetails) string {
	jsonInfo, err := json.Marshal(*obj)
	if err != nil {
+15 −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 EventNotification struct {
	// The values SERVICE_API_AVAILABLE, SERVICE_API_UNAVAILABLE, and SERVICE_API_UPDATE defined in the type \"CAPIFEvent\" shall be supported. The remaining values of that type need not be supported.
	Events []CapifEvent `json:"events,omitempty"`
}
+93 −72
Original line number Diff line number Diff line
@@ -1835,103 +1835,124 @@ func checkSerAvailNotification(sInfo *ServiceInfo, mep string, changeType CapifE
	for _, sub := range subList {

		// Unmarshal original JSON subscription
		origSub := convertJsonToSerAvailabilityNotifSub(sub.JsonSubOrig)
		origSub := convertJsonToSerAvailabilityNotifSub_1(sub.JsonSubOrig)
		if origSub == nil {
			continue
		}

		// Check subscription filter criteria
		if origSub.FilteringCriteria != nil {

			// Service Instance IDs
			if origSub.FilteringCriteria.SerInstanceIds != nil && len(origSub.FilteringCriteria.SerInstanceIds) > 0 {
		if len(origSub.EventFilters) > 0 {
			found := false
				for _, serInstanceId := range origSub.FilteringCriteria.SerInstanceIds {
					if serInstanceId == sInfo.SerInstanceId {
			// Service Instance IDs
			for _, eventFilter := range origSub.EventFilters {
				// Check if ApiIds (replacing SerInstanceIds) match
				if len(eventFilter.ApiIds) > 0 {
					for _, apiId := range eventFilter.ApiIds {
						if apiId == sInfo.SerInstanceId { // Compare with the current Service Instance ID (now ApiId)
							found = true
							break
						}
					}
				}
				if !found {
					continue
				}
			}

			// Service Names
			if origSub.FilteringCriteria.SerNames != nil && len(origSub.FilteringCriteria.SerNames) > 0 {
				found := false
				for _, serName := range origSub.FilteringCriteria.SerNames {
					if serName == sInfo.SerName {
						found = true
						break
					}
				}
				if !found {
					continue
				}
			}
			// if origSub.FilteringCriteria.SerNames != nil && len(origSub.FilteringCriteria.SerNames) > 0 {
			// 	found := false
			// 	for _, serName := range origSub.FilteringCriteria.SerNames {
			// 		if serName == sInfo.SerName {
			// 			found = true
			// 			break
			// 		}
			// 	}
			// 	if !found {
			// 		continue
			// 	}
			// }

			// Service Categories
			if origSub.FilteringCriteria.SerCategories != nil && len(origSub.FilteringCriteria.SerCategories) > 0 {
				found := false
				for _, serCategory := range origSub.FilteringCriteria.SerCategories {
					if serCategory.Href == sInfo.SerCategory.Href &&
						serCategory.Id == sInfo.SerCategory.Id &&
						serCategory.Name == sInfo.SerCategory.Name &&
						serCategory.Version == sInfo.SerCategory.Version {
						found = true
						break
					}
				}
				if !found {
					continue
				}
			}
			// if origSub.FilteringCriteria.SerCategories != nil && len(origSub.FilteringCriteria.SerCategories) > 0 {
			// 	found := false
			// 	for _, serCategory := range origSub.FilteringCriteria.SerCategories {
			// 		if serCategory.Href == sInfo.SerCategory.Href &&
			// 			serCategory.Id == sInfo.SerCategory.Id &&
			// 			serCategory.Name == sInfo.SerCategory.Name &&
			// 			serCategory.Version == sInfo.SerCategory.Version {
			// 			found = true
			// 			break
			// 		}
			// 	}
			// 	if !found {
			// 		continue
			// 	}
			// }

			// Service states
			if origSub.FilteringCriteria.States != nil && len(origSub.FilteringCriteria.States) > 0 {
				found := false
				for _, serState := range origSub.FilteringCriteria.States {
					if serState == *sInfo.State {
						found = true
						break
					}
				}
				if !found {
					continue
				}
			}
			// if origSub.FilteringCriteria.States != nil && len(origSub.FilteringCriteria.States) > 0 {
			// 	found := false
			// 	for _, serState := range origSub.FilteringCriteria.States {
			// 		if serState == *sInfo.State {
			// 			found = true
			// 			break
			// 		}
			// 	}
			// 	if !found {
			// 		continue
			// 	}
			// }

			// Service locality
			if origSub.FilteringCriteria.IsLocal && !sInfo.IsLocal {
				continue
			// // Service locality
			// if origSub.FilteringCriteria.IsLocal && !sInfo.IsLocal {
			// 	continue
			// }
		}
		// Create a new EventNotification instance
		notif := &EventNotification{}

		// Set the event type based on changeType
		var eventType CapifEvent
		switch changeType {
		case "SERVICE_API_AVAILABLE":
			eventType = AVAILABLE
		case "SERVICE_API_UNAVAILABLE":
			eventType = UNAVAILABLE
		case "SERVICE_API_UPDATE":
			eventType = UPDATE
		default:
			// Handle any default case or errors
		}

		// Create notification payload
		notif := &ServiceAvailabilityNotification{
			NotificationType: SER_AVAILABILITY_NOTIF_TYPE,
			Links: &Subscription{
				Subscription: &LinkType{
					Href: sub.Cfg.Self,
				},
			},
		// If eventType is set, append it to the Events slice
		if eventType != "" {
			notif.Events = append(notif.Events, eventType)
		}
		serAvailabilityRef := ServiceAvailabilityNotificationServiceReferences{
			Link: &LinkType{
				Href: hostUrl.String() + basePath + "services/" + sInfo.SerInstanceId,
			},
			SerName:       sInfo.SerName,
			SerInstanceId: sInfo.SerInstanceId,
			State:         sInfo.State,
			ChangeType:    &changeType,
		}
		notif.ServiceReferences = append(notif.ServiceReferences, serAvailabilityRef)
		// // Create notification payload
		// notif := &ServiceAvailabilityNotification{
		// 	NotificationType: SER_AVAILABILITY_NOTIF_TYPE,
		// 	Links: &Subscription{
		// 		Subscription: &LinkType{
		// 			Href: sub.Cfg.Self,
		// 		},
		// 	},
		// }
		// serAvailabilityRef := ServiceAvailabilityNotificationServiceReferences{
		// 	Link: &LinkType{
		// 		Href: hostUrl.String() + basePath + "services/" + sInfo.SerInstanceId,
		// 	},
		// 	SerName:       sInfo.SerName,
		// 	SerInstanceId: sInfo.SerInstanceId,
		// 	State:         sInfo.State,
		// 	ChangeType:    &changeType,
		// }
		// notif.ServiceReferences = append(notif.ServiceReferences, serAvailabilityRef)

		// Send notification
		go func(sub *subs.Subscription) {
			log.Info("Sending Service Availability notification (" + sub.Cfg.Id + ") for " + string(changeType))
			err := subMgr.SendNotification(sub, []byte(convertServiceAvailabilityNotifToJson(notif)))
			err := subMgr.SendNotification(sub, []byte(convertServiceAvailabilityNotifToJson_1(notif)))
			if err != nil {
				log.Error("Failed to send Service Availability notif with err: ", err.Error())
			}