Support for SDL config update via REST i/f 03/4703/1 v0.5.1
authorMohamed Abukar <abukar.mohamed@nokia.com>
Tue, 15 Sep 2020 06:40:07 +0000 (09:40 +0300)
committerMohamed Abukar <abukar.mohamed@nokia.com>
Tue, 15 Sep 2020 06:40:13 +0000 (09:40 +0300)
Change-Id: Ib214b97c4875967c77f48c5a1f92b9a1ae3e55fa
Signed-off-by: Mohamed Abukar <abukar.mohamed@nokia.com>
pkg/xapp/config.go [changed mode: 0644->0755]
pkg/xapp/restapi.go

old mode 100644 (file)
new mode 100755 (executable)
index 49c5e0a..1b4b806
@@ -21,6 +21,7 @@ package xapp
 
 import (
        "flag"
+       "fmt"
        "github.com/fsnotify/fsnotify"
        "github.com/spf13/viper"
        "os"
@@ -41,6 +42,8 @@ type Configurator struct {
 
 type ConfigChangeCB func(filename string)
 
+type SDLNotificationCB func(string, ...string)
+
 var ConfigChangeListeners []ConfigChangeCB
 
 func parseCmd() string {
@@ -114,6 +117,19 @@ func AddConfigChangeListener(f ConfigChangeCB) {
        ConfigChangeListeners = append(ConfigChangeListeners, f)
 }
 
+func PublishConfigChange(appName, eventJson string) error {
+       channel := fmt.Sprintf("CM_UPDATE:%s", appName)
+       if err := Sdl.StoreAndPublish(channel, eventJson, appName, eventJson); err != nil {
+               Logger.Error("Sdl.Store failed: %v", err)
+               return err
+       }
+       return nil
+}
+
+func (*Configurator) SetSDLNotificationCB(appName string, sdlNotificationCb SDLNotificationCB) error {
+       return Sdl.Subscribe(sdlNotificationCb, fmt.Sprintf("CM_UPDATE:%s", appName))
+}
+
 func (*Configurator) GetString(key string) string {
        return viper.GetString(key)
 }
index 922e42d..6562dc5 100755 (executable)
@@ -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
 }
@@ -96,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)