+
+func getConfigData() (*rtmgr.RicComponents, error) {
+ var data *rtmgr.RicComponents
+ m.Lock()
+ data, err := sdlEngine.ReadAll(xapp.Config.GetString("rtfile"))
+
+ m.Unlock()
+ if data == nil {
+ if err != nil {
+ return nil, errors.New("Cannot get data from sdl interface due to: " + err.Error())
+ } else {
+ xapp.Logger.Debug("Cannot get data from sdl interface, data is null")
+ return nil, errors.New("Cannot get data from sdl interface")
+ }
+ }
+
+ return data, nil
+}
+
+func updateEp() (err error) {
+ data, err := getConfigData()
+ if err != nil {
+ return errors.New("Routing table cannot be published due to: " + err.Error())
+ }
+ EndpointLock.Lock()
+ sbiEngine.UpdateEndpoints(data)
+ EndpointLock.Unlock()
+
+ return nil
+}
+
+func sendPartialRoutesToAll(xappSubData *models.XappSubscriptionData, updatetype rtmgr.RMRUpdateType) (err error) {
+ policies := rpeEngine.GeneratePartialPolicies(rtmgr.Eps, xappSubData, updatetype)
+ err = sbiEngine.DistributeAll(policies)
+ if err != nil {
+ return errors.New("Routing table cannot be published due to: " + err.Error())
+ }
+
+ return nil
+}
+
+func sendRoutesToAll() (err error) {
+
+ data, err := getConfigData()
+ if err != nil {
+ return errors.New("Routing table cannot be published due to: " + err.Error())
+ }
+
+ policies := rpeEngine.GeneratePolicies(rtmgr.Eps, data)
+ err = sbiEngine.DistributeAll(policies)
+ if err != nil {
+ return errors.New("Routing table cannot be published due to: " + err.Error())
+ }
+
+ return nil
+}
+
+func Serve() {
+
+ nbiErr := nbiEngine.Initialize(xapp.Config.GetString("xmurl"), xapp.Config.GetString("nbiurl"), xapp.Config.GetString("rtfile"), xapp.Config.GetString("cfgfile"), xapp.Config.GetString("e2murl"), sdlEngine, rpeEngine, &m)
+ if nbiErr != nil {
+ xapp.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
+ return
+ }
+
+ err := sbiEngine.Initialize(xapp.Config.GetString("sbiurl"))
+ if err != nil {
+ xapp.Logger.Info("Failed to open push socket due to: " + err.Error())
+ return
+ }
+ defer nbiEngine.Terminate()
+ defer sbiEngine.Terminate()
+
+ /* used for rtmgr restart case to connect to Endpoints */
+ go updateEp()
+ time.Sleep(5 * time.Second)
+ sendRoutesToAll()
+ /* Sometimes first message fails, retry after 5 sec */
+ time.Sleep(5 * time.Second)
+ sendRoutesToAll()
+
+ for {
+ xapp.Logger.Debug("Periodic Routes value = %s", xapp.Config.GetString("periodicroutes"))
+ if xapp.Config.GetString("periodicroutes") == "enable" {
+ go updateEp()
+ time.Sleep(5 * time.Second)
+ sendRoutesToAll()
+ }
+
+ rtmgr.Rtmgr_ready = true
+ time.Sleep(INTERVAL * time.Second)
+ xapp.Logger.Debug("Periodic loop timed out. Setting triggerSBI flag to distribute updated routes.")
+ }
+}