blob: bd17d4f8426c7e57aa9b6e878a451c0b10215bbc [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"
prabhukaliswamye110ee02019-12-23 09:51:01 +000048 "sync"
Balint Uveges871fa392019-04-02 20:31:11 +000049)
50
51const SERVICENAME = "rtmgr"
prabhukaliswamyb47d12d2019-12-03 15:06:30 +000052const INTERVAL time.Duration = 60
Balint Uveges871fa392019-04-02 20:31:11 +000053
zkoczkaaaf8d392019-10-02 17:16:06 +020054func initRtmgr() (nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, err error) {
wahidwa8596ec2019-12-05 06:30:42 +000055 if nbiEngine, err = nbi.GetNbi(xapp.Config.GetString("nbi")); err == nil && nbiEngine != nil {
56 if sbiEngine, err = sbi.GetSbi(xapp.Config.GetString("sbi")); err == nil && sbiEngine != nil {
57 if sdlEngine, err = sdl.GetSdl(xapp.Config.GetString("sdl")); err == nil && sdlEngine != nil {
58 if rpeEngine, err = rpe.GetRpe(xapp.Config.GetString("rpe")); err == nil && rpeEngine != nil {
zkoczkaaaf8d392019-10-02 17:16:06 +020059 return nbiEngine, sbiEngine, sdlEngine, rpeEngine, nil
Balint Uveges871fa392019-04-02 20:31:11 +000060 }
61 }
62 }
63 }
64 return nil, nil, nil, nil, err
65}
66
prabhukaliswamye110ee02019-12-23 09:51:01 +000067func serveSBI(triggerSBI <-chan bool, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, m *sync.Mutex) {
kalnagy92162652019-07-02 15:15:49 +020068 for {
69 if <-triggerSBI {
prabhukaliswamye110ee02019-12-23 09:51:01 +000070 m.Lock()
wahidwa8596ec2019-12-05 06:30:42 +000071 data, err := sdlEngine.ReadAll(xapp.Config.GetString("rtfile"))
prabhukaliswamye110ee02019-12-23 09:51:01 +000072 m.Unlock()
kalnagy92162652019-07-02 15:15:49 +020073 if err != nil || data == nil {
wahidwa8596ec2019-12-05 06:30:42 +000074 xapp.Logger.Error("Cannot get data from sdl interface due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020075 continue
76 }
77 sbiEngine.UpdateEndpoints(data)
prabhukaliswamye110ee02019-12-23 09:51:01 +000078 policies := rpeEngine.GeneratePolicies(rtmgr.Eps, data)
kalnagy92162652019-07-02 15:15:49 +020079 err = sbiEngine.DistributeAll(policies)
80 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +000081 xapp.Logger.Error("Routing table cannot be published due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +020082 }
83 }
84 }
85}
86
prabhukaliswamye110ee02019-12-23 09:51:01 +000087func serve(nbiEngine nbi.Engine, sbiEngine sbi.Engine, sdlEngine sdl.Engine, rpeEngine rpe.Engine, m *sync.Mutex) {
kalnagy92162652019-07-02 15:15:49 +020088
89 triggerSBI := make(chan bool)
90
wahidwa8596ec2019-12-05 06:30:42 +000091 nbiErr := nbiEngine.Initialize(xapp.Config.GetString("xmurl"), xapp.Config.GetString("nbiurl"), xapp.Config.GetString("rtfile"), xapp.Config.GetString("cfgfile"),
prabhukaliswamye110ee02019-12-23 09:51:01 +000092 sdlEngine, rpeEngine, triggerSBI, m)
kalnagy92162652019-07-02 15:15:49 +020093 if nbiErr != nil {
wahidwa8596ec2019-12-05 06:30:42 +000094 xapp.Logger.Error("Failed to initialize nbi due to: " + nbiErr.Error())
kalnagy92162652019-07-02 15:15:49 +020095 return
96 }
97
wahidwa8596ec2019-12-05 06:30:42 +000098 err := sbiEngine.Initialize(xapp.Config.GetString("sbiurl"))
Balint Uveges871fa392019-04-02 20:31:11 +000099 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000100 xapp.Logger.Info("Failed to open push socket due to: " + err.Error())
Balint Uveges871fa392019-04-02 20:31:11 +0000101 return
102 }
kalnagy92162652019-07-02 15:15:49 +0200103 defer nbiEngine.Terminate()
104 defer sbiEngine.Terminate()
105
106 // This SBI Go routine is trtiggered by periodic main loop and when data is recieved on REST interface.
prabhukaliswamye110ee02019-12-23 09:51:01 +0000107 go serveSBI(triggerSBI, sbiEngine, sdlEngine, rpeEngine, m)
kalnagy92162652019-07-02 15:15:49 +0200108
Balint Uveges871fa392019-04-02 20:31:11 +0000109 for {
wahidwa8596ec2019-12-05 06:30:42 +0000110 if xapp.Config.GetString("nbi") == "httpGetter" {
111 data, err := nbiEngine.(*nbi.HttpGetter).FetchAllXApps(xapp.Config.GetString("xmurl"))
kalnagy92162652019-07-02 15:15:49 +0200112 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000113 xapp.Logger.Error("Cannot fetch xapp data due to: " + err.Error())
kalnagy92162652019-07-02 15:15:49 +0200114 } else if data != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000115 sdlEngine.WriteXApps(xapp.Config.GetString("rtfile"), data)
kalnagy92162652019-07-02 15:15:49 +0200116 }
Balint Uveges871fa392019-04-02 20:31:11 +0000117 }
kalnagy92162652019-07-02 15:15:49 +0200118
119 triggerSBI <- true
prabhukaliswamyb47d12d2019-12-03 15:06:30 +0000120
121 time.Sleep(INTERVAL * time.Second)
wahidwa8596ec2019-12-05 06:30:42 +0000122 xapp.Logger.Debug("Periodic loop timed out. Setting triggerSBI flag to distribute updated routes.")
Balint Uveges871fa392019-04-02 20:31:11 +0000123 }
124}
125
kalnagy92162652019-07-02 15:15:49 +0200126func SetupCloseHandler() {
127 c := make(chan os.Signal, 2)
128 signal.Notify(c, os.Interrupt, syscall.SIGTERM)
129 go func() {
130 <-c
wahidwa8596ec2019-12-05 06:30:42 +0000131 xapp.Logger.Info("\r- Ctrl+C pressed in Terminal")
kalnagy92162652019-07-02 15:15:49 +0200132 os.Exit(0)
133 }()
134}
135
Balint Uveges871fa392019-04-02 20:31:11 +0000136func main() {
kalnagy92162652019-07-02 15:15:49 +0200137 nbiEngine, sbiEngine, sdlEngine, rpeEngine, err := initRtmgr()
Balint Uveges871fa392019-04-02 20:31:11 +0000138 if err != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000139 xapp.Logger.Error(err.Error())
Balint Uveges871fa392019-04-02 20:31:11 +0000140 os.Exit(1)
141 }
kalnagy92162652019-07-02 15:15:49 +0200142 SetupCloseHandler()
wahidwa8596ec2019-12-05 06:30:42 +0000143 xapp.Logger.Info("Start " + SERVICENAME + " service")
Peter Szilagyi16d84d62019-04-24 14:51:02 +0000144 rtmgr.Eps = make(rtmgr.Endpoints)
prabhukaliswamye110ee02019-12-23 09:51:01 +0000145
146 var m sync.Mutex
147
wahidwcd7867c2020-02-05 10:01:12 +0000148 c := sbi.NewControl()
149 go c.Run()
150
prabhukaliswamye110ee02019-12-23 09:51:01 +0000151 serve(nbiEngine, sbiEngine, sdlEngine, rpeEngine, &m)
Balint Uveges871fa392019-04-02 20:31:11 +0000152 os.Exit(0)
153}