blob: 101c74377313477923de7503f16695e877eac7bc [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.
wahidw761934a2019-11-27 06:07:26 +000017
18
19 This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 platform project (RICP).
21
22
Balint Uveges871fa392019-04-02 20:31:11 +000023==================================================================================
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*/
33package main
34
zkoczkaeb2ff0d2019-09-26 16:59:54 +020035//TODO: change flag to pflag (won't need any argument parse)
Balint Uveges871fa392019-04-02 20:31:11 +000036import (
37 "flag"
Balint Uveges871fa392019-04-02 20:31:11 +000038 "os"
kalnagy92162652019-07-02 15:15:49 +020039 "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 Uveges871fa392019-04-02 20:31:11 +000046 "time"
47)
48
49const SERVICENAME = "rtmgr"
50const INTERVAL time.Duration = 2
51
52var (
Peter Szilagyi16d84d62019-04-24 14:51:02 +000053 args map[string]*string
Balint Uveges871fa392019-04-02 20:31:11 +000054)
55
56func parseArgs() {
kalnagy92162652019-07-02 15:15:49 +020057 // TODO: arguments should be validated (filename; xm-url; sbi-if; rest-url; rest-port)
Peter Szilagyi16d84d62019-04-24 14:51:02 +000058 args = make(map[string]*string)
kalnagy92162652019-07-02 15:15:49 +020059 args["configfile"] = flag.String("configfile", "/etc/rtmgrcfg.json", "Routing manager's configuration file path")
zkoczka1892a702019-09-02 10:40:40 +020060 args["nbi"] = flag.String("nbi", "httpRESTful", "Northbound interface module to be used. Valid values are: 'httpGetter | httpRESTful'")
zkoczkaeb2ff0d2019-09-26 16:59:54 +020061 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'")
zkoczkaaaf8d392019-10-02 17:16:06 +020063 args["sdl"] = flag.String("sdl", "file", "Data store engine to be used. Valid values are: 'file'")
Peter Szilagyi16d84d62019-04-24 14:51:02 +000064 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 +020065 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 +000066 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")
zkoczkaaaf8d392019-10-02 17:16:06 +020068 args["loglevel"] = flag.String("loglevel", "INFO", "INFO | WARN | ERROR | DEBUG | TRACE")
Balint Uveges871fa392019-04-02 20:31:11 +000069 flag.Parse()
Balint Uveges871fa392019-04-02 20:31:11 +000070}
71
zkoczkaaaf8d392019-10-02 17:16:06 +020072func 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 Uveges871fa392019-04-02 20:31:11 +000078 }
79 }
80 }
81 }
82 return nil, nil, nil, nil, err
83}
84
zkoczkaaaf8d392019-10-02 17:16:06 +020085func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
kalnagy92162652019-07-02 15:15:49 +020086 for {
87 if <-triggerSBI {
88 data, err := sdlEngine.ReadAll(*args["filename"])
89 if err != nil || data == nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020090 rtmgr.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020091 continue
92 }
93 sbiEngine.UpdateEndpoints(data)
94 policies := rpeEngine.GeneratePolicies(rtmgr.Eps)
95 err = sbiEngine.DistributeAll(policies)
96 if err != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020097 rtmgr.Logger.Error("Routing table cannot be published due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020098 }
99 }
100 }
101}
102
zkoczkaaaf8d392019-10-02 17:16:06 +0200103func serve(nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
kalnagy92162652019-07-02 15:15:49 +0200104
105 triggerSBI := make(chan bool)
106
107 nbiErr := nbiEngine.Initialize(*args["xm-url"], *args["nbi-if"], *args["filename"], *args["configfile"],
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200108 sdlEngine, rpeEngine, triggerSBI)
kalnagy92162652019-07-02 15:15:49 +0200109 if nbiErr != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200110 rtmgr.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
kalnagy92162652019-07-02 15:15:49 +0200111 return
112 }
113
114 err := sbiEngine.Initialize(*args["sbi-if"])
Balint Uveges871fa392019-04-02 20:31:11 +0000115 if err != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200116 rtmgr.Logger.Info("Failed to open push socket due to: " + err.Error())
Balint Uveges871fa392019-04-02 20:31:11 +0000117 return
118 }
kalnagy92162652019-07-02 15:15:49 +0200119 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 Uveges871fa392019-04-02 20:31:11 +0000125 for {
126 time.Sleep(INTERVAL * time.Second)
kalnagy92162652019-07-02 15:15:49 +0200127 if *args["nbi"] == "httpGetter" {
zkoczkaaaf8d392019-10-02 17:16:06 +0200128 data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(*args["xm-url"])
kalnagy92162652019-07-02 15:15:49 +0200129 if err != nil {
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200130 rtmgr.Logger.Error("Cannot fetch xapp data due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +0200131 } else if data != nil {
zkoczkaaaf8d392019-10-02 17:16:06 +0200132 sdlEngine.WriteXApps(*args["filename"], data)
kalnagy92162652019-07-02 15:15:49 +0200133 }
Balint Uveges871fa392019-04-02 20:31:11 +0000134 }
kalnagy92162652019-07-02 15:15:49 +0200135
136 triggerSBI <- true
Balint Uveges871fa392019-04-02 20:31:11 +0000137 }
138}
139
kalnagy92162652019-07-02 15:15:49 +0200140func 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 Uveges871fa392019-04-02 20:31:11 +0000150func main() {
151 parseArgs()
Peter Szilagyi16d84d62019-04-24 14:51:02 +0000152 rtmgr.SetLogLevel(*args["loglevel"])
kalnagy92162652019-07-02 15:15:49 +0200153 nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr()
Balint Uveges871fa392019-04-02 20:31:11 +0000154 if err != nil {
155 rtmgr.Logger.Error(err.Error())
156 os.Exit(1)
157 }
kalnagy92162652019-07-02 15:15:49 +0200158 SetupCloseHandler()
Balint Uveges871fa392019-04-02 20:31:11 +0000159 rtmgr.Logger.Info("Start " + SERVICENAME + " service")
Peter Szilagyi16d84d62019-04-24 14:51:02 +0000160 rtmgr.Eps = make(rtmgr.Endpoints)
kalnagy92162652019-07-02 15:15:49 +0200161 serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine)
Balint Uveges871fa392019-04-02 20:31:11 +0000162 os.Exit(0)
163}