Prepare for SdlInstance usage removal in xapp-frame
[ric-plt/xapp-frame.git] / pkg / xapp / config.go
index 8d427c5..56986d3 100755 (executable)
@@ -63,9 +63,9 @@ func LoadConfig() (l *Log) {
        }
        l.Info("Using config file: %s", viper.ConfigFileUsed())
 
-       updatemtypes := func() {
+       updateMTypes := func() {
                var mtypes []mtype
-               viper.UnmarshalKey("rmr.mtypes", &mtypes)
+               viper.UnmarshalKey("messaging.mtypes", &mtypes)
 
                if len(mtypes) > 0 {
                        l.Info("Config mtypes before RICMessageTypes:%d RicMessageTypeToName:%d", len(RICMessageTypes), len(RicMessageTypeToName))
@@ -92,14 +92,19 @@ func LoadConfig() (l *Log) {
                }
        }
 
-       updatemtypes()
+       updateMTypes()
 
        viper.WatchConfig()
        viper.OnConfigChange(func(e fsnotify.Event) {
                l.Info("config file %s changed ", e.Name)
 
-               updatemtypes()
-               Logger.SetLevel(viper.GetInt("controls.logger.level"))
+               updateMTypes()
+               if viper.IsSet("controls.logger.level") {
+                       Logger.SetLevel(viper.GetInt("controls.logger.level"))
+               } else {
+                       Logger.SetLevel(viper.GetInt("logger.level"))
+               }
+
                if len(ConfigChangeListeners) > 0 {
                        for _, f := range ConfigChangeListeners {
                                go f(e.Name)
@@ -119,14 +124,35 @@ func AddConfigChangeListener(f ConfigChangeCB) {
 
 func PublishConfigChange(appName, eventJson string) error {
        channel := fmt.Sprintf("CM_UPDATE:%s", appName)
-       if err := Sdl.StoreAndPublish(channel, eventJson, appName, eventJson); err != nil {
+       if err := SdlStorage.StoreAndPublish(getCmSdlNs(), channel, eventJson, appName, eventJson); err != nil {
                Logger.Error("Sdl.Store failed: %v", err)
                return err
        }
        return nil
 }
 
+func ReadConfig(appName string) (map[string]interface{}, error) {
+       return SdlStorage.Read(getCmSdlNs(), appName)
+}
+
 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
+       }
+
+       if viper.IsSet("messaging") == false {
+               if pname == "http" {
+                       d.Port = 8080
+               }
+               if pname == "rmrdata" {
+                       d.Port = 4560
+               }
+               return
+       }
+
        for _, v := range viper.GetStringMap("messaging")["ports"].([]interface{}) {
                if n, ok := v.(map[string]interface{})["name"].(string); ok && n == pname {
                        d.Name = n
@@ -148,13 +174,20 @@ func GetPortData(pname string) (d PortData) {
                        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 getCmSdlNs() string {
+       return fmt.Sprintf("cm/%s", viper.GetString("name"))
+}
+
 func (*Configurator) SetSDLNotificationCB(appName string, sdlNotificationCb SDLNotificationCB) error {
-       return Sdl.Subscribe(sdlNotificationCb, fmt.Sprintf("CM_UPDATE:%s", appName))
+       return SdlStorage.Subscribe(getCmSdlNs(), sdlNotificationCb, fmt.Sprintf("CM_UPDATE:%s", appName))
 }
 
 func (*Configurator) GetString(key string) string {
@@ -184,3 +217,7 @@ func (*Configurator) GetStringSlice(key string) []string {
 func (*Configurator) GetStringMap(key string) map[string]interface{} {
        return viper.GetStringMap(key)
 }
+
+func (*Configurator) IsSet(key string) bool {
+       return viper.IsSet(key)
+}