blob: 24b917c4ee696f05ded1c551cbdff4a7028c9883 [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'")
zkoczkaaaf8d392019-10-02 17:16:06 +020057 args["sdl"] = flag.String("sdl", "file", "Data store engine to be used. Valid values are: 'file'")
Peter Szilagyi16d84d62019-04-24 14:51:02 +000058 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")
zkoczkaaaf8d392019-10-02 17:16:06 +020062 args["loglevel"] = flag.String("loglevel", "INFO", "INFO | WARN | ERROR | DEBUG | TRACE")
Balint Uveges871fa392019-04-02 20:31:11 +000063 flag.Parse()
Balint Uveges871fa392019-04-02 20:31:11 +000064}
65
zkoczkaaaf8d392019-10-02 17:16:06 +020066func 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 Uveges871fa392019-04-02 20:31:11 +000072 }
73 }
74 }
75 }
76 return nil, nil, nil, nil, err
77}
78
zkoczkaaaf8d392019-10-02 17:16:06 +020079func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
kalnagy92162652019-07-02 15:15:49 +020080 for {
81 if <-triggerSBI {
82 data, err := sdlEngine.ReadAll(*args["filename"])
83 if err != nil || data == nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020084 rtmgr.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020085 continue
86 }
87 sbiEngine.UpdateEndpoints(data)
88 policies := rpeEngine.GeneratePolicies(rtmgr.Eps)
89 err = sbiEngine.DistributeAll(policies)
90 if err != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020091 rtmgr.Logger.Error("Routing table cannot be published due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020092 }
93 }
94 }
95}
96
zkoczkaaaf8d392019-10-02 17:16:06 +020097func serve(nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
kalnagy92162652019-07-02 15:15:49 +020098
99 triggerSBI := make(chan bool)
100
101 nbiErr := nbiEngine.Initialize(*args["xm-url"], *args["nbi-if"], *args["filename"], *args["configfile"],
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200102 sdlEngine, rpeEngine, triggerSBI)
kalnagy92162652019-07-02 15:15:49 +0200103 if nbiErr != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200104 rtmgr.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
kalnagy92162652019-07-02 15:15:49 +0200105 return
106 }
107
108 err := sbiEngine.Initialize(*args["sbi-if"])
Balint Uveges871fa392019-04-02 20:31:11 +0000109 if err != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200110 rtmgr.Logger.Info("Failed to open push socket due to: " + err.Error())
Balint Uveges871fa392019-04-02 20:31:11 +0000111 return
112 }
kalnagy92162652019-07-02 15:15:49 +0200113 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 Uveges871fa392019-04-02 20:31:11 +0000119 for {
120 time.Sleep(INTERVAL * time.Second)
kalnagy92162652019-07-02 15:15:49 +0200121 if *args["nbi"] == "httpGetter" {
zkoczkaaaf8d392019-10-02 17:16:06 +0200122 data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(*args["xm-url"])
kalnagy92162652019-07-02 15:15:49 +0200123 if err != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200124 rtmgr.Logger.Error("Cannot fetch xapp data due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +0200125 } else if data != nil {
zkoczkaaaf8d392019-10-02 17:16:06 +0200126 sdlEngine.WriteXApps(*args["filename"], data)
kalnagy92162652019-07-02 15:15:49 +0200127 }
Balint Uveges871fa392019-04-02 20:31:11 +0000128 }
kalnagy92162652019-07-02 15:15:49 +0200129
130 triggerSBI <- true
Balint Uveges871fa392019-04-02 20:31:11 +0000131 }
132}
133
kalnagy92162652019-07-02 15:15:49 +0200134func 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 Uveges871fa392019-04-02 20:31:11 +0000144func main() {
145 parseArgs()
Peter Szilagyi16d84d62019-04-24 14:51:02 +0000146 rtmgr.SetLogLevel(*args["loglevel"])
kalnagy92162652019-07-02 15:15:49 +0200147 nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr()
Balint Uveges871fa392019-04-02 20:31:11 +0000148 if err != nil {
149 rtmgr.Logger.Error(err.Error())
150 os.Exit(1)
151 }
kalnagy92162652019-07-02 15:15:49 +0200152 SetupCloseHandler()
Balint Uveges871fa392019-04-02 20:31:11 +0000153 rtmgr.Logger.Info("Start " + SERVICENAME + " service")
Peter Szilagyi16d84d62019-04-24 14:51:02 +0000154 rtmgr.Eps = make(rtmgr.Endpoints)
kalnagy92162652019-07-02 15:15:49 +0200155 serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine)
Balint Uveges871fa392019-04-02 20:31:11 +0000156 os.Exit(0)
157}