Commit 8e768a6d authored by Ikram Haq's avatar Ikram Haq
Browse files

Implement logic to handle both zoneLocationEventSubscription and statusSubscription request Body

parent a52673c1
Loading
Loading
Loading
Loading
+267 −126
Original line number Diff line number Diff line
@@ -3859,20 +3859,50 @@ func zoneSubGET(w http.ResponseWriter, r *http.Request) {
}

func zoneSubPOST(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	var response InlineZoneStatusSubscription
	var body InlineZoneStatusSubscription
	// Decode the request body into a slice of maps
	var requestBody []map[string]interface{}

	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&body)
	if err != nil {
	if err := decoder.Decode(&requestBody); err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, "Invalid request body format", http.StatusBadRequest)
		return
	}
	zoneStatusSub := body.ZoneStatusSubscription
	if zoneStatusSub == nil {
		log.Error("Body not present")
		errHandlerProblemDetails(w, "Body not present", http.StatusBadRequest)
	hasZoneSubscription := false
	hasStatusSubscription := false

	for _, body := range requestBody {
		if _, ok := body["zoneLocationEventSubscription"]; ok {
			hasZoneSubscription = true
		} else if _, ok := body["zoneStatusSubscription"]; ok {
			hasStatusSubscription = true
		}
	}
	// Check if both types of subscriptions are present
	if hasZoneSubscription && hasStatusSubscription {
		errHandlerProblemDetails(w, "Please send only one type of subscription at a time", http.StatusBadRequest)
		return
	}
	if hasZoneSubscription {
		handleZoneLocationEventSubscription(w, requestBody)
	} else if hasStatusSubscription {
		handleZoneStatusSubscription(w, requestBody)
	} else {
		errHandlerProblemDetails(w, "No valid subscription found in the request body", http.StatusBadRequest)
		return
	}
}
func handleZoneStatusSubscription(w http.ResponseWriter, requestBody []map[string]interface{}) {
	for _, body := range requestBody {
		if statusSubscription, ok := body["zoneStatusSubscription"]; ok {
			// Convert the Status subscription to a map
			statusSubscriptionMap := statusSubscription.(map[string]interface{})
			// Decode the Zone subscription map into a struct
			var zoneStatusSub ZoneStatusSubscription
			err := mapstructure.Decode(statusSubscriptionMap, &zoneStatusSub)
			if err != nil {
				log.Error(err.Error())
				errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
				return
			}
			//checking for mandatory properties
@@ -3886,22 +3916,134 @@ func zoneSubPOST(w http.ResponseWriter, r *http.Request) {
				errHandlerProblemDetails(w, "Mandatory ZoneId parameter not present", http.StatusBadRequest)
				return
			}
			// Add your logic to register the event-based subscription
			newSubsId := nextZoneStatusSubscriptionIdAvailable
	nextZoneStatusSubscriptionIdAvailable++
			if newSubsId%2 != 0 {
				newSubsId++ // Ensure newSubsId is even
			}
			nextZoneStatusSubscriptionIdAvailable = newSubsId + 2 // Increment by 2 to ensure the next even number
			subsIdStr := strconv.Itoa(newSubsId)
			zoneStatusSub.ResourceURL = hostUrl.String() + basePath + "subscriptions/zones/" + subsIdStr
	_ = rc.JSONSetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".", convertZoneStatusSubscriptionToJson(zoneStatusSub))
			_ = rc.JSONSetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".", convertZoneStatusSubscriptionToJson(&zoneStatusSub))
			registerZoneStatus(zoneStatusSub.ZoneId, zoneStatusSub.UpperNumberOfUsersZoneThreshold, zoneStatusSub.UpperNumberOfUsersAPThreshold,
				zoneStatusSub.OperationStatus, subsIdStr)
	response.ZoneStatusSubscription = zoneStatusSub

			// Prepare response
			var response InlineZoneStatusSubscription
			response.ZoneStatusSubscription = &zoneStatusSub
			// Marshal response
			jsonResponse, err := json.Marshal(response)
			if err != nil {
				log.Error(err.Error())
				errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
				return
			}

			// Write response
			w.Header().Set("Content-Type", "application/json; charset=UTF-8")
			w.WriteHeader(http.StatusCreated)
			fmt.Fprint(w, string(jsonResponse))
			return
		}
	}
	// If no event-based subscription found in the request body
	errHandlerProblemDetails(w, "No valid event-based subscription found in the request body", http.StatusBadRequest)
}

