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/spf13/viper"
33 type ReadyCB func(interface{})
34 type ShutdownCB func()
37 // XApp is an application instance
45 Subscription *Subscriber
48 readyCbParams interface{}
55 return Rmr != nil && Rmr.IsReady() && Sdl != nil && Sdl.IsReady()
58 func SetReadyCB(cb ReadyCB, params interface{}) {
60 readyCbParams = params
63 func xappReadyCb(params interface{}) {
64 Alarm = NewAlarmClient(viper.GetString("alarm.MOId"), viper.GetString("alarm.APPId"))
66 readyCb(readyCbParams)
70 func SetShutdownCB(cb ShutdownCB) {
75 // Load xapp configuration
78 Logger.SetLevel(viper.GetInt("logger.level"))
79 Resource = NewRouter()
80 Config = Configurator{}
81 Metric = NewMetrics(viper.GetString("metrics.url"), viper.GetString("metrics.namespace"), Resource.router)
82 Subscription = NewSubscriber(viper.GetString("subscription.host"), viper.GetInt("subscription.timeout"))
84 if viper.IsSet("db.namespaces") {
85 namespaces := viper.GetStringSlice("db.namespaces")
86 if len(namespaces) > 0 && namespaces[0] != "" {
87 Sdl = NewSDLClient(viper.GetStringSlice("db.namespaces")[0])
89 if len(namespaces) > 1 && namespaces[1] != "" {
90 Rnib = NewRNIBClient(viper.GetStringSlice("db.namespaces")[1])
93 Sdl = NewSDLClient(viper.GetString("db.namespace"))
97 // Signal handlers to really exit program.
98 // shutdownCb can hang until application has
99 // made all needed gracefull shutdown actions
100 // hardcoded limit for shutdown is 20 seconds
102 interrupt := make(chan os.Signal, 1)
103 signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM)
104 //signal handler function
106 for _ = range interrupt {
107 if atomic.CompareAndSwapInt32(&shutdownFlag, 0, 1) {
111 sentry := make(chan struct{})
116 if shutdownCb != nil {
122 case <-time.After(time.Duration(timeout) * time.Second):
123 Logger.Info("xapp-frame shutdown callback took more than %d seconds", timeout)
125 Logger.Info("xapp-frame shutdown callback handled within %d seconds", timeout)
130 newCnt := atomic.AddInt32(&shutdownCnt, 1)
131 Logger.Info("xapp-frame shutdown already ongoing. Forced exit counter %d/%d ", newCnt, 5)
133 Logger.Info("xapp-frame shutdown forced exit")
143 func RunWithParams(c MessageConsumer, sdlcheck bool) {
145 Rmr.SetReadyCB(xappReadyCb, nil)
146 go http.ListenAndServe(viper.GetString("local.host"), Resource.router)
147 Logger.Info(fmt.Sprintf("Xapp started, listening on: %s", viper.GetString("local.host")))
154 func Run(c MessageConsumer) {
155 RunWithParams(c, true)