a33437adbac8a7b48302308fb9a2da96b580ae24
[ric-plt/rtmgr.git] / cmd / rtmgr.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    This source code is part of the near-RT RIC (RAN Intelligent Controller)
20    platform project (RICP).
21
22
23 ==================================================================================
24 */
25 /*
26         Mnemonic:       rtmgr.go
27         Abstract:       Routing Manager Main file. Implemets the following functions:
28                         - parseArgs: reading command line arguments
29                         - init:Rtmgr initializing the service modules
30                         - serve: running the loop
31         Date:           12 March 2019
32 */
33 package main
34
35 //TODO: change flag to pflag (won't need any argument parse)
36
37 import (
38         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
39         "os"
40         "os/signal"
41         "routing-manager/pkg/nbi"
42         "routing-manager/pkg/rpe"
43         "routing-manager/pkg/rtmgr"
44         "routing-manager/pkg/sbi"
45         "routing-manager/pkg/sdl"
46         "syscall"
47         "time"
48 )
49
50 const SERVICENAME = "rtmgr"
51 const INTERVAL time.Duration = 60
52
53 func initRtmgr() (nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, err error) {
54         if nbiEngine, err = nbi.GetNbi(xapp.Config.GetString("nbi")); err == nil && nbiEngine != nil {
55                 if sbiEngine, err = sbi.GetSbi(xapp.Config.GetString("sbi")); err == nil && sbiEngine != nil {
56                         if sdlEngine, err = sdl.GetSdl(xapp.Config.GetString("sdl")); err == nil && sdlEngine != nil {
57                                 if rpeEngine, err = rpe.GetRpe(xapp.Config.GetString("rpe")); err == nil && rpeEngine != nil {
58                                         return nbiEngine, sbiEngine, sdlEngine, rpeEngine, nil
59                                 }
60                         }
61                 }
62         }
63         return nil, nil, nil, nil, err
64 }
65
66 func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
67         for {
68                 if <-triggerSBI {
69                         data, err := sdlEngine.ReadAll(xapp.Config.GetString("rtfile"))
70                         if err != nil || data == nil {
71                                 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
72                                 continue
73                         }
74                         sbiEngine.UpdateEndpoints(data)
75                         policies := rpeEngine.GeneratePolicies(rtmgr.Eps)
76                         err = sbiEngine.DistributeAll(policies)
77                         if err != nil {
78                                 xapp.Logger.Error("Routing table cannot be published due to: " + err.Error())
79                         }
80                 }
81         }
82 }
83
84 func serve(nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
85
86         triggerSBI := make(chan bool)
87
88         nbiErr := nbiEngine.Initialize(xapp.Config.GetString("xmurl"), xapp.Config.GetString("nbiurl"), xapp.Config.GetString("rtfile"), xapp.Config.GetString("cfgfile"),
89                 sdlEngine, rpeEngine, triggerSBI)
90         if nbiErr != nil {
91                 xapp.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
92                 return
93         }
94
95         err := sbiEngine.Initialize(xapp.Config.GetString("sbiurl"))
96         if err != nil {
97                 xapp.Logger.Info("Failed to open push socket due to: " + err.Error())
98                 return
99         }
100         defer nbiEngine.Terminate()
101         defer sbiEngine.Terminate()
102
103         // This SBI Go routine is trtiggered by periodic main loop and when data is recieved on REST interface.
104         go serveSBI(triggerSBI, sbiEngine, sdlEngine, rpeEngine)
105
106         for {
107                 if xapp.Config.GetString("nbi") == "httpGetter" {
108                         data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(xapp.Config.GetString("xmurl"))
109                         if err != nil {
110                                 xapp.Logger.Error("Cannot fetch xapp data due to: " + err.Error())
111                         } else if data != nil {
112                                 sdlEngine.WriteXApps(xapp.Config.GetString("rtfile"), data)
113                         }
114                 }
115
116                 triggerSBI <- true
117
118                 time.Sleep(INTERVAL * time.Second)
119                 xapp.Logger.Debug("Periodic loop timed out. Setting triggerSBI flag to distribute updated routes.")
120         }
121 }
122
123 func SetupCloseHandler() {
124         c := make(chan os.Signal, 2)
125         signal.Notify(c, os.Interrupt, syscall.SIGTERM)
126         go func() {
127                 <-c
128                 xapp.Logger.Info("\r- Ctrl+C pressed in Terminal")
129                 os.Exit(0)
130         }()
131 }
132
133 func main() {
134         nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr()
135         if err != nil {
136                 xapp.Logger.Error(err.Error())
137                 os.Exit(1)
138         }
139         SetupCloseHandler()
140         xapp.Logger.Info("Start " + SERVICENAME + " service")
141         rtmgr.Eps = make(rtmgr.Endpoints)
142         serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine)
143         os.Exit(0)
144 }