Merge "VOL-4112 Add configuration flag to define the number of UNIs created in BBSim uni_ports integer parameter added to OltConfig as command line parameter"
diff --git a/configs/bbsim.yaml b/configs/bbsim.yaml
index 6fb4676..5d0dfcb 100644
--- a/configs/bbsim.yaml
+++ b/configs/bbsim.yaml
@@ -34,6 +34,7 @@
   device_serial_number: BBSM00000001
   pon_ports: 1
   nni_ports: 1
+  uni_ports: 4
   onus_per_port: 1
   technology: "XGS-PON"
   id: 0                 # OLT-ID of the device
diff --git a/internal/bbsim/devices/olt.go b/internal/bbsim/devices/olt.go
index 58c7fab..c72d94b 100644
--- a/internal/bbsim/devices/olt.go
+++ b/internal/bbsim/devices/olt.go
@@ -70,6 +70,7 @@
 	NumNni               int
 	NumPon               int
 	NumOnuPerPon         int
+	NumUni               int
 	InternalState        *fsm.FSM
 	channel              chan types.Message
 	dhcpServer           dhcp.DHCPServerIf
@@ -114,6 +115,7 @@
 		"NumNni":       options.Olt.NniPorts,
 		"NumPon":       options.Olt.PonPorts,
 		"NumOnuPerPon": options.Olt.OnusPonPort,
+		"NumUni":       options.Olt.UniPorts,
 	}).Debug("CreateOLT")
 
 	olt = OltDevice{
@@ -125,6 +127,7 @@
 		NumNni:              int(options.Olt.NniPorts),
 		NumPon:              int(options.Olt.PonPorts),
 		NumOnuPerPon:        int(options.Olt.OnusPonPort),
+		NumUni:              int(options.Olt.UniPorts),
 		Pons:                []*PonPort{},
 		Nnis:                []*NniPort{},
 		Delay:               options.BBSim.Delay,
diff --git a/internal/bbsim/devices/olt_test.go b/internal/bbsim/devices/olt_test.go
index 7f1d5ee..65b4e86 100644
--- a/internal/bbsim/devices/olt_test.go
+++ b/internal/bbsim/devices/olt_test.go
@@ -72,7 +72,7 @@
 				Channel: make(chan bbsim.Message, 2048),
 			}
 
-			for k := 0; k < uniPorts; k++ {
+			for k := 0; k < numUni; k++ {
 				uni := UniPort{
 					ID:     uint32(k + 1),
 					Onu:    &onu,
@@ -107,6 +107,7 @@
 			ID:          1,
 			PonPorts:    2,
 			OnusPonPort: 2,
+			UniPorts:    4,
 		},
 	}
 
@@ -130,7 +131,7 @@
 		}
 	}
 	// NOTE when unis will be configurable this test will need to adapt
-	assert.Equal(t, unis, int(common.Config.Olt.PonPorts*common.Config.Olt.OnusPonPort*uniPorts))
+	assert.Equal(t, unis, int(common.Config.Olt.PonPorts*common.Config.Olt.OnusPonPort*common.Config.Olt.UniPorts))
 
 	// count the services
 	services := 0
@@ -143,7 +144,7 @@
 		}
 	}
 	// NOTE when unis will be configurable this test will need to adapt
-	assert.Equal(t, services, int(common.Config.Olt.PonPorts)*int(common.Config.Olt.OnusPonPort)*uniPorts*len(common.Services))
+	assert.Equal(t, services, int(common.Config.Olt.PonPorts)*int(common.Config.Olt.OnusPonPort)*int(common.Config.Olt.UniPorts)*len(common.Services))
 
 	s1 := olt.Pons[0].Onus[0].UniPorts[0].(*UniPort).Services[0].(*Service)
 
