2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================================
25 "github.com/fsnotify/fsnotify"
26 "github.com/spf13/viper"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
40 type Configurator struct {
43 type ConfigChangeCB func(filename string)
45 type SDLNotificationCB func(string, ...string)
47 var ConfigChangeListeners []ConfigChangeCB
49 func parseCmd() string {
51 fileName = flag.String("f", os.Getenv("CFG_FILE"), "Specify the configuration file.")
57 func LoadConfig() (l *Log) {
58 l = NewLogger(filepath.Base(os.Args[0]))
59 viper.SetConfigFile(parseCmd())
61 if err := viper.ReadInConfig(); err != nil {
62 l.Error("Reading config file failed: %v", err.Error())
64 l.Info("Using config file: %s", viper.ConfigFileUsed())
66 updateMTypes := func() {
68 viper.UnmarshalKey("messaging.mtypes", &mtypes)
71 l.Info("Config mtypes before RICMessageTypes:%d RicMessageTypeToName:%d", len(RICMessageTypes), len(RicMessageTypeToName))
72 for _, v := range mtypes {
75 if _, ok := RICMessageTypes[v.Name]; ok == false {
78 if _, ok := RicMessageTypeToName[int(v.Id)]; ok == false {
82 l.Error("Config mtypes rmr.mtypes entry skipped due conflict with existing values %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd)
84 l.Info("Config mtypes rmr.mtypes entry added %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd)
85 RICMessageTypes[v.Name] = int(v.Id)
86 RicMessageTypeToName[int(v.Id)] = v.Name
88 l.Info("Config mtypes rmr.mtypes entry skipped %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd)
91 l.Info("Config mtypes after RICMessageTypes:%d RicMessageTypeToName:%d", len(RICMessageTypes), len(RicMessageTypeToName))
98 viper.OnConfigChange(func(e fsnotify.Event) {
99 l.Info("config file %s changed ", e.Name)
102 if viper.IsSet("controls.logger.level") {
103 Logger.SetLevel(viper.GetInt("controls.logger.level"))
105 Logger.SetLevel(viper.GetInt("logger.level"))
108 if len(ConfigChangeListeners) > 0 {
109 for _, f := range ConfigChangeListeners {
118 func AddConfigChangeListener(f ConfigChangeCB) {
119 if ConfigChangeListeners == nil {
120 ConfigChangeListeners = make([]ConfigChangeCB, 0)
122 ConfigChangeListeners = append(ConfigChangeListeners, f)
125 func PublishConfigChange(appName, eventJson string) error {
126 channel := fmt.Sprintf("CM_UPDATE:%s", appName)
127 if err := Sdl.StoreAndPublish(channel, eventJson, appName, eventJson); err != nil {
128 Logger.Error("Sdl.Store failed: %v", err)
134 func GetPortData(pname string) (d PortData) {
135 var getPolicies = func(policies []interface{}) (plist []int) {
136 for _, p := range policies {
137 plist = append(plist, int(p.(float64)))
142 if viper.IsSet("messaging") == false {
146 if pname == "rmr-data" {
152 for _, v := range viper.GetStringMap("messaging")["ports"].([]interface{}) {
153 if n, ok := v.(map[string]interface{})["name"].(string); ok && n == pname {
155 if p, _ := v.(map[string]interface{})["port"].(float64); ok {
158 if m, _ := v.(map[string]interface{})["maxSize"].(float64); ok {
161 if m, _ := v.(map[string]interface{})["threadType"].(float64); ok {
162 d.ThreadType = int(m)
164 if m, _ := v.(map[string]interface{})["lowLatency"].(bool); ok {
165 d.LowLatency = bool(m)
167 if m, _ := v.(map[string]interface{})["fastAck"].(bool); ok {
170 if m, _ := v.(map[string]interface{})["maxRetryOnFailure"].(float64); ok {
171 d.MaxRetryOnFailure = int(m)
173 if policies, ok := v.(map[string]interface{})["policies"]; ok {
174 d.Policies = getPolicies(policies.([]interface{}))
181 func (*Configurator) SetSDLNotificationCB(appName string, sdlNotificationCb SDLNotificationCB) error {
182 return Sdl.Subscribe(sdlNotificationCb, fmt.Sprintf("CM_UPDATE:%s", appName))
185 func (*Configurator) GetString(key string) string {
186 return viper.GetString(key)
189 func (*Configurator) GetInt(key string) int {
190 return viper.GetInt(key)
193 func (*Configurator) GetUint32(key string) uint32 {
194 return viper.GetUint32(key)
197 func (*Configurator) GetBool(key string) bool {
198 return viper.GetBool(key)
201 func (*Configurator) Get(key string) interface{} {
202 return viper.Get(key)
205 func (*Configurator) GetStringSlice(key string) []string {
206 return viper.GetStringSlice(key)
209 func (*Configurator) GetStringMap(key string) map[string]interface{} {
210 return viper.GetStringMap(key)
213 func (*Configurator) IsSet(key string) bool {
214 return viper.IsSet(key)