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 ==================================================================================
24 "github.com/fsnotify/fsnotify"
25 "github.com/spf13/viper"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
39 type Configurator struct {
42 type ConfigChangeCB func(filename string)
44 var ConfigChangeListeners []ConfigChangeCB
46 func parseCmd() string {
48 fileName = flag.String("f", os.Getenv("CFG_FILE"), "Specify the configuration file.")
54 func LoadConfig() (l *Log) {
55 l = NewLogger(filepath.Base(os.Args[0]))
56 viper.SetConfigFile(parseCmd())
58 if err := viper.ReadInConfig(); err != nil {
59 l.Error("Reading config file failed: %v", err.Error())
61 l.Info("Using config file: %s", viper.ConfigFileUsed())
63 updatemtypes := func() {
65 viper.UnmarshalKey("rmr.mtypes", &mtypes)
68 l.Info("Config mtypes before RICMessageTypes:%d RicMessageTypeToName:%d", len(RICMessageTypes), len(RicMessageTypeToName))
69 for _, v := range mtypes {
72 if _, ok := RICMessageTypes[v.Name]; ok == false {
75 if _, ok := RicMessageTypeToName[int(v.Id)]; ok == false {
79 l.Error("Config mtypes rmr.mtypes entry skipped due conflict with existing values %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd)
81 l.Info("Config mtypes rmr.mtypes entry added %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd)
82 RICMessageTypes[v.Name] = int(v.Id)
83 RicMessageTypeToName[int(v.Id)] = v.Name
85 l.Info("Config mtypes rmr.mtypes entry skipped %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd)
88 l.Info("Config mtypes after RICMessageTypes:%d RicMessageTypeToName:%d", len(RICMessageTypes), len(RicMessageTypeToName))
95 viper.OnConfigChange(func(e fsnotify.Event) {
96 l.Info("config file %s changed ", e.Name)
99 Logger.SetLevel(viper.GetInt("logger.level"))
100 if len(ConfigChangeListeners) > 0 {
101 for _, f := range ConfigChangeListeners {
110 func AddConfigChangeListener(f ConfigChangeCB) {
111 if ConfigChangeListeners == nil {
112 ConfigChangeListeners = make([]ConfigChangeCB, 0)
114 ConfigChangeListeners = append(ConfigChangeListeners, f)
117 func (*Configurator) GetString(key string) string {
118 return viper.GetString(key)
121 func (*Configurator) GetInt(key string) int {
122 return viper.GetInt(key)
125 func (*Configurator) GetUint32(key string) uint32 {
126 return viper.GetUint32(key)
129 func (*Configurator) GetBool(key string) bool {
130 return viper.GetBool(key)
133 func (*Configurator) Get(key string) interface{} {
134 return viper.Get(key)
137 func (*Configurator) GetStringSlice(key string) []string {
138 return viper.GetStringSlice(key)
141 func (*Configurator) GetStringMap(key string) map[string]interface{} {
142 return viper.GetStringMap(key)