Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 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 |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 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. |
| 15 | */ |
| 16 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "flag" |
Matteo Scandolo | 1100699 | 2019-08-28 11:29:46 -0700 | [diff] [blame] | 21 | "github.com/opencord/bbsim/api/bbsim" |
| 22 | "github.com/opencord/bbsim/internal/bbsim/devices" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 23 | log "github.com/sirupsen/logrus" |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 24 | "google.golang.org/grpc" |
| 25 | "google.golang.org/grpc/reflection" |
| 26 | "net" |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 27 | "os" |
| 28 | "runtime/pprof" |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 29 | "sync" |
| 30 | ) |
| 31 | |
| 32 | func getOpts() *CliOptions { |
| 33 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 34 | olt_id := flag.Int("olt_id", 0, "Number of OLT devices to be emulated (default is 1)") |
| 35 | nni := flag.Int("nni", 1, "Number of NNI ports per OLT device to be emulated (default is 1)") |
| 36 | pon := flag.Int("pon", 1, "Number of PON ports per OLT device to be emulated (default is 1)") |
| 37 | onu := flag.Int("onu", 1, "Number of ONU devices per PON port to be emulated (default is 1)") |
| 38 | s_tag := flag.Int("s_tag", 900, "S-Tag value (default is 900)") |
| 39 | c_tag_init := flag.Int("c_tag", 900, "C-Tag starting value (default is 900), each ONU will get a sequentail one (targeting 1024 ONUs per BBSim instance the range is bug enough)") |
| 40 | profileCpu := flag.String("cpuprofile", "", "write cpu profile to file") |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 41 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 42 | flag.Parse() |
| 43 | |
| 44 | o := new(CliOptions) |
| 45 | |
| 46 | o.OltID = int(*olt_id) |
| 47 | o.NumNniPerOlt = int(*nni) |
| 48 | o.NumPonPerOlt = int(*pon) |
| 49 | o.NumOnuPerPon = int(*onu) |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 50 | o.STag = int(*s_tag) |
| 51 | o.CTagInit = int(*c_tag_init) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 52 | o.profileCpu = profileCpu |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 53 | |
| 54 | return o |
| 55 | } |
| 56 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 57 | func startApiServer(channel chan bool, group *sync.WaitGroup) { |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 58 | // TODO make configurable |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 59 | address := "0.0.0.0:50070" |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 60 | log.Debugf("APIServer Listening on: %v", address) |
| 61 | lis, err := net.Listen("tcp", address) |
| 62 | if err != nil { |
| 63 | log.Fatalf("APIServer failed to listen: %v", err) |
| 64 | } |
| 65 | grpcServer := grpc.NewServer() |
| 66 | bbsim.RegisterBBSimServer(grpcServer, BBSimServer{}) |
| 67 | |
| 68 | reflection.Register(grpcServer) |
| 69 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 70 | wg := sync.WaitGroup{} |
| 71 | wg.Add(1) |
| 72 | |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 73 | go grpcServer.Serve(lis) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 74 | |
| 75 | for { |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 76 | _, ok := <-channel |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 77 | if !ok { |
| 78 | // if the olt channel is closed, stop the gRPC server |
| 79 | log.Warnf("Stopping API gRPC server") |
| 80 | grpcServer.Stop() |
| 81 | wg.Done() |
| 82 | break |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | wg.Wait() |
| 87 | group.Done() |
| 88 | return |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 91 | func init() { |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 92 | // TODO make configurable both via CLI and via ENV (for the tests) |
Matteo Scandolo | 3bc7374 | 2019-08-20 14:04:04 -0700 | [diff] [blame] | 93 | log.SetLevel(log.DebugLevel) |
| 94 | //log.SetLevel(log.TraceLevel) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 95 | //log.SetReportCaller(true) |
| 96 | } |
| 97 | |
| 98 | func main() { |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 99 | options := getOpts() |
| 100 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 101 | if *options.profileCpu != "" { |
| 102 | // start profiling |
| 103 | log.Infof("Creating profile file at: %s", *options.profileCpu) |
| 104 | f, err := os.Create(*options.profileCpu) |
| 105 | if err != nil { |
| 106 | log.Fatal(err) |
| 107 | } |
| 108 | pprof.StartCPUProfile(f) |
| 109 | } |
| 110 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 111 | log.WithFields(log.Fields{ |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 112 | "OltID": options.OltID, |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 113 | "NumNniPerOlt": options.NumNniPerOlt, |
| 114 | "NumPonPerOlt": options.NumPonPerOlt, |
| 115 | "NumOnuPerPon": options.NumOnuPerPon, |
| 116 | }).Info("BroadBand Simulator is on") |
| 117 | |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 118 | // control channels, they are only closed when the goroutine needs to be terminated |
| 119 | oltDoneChannel := make(chan bool) |
| 120 | apiDoneChannel := make(chan bool) |
| 121 | |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 122 | wg := sync.WaitGroup{} |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 123 | wg.Add(2) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 124 | |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 125 | go devices.CreateOLT(options.OltID, options.NumNniPerOlt, options.NumPonPerOlt, options.NumOnuPerPon, options.STag, options.CTagInit, &oltDoneChannel, &apiDoneChannel, &wg) |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 126 | log.Debugf("Created OLT with id: %d", options.OltID) |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 127 | go startApiServer(apiDoneChannel, &wg) |
Matteo Scandolo | 84f7d48 | 2019-08-08 19:00:47 -0700 | [diff] [blame] | 128 | log.Debugf("Started APIService") |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 129 | |
| 130 | wg.Wait() |
| 131 | |
| 132 | defer func() { |
| 133 | log.Info("BroadBand Simulator is off") |
Matteo Scandolo | 47e69bb | 2019-08-28 15:41:12 -0700 | [diff] [blame] | 134 | if *options.profileCpu != "" { |
| 135 | log.Info("Stopping profiler") |
| 136 | pprof.StopCPUProfile() |
| 137 | } |
Matteo Scandolo | 4747d29 | 2019-08-05 11:50:18 -0700 | [diff] [blame] | 138 | }() |
Matteo Scandolo | 4b3fc7e | 2019-09-17 16:49:54 -0700 | [diff] [blame^] | 139 | } |