X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fxapp%2Frestapi.go;h=40ad8d24d1f098b58fffa98f6c654cfa70063d4a;hb=0ce03386e6dbdb82b88406b22bb91c8cbe18e349;hp=46e81f633fbba0d7876e7d9474f59600246ca037;hpb=413abf5f6de7c1808a2dd716ef52ed586c04d8f6;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/restapi.go b/pkg/xapp/restapi.go index 46e81f6..40ad8d2 100755 --- a/pkg/xapp/restapi.go +++ b/pkg/xapp/restapi.go @@ -21,6 +21,7 @@ package xapp import ( "encoding/json" + "fmt" "github.com/gorilla/mux" "github.com/spf13/viper" "io/ioutil" @@ -37,6 +38,10 @@ const ( AppConfigURL = "/ric/v1/config" ) +var ( + healthReady bool +) + type StatusCb func() bool type Router struct { @@ -97,7 +102,66 @@ func (r *Router) CheckStatus() (status bool) { return } +func (r *Router) GetSymptomDataParams(w http.ResponseWriter, req *http.Request) SymptomDataParams { + params := SymptomDataParams{} + queryParams := req.URL.Query() + + for p := range queryParams { + if p == "timeout" { + fmt.Sscanf(p, "%d", ¶ms.Timeout) + } + if p == "fromtime" { + fmt.Sscanf(p, "%d", ¶ms.FromTime) + } + if p == "totime" { + fmt.Sscanf(p, "%d", ¶ms.ToTime) + } + } + return params +} + +func (r *Router) SendSymptomDataJson(w http.ResponseWriter, req *http.Request, data interface{}, n string) { + w.Header().Set("Content-Type", "application/json") + w.Header().Set("Content-Disposition", "attachment; filename="+n) + w.WriteHeader(http.StatusOK) + if data != nil { + response, _ := json.Marshal(data) + w.Write(response) + } +} + +func (r *Router) SendSymptomDataFile(w http.ResponseWriter, req *http.Request, baseDir, zipFile string) { + // Compress and reply with attachment + tmpFile, err := ioutil.TempFile("", "symptom") + if err != nil { + r.SendSymptomDataError(w, req, "Failed to create a tmp file: "+err.Error()) + return + } + defer os.Remove(tmpFile.Name()) + + var fileList []string + fileList = Util.FetchFiles(baseDir, fileList) + err = Util.ZipFiles(tmpFile, baseDir, fileList) + if err != nil { + r.SendSymptomDataError(w, req, "Failed to zip the files: "+err.Error()) + return + } + + w.Header().Set("Content-Disposition", "attachment; filename="+zipFile) + http.ServeFile(w, req, tmpFile.Name()) +} + +func (r *Router) SendSymptomDataError(w http.ResponseWriter, req *http.Request, message string) { + w.Header().Set("Content-Disposition", "attachment; filename=error_status.txt") + http.Error(w, message, http.StatusInternalServerError) +} + +func IsHealthProbeReady() bool { + return healthReady +} + func readyHandler(w http.ResponseWriter, r *http.Request) { + healthReady = true respondWithJSON(w, http.StatusOK, nil) }