package ue

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"strings"
	"time"

	"nms_nbi/lib/dborm"
	"nms_nbi/lib/global"
	"nms_nbi/lib/log"
	"nms_nbi/lib/services"
	"nms_nbi/restagent/config"
	tokenConst "nms_nbi/src/framework/constants/token"

	"github.com/go-resty/resty/v2"
	"github.com/gorilla/mux"
)

// AmfNBInfo AMF的NodeB信息
type AmfNBInfo struct {
	ID      string `json:"id"`      //NodeB ID
	Name    string `json:"name"`    // NodeB name
	Address string `json:"address"` // 基站地址
	UENum   int    `jons:"ueNum"`   // UE数量
}

// SmfUENum SMF在线用户数
type SmfUENum struct {
	UENum int `json:"ueNum"` // 当前在线用户数
}

// SmfUEInfo SMF在线用户信息
type SmfUEInfo struct {
	IMSI           string `json:"imsi"`
	MSISDN         string `json:"msisdn"`
	RatType        string `json:"ratType"`
	PduSessionInfo []struct {
		PduSessionID int    `json:"pduSessionID"`
		IPv4         string `json:"ipv4"`
		IPv6         string `json:"ipv6"`
		Dnn          string `json:"dnn"`
		Tai          string `json:"tai"`
		SstSD        string `json:"sstSD"`
		UpfN3IP      string `json:"upfN3IP"`
		RanN3IP      string `json:"ranN3IP"`
		Activetime   string `json:"activeTime"`
	} `json:"pduSessionInfo"`
}

// ImsUEInfo IMS在线用户信息
type ImsUEInfo struct {
	IMSI       string `json:"imsi"`
	MSISDN     string `json:"msisdn"`
	IMPU       string `json:"impu"`
	Barring    int    `json:"barring"`
	RegState   int    `json:"regState"`
	Activetime string `json:"activeTime"`
}

// N3iwfUEInfo N3IWF在线用户信息
type N3iwfUEInfo struct {
	IMSI string `json:"imsi"`

	NAI        string `json:"nai"`
	RegState   int    `json:"regState"`
	Activetime string `json:"activeTime"`
}

type N3iwfUEData struct {
	Data []N3iwfUEInfo `json:"data"`
}

var (
	UriNBInfo            = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/nbInfo"
	UriUEInfo            = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/ueInfo"
	UriUENum             = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/ueNum"
	UriPCFUser           = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo"
	UriPCFUserM          = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo/batch/{number}"
	UriPCFUserFileImport = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo/file/import"
	UriPCFUserFileExport = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo/file/export"
	UriNSSFAvailableAMFs = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/availableAMFs"
	UriNSSFSubscriptions = config.DefaultUriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/subscriptions"

	CustomUriNBInfo            = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/nbInfo"
	CustomUriUEInfo            = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/ueInfo"
	CustomUriUENum             = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/ueNum"
	CustomUriPCFUser           = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo"
	CustomUriPCFUserM          = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo/batch/{number}"
	CustomUriPCFUserFileImport = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo/file/import"
	CustomUriPCFUserFileExport = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/pcf/objectType/ueInfo/file/export"
	CustomUriNSSFAvailableAMFs = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/availableAMFs"
	CustomUriNSSFSubscriptions = config.UriPrefix + "/ueManagement/{apiVersion}/elementType/{elementTypeValue}/objectType/subscriptions"
)

var client = resty.New()

func init() {
	/*
		client.
			SetTimeout(10 * time.Second).
			SetRetryCount(1).
			SetRetryWaitTime(1 * time.Second).
			SetRetryMaxWaitTime(2 * time.Second).
			SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
				return 0, errors.New("quota exceeded")
			})
	*/
	client.SetTimeout(500 * time.Millisecond)
}