@@ -203,6 +204,7 @@
 func Test_Olt_FindOnuByMacAddress_Success(t *testing.T) {
 	numPon := 4
 	numOnu := 4
+	numUni := 4
 
 	services := []ServiceIf{
 		&Service{Name: "hsia"},
@@ -210,7 +212,7 @@
 		&Service{Name: "vod"},
 	}
 
-	olt := createMockOlt(numPon, numOnu, 1, services)
+	olt := createMockOlt(numPon, numOnu, numUni, services)
 
 	mac := net.HardwareAddr{0x2e, byte(olt.ID), byte(3), byte(6), byte(3), byte(1)}
 	s, err := olt.FindServiceByMacAddress(mac)
diff --git a/internal/bbsim/devices/onu.go b/internal/bbsim/devices/onu.go
index db0c747..17abfc3 100644
--- a/internal/bbsim/devices/onu.go
+++ b/internal/bbsim/devices/onu.go
@@ -20,10 +20,11 @@
 	"context"
 	"encoding/hex"
 	"fmt"
+	"sync"
+
 	"github.com/opencord/bbsim/internal/bbsim/packetHandlers"
 	"github.com/opencord/bbsim/internal/bbsim/responders/dhcp"
 	"github.com/opencord/bbsim/internal/bbsim/responders/eapol"
-	"sync"
 
 	pb "github.com/opencord/bbsim/api/bbsim"
 	"github.com/opencord/bbsim/internal/bbsim/alarmsim"
@@ -52,7 +53,6 @@
 
 const (
 	maxOmciMsgCounter = 10
-	uniPorts          = 4 // TODO this will need to be configurable
 )
 
 const (
@@ -308,8 +308,13 @@
 			},
 		},
 	)
-
-	for i := 0; i < uniPorts; i++ {
+	onuLogger.WithFields(log.Fields{
+		"OnuId":  o.ID,
+		"IntfId": o.PonPortID,
+		"OnuSn":  o.Sn(),
+		"NumUni": olt.NumUni,
+	}).Debug("creating-uni-ports")
+	for i := 0; i < olt.NumUni; i++ {
 		uni, err := NewUniPort(uint32(i), &o, nextCtag, nextStag)
 		if err != nil {
 			onuLogger.WithFields(log.Fields{
diff --git a/internal/bbsim/devices/onu_test.go b/internal/bbsim/devices/onu_test.go
index 196266d..bb5f0c1 100644
--- a/internal/bbsim/devices/onu_test.go
+++ b/internal/bbsim/devices/onu_test.go
@@ -24,8 +24,10 @@
 func Test_Onu_CreateOnu(t *testing.T) {
 	nextCtag := map[string]int{}
 	nextStag := map[string]int{}
+
 	olt := OltDevice{
-		ID: 0,
+		ID:     0,
+		NumUni: 4,
 	}
 	pon := PonPort{
 		ID:  1,
diff --git a/internal/common/options.go b/internal/common/options.go
index 545bb5c..83b5b56 100644
--- a/internal/common/options.go
+++ b/internal/common/options.go
@@ -92,6 +92,7 @@
 	OltRebootDelay     int    `yaml:"reboot_delay"`
 	PortStatsInterval  int    `yaml:"port_stats_interval"`
 	OmciResponseRate   uint8  `yaml:"omci_response_rate"`
+	UniPorts           uint32 `yaml:"uni_ports"`
 }
 
 type BBSimConfig struct {
@@ -230,6 +231,8 @@
 	nni := flag.Int("nni", int(conf.Olt.NniPorts), "Number of NNI ports per OLT device to be emulated")
 	pon := flag.Int("pon", int(conf.Olt.PonPorts), "Number of PON ports per OLT device to be emulated")
 	onu := flag.Int("onu", int(conf.Olt.OnusPonPort), "Number of ONU devices per PON port to be emulated")
+	uni := flag.Int("uni", int(conf.Olt.UniPorts), "Number of UNI Ports per ONU device to be emulated")
+
 	oltRebootDelay := flag.Int("oltRebootDelay", conf.Olt.OltRebootDelay, "Time that BBSim should before restarting after a reboot")
 	omci_response_rate := flag.Int("omci_response_rate", int(conf.Olt.OmciResponseRate), "Amount of OMCI messages to respond to")
 
@@ -258,6 +261,7 @@
 	conf.Olt.ID = int(*olt_id)
 	conf.Olt.NniPorts = uint32(*nni)
 	conf.Olt.PonPorts = uint32(*pon)
+	conf.Olt.UniPorts = uint32(*uni)
 	conf.Olt.OnusPonPort = uint32(*onu)
 	conf.Olt.OltRebootDelay = *oltRebootDelay
 	conf.Olt.OmciResponseRate = uint8(*omci_response_rate)
@@ -333,6 +337,7 @@
 			OltRebootDelay:     60,
 			PortStatsInterval:  20,
 			OmciResponseRate:   10,
+			UniPorts:           4,
 		},
 		BBRConfig{
 			LogLevel:  "debug",