blob: 75c7263d18eb42bf6ba1fa34923d3be14052592e [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
Shrey Baid64cda472020-04-24 18:58:18 +053029 "github.com/Shopify/sarama"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020030 "github.com/grpc-ecosystem/grpc-gateway/runtime"
Matteo Scandolo11006992019-08-28 11:29:46 -070031 "github.com/opencord/bbsim/api/bbsim"
Zdravko Bozakov2da76342019-10-21 09:47:35 +020032 "github.com/opencord/bbsim/api/legacy"
Matteo Scandolo82c16d02019-09-24 09:34:32 -070033 "github.com/opencord/bbsim/internal/bbsim/api"
Matteo Scandolo11006992019-08-28 11:29:46 -070034 "github.com/opencord/bbsim/internal/bbsim/devices"
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010035 "github.com/opencord/bbsim/internal/bbsim/responders/sadis"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070036 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo4747d292019-08-05 11:50:18 -070037 log "github.com/sirupsen/logrus"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070038 "google.golang.org/grpc"
39 "google.golang.org/grpc/reflection"
Matteo Scandolo4747d292019-08-05 11:50:18 -070040)
41
Zdravko Bozakov681364d2019-11-10 14:28:46 +010042func startApiServer(apiDoneChannel chan bool, group *sync.WaitGroup) {
Matteo Scandolo4a036262020-08-17 15:56:13 -070043 address := common.Config.BBSim.ApiAddress
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010044 log.Debugf("APIServer listening on %v", address)
Matteo Scandolo84f7d482019-08-08 19:00:47 -070045 lis, err := net.Listen("tcp", address)
46 if err != nil {
47 log.Fatalf("APIServer failed to listen: %v", err)
48 }
49 grpcServer := grpc.NewServer()
Matteo Scandolo82c16d02019-09-24 09:34:32 -070050 bbsim.RegisterBBSimServer(grpcServer, api.BBSimServer{})
Matteo Scandolo84f7d482019-08-08 19:00:47 -070051
52 reflection.Register(grpcServer)
53
Shrey Baid688b4242020-07-10 20:40:10 +053054 go func() { _ = grpcServer.Serve(lis) }()
Zdravko Bozakov681364d2019-11-10 14:28:46 +010055 go startApiRestServer(apiDoneChannel, group, address)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070056
Shrey Baid688b4242020-07-10 20:40:10 +053057 x := <-apiDoneChannel
58 if x {
Zdravko Bozakov681364d2019-11-10 14:28:46 +010059 // if the API channel is closed, stop the gRPC server
Zdravko Bozakov2da76342019-10-21 09:47:35 +020060 grpcServer.Stop()
61 log.Warnf("Stopping API gRPC server")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070062 }
63
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070064 group.Done()
Zdravko Bozakov2da76342019-10-21 09:47:35 +020065}
66
67// startApiRestServer method starts the REST server (grpc gateway) for BBSim.
Zdravko Bozakov681364d2019-11-10 14:28:46 +010068func startApiRestServer(apiDoneChannel chan bool, group *sync.WaitGroup, grpcAddress string) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020069 ctx := context.Background()
70 ctx, cancel := context.WithCancel(ctx)
71 defer cancel()
72
Matteo Scandolo4a036262020-08-17 15:56:13 -070073 address := common.Config.BBSim.RestApiAddress
Zdravko Bozakov2da76342019-10-21 09:47:35 +020074
75 mux := runtime.NewServeMux()
76 opts := []grpc.DialOption{grpc.WithInsecure()}
77
78 if err := bbsim.RegisterBBSimHandlerFromEndpoint(ctx, mux, grpcAddress, opts); err != nil {
79 log.Errorf("Could not register API server: %v", err)
80 return
81 }
82
83 s := &http.Server{Addr: address, Handler: mux}
84
85 go func() {
Zdravko Bozakov958d81c2019-12-13 22:09:48 +010086 log.Infof("REST API server listening on %s", address)
Zdravko Bozakov2da76342019-10-21 09:47:35 +020087 if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
88 log.Errorf("Could not start API server: %v", err)
89 return
90 }
91 }()
92
Shrey Baid688b4242020-07-10 20:40:10 +053093 x := <-apiDoneChannel
94 if x {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020095 log.Warnf("Stopping API REST server")
Shrey Baid688b4242020-07-10 20:40:10 +053096 _ = s.Shutdown(ctx)
Zdravko Bozakov2da76342019-10-21 09:47:35 +020097 }
98
99 group.Done()
100}
101
102// 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 +0100103func startLegacyApiServer(apiDoneChannel chan bool, group *sync.WaitGroup) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700104 grpcAddress := common.Config.BBSim.LegacyApiAddress
105 restAddress := common.Config.BBSim.LegacyRestApiAddress
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200106
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100107 log.Debugf("Legacy APIServer listening on %v", grpcAddress)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200108 listener, err := net.Listen("tcp", grpcAddress)
109 if err != nil {
110 log.Fatalf("Legacy APIServer failed to listen: %v", err)
111 return
112 }
113 apiserver := grpc.NewServer()
114 legacy.RegisterBBSimServiceServer(apiserver, api.BBSimLegacyServer{})
115
Shrey Baid688b4242020-07-10 20:40:10 +0530116 go func() { _ = apiserver.Serve(listener) }()
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200117 // Start rest gateway for BBSim server
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100118 go api.StartRestGatewayService(apiDoneChannel, group, grpcAddress, restAddress)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200119
Shrey Baid688b4242020-07-10 20:40:10 +0530120 x := <-apiDoneChannel
121 if x {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100122 // if the API channel is closed, stop the gRPC server
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200123 log.Warnf("Stopping legacy API gRPC server")
124 apiserver.Stop()
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200125 }
126
127 group.Done()
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700128}
129
Matteo Scandolo4747d292019-08-05 11:50:18 -0700130func main() {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100131
Matteo Scandolo4a036262020-08-17 15:56:13 -0700132 common.LoadConfig()
Matteo Scandolo4747d292019-08-05 11:50:18 -0700133
Matteo Scandolo4a036262020-08-17 15:56:13 -0700134 common.SetLogLevel(log.StandardLogger(), common.Config.BBSim.LogLevel, common.Config.BBSim.LogCaller)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700135
Matteo Scandolo4a036262020-08-17 15:56:13 -0700136 if *common.Config.BBSim.CpuProfile != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700137 // start profiling
Matteo Scandolo4a036262020-08-17 15:56:13 -0700138 log.Infof("Creating profile file at: %s", *common.Config.BBSim.CpuProfile)
139 f, err := os.Create(*common.Config.BBSim.CpuProfile)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700140 if err != nil {
141 log.Fatal(err)
142 }
Shrey Baid688b4242020-07-10 20:40:10 +0530143 _ = pprof.StartCPUProfile(f)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700144 }
145
Matteo Scandolo4747d292019-08-05 11:50:18 -0700146 log.WithFields(log.Fields{
Matteo Scandolo4a036262020-08-17 15:56:13 -0700147 "OltID": common.Config.Olt.ID,
148 "NumNniPerOlt": common.Config.Olt.NniPorts,
149 "NumPonPerOlt": common.Config.Olt.PonPorts,
150 "NumOnuPerPon": common.Config.Olt.OnusPonPort,
151 "TotalOnus": common.Config.Olt.PonPorts * common.Config.Olt.OnusPonPort,
152 "Delay": common.Config.BBSim.Delay,
153 "Events": common.Config.BBSim.Events,
154 "KafkaEventTopic": common.Config.BBSim.KafkaEventTopic,
155 "ControlledActivation": common.Config.BBSim.ControlledActivation,
156 "EnablePerf": common.Config.BBSim.EnablePerf,
157 "DhcpRetry": common.Config.BBSim.DhcpRetry,
158 "AuthRetry": common.Config.BBSim.AuthRetry,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700159 }).Info("BroadBand Simulator is on")
160
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700161 // control channels, they are only closed when the goroutine needs to be terminated
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700162 apiDoneChannel := make(chan bool)
163
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100164 olt := devices.CreateOLT(
Matteo Scandolo4a036262020-08-17 15:56:13 -0700165 *common.Config,
166 common.Services,
Matteo Scandoloc1147092019-10-29 09:38:33 -0700167 false,
168 )
Matteo Scandoloe33447a2019-10-31 12:38:23 -0700169
Matteo Scandolo4a036262020-08-17 15:56:13 -0700170 log.Debugf("Created OLT with id: %d", common.Config.Olt.ID)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100171
172 sigs := make(chan os.Signal, 1)
173 // stop API servers on SIGTERM
174 signal.Notify(sigs, syscall.SIGTERM)
175
176 go func() {
177 <-sigs
178 close(apiDoneChannel)
179 }()
180
181 wg := sync.WaitGroup{}
182 wg.Add(4)
183
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700184 go startApiServer(apiDoneChannel, &wg)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200185 go startLegacyApiServer(apiDoneChannel, &wg)
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700186 log.Debugf("Started APIService")
Matteo Scandolo4a036262020-08-17 15:56:13 -0700187 if common.Config.BBSim.SadisServer {
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100188 wg.Add(1)
189 go sadis.StartRestServer(olt, &wg)
190 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700191
Matteo Scandolo4a036262020-08-17 15:56:13 -0700192 if common.Config.BBSim.Events {
Pragya Arya324337e2020-02-20 14:35:08 +0530193 // initialize a publisher
Shrey Baid64cda472020-04-24 18:58:18 +0530194 if err := common.InitializePublisher(sarama.NewAsyncProducer, olt.ID); err == nil {
Pragya Arya324337e2020-02-20 14:35:08 +0530195 // start a go routine which will read from channel and publish on kafka
196 go common.KafkaPublisher(olt.EventChannel)
197 } else {
198 log.Errorf("Failed to start kafka publisher: %v", err)
199 }
200 }
201
Matteo Scandolo4747d292019-08-05 11:50:18 -0700202 wg.Wait()
203
204 defer func() {
205 log.Info("BroadBand Simulator is off")
Matteo Scandolo4a036262020-08-17 15:56:13 -0700206 if *common.Config.BBSim.CpuProfile != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700207 log.Info("Stopping profiler")
208 pprof.StopCPUProfile()
209 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700210 }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700211}