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) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 36 | import ( |
| 37 | "flag" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 38 | "os" |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 39 | "os/signal" |
| 40 | "routing-manager/pkg/nbi" |
| 41 | "routing-manager/pkg/rpe" |
| 42 | "routing-manager/pkg/rtmgr" |
| 43 | "routing-manager/pkg/sbi" |
| 44 | "routing-manager/pkg/sdl" |
| 45 | "syscall" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 46 | "time" |
| 47 | ) |
| 48 | |
| 49 | const SERVICENAME = "rtmgr" |
| 50 | const INTERVAL time.Duration = 2 |
| 51 | |
| 52 | var ( |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 53 | args map[string]*string |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 54 | ) |
| 55 | |
| 56 | func parseArgs() { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 57 | // TODO: arguments should be validated (filename; xm-url; sbi-if; rest-url; rest-port) |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 58 | args = make(map[string]*string) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 59 | args["configfile"] = flag.String("configfile", "/etc/rtmgrcfg.json", "Routing manager's configuration file path") |
zkoczka | 1892a70 | 2019-09-02 10:40:40 +0200 | [diff] [blame] | 60 | args["nbi"] = flag.String("nbi", "httpRESTful", "Northbound interface module to be used. Valid values are: 'httpGetter | httpRESTful'") |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 61 | args["sbi"] = flag.String("sbi", "nngpush", "Southbound interface module to be used. Valid values are: 'nngpush") |
| 62 | args["rpe"] = flag.String("rpe", "rmrpush", "Route Policy Engine to be used. Valid values are: 'rmrpush'") |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 63 | args["sdl"] = flag.String("sdl", "file", "Data store engine to be used. Valid values are: 'file'") |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 64 | args["xm-url"] = flag.String("xm-url", "http://localhost:3000/xapps", "HTTP URL where xApp Manager exposes the entire xApp List") |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 65 | args["nbi-if"] = flag.String("nbi-if", "http://localhost:8888", "Base HTTP URL where routing manager will be listening on") |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 66 | args["sbi-if"] = flag.String("sbi-if", "0.0.0.0", "IPv4 address of interface where Southbound socket to be opened") |
| 67 | args["filename"] = flag.String("filename", "/db/rt.json", "Absolute path of file where the route information to be stored") |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 68 | args["loglevel"] = flag.String("loglevel", "INFO", "INFO | WARN | ERROR | DEBUG | TRACE") |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 69 | flag.Parse() |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 70 | } |
| 71 | |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 72 | func initRtmgr() (nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, err error) { |
| 73 | if nbiEngine, err = nbi.GetNbi(*args["nbi"]); err == nil && nbiEngine != nil { |
| 74 | if sbiEngine, err = sbi.GetSbi(*args["sbi"]); err == nil && sbiEngine != nil { |
| 75 | if sdlEngine, err = sdl.GetSdl(*args["sdl"]); err == nil && sdlEngine != nil { |
| 76 | if rpeEngine, err = rpe.GetRpe(*args["rpe"]); err == nil && rpeEngine != nil { |
| 77 | return nbiEngine, sbiEngine, sdlEngine, rpeEngine, nil |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | return nil, nil, nil, nil, err |
| 83 | } |
| 84 | |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 85 | func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 86 | for { |
| 87 | if <-triggerSBI { |
| 88 | data, err := sdlEngine.ReadAll(*args["filename"]) |
| 89 | if err != nil || data == nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 90 | rtmgr.Logger.Error("Cannot get data from sdl interface due to: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 91 | continue |
| 92 | } |
| 93 | sbiEngine.UpdateEndpoints(data) |
| 94 | policies := rpeEngine.GeneratePolicies(rtmgr.Eps) |
| 95 | err = sbiEngine.DistributeAll(policies) |
| 96 | if err != nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 97 | rtmgr.Logger.Error("Routing table cannot be published due to: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 103 | func serve(nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 104 | |
| 105 | triggerSBI := make(chan bool) |
| 106 | |
| 107 | nbiErr := nbiEngine.Initialize(*args["xm-url"], *args["nbi-if"], *args["filename"], *args["configfile"], |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 108 | sdlEngine, rpeEngine, triggerSBI) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 109 | if nbiErr != nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 110 | rtmgr.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 111 | return |
| 112 | } |
| 113 | |
| 114 | err := sbiEngine.Initialize(*args["sbi-if"]) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 115 | if err != nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 116 | rtmgr.Logger.Info("Failed to open push socket due to: " + err.Error()) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 117 | return |
| 118 | } |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 119 | defer nbiEngine.Terminate() |
| 120 | defer sbiEngine.Terminate() |
| 121 | |
| 122 | // This SBI Go routine is trtiggered by periodic main loop and when data is recieved on REST interface. |
| 123 | go serveSBI(triggerSBI, sbiEngine, sdlEngine, rpeEngine) |
| 124 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 125 | for { |
| 126 | time.Sleep(INTERVAL * time.Second) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 127 | if *args["nbi"] == "httpGetter" { |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 128 | data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(*args["xm-url"]) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 129 | if err != nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 130 | rtmgr.Logger.Error("Cannot fetch xapp data due to: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 131 | } else if data != nil { |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame] | 132 | sdlEngine.WriteXApps(*args["filename"], data) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 133 | } |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 134 | } |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 135 | |
| 136 | triggerSBI <- true |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 140 | func SetupCloseHandler() { |
| 141 | c := make(chan os.Signal, 2) |
| 142 | signal.Notify(c, os.Interrupt, syscall.SIGTERM) |
| 143 | go func() { |
| 144 | <-c |
| 145 | rtmgr.Logger.Info("\r- Ctrl+C pressed in Terminal") |
| 146 | os.Exit(0) |
| 147 | }() |
| 148 | } |
| 149 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 150 | func main() { |
| 151 | parseArgs() |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 152 | rtmgr.SetLogLevel(*args["loglevel"]) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 153 | nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr() |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 154 | if err != nil { |
| 155 | rtmgr.Logger.Error(err.Error()) |
| 156 | os.Exit(1) |
| 157 | } |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 158 | SetupCloseHandler() |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 159 | rtmgr.Logger.Info("Start " + SERVICENAME + " service") |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 160 | rtmgr.Eps = make(rtmgr.Endpoints) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 161 | serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 162 | os.Exit(0) |
| 163 | } |