Revert "[VOL-4518] remove flowKey and use only flowID for OLT and ONU flow cache since flowID's are unique"

This reverts commit d444598f453757cc0b9fb4ad7295dc1726a69c3f.

Change-Id: I449099e083d363db5834dc6c56330af3fd0ce921
diff --git a/internal/bbsim/api/onus_handler.go b/internal/bbsim/api/onus_handler.go
index e5228cb..2638bf5 100644
--- a/internal/bbsim/api/onus_handler.go
+++ b/internal/bbsim/api/onus_handler.go
@@ -578,7 +578,7 @@
 	res := &bbsim.Flows{}
 
 	if req.SerialNumber == "" {
-		olt.Flows.Range(func(flowId, flow interface{}) bool {
+		olt.Flows.Range(func(flowKey, flow interface{}) bool {
 			flowObj := flow.(openolt.Flow)
 			res.Flows = append(res.Flows, &flowObj)
 			return true
@@ -592,12 +592,12 @@
 			}).Error("Can't get ONU in GetFlows request")
 			return nil, err
 		}
-		for _, flowId := range onu.FlowIds {
-			flow, _ := olt.Flows.Load(flowId)
+		for _, flowKey := range onu.Flows {
+			flow, _ := olt.Flows.Load(flowKey)
 			flowObj := flow.(openolt.Flow)
 			res.Flows = append(res.Flows, &flowObj)
 		}
-		res.FlowCount = uint32(len(onu.FlowIds))
+		res.FlowCount = uint32(len(onu.Flows))
 	}
 	return res, nil
 }
diff --git a/internal/bbsim/devices/olt.go b/internal/bbsim/devices/olt.go
index 931bd6d..8520bf2 100644
--- a/internal/bbsim/devices/olt.go
+++ b/internal/bbsim/devices/olt.go
@@ -1032,8 +1032,10 @@
 		"PortNo":    flow.PortNo,
 	}).Tracef("OLT receives FlowAdd")
 
+	flowKey := FlowKey{}
 	if !o.enablePerf {
-		o.Flows.Store(flow.FlowId, *flow)
+		flowKey = FlowKey{ID: flow.FlowId, Direction: flow.FlowType}
+		olt.Flows.Store(flowKey, *flow)
 	}
 
 	if flow.AccessIntfId == -1 {
@@ -1092,8 +1094,9 @@
 		}
 
 		if !o.enablePerf {
+			onu.Flows = append(onu.Flows, flowKey)
 			// Generate event on first flow for ONU
-			if len(onu.FlowIds) == 0 {
+			if len(onu.Flows) == 1 {
 				publishEvent("Flow-add-received", int32(onu.PonPortID), int32(onu.ID), onu.Sn())
 			}
 		}
@@ -1148,8 +1151,13 @@
 	olt.freeAllocId(flow)
 
 	if !o.enablePerf { // remove only if flow were stored
+		flowKey := FlowKey{
+			ID:        flow.FlowId,
+			Direction: flow.FlowType,
+		}
+
 		// Check if flow exists
-		storedFlowIntf, ok := o.Flows.Load(flow.FlowId)
+		storedFlowIntf, ok := o.Flows.Load(flowKey)
 		if !ok {
 			oltLogger.Errorf("Flow %v not found", flow)
 			return new(openolt.Empty), status.Errorf(codes.NotFound, "Flow not found")
@@ -1178,11 +1186,12 @@
 				}).Error("ONU-not-found")
 				return new(openolt.Empty), nil
 			}
+			onu.DeleteFlow(flowKey)
 			publishEvent("Flow-remove-received", int32(onu.PonPortID), int32(onu.ID), onu.Sn())
 		}
 
 		// delete from olt flows
-		o.Flows.Delete(flow.FlowId)
+		o.Flows.Delete(flowKey)
 	}
 
 	if flow.AccessIntfId == -1 {
diff --git a/internal/bbsim/devices/onu.go b/internal/bbsim/devices/onu.go
index 53f6564..cfc7099 100644
--- a/internal/bbsim/devices/onu.go
+++ b/internal/bbsim/devices/onu.go
@@ -93,6 +93,11 @@
 	BbrOnuStateDhcpFlowSent  = "dhcp_flow_sent"
 )
 
