[VOL-5104] - BBSIM crashed when adding flows

Change-Id: I57aa8a3b9007ec77c94612b0c0537f1827b78902
diff --git a/internal/bbsim/devices/olt.go b/internal/bbsim/devices/olt.go
index 46b57b9..bbe49bc 100644
--- a/internal/bbsim/devices/olt.go
+++ b/internal/bbsim/devices/olt.go
@@ -1180,7 +1180,9 @@
 			return nil, err
 		}
 
-		o.storeGemPortIdByFlow(flow)
+		if err := o.storeGemPortIdByFlow(flow); err != nil {
+			return nil, err
+		}
 		o.storeAllocId(flow)
 
 		msg := types.Message{
@@ -1744,7 +1746,7 @@
 	}
 }
 
-func (o *OltDevice) storeGemPortId(ponId uint32, onuId uint32, portNo uint32, gemId int32, flowId uint64) {
+func (o *OltDevice) storeGemPortId(ponId uint32, onuId uint32, portNo uint32, gemId int32, flowId uint64) error {
 	o.GemPortIDsLock.Lock()
 	defer o.GemPortIDsLock.Unlock()
 
@@ -1756,6 +1758,7 @@
 			"GemportId": gemId,
 			"FlowId":    flowId,
 		}).Error("trying-to-store-gemport-for-unknown-onu")
+		return fmt.Errorf("failed-trying-to-store-gemport-%d-for-unknown-onu-%d-and-IntfId-%d", gemId, onuId, ponId)
 	}
 
 	oltLogger.WithFields(log.Fields{
@@ -1773,9 +1776,11 @@
 		o.GemPortIDs[ponId][onuId][portNo][gemId] = make(map[uint64]bool)
 	}
 	o.GemPortIDs[ponId][onuId][portNo][gemId][flowId] = true
+
+	return nil
 }
 
-func (o *OltDevice) storeGemPortIdByFlow(flow *openolt.Flow) {
+func (o *OltDevice) storeGemPortIdByFlow(flow *openolt.Flow) error {
 	oltLogger.WithFields(log.Fields{
 		"IntfId":        flow.AccessIntfId,
 		"OnuId":         flow.OnuId,
@@ -1788,11 +1793,19 @@
 
 	if flow.ReplicateFlow {
 		for _, gem := range flow.PbitToGemport {
-			o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, int32(gem), flow.FlowId)
+			err := o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, int32(gem), flow.FlowId)
+			if err != nil {
+				return err
+			}
 		}
 	} else {
-		o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, flow.GemportId, flow.FlowId)
+		err := o.storeGemPortId(uint32(flow.AccessIntfId), uint32(flow.OnuId), flow.PortNo, flow.GemportId, flow.FlowId)
+		if err != nil {
+			return err
+		}
 	}
+
+	return nil
 }
 
 func (o *OltDevice) freeGemPortId(flow *openolt.Flow) {
diff --git a/internal/bbsim/devices/olt_test.go b/internal/bbsim/devices/olt_test.go
index 58714da..5dcad91 100644
--- a/internal/bbsim/devices/olt_test.go
+++ b/internal/bbsim/devices/olt_test.go
@@ -435,7 +435,8 @@
 		GemportId:    gem1,
 	}
 
-	olt.storeGemPortIdByFlow(flow1)
+	err := olt.storeGemPortIdByFlow(flow1)
+	assert.NoError(t, err)
 	assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 1)       // we have 1 gem port
 	assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 1) // and one flow referencing it
 
@@ -448,7 +449,8 @@
 		GemportId:    gem1,
 	}
 
-	olt.storeGemPortIdByFlow(flow2)
+	err = olt.storeGemPortIdByFlow(flow2)
+	assert.NoError(t, err)
 	assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 1)       // we have 1 gem port
 	assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 2) // and two flows referencing it
 
@@ -461,7 +463,8 @@
 		GemportId:    1025,
 	}
 
-	olt.storeGemPortIdByFlow(flow3)
+	err = olt.storeGemPortIdByFlow(flow3)
+	assert.NoError(t, err)
 	assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 2)       // we have 2 gem ports
 	assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 2) // two flows referencing the first one
 	assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem2]), 1) // and one flow referencing the second one
@@ -495,7 +498,8 @@
 		PbitToGemport: pbitToGemPortMap,
 	}
 
-	olt.storeGemPortIdByFlow(flow1)
+	err := olt.storeGemPortIdByFlow(flow1)
+	assert.NoError(t, err)
 	assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 2)       // we have 2 gem ports in the flow
 	assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 1) // and one flow referencing them
 	assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem2]), 1) // and one flow referencing them
@@ -616,7 +620,8 @@
 				FlowId:       uint64(i),
 			}
 			go func(wg *sync.WaitGroup) {
-				olt.storeGemPortIdByFlow(flow)
+				err := olt.storeGemPortIdByFlow(flow)
+				assert.NoError(b, err)
 				olt.storeAllocId(flow)
 				wg.Done()
 			}(&wg)