// w.Header().Set("Content-Type", "application/json; charset=UTF-8")
// var response InlineZoneStatusSubscription
// var body InlineZoneStatusSubscription
// decoder := json.NewDecoder(r.Body)
// err := decoder.Decode(&body)
// if err != nil {
// 	log.Error(err.Error())
// 	errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
// 	return
// }
// zoneStatusSub := body.ZoneStatusSubscription
// if zoneStatusSub == nil {
// 	log.Error("Body not present")
// 	errHandlerProblemDetails(w, "Body not present", http.StatusBadRequest)
// 	return
// }
// //checking for mandatory properties
// if zoneStatusSub.CallbackReference == nil || zoneStatusSub.CallbackReference.NotifyURL == "" {
// 	log.Error("Mandatory CallbackReference parameter not present")
// 	errHandlerProblemDetails(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
// 	return
// }
// if zoneStatusSub.ZoneId == "" {
// 	log.Error("Mandatory ZoneId parameter not present")
// 	errHandlerProblemDetails(w, "Mandatory ZoneId parameter not present", http.StatusBadRequest)
// 	return
// }
// newSubsId := nextZoneStatusSubscriptionIdAvailable
// nextZoneStatusSubscriptionIdAvailable++
// subsIdStr := strconv.Itoa(newSubsId)
// zoneStatusSub.ResourceURL = hostUrl.String() + basePath + "subscriptions/zones/" + subsIdStr
// _ = rc.JSONSetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".", convertZoneStatusSubscriptionToJson(zoneStatusSub))
// registerZoneStatus(zoneStatusSub.ZoneId, zoneStatusSub.UpperNumberOfUsersZoneThreshold, zoneStatusSub.UpperNumberOfUsersAPThreshold,
// 	zoneStatusSub.OperationStatus, subsIdStr)
// response.ZoneStatusSubscription = zoneStatusSub
// jsonResponse, err := json.Marshal(response)
// if err != nil {
// 	log.Error(err.Error())
// 	errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
// 	return
// }
// w.WriteHeader(http.StatusCreated)
// fmt.Fprint(w, string(jsonResponse))
func handleZoneLocationEventSubscription(w http.ResponseWriter, requestBody []map[string]interface{}) {
	// Iterate over each item in the request body
	for _, body := range requestBody {
		if zoneLocationEventSubscription, ok := body["zoneLocationEventSubscription"]; ok {
			zoneLocationEventSubscriptionMap := zoneLocationEventSubscription.(map[string]interface{})
			// Decode the zone subscription map into a struct
			var zonalSub ZoneLocationEventSubscription
			err := mapstructure.Decode(zoneLocationEventSubscriptionMap, &zonalSub)
			if err != nil {
				log.Error(err.Error())
				errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
				return
			}
			//checking for mandatory properties
			if zonalSub.CallbackReference == nil || zonalSub.CallbackReference.NotifyURL == "" {
				log.Error("Mandatory CallbackReference parameter not present")
				errHandlerProblemDetails(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
				return
			}
			if zonalSub.ZoneId == "" {
				log.Error("Mandatory ZoneId parameter not present")
				errHandlerProblemDetails(w, "Mandatory ZoneId parameter not present", http.StatusBadRequest)
				return
			}

			// Add your logic to register the periodic-based subscription
			newSubsId := nextZonalSubscriptionIdAvailable
			nextZonalSubscriptionIdAvailable += 2
			subsIdStr := strconv.Itoa(newSubsId)
			zonalSub.ResourceURL = hostUrl.String() + basePath + "subscriptions/zones/" + subsIdStr
			_ = rc.JSONSetEntry(baseKey+typeZonalSubscription+":"+subsIdStr, ".", convertZonalSubscriptionToJson1(&zonalSub))
			registerZonal1(zonalSub.ZoneId, zonalSub.LocationEventCriteria, subsIdStr)
			var response InlineZoneLocationEventSubscription
			response.ZoneLocationEventSubscription = &zonalSub
			// Marshal response
			jsonResponse, err := json.Marshal(response)
			if err != nil {
				log.Error(err.Error())
				errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
				return
			}
			// Write response
			w.Header().Set("Content-Type", "application/json; charset=UTF-8")
			w.WriteHeader(http.StatusCreated)
			fmt.Fprint(w, string(jsonResponse))
			return
		}
	}
	// If no zone-based subscription found in the request body
	errHandlerProblemDetails(w, "No valid zone Event-based subscription found in the request body", http.StatusBadRequest)
}

// *****************************Zone *************************Event	********************
// w.Header().Set("Content-Type", "application/json; charset=UTF-8")
@@ -3947,7 +4089,6 @@ func zoneSubPOST(w http.ResponseWriter, r *http.Request) {
// }
// w.WriteHeader(http.StatusCreated)
// fmt.Fprint(w, string(jsonResponse))
}

func zonalTrafficSubPost(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")