Add new packages to the container
[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 /*
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 */
27 package main
28
29 //TODO: change flag to pflag (won't need any argument parse)
30 import (
31         "flag"
32         "os"
33         "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"
40         "time"
41 )
42
43 const SERVICENAME = "rtmgr"
44 const INTERVAL time.Duration = 2
45
46 var (
47         args map[string]*string
48 )
49
50 func parseArgs() {
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")
63         flag.Parse()
64 }
65
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
72                                 }
73                         }
74                 }
75         }
76         return nil, nil, nil, nil, err
77 }
78
79 func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
80         for {
81                 if <-triggerSBI {
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())
85                                 continue
86                         }
87                         sbiEngine.UpdateEndpoints(data)
88                         policies := rpeEngine.GeneratePolicies(rtmgr.Eps)
89                         err = sbiEngine.DistributeAll(policies)
90                         if err != nil {
91                                 rtmgr.Logger.Error("Routing table cannot be published due to: " + err.Error())
92                         }
93                 }
94         }
95 }
96
97 func serve(nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
98
99         triggerSBI := make(chan bool)
100
101         nbiErr := nbiEngine.Initialize(*args["xm-url"], *args["nbi-if"], *args["filename"], *args["configfile"],
102                 sdlEngine, rpeEngine, triggerSBI)
103         if nbiErr != nil {
104                 rtmgr.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
105                 return
106         }
107
108         err := sbiEngine.Initialize(*args["sbi-if"])
109         if err != nil {
110                 rtmgr.Logger.Info("Failed to open push socket due to: " + err.Error())
111                 return
112         }
113         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
119         for {
120                 time.Sleep(INTERVAL * time.Second)
121                 if *args["nbi"] == "httpGetter" {
122                         data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(*args["xm-url"])
123                         if err != nil {
124                                 rtmgr.Logger.Error("Cannot fetch xapp data due to: " + err.Error())
125                         } else if data != nil {
126                                 sdlEngine.WriteXApps(*args["filename"], data)
127                         }
128                 }
129
130                 triggerSBI <- true
131         }
132 }
133
134 func 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
144 func main() {
145         parseArgs()
146         rtmgr.SetLogLevel(*args["loglevel"])
147         nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr()
148         if err != nil {
149                 rtmgr.Logger.Error(err.Error())
150                 os.Exit(1)
151         }
152         SetupCloseHandler()
153         rtmgr.Logger.Info("Start " + SERVICENAME + " service")
154         rtmgr.Eps = make(rtmgr.Endpoints)
155         serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine)
156         os.Exit(0)
157 }