[SEBA-900] Keep sending OnuDiscoveryIndications

Change-Id: I11c294bbb8051242699f96e8f6811a623c9995a7
diff --git a/internal/bbsim/devices/onu.go b/internal/bbsim/devices/onu.go
index a938433..4fd18cf 100644
--- a/internal/bbsim/devices/onu.go
+++ b/internal/bbsim/devices/onu.go
@@ -42,20 +42,21 @@
 })
 
 type Onu struct {
-	ID        uint32
-	PonPortID uint32
-	PonPort   PonPort
-	STag      int
-	CTag      int
-	Auth      bool // automatically start EAPOL if set to true
-	Dhcp      bool // automatically start DHCP if set to true
+	ID                  uint32
+	PonPortID           uint32
+	PonPort             PonPort
+	STag                int
+	CTag                int
+	Auth                bool // automatically start EAPOL if set to true
+	Dhcp                bool // automatically start DHCP if set to true
+	HwAddress           net.HardwareAddr
+	InternalState       *fsm.FSM
+	DiscoveryRetryDelay time.Duration
+
+	// ONU State
 	// PortNo comes with flows and it's used when sending packetIndications,
 	// There is one PortNo per UNI Port, for now we're only storing the first one
 	// FIXME add support for multiple UNIs
-	HwAddress     net.HardwareAddr
-	InternalState *fsm.FSM
-
-	// ONU State
 	PortNo           uint32
 	DhcpFlowReceived bool
 
@@ -80,20 +81,21 @@
 func CreateONU(olt OltDevice, pon PonPort, id uint32, sTag int, cTag int, auth bool, dhcp bool) *Onu {
 
 	o := Onu{
-		ID:               id,
-		PonPortID:        pon.ID,
-		PonPort:          pon,
-		STag:             sTag,
-		CTag:             cTag,
-		Auth:             auth,
-		Dhcp:             dhcp,
-		HwAddress:        net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
-		PortNo:           0,
-		tid:              0x1,
-		hpTid:            0x8000,
-		seqNumber:        0,
-		DoneChannel:      make(chan bool, 1),
-		DhcpFlowReceived: false,
+		ID:                  id,
+		PonPortID:           pon.ID,
+		PonPort:             pon,
+		STag:                sTag,
+		CTag:                cTag,
+		Auth:                auth,
+		Dhcp:                dhcp,
+		HwAddress:           net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(id)},
+		PortNo:              0,
+		tid:                 0x1,
+		hpTid:               0x8000,
+		seqNumber:           0,
+		DoneChannel:         make(chan bool, 1),
+		DhcpFlowReceived:    false,
+		DiscoveryRetryDelay: 60 * time.Second, // this is used to send OnuDiscoveryIndications until an activate call is received
 	}
 	o.SerialNumber = o.NewSN(olt.ID, pon.ID, o.ID)
 
@@ -416,6 +418,14 @@
 		"OnuSn":  msg.Onu.Sn(),
 		"OnuId":  o.ID,
 	}).Debug("Sent Indication_OnuDiscInd")
