Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 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. |
wahidw | 761934a | 2019-11-27 06:07:26 +0000 | [diff] [blame] | 17 | |
| 18 | |
| 19 | This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 20 | platform project (RICP). |
| 21 | |
| 22 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 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 | |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 35 | //TODO: change flag to pflag (won't need any argument parse) |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 36 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 37 | import ( |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 38 | "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 39 | "os" |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 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" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 47 | "time" |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame] | 48 | "sync" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 49 | ) |
| 50 | |
| 51 | const SERVICENAME = "rtmgr" |
prabhukaliswamy | b47d12d | 2019-12-03 15:06:30 +0000 | [diff] [blame] | 52 | const INTERVAL time.Duration = 60 |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 53 | |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 54 | func initRtmgr() (nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, err error) { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 55 | if nbiEngine, err = nbi.GetNbi(xapp.Config.GetString("nbi")); err == nil && nbiEngine != nil { |
| 56 | if sbiEngine, err = sbi.GetSbi(xapp.Config.GetString("sbi")); err == nil && sbiEngine != nil { |
| 57 | if sdlEngine, err = sdl.GetSdl(xapp.Config.GetString("sdl")); err == nil && sdlEngine != nil { |
| 58 | if rpeEngine, err = rpe.GetRpe(xapp.Config.GetString("rpe")); err == nil && rpeEngine != nil { |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 59 | return nbiEngine, sbiEngine, sdlEngine, rpeEngine, nil |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | return nil, nil, nil, nil, err |
| 65 | } |
| 66 | |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 67 | |
| 68 | |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame] | 69 | func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, m *sync.Mutex) { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 70 | for { |
| 71 | if <-triggerSBI { |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame] | 72 | m.Lock() |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 73 | data, err := sdlEngine.ReadAll(xapp.Config.GetString("rtfile")) |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame] | 74 | m.Unlock() |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 75 | if err != nil || data == nil { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 76 | xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 77 | continue |
| 78 | } |
| 79 | sbiEngine.UpdateEndpoints(data) |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame] | 80 | policies := rpeEngine.GeneratePolicies(rtmgr.Eps, data) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 81 | err = sbiEngine.DistributeAll(policies) |
| 82 | if err != nil { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 83 | xapp.Logger.Error("Routing table cannot be published due to: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 89 | func sendRoutesToAll(sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) { |
| 90 | |
| 91 | data, err := sdlEngine.ReadAll(xapp.Config.GetString("rtfile")) |
| 92 | if err != nil || data == nil { |
| 93 | xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error()) |
| 94 | return |
| 95 | } |
| 96 | sbiEngine.UpdateEndpoints(data) |
| 97 | policies := rpeEngine.GeneratePolicies(rtmgr.Eps, data) |
| 98 | err = sbiEngine.DistributeAll(policies) |
| 99 | if err != nil { |
| 100 | xapp.Logger.Error("Routing table cannot be published due to: " + err.Error()) |
| 101 | return |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame] | 106 | func serve(nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, m *sync.Mutex) { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 107 | |
| 108 | triggerSBI := make(chan bool) |
| 109 | |
rangajal | cb93dbf | 2020-02-13 11:06:11 +0000 | [diff] [blame] | 110 | nbiErr := nbiEngine.Initialize(xapp.Config.GetString("xmurl"), xapp.Config.GetString("nbiurl"), xapp.Config.GetString("rtfile"), xapp.Config.GetString("cfgfile"), xapp.Config.GetString("e2murl"), |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame] | 111 | sdlEngine, rpeEngine, triggerSBI, m) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 112 | if nbiErr != nil { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 113 | xapp.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 114 | return |
| 115 | } |
| 116 | |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 117 | err := sbiEngine.Initialize(xapp.Config.GetString("sbiurl")) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 118 | if err != nil { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 119 | xapp.Logger.Info("Failed to open push socket due to: " + err.Error()) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 120 | return |
| 121 | } |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 122 | defer nbiEngine.Terminate() |
| 123 | defer sbiEngine.Terminate() |
| 124 | |
| 125 | // This SBI Go routine is trtiggered by periodic main loop and when data is recieved on REST interface. |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame] | 126 | go serveSBI(triggerSBI, sbiEngine, sdlEngine, rpeEngine, m) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 127 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 128 | for { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 129 | if xapp.Config.GetString("nbi") == "httpGetter" { |
| 130 | data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(xapp.Config.GetString("xmurl")) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 131 | if err != nil { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 132 | xapp.Logger.Error("Cannot fetch xapp data due to: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 133 | } else if data != nil { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 134 | sdlEngine.WriteXApps(xapp.Config.GetString("rtfile"), data) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 135 | } |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 136 | } |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 137 | |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 138 | sendRoutesToAll(sbiEngine, sdlEngine, rpeEngine) |
prabhukaliswamy | b47d12d | 2019-12-03 15:06:30 +0000 | [diff] [blame] | 139 | |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 140 | rtmgr.Rtmgr_ready = true |
prabhukaliswamy | b47d12d | 2019-12-03 15:06:30 +0000 | [diff] [blame] | 141 | time.Sleep(INTERVAL * time.Second) |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 142 | xapp.Logger.Debug("Periodic loop timed out. Setting triggerSBI flag to distribute updated routes.") |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 146 | func SetupCloseHandler() { |
| 147 | c := make(chan os.Signal, 2) |
| 148 | signal.Notify(c, os.Interrupt, syscall.SIGTERM) |
| 149 | go func() { |
| 150 | <-c |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 151 | xapp.Logger.Info("\r- Ctrl+C pressed in Terminal") |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 152 | os.Exit(0) |
| 153 | }() |
| 154 | } |
| 155 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 156 | func main() { |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 157 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 158 | nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr() |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 159 | if err != nil { |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 160 | xapp.Logger.Error(err.Error()) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 161 | os.Exit(1) |
| 162 | } |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 163 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 164 | SetupCloseHandler() |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 165 | |
wahidw | a8596ec | 2019-12-05 06:30:42 +0000 | [diff] [blame] | 166 | xapp.Logger.Info("Start " + SERVICENAME + " service") |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 167 | rtmgr.Eps = make(rtmgr.Endpoints) |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 168 | rtmgr.Rtmgr_ready = false |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame] | 169 | |
| 170 | var m sync.Mutex |
| 171 | |
wahidw | dd6b056 | 2020-03-31 03:09:45 +0000 | [diff] [blame] | 172 | // RMR thread is starting port: 4560 |
| 173 | c := nbi.NewControl() |
| 174 | go c.Run(sbiEngine, sdlEngine, rpeEngine, &m) |
| 175 | |
| 176 | // Waiting for RMR to be ready |
| 177 | time.Sleep(time.Duration(2) * time.Second) |
| 178 | for xapp.Rmr.IsReady() == false { |
| 179 | time.Sleep(time.Duration(2) * time.Second) |
| 180 | } |
| 181 | |
| 182 | dummy_whid := int(xapp.Rmr.Openwh("localhost:4560")) |
| 183 | xapp.Logger.Info("created dummy Wormhole ID for routingmanager and dummy_whid :%d", dummy_whid) |
wahidw | cd7867c | 2020-02-05 10:01:12 +0000 | [diff] [blame] | 184 | |
prabhukaliswamy | e110ee0 | 2019-12-23 09:51:01 +0000 | [diff] [blame] | 185 | serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine, &m) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 186 | os.Exit(0) |
| 187 | } |