2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
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
29 //TODO: change flag to pflag (won't need any argument parse)
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"
43 const SERVICENAME = "rtmgr"
44 const INTERVAL time.Duration = 2
47 args map[string]*string
51 // TODO: arguments should be validated (filename; xm-url; sbi-if; rest-url; rest-port)
52 args = make(map[string]*string)
53 args["configfile"] = flag.String("configfile", "/etc/rtmgrcfg.json", "Routing manager's configuration file path")
54 args["nbi"] = flag.String("nbi", "httpRESTful", "Northbound interface module to be used. Valid values are: 'httpGetter | httpRESTful'")
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'")
57 args["sdl"] = flag.String("sdl", "file", "Data store engine to be used. Valid values are: 'file'")
58 args["xm-url"] = flag.String("xm-url", "http://localhost:3000/xapps", "HTTP URL where xApp Manager exposes the entire xApp List")
59 args["nbi-if"] = flag.String("nbi-if", "http://localhost:8888", "Base HTTP URL where routing manager will be listening on")
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")
62 args["loglevel"] = flag.String("loglevel", "INFO", "INFO | WARN | ERROR | DEBUG | TRACE")
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
76 return nil, nil, nil, nil, err
79 func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
82 data, err := sdlEngine.ReadAll(*args["filename"])
83 if err != nil || data == nil {
84 rtmgr.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
87 sbiEngine.UpdateEndpoints(data)
88 policies := rpeEngine.GeneratePolicies(rtmgr.Eps)
89 err = sbiEngine.DistributeAll(policies)
91 rtmgr.Logger.Error("Routing table cannot be published due to: " + err.Error())
97 func serve(nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
99 triggerSBI := make(chan bool)
101 nbiErr := nbiEngine.Initialize(*args["xm-url"], *args["nbi-if"], *args["filename"], *args["configfile"],
102 sdlEngine, rpeEngine, triggerSBI)
104 rtmgr.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
108 err := sbiEngine.Initialize(*args["sbi-if"])
110 rtmgr.Logger.Info("Failed to open push socket due to: " + err.Error())
113 defer nbiEngine.Terminate()
114 defer sbiEngine.Terminate()
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)
120 time.Sleep(INTERVAL * time.Second)
121 if *args["nbi"] == "httpGetter" {
122 data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(*args["xm-url"])
124 rtmgr.Logger.Error("Cannot fetch xapp data due to: " + err.Error())
125 } else if data != nil {
126 sdlEngine.WriteXApps(*args["filename"], data)
134 func SetupCloseHandler() {
135 c := make(chan os.Signal, 2)
136 signal.Notify(c, os.Interrupt, syscall.SIGTERM)
139 rtmgr.Logger.Info("\r- Ctrl+C pressed in Terminal")
146 rtmgr.SetLogLevel(*args["loglevel"])
147 nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr()
149 rtmgr.Logger.Error(err.Error())
153 rtmgr.Logger.Info("Start " + SERVICENAME + " service")
154 rtmgr.Eps = make(rtmgr.Endpoints)
155 serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine)