Commit 8366de44 authored by M. Hamza's avatar M. Hamza
Browse files

clean Post method in BWM

parent adca2865
Loading
Loading
Loading
Loading
+116 −164
Original line number Diff line number Diff line
@@ -985,7 +985,7 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
		return
	}

	var newBwInfo BwInfo
	//var newBwInfo BwInfo
	var jsonResponse []byte

	sessionFilterList := &sessionFilterListCheck{
@@ -996,8 +996,9 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
	keyName := baseKey + "bw_alloc:*"

	if *bwInfo.RequestType == 1 {
		// Retrieve MTS sessions from redis DB one by one and store in the mtsSessionInfoList array
		err = rc.ForEachJSONEntry(keyName, compareFlowFilters, sessionFilterList)
		// Retrieve bwInfo sessions from redis DB one by one and store in the sessionFilterList
		// to check if a bwInfo already exists in DB with same sessionFilter
		err = rc.ForEachJSONEntry(keyName, compareSessionFilters, sessionFilterList)
		if err != nil {
			errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
			return
@@ -1020,8 +1021,6 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
			if err != nil {
				errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
				return
			} else {
				newBwInfo.SessionFilter = bwInfo.SessionFilter
			}

		case "01":
@@ -1030,8 +1029,6 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
			if err != nil {
				errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
				return
			} else {
				newBwInfo.SessionFilter = bwInfo.SessionFilter
			}

		case "10":
@@ -1040,42 +1037,26 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
			if err != nil {
				errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
				return
			} else {
				newBwInfo.SessionFilter = bwInfo.SessionFilter
			}
			// if the provided destination IP range matches with the existing UE IP(s)
			err = checkDstIP(&bwInfo)
			if err != nil {
				errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
				return
			} else {
				newBwInfo.SessionFilter = bwInfo.SessionFilter
			}
		}

	}

	newBwInfo.AllocationDirection = bwInfo.AllocationDirection
	newBwInfo.RequestType = bwInfo.RequestType
	newBwInfo.AppInsId = bwInfo.AppInsId
	newBwInfo.FixedAllocation = bwInfo.FixedAllocation
	newBwInfo.FixedBWPriority = bwInfo.FixedBWPriority

	if bwInfo.AppName != "" {
		newBwInfo.AppName = bwInfo.AppName
	}

	seconds := time.Now().Unix()
	nanoseconds := time.Now().UnixNano()
	newBwInfo.TimeStamp = &BwInfoTimeStamp{
	bwInfo.TimeStamp = &BwInfoTimeStamp{
		NanoSeconds: int32(nanoseconds),
		Seconds:     int32(seconds),
	}

	// In APPLICATION_SPECIFIC_BW_ALLOCATION OR SESSION_SPECIFIC_BW_ALLOCATION
	// Uplink, Downlink and Symmetrical bandwidth allocation is performed
	if (*bwInfo.RequestType == 0) || (*bwInfo.RequestType == 1) {

	switch bwInfo.AllocationDirection {
	case "00":
		// getting downlink buffer value from redis to update
@@ -1089,11 +1070,11 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
		// To start allocating resource, buffer value should be non-zero
		if valBuff > 0 {
			var valFixedBuff uint64
				valFixedBuff, err = strconv.ParseUint(newBwInfo.FixedAllocation, 10, 64)
			valFixedBuff, err = strconv.ParseUint(bwInfo.FixedAllocation, 10, 64)
			// The value of resource needed to be allocate should also be no-zero
			if (valFixedBuff > 0) && (err == nil) {
				newAllocId := nextBwAllocIdAvailable
					newBwInfo.AllocationId = strconv.FormatUint(uint64(newAllocId), 10)
				bwInfo.AllocationId = strconv.FormatUint(uint64(newAllocId), 10)
				bufferInfo["down"] = strconv.FormatUint(valBuff-valFixedBuff, 10)
				// updateBuffer function takes updated buffer Information and store it in redis
				// returns error if unable to store allocation buffer in redis
@@ -1111,18 +1092,6 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
			log.Debug("Resources are insufficient for this allocation")
		}

			// setBwInfo function takes input of new BW allocation information and
			//	store it in json format in redis with new allocation ID.
			jsonResponse, err = setBwInfo(newBwInfo)
			if err != nil {
				log.Error("Unable to store new Allocation in redis")
				errHandlerProblemDetails(w, "Unable to store new Allocation in redis", http.StatusInternalServerError)
				return
			}

			w.WriteHeader(http.StatusCreated)
			fmt.Fprint(w, string(jsonResponse))

	case "01":
		// getting uplink buffer value from redis to update
		bufferInfo, valBuff, err := getUplinkBuff()
@@ -1135,11 +1104,11 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
		// To start allocating resource, buffer value should be non-zero
		if valBuff > 0 {
			var valFixedBuff uint64
				valFixedBuff, err = strconv.ParseUint(newBwInfo.FixedAllocation, 10, 64)
			valFixedBuff, err = strconv.ParseUint(bwInfo.FixedAllocation, 10, 64)
			// The value of resource needed to be allocate should also be no-zero
			if (valFixedBuff > 0) && (err == nil) {
				newAllocId := nextBwAllocIdAvailable
					newBwInfo.AllocationId = strconv.FormatUint(uint64(newAllocId), 10)
				bwInfo.AllocationId = strconv.FormatUint(uint64(newAllocId), 10)
				bufferInfo["up"] = strconv.FormatUint(valBuff-valFixedBuff, 10)
				// updateBuffer function takes updated buffer Information and store it in redis
				// returns error if unable to store allocation buffer in redis
@@ -1151,25 +1120,12 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
				} else {
					log.Info("Allocation uplink buffer is updated")
				}

				nextBwAllocIdAvailable++
			}
		} else {

			log.Debug("Resources are insufficient for this allocation")
		}

			// setBwInfo function takes input of new BW allocation information and
			// store it in json format in redis with new allocation ID.
			jsonResponse, err = setBwInfo(newBwInfo)
			if err != nil {
				log.Error("Unable to store new Allocation in redis")
				errHandlerProblemDetails(w, "Unable to store new Allocation in redis", http.StatusInternalServerError)
				return
			}
			w.WriteHeader(http.StatusCreated)
			fmt.Fprint(w, string(jsonResponse))

	case "10":
		// getting downlink/uplink buffer value from redis to update
		_, valBuffup, err := getUplinkBuff()
@@ -1188,11 +1144,11 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
		// To start allocating resource, buffer values should be non-zero
		if (valBuffup > 0) && (valBuffdown > 0) {
			var valFixedBuff uint64
				valFixedBuff, err = strconv.ParseUint(newBwInfo.FixedAllocation, 10, 64)
			valFixedBuff, err = strconv.ParseUint(bwInfo.FixedAllocation, 10, 64)
			// The value of resource needed to be allocate should also be no-zero
			if (valFixedBuff > 0) && (err == nil) {
				newAllocId := nextBwAllocIdAvailable
					newBwInfo.AllocationId = strconv.FormatUint(uint64(newAllocId), 10)
				bwInfo.AllocationId = strconv.FormatUint(uint64(newAllocId), 10)
				bufferInfo["up"] = strconv.FormatUint(valBuffup-(valFixedBuff/2), 10)
				bufferInfo["down"] = strconv.FormatUint(valBuffdown-(valFixedBuff/2), 10)
				// updateBuffer function takes updated buffer Information and store it in redis
@@ -1208,13 +1164,12 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
				nextBwAllocIdAvailable++
			}
		} else {

			log.Debug("Resources are insufficient for this allocation")
		}

	}
	// setBwInfo function takes input of new BW allocation information and
	// store it in json format in redis with new allocation ID.
			jsonResponse, err = setBwInfo(newBwInfo)
	jsonResponse, err = setBwInfo(bwInfo)
	if err != nil {
		log.Error("Unable to store new Allocation in redis")
		errHandlerProblemDetails(w, "Unable to store new Allocation in redis", http.StatusInternalServerError)
@@ -1222,9 +1177,6 @@ func bandwidthAllocationPost(w http.ResponseWriter, r *http.Request) {
	}
	w.WriteHeader(http.StatusCreated)
	fmt.Fprint(w, string(jsonResponse))

		}
	}
}

// bandwidthAllocationPut updates the information about a specific bandwidthAllocation at /bw_allocations/{allocationId} endpoint
@@ -2118,7 +2070,7 @@ func validateMtsSesInfoQueryParams(appInstanceId []string, appName []string, all
	return nil
}

func compareFlowFilters(key string, jsonInfo string, sessionFilterList interface{}) error {
func compareSessionFilters(key string, jsonInfo string, sessionFilterList interface{}) error {

	// Get query params & mtsSessionInfo
	data := sessionFilterList.(*sessionFilterListCheck)
@@ -2133,9 +2085,9 @@ func compareFlowFilters(key string, jsonInfo string, sessionFilterList interface
		return err
	}

	for _, flowFilterData := range data.SessionList {
		for _, redisFlowFilterData := range sessionFilterInfo.SessionFilter {
			if reflect.DeepEqual(flowFilterData, redisFlowFilterData) {
	for _, sessionFilterData := range data.SessionList {
		for _, redisSessionFilterData := range sessionFilterInfo.SessionFilter {
			if reflect.DeepEqual(sessionFilterData, redisSessionFilterData) {
				data.sessionBool = true
				return nil
			}