Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 1 | /* |
Matteo Scandolo | 9f61949 | 2019-10-25 13:11:58 -0700 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 3 | |
Matteo Scandolo | 9f61949 | 2019-10-25 13:11:58 -0700 | [diff] [blame] | 4 | * 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 Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 7 | |
Matteo Scandolo | 9f61949 | 2019-10-25 13:11:58 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 9 | |
Matteo Scandolo | 9f61949 | 2019-10-25 13:11:58 -0700 | [diff] [blame] | 10 | * 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 Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 17 | package main |
| 18 | |
| 19 | import ( |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 20 | "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 Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 30 | "github.com/opencord/bbsim/api/bbsim" |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 31 | "github.com/opencord/bbsim/api/legacy" |
Matteo Scandolo | 82c16d0 | 2019-09-24 09:34:32 -0700 | [diff] [blame] | 32 | "github.com/opencord/bbsim/internal/bbsim/api" |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 33 | "github.com/opencord/bbsim/internal/bbsim/devices" |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 34 | "github.com/opencord/bbsim/internal/common" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 35 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 36 | "google.golang.org/grpc" |
| 37 | "google.golang.org/grpc/reflection" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 38 | ) |
| 39 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 40 | func startApiServer(channel chan bool, group *sync.WaitGroup) { |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 41 | // TODO make configurable |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 42 | address := "0.0.0.0:50070" |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 43 | log.Debugf("APIServer listening on: %v", address) |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 44 | 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 Scandolo | 82c16d0 | 2019-09-24 09:34:32 -0700 | [diff] [blame] | 49 | bbsim.RegisterBBSimServer(grpcServer, api.BBSimServer{}) |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 50 | |
| 51 | reflection.Register(grpcServer) |
| 52 | |
| 53 | go grpcServer.Serve(lis) |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 54 | go startApiRestServer(channel, group, address) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 55 | |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 56 | select { |
| 57 | case <-channel: |
| 58 | // if the api channel is closed, stop the gRPC server |
| 59 | grpcServer.Stop() |
| 60 | log.Warnf("Stopping API gRPC server") |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 63 | group.Done() |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // startApiRestServer method starts the REST server (grpc gateway) for BBSim. |
| 67 | func startApiRestServer(channel chan bool, group *sync.WaitGroup, grpcAddress string) { |
| 68 | ctx := context.Background() |
| 69 | ctx, cancel := context.WithCancel(ctx) |
| 70 | defer cancel() |
| 71 | |
| 72 | // TODO make configurable |
| 73 | address := "0.0.0.0:50071" |
| 74 | |
| 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() { |
| 86 | log.Infof("REST API server listening on %s ...", address) |
| 87 | 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 { |
| 94 | case <-channel: |
| 95 | 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. |
| 103 | func startLegacyApiServer(channel chan bool, group *sync.WaitGroup) { |
| 104 | // TODO make configurable |
| 105 | grpcAddress := "0.0.0.0:50072" |
| 106 | restAddress := "0.0.0.0:50073" |
| 107 | |
| 108 | log.Debugf("Legacy APIServer listening on: %v", grpcAddress) |
| 109 | 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 | |
| 117 | go apiserver.Serve(listener) |
| 118 | // Start rest gateway for BBSim server |
| 119 | go api.StartRestGatewayService(channel, group, grpcAddress, restAddress) |
| 120 | |
| 121 | select { |
| 122 | case <-channel: |
| 123 | // if the olt channel is closed, stop the gRPC server |
| 124 | log.Warnf("Stopping legacy API gRPC server") |
| 125 | apiserver.Stop() |
| 126 | break |
| 127 | |
| 128 | } |
| 129 | |
| 130 | group.Done() |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 133 | func main() { |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 134 | options := common.GetBBSimOpts() |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 135 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 136 | common.SetLogLevel(log.StandardLogger(), options.LogLevel, options.LogCaller) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 137 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 138 | if *options.ProfileCpu != "" { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 139 | // start profiling |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 140 | log.Infof("Creating profile file at: %s", *options.ProfileCpu) |
| 141 | f, err := os.Create(*options.ProfileCpu) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 142 | if err != nil { |
| 143 | log.Fatal(err) |
| 144 | } |
| 145 | pprof.StartCPUProfile(f) |
| 146 | } |
| 147 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 148 | log.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 149 | "OltID": options.OltID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 150 | "NumNniPerOlt": options.NumNniPerOlt, |
| 151 | "NumPonPerOlt": options.NumPonPerOlt, |
| 152 | "NumOnuPerPon": options.NumOnuPerPon, |
Matteo Scandolo | fe9ac25 | 2019-10-25 11:40:17 -0700 | [diff] [blame] | 153 | "TotalOnus": options.NumPonPerOlt * options.NumOnuPerPon, |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 154 | "Auth": options.Auth, |
| 155 | "Dhcp": options.Dhcp, |
Matteo Scandolo | e33447a | 2019-10-31 12:38:23 -0700 | [diff] [blame^] | 156 | "Delay": options.Delay, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 157 | }).Info("BroadBand Simulator is on") |
| 158 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 159 | // control channels, they are only closed when the goroutine needs to be terminated |
| 160 | oltDoneChannel := make(chan bool) |
| 161 | apiDoneChannel := make(chan bool) |
| 162 | |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 163 | sigs := make(chan os.Signal, 1) |
| 164 | // stop API and OLT servers on SIGTERM |
| 165 | signal.Notify(sigs, syscall.SIGTERM) |
| 166 | |
| 167 | go func() { |
| 168 | <-sigs |
| 169 | // TODO check when these servers should be shutdown |
| 170 | close(apiDoneChannel) |
| 171 | close(oltDoneChannel) |
| 172 | }() |
| 173 | |
Matteo Scandolo | e33447a | 2019-10-31 12:38:23 -0700 | [diff] [blame^] | 174 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 175 | wg := sync.WaitGroup{} |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 176 | wg.Add(5) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 177 | |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 178 | olt := devices.CreateOLT( |
| 179 | options.OltID, |
| 180 | options.NumNniPerOlt, |
| 181 | options.NumPonPerOlt, |
| 182 | options.NumOnuPerPon, |
| 183 | options.STag, |
| 184 | options.CTagInit, |
| 185 | &oltDoneChannel, |
| 186 | &apiDoneChannel, |
| 187 | options.Auth, |
| 188 | options.Dhcp, |
Matteo Scandolo | e33447a | 2019-10-31 12:38:23 -0700 | [diff] [blame^] | 189 | options.Delay, |
Matteo Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 190 | false, |
| 191 | ) |
Matteo Scandolo | e33447a | 2019-10-31 12:38:23 -0700 | [diff] [blame^] | 192 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 193 | go devices.StartOlt(olt, &wg) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 194 | log.Debugf("Created OLT with id: %d", options.OltID) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 195 | go startApiServer(apiDoneChannel, &wg) |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 196 | go startLegacyApiServer(apiDoneChannel, &wg) |
| 197 | |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 198 | log.Debugf("Started APIService") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 199 | |
| 200 | wg.Wait() |
| 201 | |
| 202 | defer func() { |
| 203 | log.Info("BroadBand Simulator is off") |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 204 | if *options.ProfileCpu != "" { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 205 | log.Info("Stopping profiler") |
| 206 | pprof.StopCPUProfile() |
| 207 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 208 | }() |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 209 | } |