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 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 40 | func startApiServer(apiDoneChannel chan bool, group *sync.WaitGroup) { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 41 | address := common.Options.BBSim.ApiAddress |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 42 | log.Debugf("APIServer listening on: %v", address) |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 43 | 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 Scandolo | 82c16d0 | 2019-09-24 09:34:32 -0700 | [diff] [blame] | 48 | bbsim.RegisterBBSimServer(grpcServer, api.BBSimServer{}) |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 49 | |
| 50 | reflection.Register(grpcServer) |
| 51 | |
| 52 | go grpcServer.Serve(lis) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 53 | go startApiRestServer(apiDoneChannel, group, address) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 54 | |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 55 | select { |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 56 | case <-apiDoneChannel: |
| 57 | // if the API channel is closed, stop the gRPC server |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 58 | grpcServer.Stop() |
| 59 | log.Warnf("Stopping API gRPC server") |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 62 | group.Done() |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // startApiRestServer method starts the REST server (grpc gateway) for BBSim. |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 66 | func startApiRestServer(apiDoneChannel chan bool, group *sync.WaitGroup, grpcAddress string) { |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 67 | ctx := context.Background() |
| 68 | ctx, cancel := context.WithCancel(ctx) |
| 69 | defer cancel() |
| 70 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 71 | address := common.Options.BBSim.RestApiAddress |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 72 | |
| 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 Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 92 | case <-apiDoneChannel: |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 93 | 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 Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 101 | func startLegacyApiServer(apiDoneChannel chan bool, group *sync.WaitGroup) { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 102 | grpcAddress := common.Options.BBSim.LegacyApiAddress |
| 103 | restAddress := common.Options.BBSim.LegacyRestApiAddress |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 104 | |
| 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 Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 116 | go api.StartRestGatewayService(apiDoneChannel, group, grpcAddress, restAddress) |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 117 | |
| 118 | select { |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 119 | case <-apiDoneChannel: |
| 120 | // if the API channel is closed, stop the gRPC server |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 121 | log.Warnf("Stopping legacy API gRPC server") |
| 122 | apiserver.Stop() |
| 123 | break |
| 124 | |
| 125 | } |
| 126 | |
| 127 | group.Done() |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 130 | func main() { |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 131 | |
Matteo Scandolo | 40e067f | 2019-10-16 16:59:41 -0700 | [diff] [blame] | 132 | options := common.GetBBSimOpts() |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 133 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 134 | common.SetLogLevel(log.StandardLogger(), options.BBSim.LogLevel, options.BBSim.LogCaller) |
| 135 | log.Tracef("BBSim options: %+v", options) |
Matteo Scandolo | 2bf742a | 2019-10-01 11:33:34 -0700 | [diff] [blame] | 136 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 137 | if *options.BBSim.CpuProfile != "" { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 138 | // start profiling |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 139 | log.Infof("Creating profile file at: %s", *options.BBSim.CpuProfile) |
| 140 | f, err := os.Create(*options.BBSim.CpuProfile) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 141 | if err != nil { |
| 142 | log.Fatal(err) |
| 143 | } |
| 144 | pprof.StartCPUProfile(f) |
| 145 | } |
| 146 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 147 | log.WithFields(log.Fields{ |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 148 | "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 Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 156 | }).Info("BroadBand Simulator is on") |
| 157 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 158 | // control channels, they are only closed when the goroutine needs to be terminated |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 159 | apiDoneChannel := make(chan bool) |
| 160 | |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 161 | devices.CreateOLT( |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 162 | 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 Scandolo | c114709 | 2019-10-29 09:38:33 -0700 | [diff] [blame] | 171 | false, |
| 172 | ) |
Matteo Scandolo | e33447a | 2019-10-31 12:38:23 -0700 | [diff] [blame] | 173 | |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 174 | log.Debugf("Created OLT with id: %d", options.Olt.ID) |
Zdravko Bozakov | 681364d | 2019-11-10 14:28:46 +0100 | [diff] [blame] | 175 | |
| 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 Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 188 | go startApiServer(apiDoneChannel, &wg) |
Zdravko Bozakov | 2da7634 | 2019-10-21 09:47:35 +0200 | [diff] [blame] | 189 | go startLegacyApiServer(apiDoneChannel, &wg) |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 190 | log.Debugf("Started APIService") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 191 | |
| 192 | wg.Wait() |
| 193 | |
| 194 | defer func() { |
| 195 | log.Info("BroadBand Simulator is off") |
Zdravko Bozakov | 3ddb245 | 2019-11-29 14:33:41 +0100 | [diff] [blame^] | 196 | if *options.BBSim.CpuProfile != "" { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 197 | log.Info("Stopping profiler") |
| 198 | pprof.StopCPUProfile() |
| 199 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 200 | }() |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame] | 201 | } |