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. |
| 17 | ================================================================================== |
| 18 | */ |
| 19 | /* |
| 20 | Mnemonic: rtmgr.go |
| 21 | Abstract: Routing Manager Main file. Implemets the following functions: |
| 22 | - parseArgs: reading command line arguments |
| 23 | - init:Rtmgr initializing the service modules |
| 24 | - serve: running the loop |
| 25 | Date: 12 March 2019 |
| 26 | */ |
| 27 | package main |
| 28 | |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 29 | //TODO: change flag to pflag (won't need any argument parse) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 30 | import ( |
| 31 | "flag" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 32 | "os" |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 33 | "os/signal" |
| 34 | "routing-manager/pkg/nbi" |
| 35 | "routing-manager/pkg/rpe" |
| 36 | "routing-manager/pkg/rtmgr" |
| 37 | "routing-manager/pkg/sbi" |
| 38 | "routing-manager/pkg/sdl" |
| 39 | "syscall" |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 40 | "time" |
| 41 | ) |
| 42 | |
| 43 | const SERVICENAME = "rtmgr" |
| 44 | const INTERVAL time.Duration = 2 |
| 45 | |
| 46 | var ( |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 47 | args map[string]*string |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 48 | ) |
| 49 | |
| 50 | func parseArgs() { |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 51 | // 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] | 52 | args = make(map[string]*string) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 53 | 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] | 54 | 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] | 55 | args["sbi"] = flag.String("sbi", "nngpush", "Southbound interface module to be used. Valid values are: 'nngpush") |
| 56 | 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^] | 57 | 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] | 58 | 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] | 59 | 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] | 60 | args["sbi-if"] = flag.String("sbi-if", "0.0.0.0", "IPv4 address of interface where Southbound socket to be opened") |
| 61 | 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^] | 62 | args["loglevel"] = flag.String("loglevel", "INFO", "INFO | WARN | ERROR | DEBUG | TRACE") |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 63 | flag.Parse() |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 64 | } |
| 65 | |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 66 | func initRtmgr() (nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, err error) { |
| 67 | if nbiEngine, err = nbi.GetNbi(*args["nbi"]); err == nil && nbiEngine != nil { |
| 68 | if sbiEngine, err = sbi.GetSbi(*args["sbi"]); err == nil && sbiEngine != nil { |
| 69 | if sdlEngine, err = sdl.GetSdl(*args["sdl"]); err == nil && sdlEngine != nil { |
| 70 | if rpeEngine, err = rpe.GetRpe(*args["rpe"]); err == nil && rpeEngine != nil { |
| 71 | return nbiEngine, sbiEngine, sdlEngine, rpeEngine, nil |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | return nil, nil, nil, nil, err |
| 77 | } |
| 78 | |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 79 | 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] | 80 | for { |
| 81 | if <-triggerSBI { |
| 82 | data, err := sdlEngine.ReadAll(*args["filename"]) |
| 83 | if err != nil || data == nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 84 | rtmgr.Logger.Error("Cannot get data from sdl interface due to: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 85 | continue |
| 86 | } |
| 87 | sbiEngine.UpdateEndpoints(data) |
| 88 | policies := rpeEngine.GeneratePolicies(rtmgr.Eps) |
| 89 | err = sbiEngine.DistributeAll(policies) |
| 90 | if err != nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 91 | rtmgr.Logger.Error("Routing table cannot be published due to: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 97 | 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] | 98 | |
| 99 | triggerSBI := make(chan bool) |
| 100 | |
| 101 | nbiErr := nbiEngine.Initialize(*args["xm-url"], *args["nbi-if"], *args["filename"], *args["configfile"], |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 102 | sdlEngine, rpeEngine, triggerSBI) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 103 | if nbiErr != nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 104 | rtmgr.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 105 | return |
| 106 | } |
| 107 | |
| 108 | err := sbiEngine.Initialize(*args["sbi-if"]) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 109 | if err != nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 110 | rtmgr.Logger.Info("Failed to open push socket due to: " + err.Error()) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 111 | return |
| 112 | } |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 113 | defer nbiEngine.Terminate() |
| 114 | defer sbiEngine.Terminate() |
| 115 | |
| 116 | // This SBI Go routine is trtiggered by periodic main loop and when data is recieved on REST interface. |
| 117 | go serveSBI(triggerSBI, sbiEngine, sdlEngine, rpeEngine) |
| 118 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 119 | for { |
| 120 | time.Sleep(INTERVAL * time.Second) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 121 | if *args["nbi"] == "httpGetter" { |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 122 | data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(*args["xm-url"]) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 123 | if err != nil { |
zkoczka | eb2ff0d | 2019-09-26 16:59:54 +0200 | [diff] [blame] | 124 | rtmgr.Logger.Error("Cannot fetch xapp data due to: " + err.Error()) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 125 | } else if data != nil { |
zkoczka | aaf8d39 | 2019-10-02 17:16:06 +0200 | [diff] [blame^] | 126 | sdlEngine.WriteXApps(*args["filename"], data) |
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 | } |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 129 | |
| 130 | triggerSBI <- true |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 134 | func SetupCloseHandler() { |
| 135 | c := make(chan os.Signal, 2) |
| 136 | signal.Notify(c, os.Interrupt, syscall.SIGTERM) |
| 137 | go func() { |
| 138 | <-c |
| 139 | rtmgr.Logger.Info("\r- Ctrl+C pressed in Terminal") |
| 140 | os.Exit(0) |
| 141 | }() |
| 142 | } |
| 143 | |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 144 | func main() { |
| 145 | parseArgs() |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 146 | rtmgr.SetLogLevel(*args["loglevel"]) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 147 | nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr() |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 148 | if err != nil { |
| 149 | rtmgr.Logger.Error(err.Error()) |
| 150 | os.Exit(1) |
| 151 | } |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 152 | SetupCloseHandler() |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 153 | rtmgr.Logger.Info("Start " + SERVICENAME + " service") |
Peter Szilagyi | 16d84d6 | 2019-04-24 14:51:02 +0000 | [diff] [blame] | 154 | rtmgr.Eps = make(rtmgr.Endpoints) |
kalnagy | 9216265 | 2019-07-02 15:15:49 +0200 | [diff] [blame] | 155 | serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine) |
Balint Uveges | 871fa39 | 2019-04-02 20:31:11 +0000 | [diff] [blame] | 156 | os.Exit(0) |
| 157 | } |