blob: 4dade0321240094ccdab79c5f29fa8b9a87fc1fc [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) {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010043 address := common.Options.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
54 go grpcServer.Serve(lis)
Zdravko Bozakov681364d2019-11-10 14:28:46 +010055 go startApiRestServer(apiDoneChannel, group, address)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070056
Zdravko Bozakov2da76342019-10-21 09:47:35 +020057 select {
Zdravko Bozakov681364d2019-11-10 14:28:46 +010058 case <-apiDoneChannel:
59 // 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
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010073 address := common.Options.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
93 select {
Zdravko Bozakov681364d2019-11-10 14:28:46 +010094 case <-apiDoneChannel:
Zdravko Bozakov2da76342019-10-21 09:47:35 +020095 log.Warnf("Stopping API REST server")
96 s.Shutdown(ctx)
97 }
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) {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100104 grpcAddress := common.Options.BBSim.LegacyApiAddress
105 restAddress := common.Options.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
116 go apiserver.Serve(listener)
117 // 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
120 select {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100121 case <-apiDoneChannel:
122 // 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()
125 break
126
127 }
128
129 group.Done()
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700130}
131
Matteo Scandolo4747d292019-08-05 11:50:18 -0700132func main() {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100133
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700134 options := common.GetBBSimOpts()
Matteo Scandolo4747d292019-08-05 11:50:18 -0700135
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100136 common.SetLogLevel(log.StandardLogger(), options.BBSim.LogLevel, options.BBSim.LogCaller)
137 log.Tracef("BBSim options: %+v", options)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700138
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100139 if *options.BBSim.CpuProfile != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700140 // start profiling
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100141 log.Infof("Creating profile file at: %s", *options.BBSim.CpuProfile)
142 f, err := os.Create(*options.BBSim.CpuProfile)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700143 if err != nil {
144 log.Fatal(err)
145 }
146 pprof.StartCPUProfile(f)
147 }
148
Matteo Scandolo4747d292019-08-05 11:50:18 -0700149 log.WithFields(log.Fields{
Pragya Arya2225f202020-01-29 18:05:01 +0530150 "OltID": options.Olt.ID,
151 "NumNniPerOlt": options.Olt.NniPorts,
152 "NumPonPerOlt": options.Olt.PonPorts,
153 "NumOnuPerPon": options.Olt.OnusPonPort,
154 "TotalOnus": options.Olt.PonPorts * options.Olt.OnusPonPort,
155 "EnableAuth": options.BBSim.EnableAuth,
156 "Dhcp": options.BBSim.EnableDhcp,
157 "Delay": options.BBSim.Delay,
Pragya Arya324337e2020-02-20 14:35:08 +0530158 "Events": options.BBSim.Events,
Shrey Baid64cda472020-04-24 18:58:18 +0530159 "KafkaEventTopic": options.BBSim.KafkaEventTopic,
Pragya Arya2225f202020-01-29 18:05:01 +0530160 "ControlledActivation": options.BBSim.ControlledActivation,
Anand S Katti09541352020-01-29 15:54:01 +0530161 "EnablePerf": options.BBSim.EnablePerf,
Matteo Scandolof65e6872020-04-15 15:18:43 -0700162 "CTag": options.BBSim.CTag,
163 "CTagAllocation": options.BBSim.CTagAllocation,
164 "STag": options.BBSim.STag,
165 "STagAllocation": options.BBSim.STagAllocation,
166 "SadisFormat": options.BBSim.SadisFormat,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700167 }).Info("BroadBand Simulator is on")
168
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700169 // control channels, they are only closed when the goroutine needs to be terminated
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700170 apiDoneChannel := make(chan bool)
171
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100172 olt := devices.CreateOLT(
Pragya Arya996a0892020-03-09 21:47:52 +0530173 *options,
Matteo Scandoloc1147092019-10-29 09:38:33 -0700174 false,
175 )
Matteo Scandoloe33447a2019-10-31 12:38:23 -0700176
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100177 log.Debugf("Created OLT with id: %d", options.Olt.ID)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100178
179 sigs := make(chan os.Signal, 1)
180 // stop API servers on SIGTERM
181 signal.Notify(sigs, syscall.SIGTERM)
182
183 go func() {
184 <-sigs
185 close(apiDoneChannel)
186 }()
187
188 wg := sync.WaitGroup{}
189 wg.Add(4)
190
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700191 go startApiServer(apiDoneChannel, &wg)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200192 go startLegacyApiServer(apiDoneChannel, &wg)
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700193 log.Debugf("Started APIService")
Zdravko Bozakov958d81c2019-12-13 22:09:48 +0100194 if common.Options.BBSim.SadisServer != false {
195 wg.Add(1)
196 go sadis.StartRestServer(olt, &wg)
197 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700198
Pragya Arya324337e2020-02-20 14:35:08 +0530199 if options.BBSim.Events {
200 // 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")
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100213 if *options.BBSim.CpuProfile != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700214 log.Info("Stopping profiler")
215 pprof.StopCPUProfile()
216 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700217 }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700218}