blob: 49c5e0a6d3f63acc6fa4c5a53439426961f7118e [file] [log] [blame]
Mohamed Abukar2e78e422019-06-02 11:45:52 +03001/*
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
20package xapp
21
22import (
23 "flag"
24 "github.com/fsnotify/fsnotify"
25 "github.com/spf13/viper"
26 "os"
Mohamed Abukar775722c2019-06-10 16:41:57 +030027 "path/filepath"
Mohamed Abukar2e78e422019-06-02 11:45:52 +030028)
29
Juha Hyttinen065fa1d2020-06-04 10:06:32 +030030//-----------------------------------------------------------------------------
31//
32//-----------------------------------------------------------------------------
33
34type mtype struct {
35 Name string
36 Id int
37}
38
Mohamed Abukar2e78e422019-06-02 11:45:52 +030039type Configurator struct {
40}
41
42type ConfigChangeCB func(filename string)
43
44var ConfigChangeListeners []ConfigChangeCB
45
46func 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 Abukar775722c2019-06-10 16:41:57 +030054func LoadConfig() (l *Log) {
55 l = NewLogger(filepath.Base(os.Args[0]))
Mohamed Abukar2e78e422019-06-02 11:45:52 +030056 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 Hyttinen065fa1d2020-06-04 10:06:32 +030063 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 Abukar2e78e422019-06-02 11:45:52 +030094 viper.WatchConfig()
95 viper.OnConfigChange(func(e fsnotify.Event) {
96 l.Info("config file %s changed ", e.Name)
97
Juha Hyttinen065fa1d2020-06-04 10:06:32 +030098 updatemtypes()
Mohamed Abukar2e78e422019-06-02 11:45:52 +030099 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
110func AddConfigChangeListener(f ConfigChangeCB) {
111 if ConfigChangeListeners == nil {
112 ConfigChangeListeners = make([]ConfigChangeCB, 0)
113 }
114 ConfigChangeListeners = append(ConfigChangeListeners, f)
115}
116
117func (*Configurator) GetString(key string) string {
118 return viper.GetString(key)
119}
120
121func (*Configurator) GetInt(key string) int {
122 return viper.GetInt(key)
123}
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300124
Mohamed Abukarf11ab7a2019-08-14 16:55:01 +0300125func (*Configurator) GetUint32(key string) uint32 {
126 return viper.GetUint32(key)
127}
128
Mohamed Abukar3e611c62019-07-05 13:40:11 +0300129func (*Configurator) GetBool(key string) bool {
130 return viper.GetBool(key)
131}
Mohamed Abukardb268bf2019-10-07 09:42:42 +0300132
133func (*Configurator) Get(key string) interface{} {
134 return viper.Get(key)
135}
136
137func (*Configurator) GetStringSlice(key string) []string {
138 return viper.GetStringSlice(key)
139}
140
141func (*Configurator) GetStringMap(key string) map[string]interface{} {
142 return viper.GetStringMap(key)
Mohamed Abukarf0ee2c62019-10-14 19:29:24 +0300143}