blob: ad6681d994a353dadaea51de82beed6f66b5e674 [file] [log] [blame]
Matteo Scandoloe811ae92019-12-10 17:50:14 -08001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandoloe811ae92019-12-10 17:50:14 -08003
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package devices
18
19import (
David Bainbridge103cf022019-12-16 20:11:35 +000020 "context"
Matteo Scandoloe811ae92019-12-10 17:50:14 -080021 "errors"
Shrey Baid688b4242020-07-10 20:40:10 +053022 "testing"
23 "time"
24
David K. Bainbridgec415efe2021-08-19 13:05:21 +000025 "github.com/opencord/voltha-protos/v5/go/openolt"
Matteo Scandoloe811ae92019-12-10 17:50:14 -080026 "google.golang.org/grpc"
27 "gotest.tools/assert"
Matteo Scandoloe811ae92019-12-10 17:50:14 -080028)
29
30type mockStream struct {
31 grpc.ServerStream
32 CallCount int
Matteo Scandolo4a036262020-08-17 15:56:13 -070033 Calls map[int]*openolt.Indication
Matteo Scandoloe811ae92019-12-10 17:50:14 -080034 channel chan int
35 fail bool
36}
37
38func (s *mockStream) Send(ind *openolt.Indication) error {
39 s.CallCount++
40 if s.fail {
41 return errors.New("fake-error")
42 }
Matteo Scandolo4a036262020-08-17 15:56:13 -070043 s.Calls[s.CallCount] = ind
44 go func() {
45 s.channel <- s.CallCount
46 }()
Matteo Scandoloe811ae92019-12-10 17:50:14 -080047 return nil
48}
49
Matteo Scandolo9ddb3a92021-04-14 16:16:20 -070050func (s *mockStream) Context() context.Context {
51 return context.Background()
52}
53
Matteo Scandoloe811ae92019-12-10 17:50:14 -080054// test that we're sending a Discovery indication to VOLTHA
55func Test_Onu_DiscoverIndication_send_on_discovery(t *testing.T) {
56 onu := createTestOnu()
57 stream := &mockStream{
58 CallCount: 0,
Matteo Scandolo4a036262020-08-17 15:56:13 -070059 Calls: make(map[int]*openolt.Indication),
Matteo Scandoloe811ae92019-12-10 17:50:14 -080060 fail: false,
61 channel: make(chan int, 10),
62 }
David Bainbridge103cf022019-12-16 20:11:35 +000063 ctx, cancel := context.WithCancel(context.TODO())
64 go onu.ProcessOnuMessages(ctx, stream, nil)
Matteo Scandolocedde462021-03-09 17:37:16 -080065 onu.InternalState.SetState(OnuTxInitialize)
66 _ = onu.InternalState.Event(OnuTxDiscover)
Matteo Scandoloe811ae92019-12-10 17:50:14 -080067
68 select {
Shrey Baid688b4242020-07-10 20:40:10 +053069 default:
Matteo Scandoloe811ae92019-12-10 17:50:14 -080070 case <-time.After(90 * time.Millisecond):
Matteo Scandolo4a036262020-08-17 15:56:13 -070071 call := stream.Calls[1].GetOnuDiscInd()
Matteo Scandoloe811ae92019-12-10 17:50:14 -080072 assert.Equal(t, stream.CallCount, 1)
Matteo Scandolo4a036262020-08-17 15:56:13 -070073 assert.Equal(t, call.IntfId, onu.PonPortID)
74 assert.Equal(t, call.SerialNumber, onu.SerialNumber)
Matteo Scandoloe811ae92019-12-10 17:50:14 -080075 }
David Bainbridge103cf022019-12-16 20:11:35 +000076 cancel()
Matteo Scandoloe811ae92019-12-10 17:50:14 -080077}
78
79// test that if the discovery indication is not acknowledge we'll keep sending new ones
80func Test_Onu_DiscoverIndication_retry_on_discovery(t *testing.T) {
81 onu := createTestOnu()
82 stream := &mockStream{
83 CallCount: 0,
Matteo Scandolo4a036262020-08-17 15:56:13 -070084 Calls: make(map[int]*openolt.Indication),
Matteo Scandoloe811ae92019-12-10 17:50:14 -080085 fail: false,
86 channel: make(chan int, 10),
87 }
David Bainbridge103cf022019-12-16 20:11:35 +000088 ctx, cancel := context.WithCancel(context.TODO())
89 go onu.ProcessOnuMessages(ctx, stream, nil)
Matteo Scandolocedde462021-03-09 17:37:16 -080090 onu.InternalState.SetState(OnuStateInitialized)
91 _ = onu.InternalState.Event(OnuTxDiscover)
Matteo Scandoloe811ae92019-12-10 17:50:14 -080092
93 select {
Shrey Baid688b4242020-07-10 20:40:10 +053094 default:
Matteo Scandoloe811ae92019-12-10 17:50:14 -080095 case <-time.After(400 * time.Millisecond):
96 assert.Equal(t, stream.CallCount, 4)
97 }
David Bainbridge103cf022019-12-16 20:11:35 +000098 cancel()
Matteo Scandoloe811ae92019-12-10 17:50:14 -080099}
100
101// test that if the discovery indication is not acknowledge we'll send a new one
102func Test_Onu_DiscoverIndication_retry_on_discovery_stops(t *testing.T) {
103 onu := createTestOnu()
104 onu.DiscoveryRetryDelay = 500 * time.Millisecond
105 stream := &mockStream{
106 CallCount: 0,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700107 Calls: make(map[int]*openolt.Indication),
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800108 fail: false,
109 channel: make(chan int, 10),
110 }
David Bainbridge103cf022019-12-16 20:11:35 +0000111 ctx, cancel := context.WithCancel(context.TODO())
112 go onu.ProcessOnuMessages(ctx, stream, nil)
Matteo Scandolocedde462021-03-09 17:37:16 -0800113 onu.InternalState.SetState(OnuStateInitialized)
114 _ = onu.InternalState.Event(OnuTxDiscover)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800115
116 go func() {
117 for calls := range stream.channel {
118 if calls == 2 {
119 onu.InternalState.SetState("enabled")
120 }
121 }
122 }()
123
124 select {
Shrey Baid688b4242020-07-10 20:40:10 +0530125 default:
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800126 case <-time.After(1 * time.Second):
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800127 assert.Equal(t, stream.CallCount, 2)
128 }
David Bainbridge103cf022019-12-16 20:11:35 +0000129 cancel()
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800130}