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("moId"), viper.GetString("name"))
66 readyCb(readyCbParams)
70 func SetShutdownCB(cb ShutdownCB) {
74 func InstallSignalHandler() {
76 // Signal handlers to really exit program.
77 // shutdownCb can hang until application has
78 // made all needed gracefull shutdown actions
79 // hardcoded limit for shutdown is 20 seconds
81 interrupt := make(chan os.Signal, 1)
82 signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM)
83 //signal handler function
86 if atomic.CompareAndSwapInt32(&shutdownFlag, 0, 1) {
90 sentry := make(chan struct{})
95 if shutdownCb != nil {
101 case <-time.After(time.Duration(timeout) * time.Second):
102 Logger.Info("xapp-frame shutdown callback took more than %d seconds", timeout)
104 Logger.Info("xapp-frame shutdown callback handled within %d seconds", timeout)
109 newCnt := atomic.AddInt32(&shutdownCnt, 1)
110 Logger.Info("xapp-frame shutdown already ongoing. Forced exit counter %d/%d ", newCnt, 5)
112 Logger.Info("xapp-frame shutdown forced exit")
123 // Load xapp configuration
124 Logger = LoadConfig()
126 Logger.SetLevel(viper.GetInt("controls.logger.level"))
127 Resource = NewRouter()
128 Config = Configurator{}
129 Metric = NewMetrics(viper.GetString("metrics.url"), viper.GetString("metrics.namespace"), Resource.router)
130 Subscription = NewSubscriber(viper.GetString("subscription.host"), viper.GetInt("subscription.timeout"))
131 Sdl = NewSDLClient(viper.GetString("db.namespace"))
132 Rnib = NewRNIBClient()
134 InstallSignalHandler()
137 func RunWithParams(c MessageConsumer, sdlcheck bool) {
139 Rmr.SetReadyCB(XappReadyCb, nil)
141 host := fmt.Sprintf(":%d", GetPortData("http").Port)
142 go http.ListenAndServe(host, Resource.router)
143 Logger.Info(fmt.Sprintf("Xapp started, listening on: %s", host))
150 func Run(c MessageConsumer) {
151 RunWithParams(c, true)