Commit 7ae8806c authored by Ayesha Ayub's avatar Ayesha Ayub
Browse files

clean POST method in BWM

parent 2c813fdd
Loading
Loading
Loading
Loading
+92 −70
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"net"
	"net/http"
	"net/url"
@@ -699,16 +698,19 @@ func bandwidthAllocationPatch(w http.ResponseWriter, r *http.Request) {

	//Request body is of BwInfoDeltas Type
	var bwInfoDeltaInput BwInfoDeltas
	bodyBytes, _ := ioutil.ReadAll(r.Body)
	err := json.Unmarshal(bodyBytes, &bwInfoDeltaInput)
	// Read JSON input stream provided in the Request, and stores it in the buffer of a Decoder object
	decoder := json.NewDecoder(r.Body)
	// Decode function return strings containing the text provided in the request body
	err := decoder.Decode(&bwInfoDeltaInput)
	if err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Information of bandwidth allocation of specific allocationId is fetched
	jsonBwInfo, _ := rc.JSONGetEntry(baseKey+"bw_alloc:"+bwallocIdStr, ".")
	keyName := baseKey + "bw_alloc:" + bwallocIdStr
	jsonBwInfo, _ := rc.JSONGetEntry(keyName, ".")

	if jsonBwInfo == "" {
		log.Error("BW Allocation Info not found against the provided allocationId")
@@ -728,6 +730,30 @@ func bandwidthAllocationPatch(w http.ResponseWriter, r *http.Request) {
	// for copying response from request body of type BwInfoDeltas to BwInfo
	var newBwInfo BwInfo

	if bwInfoDeltaInput.AllocationId == "" {
		log.Error("Mandatory attribute allocationId is missing")
		errHandlerProblemDetails(w, "Mandatory attribute allocationId is missing", http.StatusBadRequest)
		return
	}

	if bwInfoDeltaInput.AppInsId == "" {
		log.Error("Mandatory attribute appInsId is missing")
		errHandlerProblemDetails(w, "Mandatory attribute appInsId is missing", http.StatusBadRequest)
		return
	}

	if bwInfoDeltaInput.RequestType == nil {
		log.Error("Mandatory attribute requestType is Missing")
		errHandlerProblemDetails(w, "Mandatory attribute requestType is Missing", http.StatusBadRequest)
		return
	}

	if (*bwInfoDeltaInput.RequestType != 0) && (*bwInfoDeltaInput.RequestType != 1) {
		log.Error("Invalid Mandatory attribute")
		errHandlerProblemDetails(w, "Invalid Mandatory attribute", http.StatusBadRequest)
		return
	}

	//Validating Mandatory attribute
	if bwInfoDeltaInput.AllocationId != "" {
		if bwInfoDeltaInput.AllocationId != bwallocIdStr {
@@ -738,10 +764,6 @@ func bandwidthAllocationPatch(w http.ResponseWriter, r *http.Request) {
			// copying response from type BwInfoDeltas to BwInfo
			newBwInfo.AllocationId = bwInfoDeltaInput.AllocationId
		}
	} else {
		log.Error("Mandatory attribute allocationId is missing")
		errHandlerProblemDetails(w, "Mandatory attribute allocationId is missing", http.StatusBadRequest)
		return
	}

	//Validating Mandatory attribute
@@ -772,17 +794,12 @@ func bandwidthAllocationPatch(w http.ResponseWriter, r *http.Request) {
			// copying response from type BwInfoDeltas to BwInfo
			newBwInfo.AppInsId = bwInfoDeltaInput.AppInsId
		}
	} else {
		log.Error("Mandatory attribute appInsId is missing")
		errHandlerProblemDetails(w, "Mandatory attribute appInsId is missing", http.StatusBadRequest)
		return
	}

	// Patch method does not allow to change appName
	newBwInfo.AppName = bwInfoStored.AppName

	// validate the requested changes in Mandatory attribute requestType (session/application specific)
	if bwInfoDeltaInput.RequestType != nil {
	if (*bwInfoDeltaInput.RequestType == 0) || (*bwInfoDeltaInput.RequestType == 1) {
		// copying data to response body of BwInfo type
		newBwInfo.RequestType = bwInfoDeltaInput.RequestType
@@ -814,15 +831,6 @@ func bandwidthAllocationPatch(w http.ResponseWriter, r *http.Request) {
			log.Error(err.Error())
			return
		}
		} else {
			log.Error("Invalid Mandatory attribute")
			errHandlerProblemDetails(w, "Invalid Mandatory attribute", http.StatusBadRequest)
			return
		}
	} else {
		log.Error("Mandatory attribute requestType is Missing")
		errHandlerProblemDetails(w, "Mandatory attribute requestType is Missing", http.StatusBadRequest)
		return
	}

	seconds := time.Now().Unix()
@@ -1100,11 +1108,14 @@ func bandwidthAllocationPut(w http.ResponseWriter, r *http.Request) {
	bwallocIdStr := vars["allocationId"]

	var bwInfoInput BwInfo
	bodyBytes, _ := ioutil.ReadAll(r.Body)
	err := json.Unmarshal(bodyBytes, &bwInfoInput)

	// Read JSON input stream provided in the Request, and stores it in the buffer of a Decoder object
	decoder := json.NewDecoder(r.Body)
	// Decode function return strings containing the text provided in the request body
	err := decoder.Decode(&bwInfoInput)
	if err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
		errHandlerProblemDetails(w, err.Error(), http.StatusBadRequest)
		return
	}

@@ -1118,7 +1129,8 @@ func bandwidthAllocationPut(w http.ResponseWriter, r *http.Request) {
	}

	// Information of bandwidth allocation of specific allocationId is fetched
	jsonBwInfo, _ := rc.JSONGetEntry(baseKey+"bw_alloc:"+bwallocIdStr, ".")
	keyName := baseKey + "bw_alloc:" + bwallocIdStr
	jsonBwInfo, _ := rc.JSONGetEntry(keyName, ".")

	if jsonBwInfo == "" {
		log.Error("BW Allocation Information not found against the provided allocationId")
@@ -1126,6 +1138,32 @@ func bandwidthAllocationPut(w http.ResponseWriter, r *http.Request) {
		return
	}

	if bwInfoInput.RequestType == nil {
		log.Error("Mandatory attribute requestType is Missing")
		errHandlerProblemDetails(w, "Mandatory attribute requestType is Missing", http.StatusBadRequest)
		return
	}

	if bwInfoInput.AppInsId == "" {
		log.Error("Mandatory attribute appInsId is Missing")
		errHandlerProblemDetails(w, "Mandatory attribute appInsId is Missing", http.StatusBadRequest)
		return
	}

	if bwInfoInput.RequestType != nil {
		if (*bwInfoInput.RequestType != 0) && (*bwInfoInput.RequestType != 1) {
			log.Error("Invalid Mandatory attribute requestType")
			errHandlerProblemDetails(w, "Invalid Mandatory attribute requestType", http.StatusBadRequest)
			return
		}
	}

	if (bwInfoInput.AllocationDirection == "") || (bwInfoInput.FixedAllocation == "") {
		log.Error("Mandatory attribute allocationDirection or fixedAllocation is Missing")
		errHandlerProblemDetails(w, "Mandatory attribute allocationDirection or fixedAllocation is Missing", http.StatusBadRequest)
		return
	}

	var bwInfoStored BwInfo
	err = json.Unmarshal([]byte(jsonBwInfo), &bwInfoStored)
	if err != nil {
@@ -1157,10 +1195,6 @@ func bandwidthAllocationPut(w http.ResponseWriter, r *http.Request) {
				return
			}
		}
	} else {
		log.Error("Mandatory attribute appInsId is Missing")
		errHandlerProblemDetails(w, "Mandatory attribute appInsId is Missing", http.StatusBadRequest)
		return
	}

	// validate the requested changes in Request Type (session/application specific)
@@ -1168,7 +1202,6 @@ func bandwidthAllocationPut(w http.ResponseWriter, r *http.Request) {
		if (*bwInfoInput.RequestType == 0) || (*bwInfoInput.RequestType == 1) {

			if *bwInfoInput.RequestType == 1 {

				if len(bwInfoInput.SessionFilter) > 0 {
					sessionSlice := make([]BwInfoSessionFilter, 0)
					for index, singleSessionFilter := range bwInfoInput.SessionFilter {
@@ -1191,15 +1224,7 @@ func bandwidthAllocationPut(w http.ResponseWriter, r *http.Request) {
				//errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
				return
			}
		} else {
			log.Error("Invalid Mandatory attribute requestType")
			errHandlerProblemDetails(w, "Invalid Mandatory attribute requestType", http.StatusBadRequest)
			return
		}
	} else {
		log.Error("Mandatory attribute requestType is Missing")
		errHandlerProblemDetails(w, "Mandatory attribute requestType is Missing", http.StatusBadRequest)
		return
	}

	seconds := time.Now().Unix()
@@ -1222,10 +1247,6 @@ func bandwidthAllocationPut(w http.ResponseWriter, r *http.Request) {
			errHandlerProblemDetails(w, "Invalid Mandatory attribute allocationDirection", http.StatusBadRequest)
			return
		}
	} else {
		log.Error("Mandatory attribute allocationDirection or fixedAllocation is Missing")
		errHandlerProblemDetails(w, "Mandatory attribute allocationDirection or fixedAllocation is Missing", http.StatusBadRequest)
		return
	}

	// setBwInfo function takes input of new BW allocation information and
@@ -1346,7 +1367,8 @@ func errHandlerProblemDetails(w http.ResponseWriter, error string, code int) {
func setBwInfo(bwInfo BwInfo) ([]byte, error) {
	var jsonResponse []byte
	bwallocIdStr := bwInfo.AllocationId
	_ = rc.JSONSetEntry(baseKey+"bw_alloc:"+bwallocIdStr, ".", convertBandwidthInfoToJson(&bwInfo))
	keyName := baseKey + "bw_alloc:" + bwallocIdStr
	_ = rc.JSONSetEntry(keyName, ".", convertBandwidthInfoToJson(&bwInfo))
	jsonResponse, err := json.Marshal(bwInfo)

	return jsonResponse, err