Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2019 AT&T Intellectual Property. |
| 4 | Copyright (c) 2019 Nokia |
| 5 | |
| 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 |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 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 | ================================================================================== |
| 18 | */ |
| 19 | |
| 20 | package xapp |
| 21 | |
| 22 | import ( |
| 23 | "flag" |
| 24 | "github.com/fsnotify/fsnotify" |
| 25 | "github.com/spf13/viper" |
| 26 | "os" |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 27 | "path/filepath" |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 28 | ) |
| 29 | |
Juha Hyttinen | 065fa1d | 2020-06-04 10:06:32 +0300 | [diff] [blame^] | 30 | //----------------------------------------------------------------------------- |
| 31 | // |
| 32 | //----------------------------------------------------------------------------- |
| 33 | |
| 34 | type mtype struct { |
| 35 | Name string |
| 36 | Id int |
| 37 | } |
| 38 | |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 39 | type Configurator struct { |
| 40 | } |
| 41 | |
| 42 | type ConfigChangeCB func(filename string) |
| 43 | |
| 44 | var ConfigChangeListeners []ConfigChangeCB |
| 45 | |
| 46 | func parseCmd() string { |
| 47 | var fileName *string |
| 48 | fileName = flag.String("f", os.Getenv("CFG_FILE"), "Specify the configuration file.") |
| 49 | flag.Parse() |
| 50 | |
| 51 | return *fileName |
| 52 | } |
| 53 | |
Mohamed Abukar | 775722c | 2019-06-10 16:41:57 +0300 | [diff] [blame] | 54 | func LoadConfig() (l *Log) { |
| 55 | l = NewLogger(filepath.Base(os.Args[0])) |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 56 | viper.SetConfigFile(parseCmd()) |
| 57 | |
| 58 | if err := viper.ReadInConfig(); err != nil { |
| 59 | l.Error("Reading config file failed: %v", err.Error()) |
| 60 | } |
| 61 | l.Info("Using config file: %s", viper.ConfigFileUsed()) |
| 62 | |
Juha Hyttinen | 065fa1d | 2020-06-04 10:06:32 +0300 | [diff] [blame^] | 63 | updatemtypes := func() { |
| 64 | var mtypes []mtype |
| 65 | viper.UnmarshalKey("rmr.mtypes", &mtypes) |
| 66 | |
| 67 | if len(mtypes) > 0 { |
| 68 | l.Info("Config mtypes before RICMessageTypes:%d RicMessageTypeToName:%d", len(RICMessageTypes), len(RicMessageTypeToName)) |
| 69 | for _, v := range mtypes { |
| 70 | nadd := false |
| 71 | iadd := false |
| 72 | if _, ok := RICMessageTypes[v.Name]; ok == false { |
| 73 | nadd = true |
| 74 | } |
| 75 | if _, ok := RicMessageTypeToName[int(v.Id)]; ok == false { |
| 76 | iadd = true |
| 77 | } |
| 78 | if iadd != nadd { |
| 79 | l.Error("Config mtypes rmr.mtypes entry skipped due conflict with existing values %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd) |
| 80 | } else if 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 |
| 84 | } else { |
| 85 | l.Info("Config mtypes rmr.mtypes entry skipped %s(%t) %d(%t) ", v.Name, nadd, v.Id, iadd) |
| 86 | } |
| 87 | } |
| 88 | l.Info("Config mtypes after RICMessageTypes:%d RicMessageTypeToName:%d", len(RICMessageTypes), len(RicMessageTypeToName)) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | updatemtypes() |
| 93 | |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 94 | viper.WatchConfig() |
| 95 | viper.OnConfigChange(func(e fsnotify.Event) { |
| 96 | l.Info("config file %s changed ", e.Name) |
| 97 | |
Juha Hyttinen | 065fa1d | 2020-06-04 10:06:32 +0300 | [diff] [blame^] | 98 | updatemtypes() |
Mohamed Abukar | 2e78e42 | 2019-06-02 11:45:52 +0300 | [diff] [blame] | 99 | Logger.SetLevel(viper.GetInt("logger.level")) |
| 100 | if len(ConfigChangeListeners) > 0 { |
| 101 | for _, f := range ConfigChangeListeners { |
| 102 | go f(e.Name) |
| 103 | } |
| 104 | } |
| 105 | }) |
| 106 | |
| 107 | return |
| 108 | } |
| 109 | |
| 110 | func AddConfigChangeListener(f ConfigChangeCB) { |
| 111 | if ConfigChangeListeners == nil { |
| 112 | ConfigChangeListeners = make([]ConfigChangeCB, 0) |
| 113 | } |
| 114 | ConfigChangeListeners = append(ConfigChangeListeners, f) |
| 115 | } |
| 116 | |
| 117 | func (*Configurator) GetString(key string) string { |
| 118 | return viper.GetString(key) |
| 119 | } |
| 120 | |
| 121 | func (*Configurator) GetInt(key string) int { |
| 122 | return viper.GetInt(key) |
| 123 | } |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 124 | |
Mohamed Abukar | f11ab7a | 2019-08-14 16:55:01 +0300 | [diff] [blame] | 125 | func (*Configurator) GetUint32(key string) uint32 { |
| 126 | return viper.GetUint32(key) |
| 127 | } |
| 128 | |
Mohamed Abukar | 3e611c6 | 2019-07-05 13:40:11 +0300 | [diff] [blame] | 129 | func (*Configurator) GetBool(key string) bool { |
| 130 | return viper.GetBool(key) |
| 131 | } |
Mohamed Abukar | db268bf | 2019-10-07 09:42:42 +0300 | [diff] [blame] | 132 | |
| 133 | func (*Configurator) Get(key string) interface{} { |
| 134 | return viper.Get(key) |
| 135 | } |
| 136 | |
| 137 | func (*Configurator) GetStringSlice(key string) []string { |
| 138 | return viper.GetStringSlice(key) |
| 139 | } |
| 140 | |
| 141 | func (*Configurator) GetStringMap(key string) map[string]interface{} { |
| 142 | return viper.GetStringMap(key) |
Mohamed Abukar | f0ee2c6 | 2019-10-14 19:29:24 +0300 | [diff] [blame] | 143 | } |