Revert "Fix the storage of TCONT in bbsim. The TCONTs should be referenced"

This reverts commit 08b583925be707bf931ea26a1c0adcc0fe8a850f.

Change-Id: I25e418fa548b3f11b7e654cdbd8cebe1a9c2a77a
diff --git a/internal/bbsim/api/grpc_api_server.go b/internal/bbsim/api/grpc_api_server.go
index 8a9101d..ec25a10 100644
--- a/internal/bbsim/api/grpc_api_server.go
+++ b/internal/bbsim/api/grpc_api_server.go
@@ -89,10 +89,10 @@
 			allocatedGemPorts = append(allocatedGemPorts, resource)
 		}
 
-		for _, v := range pon.AllocatedAllocIds {
+		for k, v := range pon.AllocatedAllocIds {
 			resource := &bbsim.PonAllocatedResources{
-				SerialNumber: common.OnuSnToString(v.OnuSn),
-				Id:           int32(v.AllocID),
+				SerialNumber: common.OnuSnToString(v),
+				Id:           int32(k),
 			}
 			allocatedAllocIds = append(allocatedAllocIds, resource)
 		}
diff --git a/internal/bbsim/devices/onu.go b/internal/bbsim/devices/onu.go
index 5eed730..1353a50 100644
--- a/internal/bbsim/devices/onu.go
+++ b/internal/bbsim/devices/onu.go
@@ -382,7 +382,7 @@
 	// clean the ONU state
 	o.Flows = []FlowKey{}
 	o.PonPort.removeOnuId(o.ID)
-	o.PonPort.removeAllocIdsForOnuSn(o.SerialNumber)
+	o.PonPort.removeAllocId(o.SerialNumber)
 	o.PonPort.removeGemPortBySn(o.SerialNumber)
 
 	o.onuAlarmsInfoLock.Lock()
@@ -807,7 +807,7 @@
 			o.MibDataSync = 0
 
 			// if the MIB reset is successful then remove all the stored AllocIds and GemPorts
-			o.PonPort.removeAllocIdsForOnuSn(o.SerialNumber)
+			o.PonPort.removeAllocId(o.SerialNumber)
 			o.PonPort.removeGemPortBySn(o.SerialNumber)
 		}
 	case omci.MibUploadRequestType:
@@ -887,7 +887,6 @@
 			}).Debug("set-onu-admin-lock-state")
 		case me.TContClassID:
 			allocId := msgObj.Attributes[me.TCont_AllocId].(uint16)
-			entityID := msgObj.Attributes[me.ManagedEntityID].(uint16)
 
 			// if the AllocId is 255 (0xFF) or 65535 (0xFFFF) it means we are removing it,
 			// otherwise we are adding it
@@ -899,15 +898,15 @@
 					"AllocId":      allocId,
 					"SerialNumber": o.Sn(),
 				}).Trace("freeing-alloc-id-via-omci")
-				o.PonPort.removeAllocId(entityID)
+				o.PonPort.removeAllocId(o.SerialNumber)
 			} else {
-				if used, allocObj := o.PonPort.isAllocIdAllocated(entityID); used {
+				if used, sn := o.PonPort.isAllocIdAllocated(allocId); used {
 					onuLogger.WithFields(log.Fields{
 						"IntfId":       o.PonPortID,
 						"OnuId":        o.ID,
 						"AllocId":      allocId,
 						"SerialNumber": o.Sn(),
-					}).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(allocObj.OnuSn))
+					}).Errorf("allocid-already-allocated-to-onu-with-sn-%s", common.OnuSnToString(sn))
 					success = false
 				} else {
 					onuLogger.WithFields(log.Fields{
@@ -917,7 +916,7 @@
 						"AllocId":      allocId,
 						"SerialNumber": o.Sn(),
 					}).Trace("storing-alloc-id-via-omci")
-					o.PonPort.storeAllocId(entityID, allocId, o.SerialNumber)
+					o.PonPort.storeAllocId(allocId, o.SerialNumber)
 				}
 			}
 		case me.EthernetFrameExtendedPmClassID,
