101c74377313477923de7503f16695e877eac7bc
[ric-plt/rtmgr.git] / cmd / rtmgr.go
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    This source code is part of the near-RT RIC (RAN Intelligent Controller)
20    platform project (RICP).
21
22
23 ==================================================================================
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 */
33 package main
34
35 //TODO: change flag to pflag (won't need any argument parse)
36 import (
37         "flag"
38         "os"
39         "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"
46         "time"
47 )
48
49 const SERVICENAME = "rtmgr"
50 const INTERVAL time.Duration = 2
51
52 var (
53         args map[string]*string
54 )
55
56 func parseArgs() {
57         // TODO: arguments should be validated (filename; xm-url; sbi-if; rest-url; rest-port)
58         args = make(map[string]*string)
59         args["configfile"] = flag.String("configfile", "/etc/rtmgrcfg.json", "Routing manager's configuration file path")
60         args["nbi"] = flag.String("nbi", "httpRESTful", "Northbound interface module to be used. Valid values are: 'httpGetter | httpRESTful'")
61         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'")
63         args["sdl"] = flag.String("sdl", "file", "Data store engine to be used. Valid values are: 'file'")
64         args["xm-url"] = flag.String("xm-url", "http://localhost:3000/xapps", "HTTP URL where xApp Manager exposes the entire xApp List")
65         args["nbi-if"] = flag.String("nbi-if", "http://localhost:8888", "Base HTTP URL where routing manager will be listening on")
66         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")
68         args["loglevel"] = flag.String("loglevel", "INFO", "INFO | WARN | ERROR | DEBUG | TRACE")
69         flag.Parse()
70 }
71
72 func 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
78                                 }
79                         }
80                 }
81         }
82         return nil, nil, nil, nil, err
83 }
84
85 func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
86         for {
87                 if <-triggerSBI {
88                         data, err := sdlEngine.ReadAll(*args["filename"])
89                         if err != nil || data == nil {
90                                 rtmgr.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
91                                 continue
92                         }
93                         sbiEngine.UpdateEndpoints(data)
94                         policies := rpeEngine.GeneratePolicies(rtmgr.Eps)
95                         err = sbiEngine.DistributeAll(policies)
96                         if err != nil {
97                                 rtmgr.Logger.Error("Routing table cannot be published due to: " + err.Error())
98                         }
99                 }
100         }
101 }
102
103 func serve(nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
104
105         triggerSBI := make(chan bool)
106
107         nbiErr := nbiEngine.Initialize(*args["xm-url"], *args["nbi-if"], *args["filename"], *args["configfile"],
108                 sdlEngine, rpeEngine, triggerSBI)
109         if nbiErr != nil {
110                 rtmgr.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
111                 return
112         }
113
114         err := sbiEngine.Initialize(*args["sbi-if"])
115         if err != nil {
116                 rtmgr.Logger.Info("Failed to open push socket due to: " + err.Error())
117                 return
118         }
119         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
125         for {
126                 time.Sleep(INTERVAL * time.Second)
127                 if *args["nbi"] == "httpGetter" {
128                         data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(*args["xm-url"])
129                         if err != nil {
130                                 rtmgr.Logger.Error("Cannot fetch xapp data due to: " + err.Error())
131                         } else if data != nil {
132                                 sdlEngine.WriteXApps(*args["filename"], data)
133                         }
134                 }
135
136                 triggerSBI <- true
137         }
138 }
139
140 func 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
150 func main() {
151         parseArgs()
152         rtmgr.SetLogLevel(*args["loglevel"])
153         nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr()
154         if err != nil {
155                 rtmgr.Logger.Error(err.Error())
156                 os.Exit(1)
157         }
158         SetupCloseHandler()
159         rtmgr.Logger.Info("Start " + SERVICENAME + " service")
160         rtmgr.Eps = make(rtmgr.Endpoints)
161         serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine)
162         os.Exit(0)
163 }