// Get AvailableAMFs from NSSF
func GetAvailableAMFsFromNSSF(w http.ResponseWriter, r *http.Request) {
	log.Info("GetAvailableAMFsFromNSSF processing... ")

	vars := mux.Vars(r)
	neType := vars["elementTypeValue"]
	if strings.ToLower(neType) != "nssf" {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	//neTypeLower := strings.ToLower(neType)
	var neId string
	neIds := services.GetParamsArrByName("neId", r)
	if len(neIds) == 1 {
		neId = neIds[0]
	} else {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}

	token, err := services.CheckFrontValidRequest(w, r)
	if err != nil {
		log.Error("Request error:", err)
		return
	}
	log.Debug("token:", token)

	neInfo, err := dborm.XormGetNeInfo(neType, neId)
	if err != nil {
		log.Error("Failed to XormGetNeInfo:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else if neInfo == nil {
		err := global.ErrCMNotFoundTargetNE
		log.Error(global.ErrCMNotFoundTargetNE)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	}
	log.Trace("neInfo:", neInfo)

	hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
	requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)

	log.Debug("requestURI2NF:", requestURI2NF)

	resp, err := client.R().
		EnableTrace().
		SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
		SetHeaders(map[string]string{"accessToken": token}).
		SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
		SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
		Get(requestURI2NF)
	if err != nil {
		log.Error("Get system state from NF is failed:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else {
		var response services.MapResponse
		_ = json.Unmarshal(resp.Body(), &response)
		services.ResponseWithJson(w, resp.StatusCode(), response)
		return
	}
}

// Get Subscriptions from NSSF
func GetSubscriptionsFromNSSF(w http.ResponseWriter, r *http.Request) {
	log.Info("GetSubscriptionsFromNSSF processing... ")

	vars := mux.Vars(r)
	neType := vars["elementTypeValue"]
	if strings.ToLower(neType) != "nssf" {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	//neTypeLower := strings.ToLower(neType)
	var neId string
	neIds := services.GetParamsArrByName("neId", r)
	if len(neIds) == 1 {
		neId = neIds[0]
	} else {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}

	token, err := services.CheckFrontValidRequest(w, r)
	if err != nil {
		log.Error("Request error:", err)
		return
	}
	log.Debug("token:", token)

	neInfo, err := dborm.XormGetNeInfo(neType, neId)
	if err != nil {
		log.Error("Failed to XormGetNeInfo:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else if neInfo == nil {
		err := global.ErrCMNotFoundTargetNE
		log.Error(global.ErrCMNotFoundTargetNE)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	}
	log.Trace("neInfo:", neInfo)

	hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
	requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)

	log.Debug("requestURI2NF:", requestURI2NF)

	resp, err := client.R().
		EnableTrace().
		SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
		SetHeaders(map[string]string{"accessToken": token}).
		SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
		SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
		Get(requestURI2NF)
	if err != nil {
		log.Error("Get system state from NF is failed:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else {
		var response services.MapResponse
		_ = json.Unmarshal(resp.Body(), &response)
		services.ResponseWithJson(w, resp.StatusCode(), response)
		return
	}
}

// Get UEInfo from NF/NFs
func GetUEInfoFromNF(w http.ResponseWriter, r *http.Request) {
	log.Info("GetUEInfoFromNF processing... ")

	vars := mux.Vars(r)
	neType := vars["elementTypeValue"]
	if neType == "" {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	//neTypeLower := strings.ToLower(neType)
	var neId string
	neIds := services.GetParamsArrByName("neId", r)
	if len(neIds) == 1 {
		neId = neIds[0]
	} else {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}

	token, err := services.CheckFrontValidRequest(w, r)
	if err != nil {
		log.Error("Request error:", err)
		return
	}
	log.Debug("token:", token)

	neInfo, err := dborm.XormGetNeInfo(neType, neId)
	if err != nil {
		log.Error("Failed to XormGetNeInfo:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else if neInfo == nil {
		err := global.ErrCMNotFoundTargetNE
		log.Error(global.ErrCMNotFoundTargetNE)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	}
	log.Trace("neInfo:", neInfo)

	hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
	requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)

	log.Debug("requestURI2NF:", requestURI2NF)

	resp, err := client.R().
		EnableTrace().
		SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
		SetHeaders(map[string]string{"accessToken": token}).
		SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
		SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
		Get(requestURI2NF)
	if err != nil {
		log.Error("Get system state from NF is failed:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else {
		var response services.MapResponse
		_ = json.Unmarshal(resp.Body(), &response)
		services.ResponseWithJson(w, resp.StatusCode(), response)
		return
	}
}

// POST User Info from NF/NFs
func PostPCFUserInfo(w http.ResponseWriter, r *http.Request) {
	log.Info("PostPCFUserInfo processing... ")

	// vars := mux.Vars(r)
	// neType := vars["elementTypeValue"]
	// if neType == "" {
	// 	services.ResponseNotFound404UriNotExist(w, r)
	// 	return
	// }
	//neTypeLower := strings.ToLower(neType)
	neType := "PCF"
	var neId string
	neIds := services.GetParamsArrByName("neId", r)
	if len(neIds) == 1 {
		neId = neIds[0]
	} else {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}

	// token, err := services.CheckFrontValidRequest(w, r)
	// if err != nil {
	// 	log.Error("Request error:", err)
	// 	return
	// }
	// log.Debug("token:", token)

	neInfo, err := dborm.XormGetNeInfo(neType, neId)
	if err != nil {
		log.Error("Failed to XormGetNeInfo:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else if neInfo == nil {
		err := global.ErrCMNotFoundTargetNE
		log.Error(global.ErrCMNotFoundTargetNE)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	}
	log.Trace("neInfo:", neInfo)

	hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
	requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)

	log.Debug("requestURI2NF:", requestURI2NF)

	body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen)) //io.LimitReader限制大小
	if err != nil {
		log.Error("io.ReadAll is failed:", err)
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	client.SetTimeout(1 * time.Minute)
	resp, err := client.R().
		EnableTrace().
		SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
		//SetHeaders(map[string]string{"accessToken": token}).
		SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
		SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
		SetBody(body).
		Post(requestURI2NF)
	if err != nil {
		log.Error("Failed to post:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else {
		var response services.MapResponse
		_ = json.Unmarshal(resp.Body(), &response)
		services.ResponseWithJson(w, resp.StatusCode(), response)
		return
	}
}

// PUT PCF User Info from NF/NFs
func PutPCFUserInfo(w http.ResponseWriter, r *http.Request) {
	log.Info("PutPCFUserInfo processing... ")

	// vars := mux.Vars(r)
	// neType := vars["elementTypeValue"]
	// if neType == "" {
	// 	services.ResponseNotFound404UriNotExist(w, r)
	// 	return
	// }
	//neTypeLower := strings.ToLower(neType)
	neType := "PCF"
	var neId string
	neIds := services.GetParamsArrByName("neId", r)
	if len(neIds) == 1 {
		neId = neIds[0]
	} else {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}

	// token, err := services.CheckFrontValidRequest(w, r)
	// if err != nil {
	// 	log.Error("Request error:", err)
	// 	return
	// }
	// log.Debug("token:", token)

	neInfo, err := dborm.XormGetNeInfo(neType, neId)
	if err != nil {
		log.Error("Failed to XormGetNeInfo:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else if neInfo == nil {
		err := global.ErrCMNotFoundTargetNE
		log.Error(global.ErrCMNotFoundTargetNE)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	}
	log.Trace("neInfo:", neInfo)

	hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
	requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)

	log.Debug("requestURI2NF:", requestURI2NF)

	body, err := io.ReadAll(io.LimitReader(r.Body, global.RequestBodyMaxLen)) //io.LimitReader限制大小
	if err != nil {
		log.Error("io.ReadAll is failed:", err)
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	client.SetTimeout(1 * time.Minute)
	resp, err := client.R().
		EnableTrace().
		SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
		//SetHeaders(map[string]string{"accessToken": token}).
		SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
		SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
		SetBody(body).
		Put(requestURI2NF)
	if err != nil {
		log.Error("Failed to put:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else {
		var response services.MapResponse
		_ = json.Unmarshal(resp.Body(), &response)
		services.ResponseWithJson(w, resp.StatusCode(), response)
		return
	}
}

// Get UEInfo from NF/NFs
func DeletePCFUserInfo(w http.ResponseWriter, r *http.Request) {
	log.Info("DeletePCFUserInfo processing... ")

	// vars := mux.Vars(r)
	// neType := vars["elementTypeValue"]
	// if neType == "" {
	// 	services.ResponseNotFound404UriNotExist(w, r)
	// 	return
	// }
	//neTypeLower := strings.ToLower(neType)
	neType := "PCF"
	var neId string
	neIds := services.GetParamsArrByName("neId", r)
	if len(neIds) == 1 {
		neId = neIds[0]
	} else {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}

	// token, err := services.CheckFrontValidRequest(w, r)
	// if err != nil {
	// 	log.Error("Request error:", err)
	// 	return
	// }
	// log.Debug("token:", token)

	neInfo, err := dborm.XormGetNeInfo(neType, neId)
	if err != nil {
		log.Error("Failed to XormGetNeInfo:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else if neInfo == nil {
		err := global.ErrCMNotFoundTargetNE
		log.Error(global.ErrCMNotFoundTargetNE)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	}
	log.Trace("neInfo:", neInfo)

	hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
	requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)

	log.Debug("requestURI2NF:", requestURI2NF)

	resp, err := client.R().
		EnableTrace().
		SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
		//SetHeaders(map[string]string{"accessToken": token}).
		SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
		SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
		Delete(requestURI2NF)
	if err != nil {
		log.Error("Failed to delete:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else {
		w.WriteHeader(resp.StatusCode())
		w.Write(resp.Body())
		// var response services.MapResponse
		// _ = json.Unmarshal(resp.Body(), &response)
		// services.ResponseWithJson(w, resp.StatusCode(), response)
		return
	}
}

// Get UEInfo from SMF
func GetUENumFromNF(w http.ResponseWriter, r *http.Request) {
	log.Info("GetUENumFromNF processing... ")

	vars := mux.Vars(r)
	neType := vars["elementTypeValue"]
	if neType == "" {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	//neTypeLower := strings.ToLower(neType)
	var neId string
	neIds := services.GetParamsArrByName("neId", r)
	if len(neIds) == 1 {
		neId = neIds[0]
	} else {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}

	token, err := services.CheckFrontValidRequest(w, r)
	if err != nil {
		log.Error("Request error:", err)
		return
	}
	log.Debug("token:", token)

	neInfo, err := dborm.XormGetNeInfo(neType, neId)
	if err != nil {
		log.Error("Failed to XormGetNeInfo:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else if neInfo == nil {
		err := global.ErrCMNotFoundTargetNE
		log.Error(global.ErrCMNotFoundTargetNE)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	}
	log.Trace("neInfo:", neInfo)

	hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
	requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)

	log.Debug("requestURI2NF:", requestURI2NF)

	resp, err := client.R().
		EnableTrace().
		SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
		SetHeaders(map[string]string{"accessToken": token}).
		SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
		SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
		Get(requestURI2NF)
	if err != nil {
		log.Error("Get system state from NF is failed:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else {
		var response services.MapResponse
		_ = json.Unmarshal(resp.Body(), &response)
		services.ResponseWithJson(w, resp.StatusCode(), response)
		return
	}
}

// Get Radio Info from NF/NFs
func GetNBInfoFromNF(w http.ResponseWriter, r *http.Request) {
	log.Info("GetNBInfoFromNF processing... ")

	vars := mux.Vars(r)
	neType := vars["elementTypeValue"]
	if neType == "" {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	//neTypeLower := strings.ToLower(neType)
	var neId string
	neIds := services.GetParamsArrByName("neId", r)
	if len(neIds) == 1 {
		neId = neIds[0]
	} else {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}

	token, err := services.CheckFrontValidRequest(w, r)
	if err != nil {
		log.Error("Request error:", err)
		return
	}
	log.Debug("token:", token)

	neInfo, err := dborm.XormGetNeInfo(neType, neId)
	if err != nil {
		log.Error("Failed to XormGetNeInfo:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else if neInfo == nil {
		err := global.ErrCMNotFoundTargetNE
		log.Error(global.ErrCMNotFoundTargetNE)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	}
	log.Trace("neInfo:", neInfo)

	hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
	requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)

	log.Debug("requestURI2NF:", requestURI2NF)

	resp, err := client.R().
		EnableTrace().
		SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
		SetHeaders(map[string]string{"accessToken": token}).
		SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
		SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
		Get(requestURI2NF)
	if err != nil {
		log.Error("Get system state from NF is failed:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else {
		var response services.MapResponse
		_ = json.Unmarshal(resp.Body(), &response)
		services.ResponseWithJson(w, resp.StatusCode(), response)
		return
	}
}

// Get Radio Info from NF/NFs
func GetNBInfoAllFromNF(w http.ResponseWriter, r *http.Request) {
	log.Info("GetNBInfoAllFromNF processing... ")

	vars := mux.Vars(r)
	neType := vars["elementTypeValue"]
	if neType == "" {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}
	//neTypeLower := strings.ToLower(neType)
	// var neID string
	neIDs := services.GetParamsArrByName("neId", r)
	// if len(neIDs) == 1 {
	// 	neID = neIDs[0]
	// } else {
	// 	services.ResponseNotFound404UriNotExist(w, r)
	// 	return
	// }

	// token, err := services.CheckFrontValidRequest(w, r)
	// if err != nil {
	// 	log.Error("Request error:", err)
	// 	return
	// }
	// log.Debug("token:", token)

	//var ret error
	var statusCode int = 500
	var dataResponse []services.MapResponse
	var neInfos []dborm.NeInfo
	dborm.XormGetNeInfo2(neType, neIDs, &neInfos)
	for _, neInfo := range neInfos {
		// neInfo, err := dborm.XormGetNeInfo(neType, neID)
		// if err != nil {
		// 	log.Error("Failed to XormGetNeInfo:", err)
		// 	services.ResponseInternalServerError500ProcessError(w, err)
		// 	return
		// } else if neInfo == nil {
		// 	err := global.ErrCMNotFoundTargetNE
		// 	log.Error(global.ErrCMNotFoundTargetNE)
		// 	services.ResponseInternalServerError500ProcessError(w, err)
		// 	return
		// }

		hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
		requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)

		log.Debug("requestURI2NF:", requestURI2NF)

		resp, err := client.R().
			EnableTrace().
			SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
			//SetHeaders(map[string]string{"accessToken": token}).
			SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
			SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
			Get(requestURI2NF)
		if err != nil {
			log.Error("Failed to GET:", err)
			continue
			// services.ResponseInternalServerError500ProcessError(w, err)
			// return
		} else {
			switch resp.StatusCode() {
			case http.StatusOK, http.StatusCreated, http.StatusNoContent, http.StatusAccepted:
				var response services.MapResponse
				_ = json.Unmarshal(resp.Body(), &response)
				dataResponse = append(dataResponse, response)
				statusCode = http.StatusOK
			}
		}
	}
	var response services.DataResponse
	response.Data = dataResponse
	services.ResponseWithJson(w, statusCode, response)
}

// Get GetUEInfoFileExportNF from NF/NFs
func GetUEInfoFileExportNF(w http.ResponseWriter, r *http.Request) {
	log.Info("GetUEInfoFromNF processing... ")

	// vars := mux.Vars(r)
	// neType := vars["elementTypeValue"]
	// if neType == "" {
	// 	services.ResponseNotFound404UriNotExist(w, r)
	// 	return
	// }
	//neTypeLower := strings.ToLower(neType)
	var neId string
	neIds := services.GetParamsArrByName("neId", r)
	if len(neIds) == 1 {
		neId = neIds[0]
	} else {
		services.ResponseNotFound404UriNotExist(w, r)
		return
	}

	// token, err := services.CheckFrontValidRequest(w, r)
	// if err != nil {
	// 	log.Error("Request error:", err)
	// 	return
	// }
	// log.Debug("token:", token)

	neInfo, err := dborm.XormGetNeInfo("PCF", neId)
	if err != nil {
		log.Error("Failed to XormGetNeInfo:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else if neInfo == nil {
		err := global.ErrCMNotFoundTargetNE
		log.Error(global.ErrCMNotFoundTargetNE)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	}
	log.Trace("neInfo:", neInfo)

	hostUri := fmt.Sprintf("http://%s:%v", neInfo.Ip, neInfo.Port)
	requestURI2NF := fmt.Sprintf("%s%s", hostUri, r.RequestURI)

	log.Debug("requestURI2NF:", requestURI2NF)

	client.SetTimeout(3 * time.Minute)
	resp, err := client.R().
		EnableTrace().
		SetHeaders(map[string]string{tokenConst.HEADER_KEY: r.Header.Get(tokenConst.HEADER_KEY)}).
		// SetHeaders(map[string]string{"accessToken": token}).
		SetHeaders(map[string]string{"User-Agent": config.GetDefaultUserAgent()}).
		SetHeaders(map[string]string{"Content-Type": "application/json;charset=UTF-8"}).
		Get(requestURI2NF)
	if err != nil {
		log.Error("Get system state from NF is failed:", err)
		services.ResponseInternalServerError500ProcessError(w, err)
		return
	} else {
		w.WriteHeader(resp.StatusCode())
		w.Write(resp.Body())
		// var response services.MapResponse
		// _ = json.Unmarshal(resp.Body(), &response)
		// services.ResponseWithJson(w, resp.StatusCode(), response)
		return
	}
}
