Moving BBSim entrypoint in the cmd folder, building bbsimctl in the container
Change-Id: I4823578ed17c40b13c1c9a561e7aa7e6640f7f19
diff --git a/cmd/.gitignore b/cmd/.gitignore
deleted file mode 100644
index c96a04f..0000000
--- a/cmd/.gitignore
+++ /dev/null
@@ -1,2 +0,0 @@
-*
-!.gitignore
\ No newline at end of file
diff --git a/cmd/bbsim/bbsim.go b/cmd/bbsim/bbsim.go
new file mode 100644
index 0000000..7d8ea06
--- /dev/null
+++ b/cmd/bbsim/bbsim.go
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package main
+
+import (
+ "flag"
+ "github.com/opencord/bbsim/api/bbsim"
+ "github.com/opencord/bbsim/internal/bbsim/api"
+ "github.com/opencord/bbsim/internal/bbsim/devices"
+ log "github.com/sirupsen/logrus"
+ "google.golang.org/grpc"
+ "google.golang.org/grpc/reflection"
+ "net"
+ "os"
+ "runtime/pprof"
+ "sync"
+)
+
+type CliOptions struct {
+ OltID int
+ NumNniPerOlt int
+ NumPonPerOlt int
+ NumOnuPerPon int
+ STag int
+ CTagInit int
+ profileCpu *string
+}
+
+func getOpts() *CliOptions {
+
+ olt_id := flag.Int("olt_id", 0, "Number of OLT devices to be emulated (default is 1)")
+ nni := flag.Int("nni", 1, "Number of NNI ports per OLT device to be emulated (default is 1)")
+ pon := flag.Int("pon", 1, "Number of PON ports per OLT device to be emulated (default is 1)")
+ onu := flag.Int("onu", 1, "Number of ONU devices per PON port to be emulated (default is 1)")
+ s_tag := flag.Int("s_tag", 900, "S-Tag value (default is 900)")
+ 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)")
+ profileCpu := flag.String("cpuprofile", "", "write cpu profile to file")
+
+ flag.Parse()
+
+ o := new(CliOptions)
+
+ o.OltID = int(*olt_id)
+ o.NumNniPerOlt = int(*nni)
+ o.NumPonPerOlt = int(*pon)
+ o.NumOnuPerPon = int(*onu)
+ o.STag = int(*s_tag)
+ o.CTagInit = int(*c_tag_init)
+ o.profileCpu = profileCpu
+
+ return o
+}
+
+func startApiServer(channel chan bool, group *sync.WaitGroup) {
+ // TODO make configurable
+ address := "0.0.0.0:50070"
+ log.Debugf("APIServer Listening on: %v", address)
+ lis, err := net.Listen("tcp", address)
+ if err != nil {
+ log.Fatalf("APIServer failed to listen: %v", err)
+ }
+ grpcServer := grpc.NewServer()
+ bbsim.RegisterBBSimServer(grpcServer, api.BBSimServer{})
+
+ reflection.Register(grpcServer)
+
+ wg := sync.WaitGroup{}
+ wg.Add(1)
+
+ go grpcServer.Serve(lis)
+
+ for {
+ _, ok := <-channel
+ if !ok {
+ // if the olt channel is closed, stop the gRPC server
+ log.Warnf("Stopping API gRPC server")
+ grpcServer.Stop()
+ wg.Done()
+ break
+ }
+ }
+
+ wg.Wait()
+ group.Done()
+ return
+}
+
+func init() {
+ // TODO make configurable both via CLI and via ENV (for the tests)
+ log.SetLevel(log.DebugLevel)
+ //log.SetLevel(log.TraceLevel)
+ //log.SetReportCaller(true)
+}
+
+func main() {
+ options := getOpts()
+
+ if *options.profileCpu != "" {
+ // start profiling
+ log.Infof("Creating profile file at: %s", *options.profileCpu)
+ f, err := os.Create(*options.profileCpu)
+ if err != nil {
+ log.Fatal(err)
+ }
+ pprof.StartCPUProfile(f)
+ }
+
+ log.WithFields(log.Fields{
+ "OltID": options.OltID,
+ "NumNniPerOlt": options.NumNniPerOlt,
+ "NumPonPerOlt": options.NumPonPerOlt,
+ "NumOnuPerPon": options.NumOnuPerPon,
+ }).Info("BroadBand Simulator is on")
+
+ // control channels, they are only closed when the goroutine needs to be terminated
+ oltDoneChannel := make(chan bool)
+ apiDoneChannel := make(chan bool)
+
+ wg := sync.WaitGroup{}
+ wg.Add(2)
+
+ go devices.CreateOLT(options.OltID, options.NumNniPerOlt, options.NumPonPerOlt, options.NumOnuPerPon, options.STag, options.CTagInit, &oltDoneChannel, &apiDoneChannel, &wg)
+ log.Debugf("Created OLT with id: %d", options.OltID)
+ go startApiServer(apiDoneChannel, &wg)
+ log.Debugf("Started APIService")
+
+ wg.Wait()
+
+ defer func() {
+ log.Info("BroadBand Simulator is off")
+ if *options.profileCpu != "" {
+ log.Info("Stopping profiler")
+ pprof.StopCPUProfile()
+ }
+ }()
+}
diff --git a/cmd/bbsimctl/bbsimctl.go b/cmd/bbsimctl/bbsimctl.go
new file mode 100644
index 0000000..29b45c5
--- /dev/null
+++ b/cmd/bbsimctl/bbsimctl.go
@@ -0,0 +1,63 @@
+/*
+ * Portions copyright 2019-present Open Networking Foundation
+ * Original copyright 2019-present Ciena Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package main
+
+import (
+ "fmt"
+ "github.com/jessevdk/go-flags"
+ "github.com/opencord/bbsim/internal/bbsimctl/commands"
+ "github.com/opencord/bbsim/internal/bbsimctl/config"
+ "os"
+ "path"
+)
+
+func main() {
+
+ parser := flags.NewNamedParser(path.Base(os.Args[0]),
+ flags.HelpFlag|flags.PassDoubleDash|flags.PassAfterNonOption)
+ _, err := parser.AddGroup("Global Options", "", &config.GlobalOptions)
+ if err != nil {
+ panic(err)
+ }
+
+ commands.RegisterConfigCommands(parser)
+ commands.RegisterOltCommands(parser)
+ commands.RegisterONUCommands(parser)
+
+ _, err = parser.ParseArgs(os.Args[1:])
+ if err != nil {
+ _, ok := err.(*flags.Error)
+ if ok {
+ real := err.(*flags.Error)
+ if real.Type == flags.ErrHelp {
+ os.Stdout.WriteString(err.Error() + "\n")
+ return
+ }
+ }
+
+ //corderror, ok := err.(corderrors.CordCtlError)
+ //if ok {
+ // if corderror.ShouldDumpStack() || config.GlobalOptions.Debug {
+ // os.Stderr.WriteString("\n" + corderror.Stack())
+ // }
+ //}
+
+ fmt.Fprintf(os.Stderr, "%s: %s\n", os.Args[0], err.Error())
+
+ os.Exit(1)
+ }
+}