blob: d7a85b980c4ea99979583a58e623d1f4013f1f6b [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"
Matteo Scandolocedde462021-03-09 17:37:16 -080021 "github.com/opencord/bbsim/internal/bbsim/responders/webserver"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020022 "net"
23 "net/http"
24 "os"
25 "os/signal"
26 "runtime/pprof"
27 "sync"
28 "syscall"
29
Shrey Baid64cda472020-04-24 18:58:18 +053030 "github.com/Shopify/sarama"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020031 "github.com/grpc-ecosystem/grpc-gateway/runtime"
Matteo Scandolo11006992019-08-28 11:29:46 -070032 "github.com/opencord/bbsim/api/bbsim"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020033 "github.com/opencord/bbsim/api/legacy"
Matteo Scandolo82c16d02019-09-24 09:34:32 -070034 "github.com/opencord/bbsim/internal/bbsim/api"
Matteo Scandolo11006992019-08-28 11:29:46 -070035 "github.com/opencord/bbsim/internal/bbsim/devices"
amit.ghosh258d14c2020-10-02 15:13:38 +020036 "github.com/opencord/bbsim/internal/bbsim/dmiserver"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070037 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo4747d292019-08-05 11:50:18 -070038 log "github.com/sirupsen/logrus"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070039 "google.golang.org/grpc"
40 "google.golang.org/grpc/reflection"
Matteo Scandolo4747d292019-08-05 11:50:18 -070041)
42
Zdravko Bozakov681364d2019-11-10 14:28:46 +010043func startApiServer(apiDoneChannel chan bool, group *sync.WaitGroup) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070044 address := common.Config.BBSim.ApiAddress
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010045 log.Debugf("APIServer listening on %v", address)
Matteo Scandolo84f7d482019-08-08 19:00:47 -070046 lis, err := net.Listen("tcp", address)
47 if err != nil {
48 log.Fatalf("APIServer failed to listen: %v", err)
49 }
50 grpcServer := grpc.NewServer()
Matteo Scandolo82c16d02019-09-24 09:34:32 -070051 bbsim.RegisterBBSimServer(grpcServer, api.BBSimServer{})
Matteo Scandolo84f7d482019-08-08 19:00:47 -070052
53 reflection.Register(grpcServer)
54
Shrey Baid688b4242020-07-10 20:40:10 +053055 go func() { _ = grpcServer.Serve(lis) }()
Zdravko Bozakov681364d2019-11-10 14:28:46 +010056 go startApiRestServer(apiDoneChannel, group, address)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070057
Shrey Baid688b4242020-07-10 20:40:10 +053058 x := <-apiDoneChannel
59 if x {
Zdravko Bozakov681364d2019-11-10 14:28:46 +010060 // if the API channel is closed, stop the gRPC server
Zdravko Bozakov2da76342019-10-21 09:47:35 +020061 grpcServer.Stop()
62 log.Warnf("Stopping API gRPC server")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070063 }
64
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070065 group.Done()
Zdravko Bozakov2da76342019-10-21 09:47:35 +020066}
67
68// startApiRestServer method starts the REST server (grpc gateway) for BBSim.
Zdravko Bozakov681364d2019-11-10 14:28:46 +010069func startApiRestServer(apiDoneChannel chan bool, group *sync.WaitGroup, grpcAddress string) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020070 ctx := context.Background()
71 ctx, cancel := context.WithCancel(ctx)
72 defer cancel()
73
Matteo Scandolo4a036262020-08-17 15:56:13 -070074 address := common.Config.BBSim.RestApiAddress
Zdravko Bozakov2da76342019-10-21 09:47:35 +020075
76 mux := runtime.NewServeMux()
77 opts := []grpc.DialOption{grpc.WithInsecure()}
78
79 if err := bbsim.RegisterBBSimHandlerFromEndpoint(ctx, mux, grpcAddress, opts); err != nil {
80 log.Errorf("Could not register API server: %v", err)
81 return
82 }
83
84 s := &http.Server{Addr: address, Handler: mux}
85
86 go func() {
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010087 log.Infof("REST API server listening on %s", address)
Zdravko Bozakov2da76342019-10-21 09:47:35 +020088 if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
89 log.Errorf("Could not start API server: %v", err)
90 return
91 }
92 }()
93
Shrey Baid688b4242020-07-10 20:40:10 +053094 x := <-apiDoneChannel
95 if x {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020096 log.Warnf("Stopping API REST server")
Shrey Baid688b4242020-07-10 20:40:10 +053097 _ = s.Shutdown(ctx)
Zdravko Bozakov2da76342019-10-21 09:47:35 +020098 }
99
100 group.Done()
101}
102
103// 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 +0100104func startLegacyApiServer(apiDoneChannel chan bool, group *sync.WaitGroup) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700105 grpcAddress := common.Config.BBSim.LegacyApiAddress
106 restAddress := common.Config.BBSim.LegacyRestApiAddress
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200107
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100108 log.Debugf("Legacy APIServer listening on %v", grpcAddress)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200109 listener, err := net.Listen("tcp", grpcAddress)
110 if err != nil {
111 log.Fatalf("Legacy APIServer failed to listen: %v", err)
112 return
113 }
114 apiserver := grpc.NewServer()
115 legacy.RegisterBBSimServiceServer(apiserver, api.BBSimLegacyServer{})
116
Shrey Baid688b4242020-07-10 20:40:10 +0530117 go func() { _ = apiserver.Serve(listener) }()
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200118 // Start rest gateway for BBSim server
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100119 go api.StartRestGatewayService(apiDoneChannel, group, grpcAddress, restAddress)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200120
Shrey Baid688b4242020-07-10 20:40:10 +0530121 x := <-apiDoneChannel
122 if x {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100123 // if the API channel is closed, stop the gRPC server
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200124 log.Warnf("Stopping legacy API gRPC server")
125 apiserver.Stop()
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200126 }
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 Scandolo4a036262020-08-17 15:56:13 -0700133 common.LoadConfig()
Matteo Scandolo4747d292019-08-05 11:50:18 -0700134
Matteo Scandolo4a036262020-08-17 15:56:13 -0700135 common.SetLogLevel(log.StandardLogger(), common.Config.BBSim.LogLevel, common.Config.BBSim.LogCaller)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700136
Matteo Scandolo4a036262020-08-17 15:56:13 -0700137 if *common.Config.BBSim.CpuProfile != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700138 // start profiling
Matteo Scandolo4a036262020-08-17 15:56:13 -0700139 log.Infof("Creating profile file at: %s", *common.Config.BBSim.CpuProfile)
140 f, err := os.Create(*common.Config.BBSim.CpuProfile)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700141 if err != nil {
142 log.Fatal(err)
143 }
Shrey Baid688b4242020-07-10 20:40:10 +0530144 _ = pprof.StartCPUProfile(f)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700145 }
146
Matteo Scandolo4747d292019-08-05 11:50:18 -0700147 log.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -0700148 "OltID": common.Config.Olt.ID,
149 "NumNniPerOlt": common.Config.Olt.NniPorts,
150 "NumPonPerOlt": common.Config.Olt.PonPorts,
151 "NumOnuPerPon": common.Config.Olt.OnusPonPort,
152 "TotalOnus": common.Config.Olt.PonPorts * common.Config.Olt.OnusPonPort,
153 "Delay": common.Config.BBSim.Delay,
154 "Events": common.Config.BBSim.Events,
155 "KafkaEventTopic": common.Config.BBSim.KafkaEventTopic,
156 "ControlledActivation": common.Config.BBSim.ControlledActivation,
157 "EnablePerf": common.Config.BBSim.EnablePerf,
158 "DhcpRetry": common.Config.BBSim.DhcpRetry,
159 "AuthRetry": common.Config.BBSim.AuthRetry,
Matteo Scandolo93566702020-09-30 15:19:27 -0700160 "OltRebootDelay": common.Config.Olt.OltRebootDelay,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700161 }).Info("BroadBand Simulator is on")
162
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700163 // control channels, they are only closed when the goroutine needs to be terminated
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700164 apiDoneChannel := make(chan bool)
165
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100166 olt := devices.CreateOLT(
Matteo Scandolo4a036262020-08-17 15:56:13 -0700167 *common.Config,
168 common.Services,
Matteo Scandoloc1147092019-10-29 09:38:33 -0700169 false,
170 )
Matteo Scandoloe33447a2019-10-31 12:38:23 -0700171
Matteo Scandolo4a036262020-08-17 15:56:13 -0700172 log.Debugf("Created OLT with id: %d", common.Config.Olt.ID)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100173
174 sigs := make(chan os.Signal, 1)
175 // stop API servers on SIGTERM
176 signal.Notify(sigs, syscall.SIGTERM)
177
178 go func() {
179 <-sigs
180 close(apiDoneChannel)
181 }()
182
183 wg := sync.WaitGroup{}
184 wg.Add(4)
185
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700186 go startApiServer(apiDoneChannel, &wg)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200187 go startLegacyApiServer(apiDoneChannel, &wg)
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700188 log.Debugf("Started APIService")
Matteo Scandolo4a036262020-08-17 15:56:13 -0700189 if common.Config.BBSim.SadisServer {
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100190 wg.Add(1)
Matteo Scandolocedde462021-03-09 17:37:16 -0800191 go webserver.StartRestServer(olt, &wg)
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100192 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700193
amit.ghosh258d14c2020-10-02 15:13:38 +0200194 dms, dmserr := dmiserver.StartDmiAPIServer()
195 if dmserr != nil {
196 log.Errorf("Failed to start Device Management Interface Server %v", dmserr)
197 }
198
Matteo Scandolo4a036262020-08-17 15:56:13 -0700199 if common.Config.BBSim.Events {
Pragya Arya324337e2020-02-20 14:35:08 +0530200 // initialize a publisher
Shrey Baid64cda472020-04-24 18:58:18 +0530201 if err := common.InitializePublisher(sarama.NewAsyncProducer, olt.ID); err == nil {
Pragya Arya324337e2020-02-20 14:35:08 +0530202 // start a go routine which will read from channel and publish on kafka
203 go common.KafkaPublisher(olt.EventChannel)
204 } else {
205 log.Errorf("Failed to start kafka publisher: %v", err)
206 }
207 }
208
Matteo Scandolo4747d292019-08-05 11:50:18 -0700209 wg.Wait()
210
211 defer func() {
212 log.Info("BroadBand Simulator is off")
amit.ghosh258d14c2020-10-02 15:13:38 +0200213
214 dms.Stop()
215
Matteo Scandolo4a036262020-08-17 15:56:13 -0700216 if *common.Config.BBSim.CpuProfile != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700217 log.Info("Stopping profiler")
218 pprof.StopCPUProfile()
219 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700220 }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700221}