diff --git a/internal/bbsim/devices/onu_omci_test.go b/internal/bbsim/devices/onu_omci_test.go
index a8c668b..139560c 100644
--- a/internal/bbsim/devices/onu_omci_test.go
+++ b/internal/bbsim/devices/onu_omci_test.go
@@ -284,7 +284,7 @@
 
 	// create a GemPort and an AllocId for this ONU
 	onu.PonPort.storeGemPort(1024, onu.SerialNumber)
-	onu.PonPort.storeAllocId(1024, 1024, onu.SerialNumber)
+	onu.PonPort.storeAllocId(1024, onu.SerialNumber)
 
 	// send a MibReset
 	err := onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciMibResetRequest(t)), stream)
diff --git a/internal/bbsim/devices/onu_state_machine_test.go b/internal/bbsim/devices/onu_state_machine_test.go
index 149ae2e..1c8470c 100644
--- a/internal/bbsim/devices/onu_state_machine_test.go
+++ b/internal/bbsim/devices/onu_state_machine_test.go
@@ -50,7 +50,7 @@
 	}
 	onu.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{SequenceNo: 1, AlarmBitMap: [28]byte{}}
 	onu.PonPort.storeOnuId(onu.ID, onu.SerialNumber)
-	onu.PonPort.storeAllocId(1, 1024, onu.SerialNumber)
+	onu.PonPort.storeAllocId(1, onu.SerialNumber)
 	onu.PonPort.storeGemPort(1, onu.SerialNumber)
 
 	_ = onu.InternalState.Event(OnuTxDisable)
diff --git a/internal/bbsim/devices/onu_test_helpers.go b/internal/bbsim/devices/onu_test_helpers.go
index 86c4b4c..e255f5b 100644
--- a/internal/bbsim/devices/onu_test_helpers.go
+++ b/internal/bbsim/devices/onu_test_helpers.go
@@ -153,7 +153,7 @@
 		PonPortID: ponPortId,
 		PonPort: &PonPort{
 			AllocatedGemPorts: make(map[uint16]*openolt.SerialNumber),
-			AllocatedAllocIds: make(map[uint16]*AllocIDKey),
+			AllocatedAllocIds: make(map[uint16]*openolt.SerialNumber),
 			Olt:               &OltDevice{},
 		},
 		OmciResponseRate: 10,
diff --git a/internal/bbsim/devices/pon.go b/internal/bbsim/devices/pon.go
index 9005f2d..4db0e09 100644
--- a/internal/bbsim/devices/pon.go
+++ b/internal/bbsim/devices/pon.go
@@ -30,11 +30,6 @@
 	"module": "PON",
 })
 
-type AllocIDKey struct {
-	OnuSn   *openolt.SerialNumber
-	AllocID uint16
-}
-
 type PonPort struct {
 	// BBSIM Internals
 	ID            uint32
@@ -55,7 +50,7 @@
 	allocatedGemPortsLock sync.RWMutex
 	AllocatedOnuIds       map[uint32]*openolt.SerialNumber
 	allocatedOnuIdsLock   sync.RWMutex
-	AllocatedAllocIds     map[uint16]*AllocIDKey // key is TCONT entity ID
+	AllocatedAllocIds     map[uint16]*openolt.SerialNumber
 	allocatedAllocIdsLock sync.RWMutex
 }
 
@@ -70,7 +65,7 @@
 		Onus:              []*Onu{},
 		AllocatedGemPorts: make(map[uint16]*openolt.SerialNumber),
 		AllocatedOnuIds:   make(map[uint32]*openolt.SerialNumber),
-		AllocatedAllocIds: make(map[uint16]*AllocIDKey),
+		AllocatedAllocIds: make(map[uint16]*openolt.SerialNumber),
 	}
 
 	ponPort.InternalState = fsm.NewFSM(
@@ -296,26 +291,20 @@
 }
 
 // storeAllocId adds the Id to the ONU Ids already allocated to this PON port