+type FlowKey struct {
+	ID        uint64
+	Direction string
+}
+
 type Onu struct {
 	ID                  uint32
 	PonPortID           uint32
@@ -105,6 +110,7 @@
 	// ONU State
 	UniPorts  []UniPortIf
 	PotsPorts []PotsPortIf
+	Flows     []FlowKey
 	FlowIds   []uint64 // keep track of the flows we currently have in the ONU
 
 	OperState    *fsm.FSM
@@ -155,7 +161,7 @@
 		seqNumber:                     0,
 		DoneChannel:                   make(chan bool, 1),
 		DiscoveryRetryDelay:           60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
-		FlowIds:                       []uint64{},
+		Flows:                         []FlowKey{},
 		DiscoveryDelay:                delay,
 		MibDataSync:                   0,
 		ImageSoftwareExpectedSections: 0, // populated during OMCI StartSoftwareDownloadRequest
@@ -374,7 +380,7 @@
 // cleanupOnuState this method is to clean the local state when the ONU is disabled
 func (o *Onu) cleanupOnuState() {
 	// clean the ONU state
-	o.FlowIds = []uint64{}
+	o.Flows = []FlowKey{}
 	o.PonPort.removeOnuId(o.ID)
 	o.PonPort.removeAllocId(o.SerialNumber)
 	o.PonPort.removeGemPortBySn(o.SerialNumber)
@@ -1742,12 +1748,15 @@
 	}).Info("Sent DHCP Flow")
 }
 
-// DeleteFlow method search and delete flowId from the onu flowIds slice
-func (onu *Onu) DeleteFlow(id uint64) {
-	for pos, flowId := range onu.FlowIds {
-		if flowId == id {
-			// delete the flowId by shifting all flowIds by one
-			onu.FlowIds = append(onu.FlowIds[:pos], onu.FlowIds[pos+1:]...)
+// DeleteFlow method search and delete flowKey from the onu flows slice
+func (onu *Onu) DeleteFlow(key FlowKey) {
+	for pos, flowKey := range onu.Flows {
+		if flowKey == key {
+			// delete the flowKey by shifting all flowKeys by one
+			onu.Flows = append(onu.Flows[:pos], onu.Flows[pos+1:]...)
+			t := make([]FlowKey, len(onu.Flows))
+			copy(t, onu.Flows)
+			onu.Flows = t
 			break
 		}
 	}
diff --git a/internal/bbsim/devices/onu_state_machine_test.go b/internal/bbsim/devices/onu_state_machine_test.go
index a324d81..c1f6d8e 100644
--- a/internal/bbsim/devices/onu_state_machine_test.go
+++ b/internal/bbsim/devices/onu_state_machine_test.go
@@ -40,7 +40,10 @@
 	onu.InternalState.SetState(OnuStateEnabled)
 	assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
 
-	onu.FlowIds = []uint64{1, 2}
+	onu.Flows = []FlowKey{
+		{ID: 1, Direction: "upstream"},
+		{ID: 2, Direction: "downstream"},
+	}
 	key := omcilib.OnuAlarmInfoMapKey{
 		MeInstance: 257,
 		MeClassID:  me.PhysicalPathTerminationPointEthernetUniClassID,
@@ -54,7 +57,7 @@
 	assert.Equal(t, onu.InternalState.Current(), OnuStateDisabled)
 
 	assert.Equal(t, len(onu.onuAlarmsInfo), 0)
-	assert.Equal(t, len(onu.FlowIds), 0)
+	assert.Equal(t, len(onu.Flows), 0)
 	assert.Equal(t, len(onu.PonPort.AllocatedOnuIds), 0)
 	assert.Equal(t, len(onu.PonPort.AllocatedAllocIds), 0)
 	assert.Equal(t, len(onu.PonPort.AllocatedGemPorts), 0)