+
+	// after DiscoveryRetryDelay check if the state is the same and in case send a new OnuDiscIndication
+	go func(delay time.Duration) {
+		if o.InternalState.Current() == "discovered" {
+			time.Sleep(delay)
+			o.sendOnuDiscIndication(msg, stream)
+		}
+	}(o.DiscoveryRetryDelay)
 }
 
 func (o *Onu) sendOnuIndication(msg OnuIndicationMessage, stream openolt.Openolt_EnableIndicationServer) {
diff --git a/internal/bbsim/devices/onu_indications_test.go b/internal/bbsim/devices/onu_indications_test.go
new file mode 100644
index 0000000..d12c5a6
--- /dev/null
+++ b/internal/bbsim/devices/onu_indications_test.go
@@ -0,0 +1,113 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package devices
+
+import (
+	"errors"
+	"github.com/opencord/voltha-protos/v2/go/openolt"
+	"google.golang.org/grpc"
+	"gotest.tools/assert"
+	"testing"
+	"time"
+)
+
+type mockStream struct {
+	grpc.ServerStream
+	CallCount int
+	Calls     map[int]*openolt.OnuDiscIndication
+	channel   chan int
+	fail      bool
+}
+
+func (s *mockStream) Send(ind *openolt.Indication) error {
+	s.CallCount++
+	if s.fail {
+		return errors.New("fake-error")
+	}
+	s.Calls[s.CallCount] = ind.GetOnuDiscInd()
+	s.channel <- s.CallCount
+	return nil
+}
+
+// test that we're sending a Discovery indication to VOLTHA
+func Test_Onu_DiscoverIndication_send_on_discovery(t *testing.T) {
+	onu := createTestOnu()
+	stream := &mockStream{
+		CallCount: 0,
+		Calls:     make(map[int]*openolt.OnuDiscIndication),
+		fail:      false,
+		channel:   make(chan int, 10),
+	}
+	go onu.ProcessOnuMessages(stream, nil)
+	onu.InternalState.SetState("initialized")
+	onu.InternalState.Event("discover")
+
+	select {
+	case <-time.After(90 * time.Millisecond):
+		assert.Equal(t, stream.CallCount, 1)
+		assert.Equal(t, stream.Calls[1].IntfId, onu.PonPortID)
+		assert.Equal(t, stream.Calls[1].SerialNumber, onu.SerialNumber)
+	}
+}
+
+// test that if the discovery indication is not acknowledge we'll keep sending new ones
+func Test_Onu_DiscoverIndication_retry_on_discovery(t *testing.T) {
+	onu := createTestOnu()
+	stream := &mockStream{
+		CallCount: 0,
+		Calls:     make(map[int]*openolt.OnuDiscIndication),
+		fail:      false,
+		channel:   make(chan int, 10),
+	}
+	go onu.ProcessOnuMessages(stream, nil)
+	onu.InternalState.SetState("initialized")
+	onu.InternalState.Event("discover")
+
+	select {
+	case <-time.After(400 * time.Millisecond):
+		assert.Equal(t, stream.CallCount, 4)
+	}
+}
+
+// test that if the discovery indication is not acknowledge we'll send a new one
+func Test_Onu_DiscoverIndication_retry_on_discovery_stops(t *testing.T) {
+	onu := createTestOnu()
+	onu.DiscoveryRetryDelay = 500 * time.Millisecond
+	stream := &mockStream{
+		CallCount: 0,
+		Calls:     make(map[int]*openolt.OnuDiscIndication),
+		fail:      false,
+		channel:   make(chan int, 10),
+	}
+	go onu.ProcessOnuMessages(stream, nil)
+	onu.InternalState.SetState("initialized")
+	onu.InternalState.Event("discover")
+
+	go func() {
+		for calls := range stream.channel {
+			if calls == 2 {
+				onu.InternalState.SetState("enabled")
+			}
+		}
+	}()
+
+	select {
+	case <-time.After(1 * time.Second):
+
+		assert.Equal(t, stream.CallCount, 2)
+	}
+}
diff --git a/internal/bbsim/devices/onu_test_helpers.go b/internal/bbsim/devices/onu_test_helpers.go
index 8e071c1..bca0ab8 100644
--- a/internal/bbsim/devices/onu_test_helpers.go
+++ b/internal/bbsim/devices/onu_test_helpers.go
@@ -23,6 +23,7 @@
 	"github.com/opencord/voltha-protos/v2/go/tech_profile"
 	"google.golang.org/grpc"
 	"net"
+	"time"
 )
 
 type FlowAddSpy struct {
@@ -131,5 +132,6 @@
 	onu := CreateONU(olt, pon, 1, 900, 900, false, false)
 	// NOTE we need this in order to create the OnuChannel
 	onu.InternalState.Event("initialize")
+	onu.DiscoveryRetryDelay = 100 * time.Millisecond
 	return onu
 }