X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fxapp%2Frestapi.go;h=205f5f6f8a6c50120ba29d90bdf2a5548b4f8cdb;hb=refs%2Fchanges%2F40%2F6340%2F1;hp=e335c0835960153398b7cf063c91e323cb97a783;hpb=256c304042f0cd6c766db505fe5b03bbc3163fef;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/restapi.go b/pkg/xapp/restapi.go index e335c08..205f5f6 100755 --- a/pkg/xapp/restapi.go +++ b/pkg/xapp/restapi.go @@ -21,12 +21,14 @@ package xapp import ( "encoding/json" - "github.com/gorilla/mux" - "github.com/spf13/viper" + "fmt" "io/ioutil" "net/http" "os" + "github.com/gorilla/mux" + "github.com/spf13/viper" + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models" ) @@ -101,6 +103,117 @@ func (r *Router) CheckStatus() (status bool) { return } +func (r *Router) GetSymptomDataParams(w http.ResponseWriter, req *http.Request) SymptomDataParams { + Logger.Info("GetSymptomDataParams ...") + + params := SymptomDataParams{} + queryParams := req.URL.Query() + + Logger.Info("GetSymptomDataParams: %+v", queryParams) + + 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) CollectDefaultSymptomData(fileName string, data interface{}) string { + baseDir := Config.GetString("controls.symptomdata.baseDir") + if baseDir == "" { + baseDir = "/tmp/xapp/" + } + + if err := Util.CreateDir(baseDir); err != nil { + Logger.Error("CreateDir failed: %v", err) + return "" + } + + if metrics, err := r.GetLocalMetrics(GetPortData("http").Port); err == nil { + if err := Util.WriteToFile(baseDir+"metrics.json", metrics); err != nil { + Logger.Error("writeToFile failed for metrics.json: %v", err) + } + } + + if data != nil { + if b, err := json.MarshalIndent(data, "", " "); err == nil { + Util.WriteToFile(baseDir+fileName, string(b)) + } + } + + rtPath := os.Getenv("RMR_STASH_RT") + if rtPath == "" { + return baseDir + } + + input, err := ioutil.ReadFile(rtPath) + if err != nil { + Logger.Error("ioutil.ReadFile failed: %v", err) + return baseDir + } + + Util.WriteToFile(baseDir+"rttable.txt", string(input)) + return baseDir +} + +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.MarshalIndent(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 (r *Router) GetLocalMetrics(port int) (string, error) { + resp, err := http.Get(fmt.Sprintf("http://localhost:%d/ric/v1/metrics", port)) + if err != nil { + return "", err + } + defer resp.Body.Close() + + metrics, err := ioutil.ReadAll(resp.Body) + if err != nil { + return "", err + } + + return string(metrics), nil +} + func IsHealthProbeReady() bool { return healthReady }