-func (p *PonPort) storeAllocId(entityID uint16, allocId uint16, onuSn *openolt.SerialNumber) {
+func (p *PonPort) storeAllocId(allocId uint16, onuSn *openolt.SerialNumber) {
 	p.allocatedAllocIdsLock.Lock()
 	defer p.allocatedAllocIdsLock.Unlock()
-	p.AllocatedAllocIds[entityID] = &AllocIDKey{AllocID: allocId, OnuSn: onuSn}
+	p.AllocatedAllocIds[allocId] = onuSn
 }
 
 // removeAllocId removes the AllocId from the allocated resources
-func (p *PonPort) removeAllocId(entityID uint16) {
+// this is done via SN as the AllocId is not remove but set to a default value
+func (p *PonPort) removeAllocId(onuSn *openolt.SerialNumber) {
 	p.allocatedAllocIdsLock.Lock()
 	defer p.allocatedAllocIdsLock.Unlock()
-	delete(p.AllocatedAllocIds, entityID)
-}
-
-// 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)
+	for allocId, sn := range p.AllocatedAllocIds {
+		if sn == onuSn {
+			delete(p.AllocatedAllocIds, allocId)
 		}
 	}
 }
@@ -323,16 +312,16 @@
 func (p *PonPort) removeAllAllocIds() {
 	p.allocatedAllocIdsLock.Lock()
 	defer p.allocatedAllocIdsLock.Unlock()
-	p.AllocatedAllocIds = make(map[uint16]*AllocIDKey)
+	p.AllocatedAllocIds = make(map[uint16]*openolt.SerialNumber)
 }
 
 // isAllocIdAllocated returns whether this AllocId is already in use on this PON
-func (p *PonPort) isAllocIdAllocated(entityID uint16) (bool, *AllocIDKey) {
+func (p *PonPort) isAllocIdAllocated(allocId uint16) (bool, *openolt.SerialNumber) {
 	p.allocatedAllocIdsLock.RLock()
 	defer p.allocatedAllocIdsLock.RUnlock()
 
-	if _, ok := p.AllocatedAllocIds[entityID]; ok {
-		return true, p.AllocatedAllocIds[entityID]
+	if _, ok := p.AllocatedAllocIds[allocId]; ok {
+		return true, p.AllocatedAllocIds[allocId]
 	}
 	return false, nil
 }
diff --git a/internal/bbsim/devices/pon_test.go b/internal/bbsim/devices/pon_test.go
index c0cd09b..5467413 100644
--- a/internal/bbsim/devices/pon_test.go
+++ b/internal/bbsim/devices/pon_test.go
@@ -129,26 +129,23 @@
 func Test_removeAllocId(t *testing.T) {
 
 	const (
-		entityID1 = 1024
-		entityID2 = 1025
-		allocId1  = 1024
-		allocId2  = 1025
+		allocId1 = 1024
+		allocId2 = 1025
 	)
 
 	pon := &PonPort{
-		AllocatedAllocIds: make(map[uint16]*AllocIDKey),
+		AllocatedAllocIds: make(map[uint16]*openolt.SerialNumber),
 	}
 
-	pon.AllocatedAllocIds[entityID1] = &AllocIDKey{sn1, allocId1}
-	pon.AllocatedAllocIds[entityID2] = &AllocIDKey{sn2, allocId2}
+	pon.AllocatedAllocIds[allocId1] = sn1
+	pon.AllocatedAllocIds[allocId2] = sn2
 
 	assert.Equal(t, len(pon.AllocatedAllocIds), 2)
 
-	pon.removeAllocId(entityID1)
+	pon.removeAllocId(sn1)
 
 	assert.Equal(t, len(pon.AllocatedAllocIds), 1)
-	assert.Nil(t, pon.AllocatedAllocIds[entityID1])
-	assert.NotNil(t, pon.AllocatedAllocIds[entityID2])
-	assert.Equal(t, pon.AllocatedAllocIds[entityID2].OnuSn, sn2)
+	assert.Nil(t, pon.AllocatedAllocIds[allocId1])
+	assert.Equal(t, pon.AllocatedAllocIds[allocId2], sn2)
 
 }