blob: 248226bd758ab22f0b9306ae2b9c132abf247b20 [file] [log] [blame]
Balint Uveges871fa392019-04-02 20:31:11 +00001/*
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*/
27package main
28
zkoczkaeb2ff0d2019-09-26 16:59:54 +020029//TODO: change flag to pflag (won't need any argument parse)
Balint Uveges871fa392019-04-02 20:31:11 +000030import (
31 "flag"
Balint Uveges871fa392019-04-02 20:31:11 +000032 "os"
kalnagy92162652019-07-02 15:15:49 +020033 "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 Uveges871fa392019-04-02 20:31:11 +000040 "time"
41)
42
43const SERVICENAME = "rtmgr"
44const INTERVAL time.Duration = 2
45
46var (
Peter Szilagyi16d84d62019-04-24 14:51:02 +000047 args map[string]*string
Balint Uveges871fa392019-04-02 20:31:11 +000048)
49
50func parseArgs() {
kalnagy92162652019-07-02 15:15:49 +020051 // TODO: arguments should be validated (filename; xm-url; sbi-if; rest-url; rest-port)
Peter Szilagyi16d84d62019-04-24 14:51:02 +000052 args = make(map[string]*string)
kalnagy92162652019-07-02 15:15:49 +020053 args["configfile"] = flag.String("configfile", "/etc/rtmgrcfg.json", "Routing manager's configuration file path")
zkoczka1892a702019-09-02 10:40:40 +020054 args["nbi"] = flag.String("nbi", "httpRESTful", "Northbound interface module to be used. Valid values are: 'httpGetter | httpRESTful'")
zkoczkaeb2ff0d2019-09-26 16:59:54 +020055 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'")
Peter Szilagyi16d84d62019-04-24 14:51:02 +000057 args["sdl"] = flag.String("sdl", "file", "Datastore enginge 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")
kalnagy92162652019-07-02 15:15:49 +020059 args["nbi-if"] = flag.String("nbi-if", "http://localhost:8888", "Base HTTP URL where routing manager will be listening on")
Peter Szilagyi16d84d62019-04-24 14:51:02 +000060 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")
Balint Uveges871fa392019-04-02 20:31:11 +000063 flag.Parse()
Balint Uveges871fa392019-04-02 20:31:11 +000064}
65
kalnagy92162652019-07-02 15:15:49 +020066func initRtmgr() (nbi.NbiEngine, sbi.SbiEngine, sdl.SdlEngine, rpe.RpeEngine, error) {
Balint Uveges871fa392019-04-02 20:31:11 +000067 var err error
kalnagy92162652019-07-02 15:15:49 +020068 var nbii nbi.NbiEngine
69 var sbii sbi.SbiEngine
70 var sdli sdl.SdlEngine
71 var rpei rpe.RpeEngine
Peter Szilagyi16d84d62019-04-24 14:51:02 +000072 if nbii, err = nbi.GetNbi(*args["nbi"]); err == nil && nbii != nil {
73 if sbii, err = sbi.GetSbi(*args["sbi"]); err == nil && sbii != nil {
74 if sdli, err = sdl.GetSdl(*args["sdl"]); err == nil && sdli != nil {
75 if rpei, err = rpe.GetRpe(*args["rpe"]); err == nil && rpei != nil {
76 return nbii, sbii, sdli, rpei, nil
Balint Uveges871fa392019-04-02 20:31:11 +000077 }
78 }
79 }
80 }
81 return nil, nil, nil, nil, err
82}
83
kalnagy92162652019-07-02 15:15:49 +020084func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.SbiEngine, sdlEngine sdl.SdlEngine, rpeEngine rpe.RpeEngine) {
85 for {
86 if <-triggerSBI {
87 data, err := sdlEngine.ReadAll(*args["filename"])
88 if err != nil || data == nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020089 rtmgr.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020090 continue
91 }
92 sbiEngine.UpdateEndpoints(data)
93 policies := rpeEngine.GeneratePolicies(rtmgr.Eps)
94 err = sbiEngine.DistributeAll(policies)
95 if err != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020096 rtmgr.Logger.Error("Routing table cannot be published due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020097 }
98 }
99 }
100}
101
102func serve(nbiEngine nbi.NbiEngine, sbiEngine sbi.SbiEngine, sdlEngine sdl.SdlEngine, rpeEngine rpe.RpeEngine) {
103
104 triggerSBI := make(chan bool)
105
106 nbiErr := nbiEngine.Initialize(*args["xm-url"], *args["nbi-if"], *args["filename"], *args["configfile"],
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200107 sdlEngine, rpeEngine, triggerSBI)
kalnagy92162652019-07-02 15:15:49 +0200108 if nbiErr != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200109 rtmgr.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
kalnagy92162652019-07-02 15:15:49 +0200110 return
111 }
112
113 err := sbiEngine.Initialize(*args["sbi-if"])
Balint Uveges871fa392019-04-02 20:31:11 +0000114 if err != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200115 rtmgr.Logger.Info("Failed to open push socket due to: " + err.Error())
Balint Uveges871fa392019-04-02 20:31:11 +0000116 return
117 }
kalnagy92162652019-07-02 15:15:49 +0200118 defer nbiEngine.Terminate()
119 defer sbiEngine.Terminate()
120
121 // This SBI Go routine is trtiggered by periodic main loop and when data is recieved on REST interface.
122 go serveSBI(triggerSBI, sbiEngine, sdlEngine, rpeEngine)
123
Balint Uveges871fa392019-04-02 20:31:11 +0000124 for {
125 time.Sleep(INTERVAL * time.Second)
kalnagy92162652019-07-02 15:15:49 +0200126 if *args["nbi"] == "httpGetter" {
127 data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXapps(*args["xm-url"])
128 if err != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200129 rtmgr.Logger.Error("Cannot fetch xapp data due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +0200130 } else if data != nil {
131 sdlEngine.WriteXapps(*args["filename"], data)
132 }
Balint Uveges871fa392019-04-02 20:31:11 +0000133 }
kalnagy92162652019-07-02 15:15:49 +0200134
135 triggerSBI <- true
Balint Uveges871fa392019-04-02 20:31:11 +0000136 }
137}
138
kalnagy92162652019-07-02 15:15:49 +0200139func SetupCloseHandler() {
140 c := make(chan os.Signal, 2)
141 signal.Notify(c, os.Interrupt, syscall.SIGTERM)
142 go func() {
143 <-c
144 rtmgr.Logger.Info("\r- Ctrl+C pressed in Terminal")
145 os.Exit(0)
146 }()
147}
148
Balint Uveges871fa392019-04-02 20:31:11 +0000149func main() {
150 parseArgs()
Peter Szilagyi16d84d62019-04-24 14:51:02 +0000151 rtmgr.SetLogLevel(*args["loglevel"])
kalnagy92162652019-07-02 15:15:49 +0200152 nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr()
Balint Uveges871fa392019-04-02 20:31:11 +0000153 if err != nil {
154 rtmgr.Logger.Error(err.Error())
155 os.Exit(1)
156 }
kalnagy92162652019-07-02 15:15:49 +0200157 SetupCloseHandler()
Balint Uveges871fa392019-04-02 20:31:11 +0000158 rtmgr.Logger.Info("Start " + SERVICENAME + " service")
Peter Szilagyi16d84d62019-04-24 14:51:02 +0000159 rtmgr.Eps = make(rtmgr.Endpoints)
kalnagy92162652019-07-02 15:15:49 +0200160 serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine)
Balint Uveges871fa392019-04-02 20:31:11 +0000161 os.Exit(0)
162}