Fix the storage of TCONT in bbsim.

Change-Id: Ia6c702fd0ec6c3c478ffda364115cd076338ac5a
diff --git a/internal/bbsim/devices/pon.go b/internal/bbsim/devices/pon.go
index 4db0e09..c1b3257 100644
--- a/internal/bbsim/devices/pon.go
+++ b/internal/bbsim/devices/pon.go
@@ -30,6 +30,17 @@
 	"module": "PON",
 })
 
+type AllocIDVal struct {
+	OnuSn   *openolt.SerialNumber
+	AllocID uint16
+}
+
+type AllocIDKey struct {
+	PonID    uint32
+	OnuID    uint32
+	EntityID uint16
+}
+
 type PonPort struct {
 	// BBSIM Internals
 	ID            uint32
@@ -50,7 +61,7 @@
 	allocatedGemPortsLock sync.RWMutex
 	AllocatedOnuIds       map[uint32]*openolt.SerialNumber
 	allocatedOnuIdsLock   sync.RWMutex
-	AllocatedAllocIds     map[uint16]*openolt.SerialNumber
+	AllocatedAllocIds     map[AllocIDKey]*AllocIDVal // key is AllocIDKey
 	allocatedAllocIdsLock sync.RWMutex
 }
 
@@ -65,7 +76,7 @@
 		Onus:              []*Onu{},
 		AllocatedGemPorts: make(map[uint16]*openolt.SerialNumber),
 		AllocatedOnuIds:   make(map[uint32]*openolt.SerialNumber),
-		AllocatedAllocIds: make(map[uint16]*openolt.SerialNumber),
+		AllocatedAllocIds: make(map[AllocIDKey]*AllocIDVal),
 	}
 
 	ponPort.InternalState = fsm.NewFSM(
@@ -291,20 +302,27 @@
 }
 
 // storeAllocId adds the Id to the ONU Ids already allocated to this PON port
-func (p *PonPort) storeAllocId(allocId uint16, onuSn *openolt.SerialNumber) {
+func (p *PonPort) storeAllocId(ponID uint32, onuID uint32, entityID uint16, allocId uint16, onuSn *openolt.SerialNumber) {
 	p.allocatedAllocIdsLock.Lock()
 	defer p.allocatedAllocIdsLock.Unlock()
-	p.AllocatedAllocIds[allocId] = onuSn
+	p.AllocatedAllocIds[AllocIDKey{ponID, onuID, entityID}] = &AllocIDVal{onuSn, allocId}
 }
 
 // removeAllocId removes the AllocId from the allocated resources
-// this is done via SN as the AllocId is not remove but set to a default value
-func (p *PonPort) removeAllocId(onuSn *openolt.SerialNumber) {
+func (p *PonPort) removeAllocId(ponID uint32, onuID uint32, entityID uint16) {
 	p.allocatedAllocIdsLock.Lock()
 	defer p.allocatedAllocIdsLock.Unlock()
-	for allocId, sn := range p.AllocatedAllocIds {
-		if sn == onuSn {
-			delete(p.AllocatedAllocIds, allocId)
+	allocKey := AllocIDKey{ponID, onuID, entityID}
+	delete(p.AllocatedAllocIds, allocKey)
+}
+
+// removeAllocIdsForOnuSn removes the all AllocIds for the given onu serial number
+func (p *PonPort) removeAllocIdsForOnuSn(onuSn *openolt.SerialNumber) {
+	p.allocatedAllocIdsLock.Lock()
+	defer p.allocatedAllocIdsLock.Unlock()
+	for id, allocObj := range p.AllocatedAllocIds {
+		if onuSn == allocObj.OnuSn {
+			delete(p.AllocatedAllocIds, id)
 		}
 	}
 }
@@ -312,16 +330,16 @@
 func (p *PonPort) removeAllAllocIds() {
 	p.allocatedAllocIdsLock.Lock()
 	defer p.allocatedAllocIdsLock.Unlock()
-	p.AllocatedAllocIds = make(map[uint16]*openolt.SerialNumber)
+	p.AllocatedAllocIds = make(map[AllocIDKey]*AllocIDVal)
 }
 
 // isAllocIdAllocated returns whether this AllocId is already in use on this PON
-func (p *PonPort) isAllocIdAllocated(allocId uint16) (bool, *openolt.SerialNumber) {
+func (p *PonPort) isAllocIdAllocated(ponID uint32, onuID uint32, entityID uint16) (bool, *AllocIDVal) {
 	p.allocatedAllocIdsLock.RLock()
 	defer p.allocatedAllocIdsLock.RUnlock()
-
-	if _, ok := p.AllocatedAllocIds[allocId]; ok {
-		return true, p.AllocatedAllocIds[allocId]
+	allocKey := AllocIDKey{ponID, onuID, entityID}
+	if _, ok := p.AllocatedAllocIds[allocKey]; ok {
+		return true, p.AllocatedAllocIds[allocKey]
 	}
 	return false, nil
 }