blob: 48b9743d0fef77f1a3234b4501eae076ceb96ea7 [file] [log] [blame]
Matteo Scandolo11006992019-08-28 11:29:46 -07001/*
Matteo Scandolo9f619492019-10-25 13:11:58 -07002* Copyright 2018-present Open Networking Foundation
Matteo Scandolo11006992019-08-28 11:29:46 -07003
Matteo Scandolo9f619492019-10-25 13:11:58 -07004* 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
Matteo Scandolo11006992019-08-28 11:29:46 -07007
Matteo Scandolo9f619492019-10-25 13:11:58 -07008* http://www.apache.org/licenses/LICENSE-2.0
Matteo Scandolo11006992019-08-28 11:29:46 -07009
Matteo Scandolo9f619492019-10-25 13:11:58 -070010* 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.
Matteo Scandolo11006992019-08-28 11:29:46 -070015 */
16
Matteo Scandolo4747d292019-08-05 11:50:18 -070017package main
18
19import (
Matteo Scandolo11006992019-08-28 11:29:46 -070020 "github.com/opencord/bbsim/api/bbsim"
Matteo Scandolo82c16d02019-09-24 09:34:32 -070021 "github.com/opencord/bbsim/internal/bbsim/api"
Matteo Scandolo11006992019-08-28 11:29:46 -070022 "github.com/opencord/bbsim/internal/bbsim/devices"
Matteo Scandolo40e067f2019-10-16 16:59:41 -070023 "github.com/opencord/bbsim/internal/common"
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 Scandolo4b3fc7e2019-09-17 16:49:54 -070033func startApiServer(channel chan bool, group *sync.WaitGroup) {
Matteo Scandolo84f7d482019-08-08 19:00:47 -070034 // TODO make configurable
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070035 address := "0.0.0.0:50070"
Matteo Scandolo84f7d482019-08-08 19:00:47 -070036 log.Debugf("APIServer Listening on: %v", address)
37 lis, err := net.Listen("tcp", address)
38 if err != nil {
39 log.Fatalf("APIServer failed to listen: %v", err)
40 }
41 grpcServer := grpc.NewServer()
Matteo Scandolo82c16d02019-09-24 09:34:32 -070042 bbsim.RegisterBBSimServer(grpcServer, api.BBSimServer{})
Matteo Scandolo84f7d482019-08-08 19:00:47 -070043
44 reflection.Register(grpcServer)
45
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070046 wg := sync.WaitGroup{}
47 wg.Add(1)
48
Matteo Scandolo84f7d482019-08-08 19:00:47 -070049 go grpcServer.Serve(lis)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070050
51 for {
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070052 _, ok := <-channel
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070053 if !ok {
54 // if the olt channel is closed, stop the gRPC server
55 log.Warnf("Stopping API gRPC server")
56 grpcServer.Stop()
57 wg.Done()
58 break
59 }
60 }
61
62 wg.Wait()
63 group.Done()
64 return
Matteo Scandolo84f7d482019-08-08 19:00:47 -070065}
66
Matteo Scandolo4747d292019-08-05 11:50:18 -070067func main() {
Matteo Scandolo40e067f2019-10-16 16:59:41 -070068 options := common.GetBBSimOpts()
Matteo Scandolo4747d292019-08-05 11:50:18 -070069
Matteo Scandolo40e067f2019-10-16 16:59:41 -070070 common.SetLogLevel(log.StandardLogger(), options.LogLevel, options.LogCaller)
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070071
Matteo Scandolo40e067f2019-10-16 16:59:41 -070072 if *options.ProfileCpu != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070073 // start profiling
Matteo Scandolo40e067f2019-10-16 16:59:41 -070074 log.Infof("Creating profile file at: %s", *options.ProfileCpu)
75 f, err := os.Create(*options.ProfileCpu)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070076 if err != nil {
77 log.Fatal(err)
78 }
79 pprof.StartCPUProfile(f)
80 }
81
Matteo Scandolo4747d292019-08-05 11:50:18 -070082 log.WithFields(log.Fields{
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -070083 "OltID": options.OltID,
Matteo Scandolo4747d292019-08-05 11:50:18 -070084 "NumNniPerOlt": options.NumNniPerOlt,
85 "NumPonPerOlt": options.NumPonPerOlt,
86 "NumOnuPerPon": options.NumOnuPerPon,
87 }).Info("BroadBand Simulator is on")
88
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070089 // control channels, they are only closed when the goroutine needs to be terminated
90 oltDoneChannel := make(chan bool)
91 apiDoneChannel := make(chan bool)
92
Matteo Scandolo4747d292019-08-05 11:50:18 -070093 wg := sync.WaitGroup{}
Matteo Scandolo84f7d482019-08-08 19:00:47 -070094 wg.Add(2)
Matteo Scandolo4747d292019-08-05 11:50:18 -070095
Matteo Scandolo40e067f2019-10-16 16:59:41 -070096 olt := devices.CreateOLT(options.OltID, options.NumNniPerOlt, options.NumPonPerOlt, options.NumOnuPerPon, options.STag, options.CTagInit, &oltDoneChannel, &apiDoneChannel, false)
97 go devices.StartOlt(olt, &wg)
Matteo Scandolo4747d292019-08-05 11:50:18 -070098 log.Debugf("Created OLT with id: %d", options.OltID)
Matteo Scandolo47e69bb2019-08-28 15:41:12 -070099 go startApiServer(apiDoneChannel, &wg)
Matteo Scandolo84f7d482019-08-08 19:00:47 -0700100 log.Debugf("Started APIService")
Matteo Scandolo4747d292019-08-05 11:50:18 -0700101
102 wg.Wait()
103
104 defer func() {
105 log.Info("BroadBand Simulator is off")
Matteo Scandolo40e067f2019-10-16 16:59:41 -0700106 if *options.ProfileCpu != "" {
Matteo Scandolo47e69bb2019-08-28 15:41:12 -0700107 log.Info("Stopping profiler")
108 pprof.StopCPUProfile()
109 }
Matteo Scandolo4747d292019-08-05 11:50:18 -0700110 }()
Matteo Scandolo4b3fc7e2019-09-17 16:49:54 -0700111}