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