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