[SEBA-836] BBSim Reflector

Change-Id: Ib4ae5a2c24880dc62209bebb81188eca5f57865d
diff --git a/cmd/bbr/bbr.go b/cmd/bbr/bbr.go
new file mode 100644
index 0000000..69b8639
--- /dev/null
+++ b/cmd/bbr/bbr.go
@@ -0,0 +1,80 @@
+/*
+ * 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.
+ */
+
+// BBR (BBSim Reflector) is a tool designed to scale test BBSim
+// It shared most of the code with BBSim itself and replies to messages
+// pretending to be VOLTHA.
+// The idea behind it is that, given that the BBSim and BBR are based on the same
+// codebase, BBR is acting as a wall for BBSim. And you can't beat the wall.
+package main
+
+import (
+	bbrdevices "github.com/opencord/bbsim/internal/bbr/devices"
+	"github.com/opencord/bbsim/internal/bbsim/devices"
+	"github.com/opencord/bbsim/internal/common"
+	log "github.com/sirupsen/logrus"
+	"os"
+	"runtime/pprof"
+	"time"
+)
+
+// usage
+func main() {
+	options := common.GetBBROpts()
+
+	common.SetLogLevel(log.StandardLogger(), options.LogLevel, options.LogCaller)
+
+	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 Reflector is on")
+
+	// NOTE this are probably useless in the MockOLT case, check if we can avoid using them in the CreateOlt method
+	oltDoneChannel := make(chan bool)
+	apiDoneChannel := make(chan bool)
+
+	// create the OLT device
+	olt := devices.CreateOLT(options.OltID, options.NumNniPerOlt, options.NumPonPerOlt, options.NumOnuPerPon, options.STag, options.CTagInit, &oltDoneChannel, &apiDoneChannel, true)
+	oltMock := bbrdevices.OltMock{
+		Olt:           olt,
+		TargetOnus:    options.NumPonPerOlt * options.NumOnuPerPon,
+		CompletedOnus: 0,
+		BBSimIp:       options.BBSimIp,
+		BBSimPort:     options.BBSimPort,
+		BBSimApiPort:  options.BBSimApiPort,
+	}
+
+	// start the enable sequence
+	startTime := time.Now()
+	defer func() {
+		endTime := time.Now()
+		runTime := endTime.Sub(startTime)
+		log.WithField("Duration", runTime).Info("BBR done!")
+	}()
+	oltMock.Start()
+}
diff --git a/cmd/bbsim/bbsim.go b/cmd/bbsim/bbsim.go
index f243801..2ceb50a 100644
--- a/cmd/bbsim/bbsim.go
+++ b/cmd/bbsim/bbsim.go
@@ -17,11 +17,10 @@
 package main
 
 import (
-	"flag"
 	"github.com/opencord/bbsim/api/bbsim"
 	"github.com/opencord/bbsim/internal/bbsim/api"
 	"github.com/opencord/bbsim/internal/bbsim/devices"
-	bbsimLogger "github.com/opencord/bbsim/internal/bbsim/logger"
+	"github.com/opencord/bbsim/internal/common"
 	log "github.com/sirupsen/logrus"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/reflection"
@@ -31,50 +30,6 @@
 	"sync"
 )
 
-type CliOptions struct {
-	OltID        int
-	NumNniPerOlt int
-	NumPonPerOlt int
-	NumOnuPerPon int
-	STag         int
-	CTagInit     int
-	profileCpu   *string
-	logLevel     string
-	logCaller    bool
-}
-
-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")
-
-	logLevel := flag.String("logLevel", "debug", "Set the log level (trace, debug, info, warn, error)")
-	logCaller := flag.Bool("logCaller", false, "Whether to print the caller filename or not")
-
-	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
-	o.logLevel = *logLevel
-	o.logCaller = *logCaller
-
-	return o
-}
-
 func startApiServer(channel chan bool, group *sync.WaitGroup) {
 	// TODO make configurable
 	address := "0.0.0.0:50070"
@@ -110,14 +65,14 @@
 }
 
 func main() {
-	options := getOpts()
+	options := common.GetBBSimOpts()
 
-	bbsimLogger.SetLogLevel(log.StandardLogger(), options.logLevel, options.logCaller)
+	common.SetLogLevel(log.StandardLogger(), options.LogLevel, options.LogCaller)
 
-	if *options.profileCpu != "" {
+	if *options.ProfileCpu != "" {
 		// start profiling
-		log.Infof("Creating profile file at: %s", *options.profileCpu)
-		f, err := os.Create(*options.profileCpu)
+		log.Infof("Creating profile file at: %s", *options.ProfileCpu)
+		f, err := os.Create(*options.ProfileCpu)
 		if err != nil {
 			log.Fatal(err)
 		}
@@ -138,7 +93,8 @@
 	wg := sync.WaitGroup{}
 	wg.Add(2)
 
-	go devices.CreateOLT(options.OltID, options.NumNniPerOlt, options.NumPonPerOlt, options.NumOnuPerPon, options.STag, options.CTagInit, &oltDoneChannel, &apiDoneChannel, &wg)
+	olt := devices.CreateOLT(options.OltID, options.NumNniPerOlt, options.NumPonPerOlt, options.NumOnuPerPon, options.STag, options.CTagInit, &oltDoneChannel, &apiDoneChannel, false)
+	go devices.StartOlt(olt, &wg)
 	log.Debugf("Created OLT with id: %d", options.OltID)
 	go startApiServer(apiDoneChannel, &wg)
 	log.Debugf("Started APIService")
@@ -147,7 +103,7 @@
 
 	defer func() {
 		log.Info("BroadBand Simulator is off")
-		if *options.profileCpu != "" {
+		if *options.ProfileCpu != "" {
 			log.Info("Stopping profiler")
 			pprof.StopCPUProfile()
 		}