blob: 7d8ea06a4365f9c8e5d45e8bc9bae363ce1aad07 [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
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 Scandolo4747d292019-08-05 11:50:18 -070017package main
18
19import (
20 "flag"
Matteo Scandolo11006992019-08-28 11:29:46 -070021 "github.com/opencord/bbsim/api/bbsim"
Matteo Scandolo82c16d02019-09-24 09:34:32 -070022 "github.com/opencord/bbsim/internal/bbsim/api"
Matteo Scandolo11006992019-08-28 11:29:46 -070023 "github.com/opencord/bbsim/internal/bbsim/devices"
Matteo Scandolo4747d292019-08-05 11:50:18 -070024 log "github.com/sirupsen/logrus"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070025 "google.golang.org/grpc"
26 "google.golang.org/grpc/reflection"
27 "net"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070028 "os"
29 "runtime/pprof"
Matteo Scandolo4747d292019-08-05 11:50:18 -070030 "sync"
31)
32
Matteo Scandolo82c16d02019-09-24 09:34:32 -070033type 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 Scandolo4747d292019-08-05 11:50:18 -070043func getOpts() *CliOptions {
44
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070045 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 Scandolo47e69bb2019-08-28 15:41:12 -070052
Matteo Scandolo4747d292019-08-05 11:50:18 -070053 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 Scandolo4b3fc7e2019-09-17 16:49:54 -070061 o.STag = int(*s_tag)
62 o.CTagInit = int(*c_tag_init)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070063 o.profileCpu = profileCpu
Matteo Scandolo4747d292019-08-05 11:50:18 -070064
65 return o
66}
67
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070068func startApiServer(channel chan bool, group *sync.WaitGroup) {
Matteo Scandolo84f7d482019-08-08 19:00:47 -070069 // TODO make configurable
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070070 address := "0.0.0.0:50070"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070071 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 Scandolo82c16d02019-09-24 09:34:32 -070077 bbsim.RegisterBBSimServer(grpcServer, api.BBSimServer{})
Matteo Scandolo84f7d482019-08-08 19:00:47 -070078
79 reflection.Register(grpcServer)
80
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070081 wg := sync.WaitGroup{}
82 wg.Add(1)
83
Matteo Scandolo84f7d482019-08-08 19:00:47 -070084 go grpcServer.Serve(lis)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070085
86 for {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070087 _, ok := <-channel
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070088 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 Scandolo84f7d482019-08-08 19:00:47 -0700100}
101
Matteo Scandolo4747d292019-08-05 11:50:18 -0700102func init() {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700103 // TODO make configurable both via CLI and via ENV (for the tests)
Matteo Scandolo3bc73742019-08-20 14:04:04 -0700104 log.SetLevel(log.DebugLevel)
105 //log.SetLevel(log.TraceLevel)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700106 //log.SetReportCaller(true)
107}
108
109func main() {
Matteo Scandolo4747d292019-08-05 11:50:18 -0700110 options := getOpts()
111
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700112 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 Scandolo4747d292019-08-05 11:50:18 -0700122 log.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700123 "OltID": options.OltID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700124 "NumNniPerOlt": options.NumNniPerOlt,
125 "NumPonPerOlt": options.NumPonPerOlt,
126 "NumOnuPerPon": options.NumOnuPerPon,
127 }).Info("BroadBand Simulator is on")
128
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700129 // 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 Scandolo4747d292019-08-05 11:50:18 -0700133 wg := sync.WaitGroup{}
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700134 wg.Add(2)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700135
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700136 go devices.CreateOLT(options.OltID, options.NumNniPerOlt, options.NumPonPerOlt, options.NumOnuPerPon, options.STag, options.CTagInit, &oltDoneChannel, &apiDoneChannel, &wg)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700137 log.Debugf("Created OLT with id: %d", options.OltID)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700138 go startApiServer(apiDoneChannel, &wg)
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700139 log.Debugf("Started APIService")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700140
141 wg.Wait()
142
143 defer func() {
144 log.Info("BroadBand Simulator is off")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700145 if *options.profileCpu != "" {
146 log.Info("Stopping profiler")
147 pprof.StopCPUProfile()
148 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700149 }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700150}