Capability to add rmr message name-id mapping via configuration
[ric-plt/xapp-frame.git] / pkg / xapp / config.go
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"
27         "path/filepath"
28 )
29
30 //-----------------------------------------------------------------------------
31 //
32 //-----------------------------------------------------------------------------
33
34 type mtype struct {
35         Name string
36         Id   int
37 }
38
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
54 func LoadConfig() (l *Log) {
55         l = NewLogger(filepath.Base(os.Args[0]))
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
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
94         viper.WatchConfig()
95         viper.OnConfigChange(func(e fsnotify.Event) {
96                 l.Info("config file %s changed ", e.Name)
97
98                 updatemtypes()
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 }
124
125 func (*Configurator) GetUint32(key string) uint32 {
126         return viper.GetUint32(key)
127 }
128
129 func (*Configurator) GetBool(key string) bool {
130         return viper.GetBool(key)
131 }
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)
143 }