X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fxapp%2Fconfig.go;h=bb76a07234a53b69d1155b18a5fd9f6b04e95a5b;hb=refs%2Fchanges%2F16%2F5016%2F1;hp=94e88a181b23e9aaa9b6ca3b720106fa3c434dfe;hpb=f0ee2c6552c6ea705c6c015e2a4e263532a4ce44;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/config.go b/pkg/xapp/config.go index 94e88a1..bb76a07 100755 --- a/pkg/xapp/config.go +++ b/pkg/xapp/config.go @@ -21,17 +21,29 @@ package xapp import ( "flag" + "fmt" "github.com/fsnotify/fsnotify" "github.com/spf13/viper" "os" "path/filepath" ) +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- + +type mtype struct { + Name string + Id int +} + type Configurator struct { } type ConfigChangeCB func(filename string) +type SDLNotificationCB func(string, ...string) + var ConfigChangeListeners []ConfigChangeCB func parseCmd() string { @@ -51,11 +63,43 @@ func LoadConfig() (l *Log) { } l.Info("Using config file: %s", viper.ConfigFileUsed()) + updatemtypes := func() { + var mtypes []mtype + viper.UnmarshalKey("rmr.mtypes", &mtypes) + + if len(mtypes) > 0 { + l.Info("Config mtypes before RICMessageTypes:%d RicMessageTypeToName:%d", len(RICMessageTypes), len(RicMessageTypeToName)) + for _, v := range mtypes { + nadd := false + iadd := false + if _, ok := RICMessageTypes[v.Name]; ok == false { + nadd = true + } + if _, ok := RicMessageTypeToName[int(v.Id)]; ok == false { + iadd = true + } + if iadd != nadd { + l.Error("Config mtypes rmr.mtypes entry skipped due conflict with existing values %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd) + } else if iadd { + l.Info("Config mtypes rmr.mtypes entry added %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd) + RICMessageTypes[v.Name] = int(v.Id) + RicMessageTypeToName[int(v.Id)] = v.Name + } else { + l.Info("Config mtypes rmr.mtypes entry skipped %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd) + } + } + l.Info("Config mtypes after RICMessageTypes:%d RicMessageTypeToName:%d", len(RICMessageTypes), len(RicMessageTypeToName)) + } + } + + updatemtypes() + viper.WatchConfig() viper.OnConfigChange(func(e fsnotify.Event) { l.Info("config file %s changed ", e.Name) - Logger.SetLevel(viper.GetInt("logger.level")) + updatemtypes() + Logger.SetLevel(viper.GetInt("controls.logger.level")) if len(ConfigChangeListeners) > 0 { for _, f := range ConfigChangeListeners { go f(e.Name) @@ -73,6 +117,56 @@ 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 GetPortData(pname string) (d PortData) { + var getPolicies = func(policies []interface{}) (plist []int) { + for _, p := range policies { + plist = append(plist, int(p.(float64))) + } + return plist + } + + for _, v := range viper.GetStringMap("messaging")["ports"].([]interface{}) { + if n, ok := v.(map[string]interface{})["name"].(string); ok && n == pname { + d.Name = n + if p, _ := v.(map[string]interface{})["port"].(float64); ok { + d.Port = int(p) + } + if m, _ := v.(map[string]interface{})["maxSize"].(float64); ok { + d.MaxSize = int(m) + } + if m, _ := v.(map[string]interface{})["threadType"].(float64); ok { + d.ThreadType = int(m) + } + if m, _ := v.(map[string]interface{})["lowLatency"].(bool); ok { + d.LowLatency = bool(m) + } + if m, _ := v.(map[string]interface{})["fastAck"].(bool); ok { + d.FastAck = bool(m) + } + if m, _ := v.(map[string]interface{})["maxRetryOnFailure"].(float64); ok { + d.MaxRetryOnFailure = int(m) + } + if policies, ok := v.(map[string]interface{})["policies"]; ok { + d.Policies = getPolicies(policies.([]interface{})) + } + } + } + return +} + +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) }