X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=pkg%2Fxapp%2Frestapi.go;h=46e81f633fbba0d7876e7d9474f59600246ca037;hb=d9ff71cf313086a71c417273a20b378a98c681fc;hp=922e42d5fd26dd7e117c16d736e034d37ac416d3;hpb=5bd7273045e37ac20a0d79c03e9e9a24415b2b67;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/restapi.go b/pkg/xapp/restapi.go index 922e42d..46e81f6 100755 --- a/pkg/xapp/restapi.go +++ b/pkg/xapp/restapi.go @@ -22,12 +22,19 @@ package xapp import ( "encoding/json" "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" + ReadyURL = "/ric/v1/health/ready" + AliveURL = "/ric/v1/health/alive" + ConfigURL = "/ric/v1/cm/{name}" + AppConfigURL = "/ric/v1/config" ) type StatusCb func() bool @@ -46,6 +53,8 @@ func NewRouter() *Router { // Inject default routes for health probes r.InjectRoute(ReadyURL, readyHandler, "GET") r.InjectRoute(AliveURL, aliveHandler, "GET") + r.InjectRoute(ConfigURL, configHandler, "POST") + r.InjectRoute(AppConfigURL, appconfigHandler, "GET") return r } @@ -96,6 +105,29 @@ func aliveHandler(w http.ResponseWriter, r *http.Request) { respondWithJSON(w, http.StatusOK, nil) } +func configHandler(w http.ResponseWriter, r *http.Request) { + xappName := mux.Vars(r)["name"] + if xappName == "" || r.Body == nil { + respondWithJSON(w, http.StatusBadRequest, nil) + return + } + defer r.Body.Close() + + body, err := ioutil.ReadAll(r.Body) + if err != nil { + Logger.Error("ioutil.ReadAll failed: %v", err) + respondWithJSON(w, http.StatusInternalServerError, nil) + return + } + + if err := PublishConfigChange(xappName, string(body)); err != nil { + respondWithJSON(w, http.StatusInternalServerError, nil) + return + } + + respondWithJSON(w, http.StatusOK, nil) +} + func respondWithJSON(w http.ResponseWriter, code int, payload interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(code) @@ -104,3 +136,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 +}