blob: 7c47e9102430b6f7fc2bf93a551dfb4ff021ea48 [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"
22 "github.com/opencord/bbsim/internal/bbsim/devices"
Matteo Scandolo4747d292019-08-05 11:50:18 -070023 log "github.com/sirupsen/logrus"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070024 "google.golang.org/grpc"
25 "google.golang.org/grpc/reflection"
26 "net"
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070027 "os"
28 "runtime/pprof"
Matteo Scandolo4747d292019-08-05 11:50:18 -070029 "sync"
30)
31
32func getOpts() *CliOptions {
33
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070034 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 Scandolo47e69bb2019-08-28 15:41:12 -070041
Matteo Scandolo4747d292019-08-05 11:50:18 -070042 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 Scandolo4b3fc7e2019-09-17 16:49:54 -070050 o.STag = int(*s_tag)
51 o.CTagInit = int(*c_tag_init)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070052 o.profileCpu = profileCpu
Matteo Scandolo4747d292019-08-05 11:50:18 -070053
54 return o
55}
56
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070057func startApiServer(channel chan bool, group *sync.WaitGroup) {
Matteo Scandolo84f7d482019-08-08 19:00:47 -070058 // TODO make configurable
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070059 address := "0.0.0.0:50070"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070060 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 Scandolo47e69bb2019-08-28 15:41:12 -070070 wg := sync.WaitGroup{}
71 wg.Add(1)
72
Matteo Scandolo84f7d482019-08-08 19:00:47 -070073 go grpcServer.Serve(lis)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070074
75 for {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070076 _, ok := <-channel
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070077 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 Scandolo84f7d482019-08-08 19:00:47 -070089}
90
Matteo Scandolo4747d292019-08-05 11:50:18 -070091func init() {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070092 // TODO make configurable both via CLI and via ENV (for the tests)
Matteo Scandolo3bc73742019-08-20 14:04:04 -070093 log.SetLevel(log.DebugLevel)
94 //log.SetLevel(log.TraceLevel)
Matteo Scandolo4747d292019-08-05 11:50:18 -070095 //log.SetReportCaller(true)
96}
97
98func main() {
Matteo Scandolo4747d292019-08-05 11:50:18 -070099 options := getOpts()
100
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700101 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 Scandolo4747d292019-08-05 11:50:18 -0700111 log.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700112 "OltID": options.OltID,
Matteo Scandolo4747d292019-08-05 11:50:18 -0700113 "NumNniPerOlt": options.NumNniPerOlt,
114 "NumPonPerOlt": options.NumPonPerOlt,
115 "NumOnuPerPon": options.NumOnuPerPon,
116 }).Info("BroadBand Simulator is on")
117
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700118 // 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 Scandolo4747d292019-08-05 11:50:18 -0700122 wg := sync.WaitGroup{}
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700123 wg.Add(2)
Matteo Scandolo4747d292019-08-05 11:50:18 -0700124
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700125 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 -0700126 log.Debugf("Created OLT with id: %d", options.OltID)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700127 go startApiServer(apiDoneChannel, &wg)
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700128 log.Debugf("Started APIService")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700129
130 wg.Wait()
131
132 defer func() {
133 log.Info("BroadBand Simulator is off")
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700134 if *options.profileCpu != "" {
135 log.Info("Stopping profiler")
136 pprof.StopCPUProfile()
137 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700138 }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700139}