blob: 52d5772186aef56f33e10781ecd8b332500499e1 [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"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070034 "github.com/opencord/bbsim/internal/common"
Matteo Scandolo4747d292019-08-05 11:50:18 -070035 log "github.com/sirupsen/logrus"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070036 "google.golang.org/grpc"
37 "google.golang.org/grpc/reflection"
Matteo Scandolo4747d292019-08-05 11:50:18 -070038)
39
Zdravko Bozakov681364d2019-11-10 14:28:46 +010040func startApiServer(apiDoneChannel chan bool, group *sync.WaitGroup) {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010041 address := common.Options.BBSim.ApiAddress
Zdravko Bozakov2da76342019-10-21 09:47:35 +020042 log.Debugf("APIServer listening on: %v", address)
Matteo Scandolo84f7d482019-08-08 19:00:47 -070043 lis, err := net.Listen("tcp", address)
44 if err != nil {
45 log.Fatalf("APIServer failed to listen: %v", err)
46 }
47 grpcServer := grpc.NewServer()
Matteo Scandolo82c16d02019-09-24 09:34:32 -070048 bbsim.RegisterBBSimServer(grpcServer, api.BBSimServer{})
Matteo Scandolo84f7d482019-08-08 19:00:47 -070049
50 reflection.Register(grpcServer)
51
52 go grpcServer.Serve(lis)
Zdravko Bozakov681364d2019-11-10 14:28:46 +010053 go startApiRestServer(apiDoneChannel, group, address)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070054
Zdravko Bozakov2da76342019-10-21 09:47:35 +020055 select {
Zdravko Bozakov681364d2019-11-10 14:28:46 +010056 case <-apiDoneChannel:
57 // if the API channel is closed, stop the gRPC server
Zdravko Bozakov2da76342019-10-21 09:47:35 +020058 grpcServer.Stop()
59 log.Warnf("Stopping API gRPC server")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070060 }
61
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070062 group.Done()
Zdravko Bozakov2da76342019-10-21 09:47:35 +020063}
64
65// startApiRestServer method starts the REST server (grpc gateway) for BBSim.
Zdravko Bozakov681364d2019-11-10 14:28:46 +010066func startApiRestServer(apiDoneChannel chan bool, group *sync.WaitGroup, grpcAddress string) {
Zdravko Bozakov2da76342019-10-21 09:47:35 +020067 ctx := context.Background()
68 ctx, cancel := context.WithCancel(ctx)
69 defer cancel()
70
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +010071 address := common.Options.BBSim.RestApiAddress
Zdravko Bozakov2da76342019-10-21 09:47:35 +020072
73 mux := runtime.NewServeMux()
74 opts := []grpc.DialOption{grpc.WithInsecure()}
75
76 if err := bbsim.RegisterBBSimHandlerFromEndpoint(ctx, mux, grpcAddress, opts); err != nil {
77 log.Errorf("Could not register API server: %v", err)
78 return
79 }
80
81 s := &http.Server{Addr: address, Handler: mux}
82
83 go func() {
84 log.Infof("REST API server listening on %s ...", address)
85 if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
86 log.Errorf("Could not start API server: %v", err)
87 return
88 }
89 }()
90
91 select {
Zdravko Bozakov681364d2019-11-10 14:28:46 +010092 case <-apiDoneChannel:
Zdravko Bozakov2da76342019-10-21 09:47:35 +020093 log.Warnf("Stopping API REST server")
94 s.Shutdown(ctx)
95 }
96
97 group.Done()
98}
99
100// 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 +0100101func startLegacyApiServer(apiDoneChannel chan bool, group *sync.WaitGroup) {
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100102 grpcAddress := common.Options.BBSim.LegacyApiAddress
103 restAddress := common.Options.BBSim.LegacyRestApiAddress
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200104
105 log.Debugf("Legacy APIServer listening on: %v", grpcAddress)
106 listener, err := net.Listen("tcp", grpcAddress)
107 if err != nil {
108 log.Fatalf("Legacy APIServer failed to listen: %v", err)
109 return
110 }
111 apiserver := grpc.NewServer()
112 legacy.RegisterBBSimServiceServer(apiserver, api.BBSimLegacyServer{})
113
114 go apiserver.Serve(listener)
115 // Start rest gateway for BBSim server
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100116 go api.StartRestGatewayService(apiDoneChannel, group, grpcAddress, restAddress)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200117
118 select {
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100119 case <-apiDoneChannel:
120 // if the API channel is closed, stop the gRPC server
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200121 log.Warnf("Stopping legacy API gRPC server")
122 apiserver.Stop()
123 break
124
125 }
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 Scandolo40e067f2019-10-16 16:59:41 -0700132 options := common.GetBBSimOpts()
Matteo Scandolo4747d292019-08-05 11:50:18 -0700133
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100134 common.SetLogLevel(log.StandardLogger(), options.BBSim.LogLevel, options.BBSim.LogCaller)
135 log.Tracef("BBSim options: %+v", options)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -0700136
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100137 if *options.BBSim.CpuProfile != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700138 // start profiling
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100139 log.Infof("Creating profile file at: %s", *options.BBSim.CpuProfile)
140 f, err := os.Create(*options.BBSim.CpuProfile)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700141 if err != nil {
142 log.Fatal(err)
143 }
144 pprof.StartCPUProfile(f)
145 }
146
Matteo Scandolo4747d292019-08-05 11:50:18 -0700147 log.WithFields(log.Fields{
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100148 "OltID": options.Olt.ID,
149 "NumNniPerOlt": options.Olt.NniPorts,
150 "NumPonPerOlt": options.Olt.PonPorts,
151 "NumOnuPerPon": options.Olt.OnusPonPort,
152 "TotalOnus": options.Olt.PonPorts * options.Olt.OnusPonPort,
153 "EnableAuth": options.BBSim.EnableAuth,
154 "Dhcp": options.BBSim.EnableDhcp,
155 "Delay": options.BBSim.Delay,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700156 }).Info("BroadBand Simulator is on")
157
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700158 // control channels, they are only closed when the goroutine needs to be terminated
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700159 apiDoneChannel := make(chan bool)
160
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100161 devices.CreateOLT(
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100162 options.Olt.ID,
163 int(options.Olt.NniPorts),
164 int(options.Olt.PonPorts),
165 int(options.Olt.OnusPonPort),
166 options.BBSim.STag,
167 options.BBSim.CTagInit,
168 options.BBSim.EnableAuth,
169 options.BBSim.EnableDhcp,
170 options.BBSim.Delay,
Matteo Scandoloc1147092019-10-29 09:38:33 -0700171 false,
172 )
Matteo Scandoloe33447a2019-10-31 12:38:23 -0700173
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100174 log.Debugf("Created OLT with id: %d", options.Olt.ID)
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100175
176 sigs := make(chan os.Signal, 1)
177 // stop API servers on SIGTERM
178 signal.Notify(sigs, syscall.SIGTERM)
179
180 go func() {
181 <-sigs
182 close(apiDoneChannel)
183 }()
184
185 wg := sync.WaitGroup{}
186 wg.Add(4)
187
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700188 go startApiServer(apiDoneChannel, &wg)
Zdravko Bozakov2da76342019-10-21 09:47:35 +0200189 go startLegacyApiServer(apiDoneChannel, &wg)
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700190 log.Debugf("Started APIService")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700191
192 wg.Wait()
193
194 defer func() {
195 log.Info("BroadBand Simulator is off")
Zdravko Bozakov3ddb2452019-11-29 14:33:41 +0100196 if *options.BBSim.CpuProfile != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700197 log.Info("Stopping profiler")
198 pprof.StopCPUProfile()
199 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700200 }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700201}