-
Notifications
You must be signed in to change notification settings - Fork 0
/
ue_init.go
215 lines (180 loc) · 4.82 KB
/
ue_init.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package ue_service
import (
"bufio"
"fmt"
"github.com/free5gc/http2_util"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/matanbroner/UESimulator/src/ue/factory"
"github.com/matanbroner/UESimulator/src/ue/logger"
"github.com/matanbroner/UESimulator/src/ue/rest_api"
"github.com/matanbroner/UESimulator/src/ue/ue_context"
"github.com/matanbroner/UESimulator/src/ue/ue_handler"
"github.com/matanbroner/UESimulator/src/ue/ue_util"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"os"
"os/exec"
"os/signal"
"sync"
"syscall"
)
type UE struct{}
type (
Config struct {
uecfg string
ue_http_addr string
ue_http_port string
}
)
var config Config
var ueCLi = []cli.Flag{
cli.StringFlag{
Name: "free5gccfg",
Usage: "common config file",
},
cli.StringFlag{
Name: "uecfg",
Usage: "ue config file",
},
cli.StringFlag{
Name: "ue_http_addr",
Usage: "ue http binding address",
},
cli.StringFlag{
Name: "ue_http_port",
Usage: "ue http binding port",
},
}
var initLog *logrus.Entry
func init() {
initLog = logger.InitLog
}
func (*UE) GetCliCmd() (flags []cli.Flag) {
return ueCLi
}
func (*UE) Initialize(c *cli.Context) {
config = Config{
uecfg: c.String("uecfg"),
ue_http_addr: c.String("ue_http_addr"),
ue_http_port: c.String("ue_http_port"),
}
fmt.Println(c.Args())
// TODO: make this whole setup more clean, there should be an env set for where to search for config files
if config.uecfg != "" {
fmt.Println("Using default UE config file")
factory.InitConfigFactory("./config/" + config.uecfg)
} else {
factory.InitConfigFactory(ue_util.DefaultUeConfigPath)
}
// override uecfg.conf file is command line arg is informed
if config.ue_http_addr != "" {
factory.UeConfig.Configuration.UEConfiguration.HttpIPv4Address = config.ue_http_addr
}
if config.ue_http_port != "" {
factory.UeConfig.Configuration.UEConfiguration.HttpIPv4Port = config.ue_http_port
}
// TODO: get these two variables from ue-iot-non3gpp config file
DebugLevel := "info"
ReportCaller := true
initLog.Traceln("UE debug level(string):", DebugLevel)
if DebugLevel != "" {
initLog.Infoln("UE debug level(string):", DebugLevel)
level, err := logrus.ParseLevel(DebugLevel)
if err == nil {
logger.SetLogLevel(level)
}
}
logger.SetReportCaller(ReportCaller)
}
func (ue *UE) FilterCli(c *cli.Context) (args []string) {
for _, flag := range ue.GetCliCmd() {
name := flag.GetName()
value := fmt.Sprint(c.Generic(name))
if value == "" {
continue
}
args = append(args, "--"+name, value)
}
return args
}
func (ue *UE) Start() {
initLog.Infoln("Server started")
router := gin.Default()
router.Use(cors.New(cors.Config{
AllowMethods: []string{"GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE"},
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type", "User-Agent", "Referrer", "Host", "Token", "X-Requested-With"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
AllowAllOrigins: true,
MaxAge: 86400,
}))
// Add endpoints supported by the UE
rest_api.AddService(router)
//self := ue_context.UE_Self()
ue_context.InitUeContext()
go ue_handler.Handle()
addr := fmt.Sprintf("%s:%s",
factory.UeConfig.Configuration.UEConfiguration.HttpIPv4Address,
factory.UeConfig.Configuration.UEConfiguration.HttpIPv4Port)
// handle terminate signal
signalChannel := make(chan os.Signal, 1)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
go func() {
<-signalChannel
ue.Terminate()
os.Exit(0)
}()
initLog.Infoln(addr)
server, err := http2_util.NewServer(addr, ue_util.UeLogPath, router)
if err == nil && server != nil {
//initLog.Infoln(server.ListenAndServeTLS(ue_util.UePemPath, ue_util.UeKeyPath))
initLog.Infoln(server.ListenAndServe())
} else {
initLog.Errorf("Initialize http2 server failed: %+v", err)
}
}
func (ue *UE) Exec(c *cli.Context) error {
initLog.Traceln("args:", c.String("uecfg"))
args := ue.FilterCli(c)
initLog.Traceln("filter: ", args)
command := exec.Command("./ue", args...)
stdout, err := command.StdoutPipe()
if err != nil {
initLog.Fatalln(err)
}
wg := sync.WaitGroup{}
wg.Add(3)
go func() {
in := bufio.NewScanner(stdout)
for in.Scan() {
fmt.Println(in.Text())
}
wg.Done()
}()
stderr, err := command.StderrPipe()
if err != nil {
initLog.Fatalln(err)
}
go func() {
in := bufio.NewScanner(stderr)
for in.Scan() {
fmt.Println(in.Text())
}
wg.Done()
}()
go func() {
if err := command.Start(); err != nil {
initLog.Errorf("UE Start error: %v", err)
}
wg.Done()
}()
wg.Wait()
return err
}
func (ue *UE) Terminate() {
logger.InitLog.Infof("Terminating UE...")
//ueSelf := ue_context.UE_Self()
// clean resources, removing tunnels and interfaces, send deregistration message if registered
logger.InitLog.Infof("UE terminated")
}