blob: 8ffb94159ee48b3f3dc12c514f85e8ef5b8dd743 [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
Matteo Scandolo9f619492019-10-25 13:11:58 -07002* Copyright 2018-present Open Networking Foundation
Matteo Scandolo11006992019-08-28 11:29:46 -07003
Matteo Scandolo9f619492019-10-25 13:11:58 -07004* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
Matteo Scandolo11006992019-08-28 11:29:46 -07007
Matteo Scandolo9f619492019-10-25 13:11:58 -07008* http://www.apache.org/licenses/LICENSE-2.0
Matteo Scandolo11006992019-08-28 11:29:46 -07009
Matteo Scandolo9f619492019-10-25 13:11:58 -070010* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
Matteo Scandolo11006992019-08-28 11:29:46 -070015 */
16
Matteo Scandolo4747d292019-08-05 11:50:18 -070017package main
18
19import (
Zdravko Bozakov2da76342019-10-21 09:47:35 +020020 "context"
21 "net"
22 "net/http"
23 "os"
24 "os/signal"
25 "runtime/pprof"
26 "sync"
27 "syscall"
28
29 "github.com/grpc-ecosystem/grpc-gateway/runtime"
Matteo Scandolo11006992019-08-28 11:29:46 -070030 "github.com/opencord/bbsim/api/bbsim"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020031 "github.com/opencord/bbsim/api/legacy"
Matteo Scandolo82c16d02019-09-24 09:34:32 -070032 "github.com/opencord/bbsim/internal/bbsim/api"
Matteo Scandolo11006992019-08-28 11:29:46 -070033 "github.com/opencord/bbsim/internal/bbsim/devices"
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010034 "github.com/opencord/bbsim/internal/bbsim/responders/sadis"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070035 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo4747d292019-08-05 11:50:18 -070036 log "github.com/sirupsen/logrus"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070037 "google.golang.org/grpc"
38 "google.golang.org/grpc/reflection"
Matteo Scandolo4747d292019-08-05 11:50:18 -070039)
40
Zdravko Bozakov681364d2019-11-10 14:28:46 +010041func startApiServer(apiDoneChannel chan bool, group *sync.WaitGroup) {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010042 address := common.Options.BBSim.ApiAddress
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010043 log.Debugf("APIServer listening on %v", address)
Matteo Scandolo84f7d482019-08-08 19:00:47 -070044 lis, err := net.Listen("tcp", address)
45 if err != nil {
46 log.Fatalf("APIServer failed to listen: %v", err)
47 }
48 grpcServer := grpc.NewServer()
Matteo Scandolo82c16d02019-09-24 09:34:32 -070049 bbsim.RegisterBBSimServer(grpcServer, api.BBSimServer{})
Matteo Scandolo84f7d482019-08-08 19:00:47 -070050
51 reflection.Register(grpcServer)
52
53 go grpcServer.Serve(lis)
Zdravko Bozakov681364d2019-11-10 14:28:46 +010054 go startApiRestServer(apiDoneChannel, group, address)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070055
Zdravko Bozakov2da76342019-10-21 09:47:35 +020056 select {
Zdravko Bozakov681364d2019-11-10 14:28:46 +010057 case <-apiDoneChannel:
58 // if the API channel is closed, stop the gRPC server
Zdravko Bozakov2da76342019-10-21 09:47:35 +020059 grpcServer.Stop()
60 log.Warnf("Stopping API gRPC server")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070061 }
62
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070063 group.Done()
Zdravko Bozakov2da76342019-10-21 09:47:35 +020064}
65
66// startApiRestServer method starts the REST server (grpc gateway) for BBSim.
Zdravko Bozakov681364d2019-11-10 14:28:46 +010067func startApiRestServer(apiDoneChannel chan bool, group *sync.WaitGroup, grpcAddress string) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020068 ctx := context.Background()
69 ctx, cancel := context.WithCancel(ctx)
70 defer cancel()
71
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010072 address := common.Options.BBSim.RestApiAddress
Zdravko Bozakov2da76342019-10-21 09:47:35 +020073
74 mux := runtime.NewServeMux()
75 opts := []grpc.DialOption{grpc.WithInsecure()}
76
77 if err := bbsim.RegisterBBSimHandlerFromEndpoint(ctx, mux, grpcAddress, opts); err != nil {
78 log.Errorf("Could not register API server: %v", err)
79 return
80 }
81
82 s := &http.Server{Addr: address, Handler: mux}
83
84 go func() {
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010085 log.Infof("REST API server listening on %s", address)
Zdravko Bozakov2da76342019-10-21 09:47:35 +020086 if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
87 log.Errorf("Could not start API server: %v", err)
88 return
89 }
90 }()
91
92 select {
Zdravko Bozakov681364d2019-11-10 14:28:46 +010093 case <-apiDoneChannel:
Zdravko Bozakov2da76342019-10-21 09:47:35 +020094 log.Warnf("Stopping API REST server")
95 s.Shutdown(ctx)
96 }
97
98 group.Done()
99}
100
101// This server aims to provide compatibility with the previous BBSim version. It is deprecated and will be removed in the future.
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100102func startLegacyApiServer(apiDoneChannel chan bool, group *sync.WaitGroup) {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100103 grpcAddress := common.Options.BBSim.LegacyApiAddress
104 restAddress := common.Options.BBSim.LegacyRestApiAddress
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200105
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100106 log.Debugf("Legacy APIServer listening on %v", grpcAddress)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200107 listener, err := net.Listen("tcp", grpcAddress)
108 if err != nil {
109 log.Fatalf("Legacy APIServer failed to listen: %v", err)
110 return
111 }
112 apiserver := grpc.NewServer()
113 legacy.RegisterBBSimServiceServer(apiserver, api.BBSimLegacyServer{})
114
115 go apiserver.Serve(listener)
116 // Start rest gateway for BBSim server
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100117 go api.StartRestGatewayService(apiDoneChannel, group, grpcAddress, restAddress)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200118
119 select {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100120 case <-apiDoneChannel:
121 // if the API channel is closed, stop the gRPC server
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200122 log.Warnf("Stopping legacy API gRPC server")
123 apiserver.Stop()
124 break
125
126 }
127
128 group.Done()
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700129}
130
Matteo Scandolo4747d292019-08-05 11:50:18 -0700131func main() {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100132
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700133 options := common.GetBBSimOpts()
Matteo Scandolo4747d292019-08-05 11:50:18 -0700134
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100135 common.SetLogLevel(log.StandardLogger(), options.BBSim.LogLevel, options.BBSim.LogCaller)
136 log.Tracef("BBSim options: %+v", options)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700137
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100138 if *options.BBSim.CpuProfile != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700139 // start profiling
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100140 log.Infof("Creating profile file at: %s", *options.BBSim.CpuProfile)
141 f, err := os.Create(*options.BBSim.CpuProfile)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700142 if err != nil {
143 log.Fatal(err)
144 }
145 pprof.StartCPUProfile(f)
146 }
147
Matteo Scandolo4747d292019-08-05 11:50:18 -0700148 log.WithFields(log.Fields{
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100149 "OltID": options.Olt.ID,
150 "NumNniPerOlt": options.Olt.NniPorts,
151 "NumPonPerOlt": options.Olt.PonPorts,
152 "NumOnuPerPon": options.Olt.OnusPonPort,
153 "TotalOnus": options.Olt.PonPorts * options.Olt.OnusPonPort,
154 "EnableAuth": options.BBSim.EnableAuth,
155 "Dhcp": options.BBSim.EnableDhcp,
156 "Delay": options.BBSim.Delay,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700157 }).Info("BroadBand Simulator is on")
158
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700159 // control channels, they are only closed when the goroutine needs to be terminated
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700160 apiDoneChannel := make(chan bool)
161
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100162 olt := devices.CreateOLT(
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100163 options.Olt.ID,
164 int(options.Olt.NniPorts),
165 int(options.Olt.PonPorts),
166 int(options.Olt.OnusPonPort),
167 options.BBSim.STag,
168 options.BBSim.CTagInit,
169 options.BBSim.EnableAuth,
170 options.BBSim.EnableDhcp,
171 options.BBSim.Delay,
Matteo Scandoloc1147092019-10-29 09:38:33 -0700172 false,
173 )
Matteo Scandoloe33447a2019-10-31 12:38:23 -0700174
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100175 log.Debugf("Created OLT with id: %d", options.Olt.ID)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100176
177 sigs := make(chan os.Signal, 1)
178 // stop API servers on SIGTERM
179 signal.Notify(sigs, syscall.SIGTERM)
180
181 go func() {
182 <-sigs
183 close(apiDoneChannel)
184 }()
185
186 wg := sync.WaitGroup{}
187 wg.Add(4)
188
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700189 go startApiServer(apiDoneChannel, &wg)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200190 go startLegacyApiServer(apiDoneChannel, &wg)
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700191 log.Debugf("Started APIService")
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100192 if common.Options.BBSim.SadisServer != false {
193 wg.Add(1)
194 go sadis.StartRestServer(olt, &wg)
195 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700196
197 wg.Wait()
198
199 defer func() {
200 log.Info("BroadBand Simulator is off")
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100201 if *options.BBSim.CpuProfile != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700202 log.Info("Stopping profiler")
203 pprof.StopCPUProfile()
204 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700205 }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700206}