X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fxapp%2Frestapi.go;h=24755fae8109afb97c1e19444ff0797416a5859e;hb=refs%2Ftags%2Fv0.7.3;hp=6562dc579e19191ac1dd8148546c583b2c9287d4;hpb=f466893fdbf2f782bb2b2a8207d20c38c8dabdba;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/restapi.go b/pkg/xapp/restapi.go index 6562dc5..24755fa 100755 --- a/pkg/xapp/restapi.go +++ b/pkg/xapp/restapi.go @@ -21,15 +21,25 @@ package xapp import ( "encoding/json" + "fmt" "github.com/gorilla/mux" + "github.com/spf13/viper" "io/ioutil" "net/http" + "os" + + "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/models" ) const ( - ReadyURL = "/ric/v1/health/ready" - AliveURL = "/ric/v1/health/alive" - ConfigURL = "/ric/v1/cm/{name}" + ReadyURL = "/ric/v1/health/ready" + AliveURL = "/ric/v1/health/alive" + ConfigURL = "/ric/v1/cm/{name}" + AppConfigURL = "/ric/v1/config" +) + +var ( + healthReady bool ) type StatusCb func() bool @@ -49,6 +59,7 @@ func NewRouter() *Router { r.InjectRoute(ReadyURL, readyHandler, "GET") r.InjectRoute(AliveURL, aliveHandler, "GET") r.InjectRoute(ConfigURL, configHandler, "POST") + r.InjectRoute(AppConfigURL, appconfigHandler, "GET") return r } @@ -91,7 +102,81 @@ 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 (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 +} + func readyHandler(w http.ResponseWriter, r *http.Request) { + healthReady = true respondWithJSON(w, http.StatusOK, nil) } @@ -130,3 +215,36 @@ func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { w.Write(response) } } + +func appconfigHandler(w http.ResponseWriter, r *http.Request) { + + Logger.Info("Inside appconfigHandler") + + var appconfig models.XappConfigList + var metadata models.ConfigMetadata + var xappconfig models.XAppConfig + name := viper.GetString("name") + configtype := "json" + metadata.XappName = &name + metadata.ConfigType = &configtype + + configFile, err := os.Open("/opt/ric/config/config-file.json") + if err != nil { + Logger.Error("Cannot open config file: %v", err) + respondWithJSON(w, http.StatusInternalServerError, nil) + // return nil,errors.New("Could Not parse the config file") + } + + body, err := ioutil.ReadAll(configFile) + + defer configFile.Close() + + xappconfig.Metadata = &metadata + xappconfig.Config = string(body) + + appconfig = append(appconfig, &xappconfig) + + respondWithJSON(w, http.StatusOK, appconfig) + + //return appconfig,nil +}