blob: a33437adbac8a7b48302308fb9a2da96b580ae24 [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)
wahidwa8596ec2019-12-05 06:30:42 +000036
Balint Uveges871fa392019-04-02 20:31:11 +000037import (
wahidwa8596ec2019-12-05 06:30:42 +000038 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
Balint Uveges871fa392019-04-02 20:31:11 +000039 "os"
kalnagy92162652019-07-02 15:15:49 +020040 "os/signal"
41 "routing-manager/pkg/nbi"
42 "routing-manager/pkg/rpe"
43 "routing-manager/pkg/rtmgr"
44 "routing-manager/pkg/sbi"
45 "routing-manager/pkg/sdl"
46 "syscall"
Balint Uveges871fa392019-04-02 20:31:11 +000047 "time"
48)
49
50const SERVICENAME = "rtmgr"
prabhukaliswamyb47d12d2019-12-03 15:06:30 +000051const INTERVAL time.Duration = 60
Balint Uveges871fa392019-04-02 20:31:11 +000052
zkoczkaaaf8d392019-10-02 17:16:06 +020053func initRtmgr() (nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, err error) {
wahidwa8596ec2019-12-05 06:30:42 +000054 if nbiEngine, err = nbi.GetNbi(xapp.Config.GetString("nbi")); err == nil && nbiEngine != nil {
55 if sbiEngine, err = sbi.GetSbi(xapp.Config.GetString("sbi")); err == nil && sbiEngine != nil {
56 if sdlEngine, err = sdl.GetSdl(xapp.Config.GetString("sdl")); err == nil && sdlEngine != nil {
57 if rpeEngine, err = rpe.GetRpe(xapp.Config.GetString("rpe")); err == nil && rpeEngine != nil {
zkoczkaaaf8d392019-10-02 17:16:06 +020058 return nbiEngine, sbiEngine, sdlEngine, rpeEngine, nil
Balint Uveges871fa392019-04-02 20:31:11 +000059 }
60 }
61 }
62 }
63 return nil, nil, nil, nil, err
64}
65
zkoczkaaaf8d392019-10-02 17:16:06 +020066func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
kalnagy92162652019-07-02 15:15:49 +020067 for {
68 if <-triggerSBI {
wahidwa8596ec2019-12-05 06:30:42 +000069 data, err := sdlEngine.ReadAll(xapp.Config.GetString("rtfile"))
kalnagy92162652019-07-02 15:15:49 +020070 if err != nil || data == nil {
wahidwa8596ec2019-12-05 06:30:42 +000071 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020072 continue
73 }
74 sbiEngine.UpdateEndpoints(data)
75 policies := rpeEngine.GeneratePolicies(rtmgr.Eps)
76 err = sbiEngine.DistributeAll(policies)
77 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +000078 xapp.Logger.Error("Routing table cannot be published due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020079 }
80 }
81 }
82}
83
zkoczkaaaf8d392019-10-02 17:16:06 +020084func serve(nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine) {
kalnagy92162652019-07-02 15:15:49 +020085
86 triggerSBI := make(chan bool)
87
wahidwa8596ec2019-12-05 06:30:42 +000088 nbiErr := nbiEngine.Initialize(xapp.Config.GetString("xmurl"), xapp.Config.GetString("nbiurl"), xapp.Config.GetString("rtfile"), xapp.Config.GetString("cfgfile"),
zkoczkaeb2ff0d2019-09-26 16:59:54 +020089 sdlEngine, rpeEngine, triggerSBI)
kalnagy92162652019-07-02 15:15:49 +020090 if nbiErr != nil {
wahidwa8596ec2019-12-05 06:30:42 +000091 xapp.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
kalnagy92162652019-07-02 15:15:49 +020092 return
93 }
94
wahidwa8596ec2019-12-05 06:30:42 +000095 err := sbiEngine.Initialize(xapp.Config.GetString("sbiurl"))
Balint Uveges871fa392019-04-02 20:31:11 +000096 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +000097 xapp.Logger.Info("Failed to open push socket due to: " + err.Error())
Balint Uveges871fa392019-04-02 20:31:11 +000098 return
99 }
kalnagy92162652019-07-02 15:15:49 +0200100 defer nbiEngine.Terminate()
101 defer sbiEngine.Terminate()
102
103 // This SBI Go routine is trtiggered by periodic main loop and when data is recieved on REST interface.
104 go serveSBI(triggerSBI, sbiEngine, sdlEngine, rpeEngine)
105
Balint Uveges871fa392019-04-02 20:31:11 +0000106 for {
wahidwa8596ec2019-12-05 06:30:42 +0000107 if xapp.Config.GetString("nbi") == "httpGetter" {
108 data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(xapp.Config.GetString("xmurl"))
kalnagy92162652019-07-02 15:15:49 +0200109 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000110 xapp.Logger.Error("Cannot fetch xapp data due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +0200111 } else if data != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000112 sdlEngine.WriteXApps(xapp.Config.GetString("rtfile"), data)
kalnagy92162652019-07-02 15:15:49 +0200113 }
Balint Uveges871fa392019-04-02 20:31:11 +0000114 }
kalnagy92162652019-07-02 15:15:49 +0200115
116 triggerSBI <- true
prabhukaliswamyb47d12d2019-12-03 15:06:30 +0000117
118 time.Sleep(INTERVAL * time.Second)
wahidwa8596ec2019-12-05 06:30:42 +0000119 xapp.Logger.Debug("Periodic loop timed out. Setting triggerSBI flag to distribute updated routes.")
Balint Uveges871fa392019-04-02 20:31:11 +0000120 }
121}
122
kalnagy92162652019-07-02 15:15:49 +0200123func SetupCloseHandler() {
124 c := make(chan os.Signal, 2)
125 signal.Notify(c, os.Interrupt, syscall.SIGTERM)
126 go func() {
127 <-c
wahidwa8596ec2019-12-05 06:30:42 +0000128 xapp.Logger.Info("\r- Ctrl+C pressed in Terminal")
kalnagy92162652019-07-02 15:15:49 +0200129 os.Exit(0)
130 }()
131}
132
Balint Uveges871fa392019-04-02 20:31:11 +0000133func main() {
kalnagy92162652019-07-02 15:15:49 +0200134 nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr()
Balint Uveges871fa392019-04-02 20:31:11 +0000135 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000136 xapp.Logger.Error(err.Error())
Balint Uveges871fa392019-04-02 20:31:11 +0000137 os.Exit(1)
138 }
kalnagy92162652019-07-02 15:15:49 +0200139 SetupCloseHandler()
wahidwa8596ec2019-12-05 06:30:42 +0000140 xapp.Logger.Info("Start " + SERVICENAME + " service")
Peter Szilagyi16d84d62019-04-24 14:51:02 +0000141 rtmgr.Eps = make(rtmgr.Endpoints)
kalnagy92162652019-07-02 15:15:49 +0200142 serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine)
Balint Uveges871fa392019-04-02 20:31:11 +0000143 os.Exit(0)
144}