X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fxapp%2Frestapi.go;h=6562dc579e19191ac1dd8148546c583b2c9287d4;hb=f22b458846a20a4a9fcafb49e3195ab44a16840e;hp=1eab7b66bb58c4c3bd01968568ff641352a1cc26;hpb=9cfde09a7042e9aaf0f5aa3b97cd2017915e7dbc;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/restapi.go b/pkg/xapp/restapi.go index 1eab7b6..6562dc5 100755 --- a/pkg/xapp/restapi.go +++ b/pkg/xapp/restapi.go @@ -22,12 +22,14 @@ package xapp import ( "encoding/json" "github.com/gorilla/mux" + "io/ioutil" "net/http" ) 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}" ) type StatusCb func() bool @@ -46,6 +48,7 @@ func NewRouter() *Router { // Inject default routes for health probes r.InjectRoute(ReadyURL, readyHandler, "GET") r.InjectRoute(AliveURL, aliveHandler, "GET") + r.InjectRoute(ConfigURL, configHandler, "POST") return r } @@ -62,11 +65,15 @@ func (r *Router) serviceChecker(inner http.HandlerFunc) http.HandlerFunc { } func (r *Router) InjectRoute(url string, handler http.HandlerFunc, method string) *mux.Route { - return r.router.HandleFunc(url, r.serviceChecker(handler)).Methods(method) + return r.router.Path(url).HandlerFunc(r.serviceChecker(handler)).Methods(method) } func (r *Router) InjectQueryRoute(url string, h http.HandlerFunc, m string, q ...string) *mux.Route { - return r.router.HandleFunc(url, r.serviceChecker(h)).Methods(m).Queries(q...) + return r.router.Path(url).HandlerFunc(r.serviceChecker(h)).Methods(m).Queries(q...) +} + +func (r *Router) InjectRoutePrefix(prefix string, handler http.HandlerFunc) *mux.Route { + return r.router.PathPrefix(prefix).HandlerFunc(r.serviceChecker(handler)) } func (r *Router) InjectStatusCb(f StatusCb) { @@ -92,6 +99,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)