blob: 1a34da1a9c0952eb985bb2430d05556141292d41 [file] [log] [blame]
Matteo Scandoloe811ae92019-12-10 17:50:14 -08001/*
2 * Copyright 2018-present Open Networking Foundation
3
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
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070025 "github.com/opencord/voltha-protos/v4/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
50// test that we're sending a Discovery indication to VOLTHA
51func Test_Onu_DiscoverIndication_send_on_discovery(t *testing.T) {
52 onu := createTestOnu()
53 stream := &mockStream{
54 CallCount: 0,
Matteo Scandolo4a036262020-08-17 15:56:13 -070055 Calls: make(map[int]*openolt.Indication),
Matteo Scandoloe811ae92019-12-10 17:50:14 -080056 fail: false,
57 channel: make(chan int, 10),
58 }
David Bainbridge103cf022019-12-16 20:11:35 +000059 ctx, cancel := context.WithCancel(context.TODO())
60 go onu.ProcessOnuMessages(ctx, stream, nil)
Matteo Scandoloe811ae92019-12-10 17:50:14 -080061 onu.InternalState.SetState("initialized")
Shrey Baid688b4242020-07-10 20:40:10 +053062 _ = onu.InternalState.Event("discover")
Matteo Scandoloe811ae92019-12-10 17:50:14 -080063
64 select {
Shrey Baid688b4242020-07-10 20:40:10 +053065 default:
Matteo Scandoloe811ae92019-12-10 17:50:14 -080066 case <-time.After(90 * time.Millisecond):
Matteo Scandolo4a036262020-08-17 15:56:13 -070067 call := stream.Calls[1].GetOnuDiscInd()
Matteo Scandoloe811ae92019-12-10 17:50:14 -080068 assert.Equal(t, stream.CallCount, 1)
Matteo Scandolo4a036262020-08-17 15:56:13 -070069 assert.Equal(t, call.IntfId, onu.PonPortID)
70 assert.Equal(t, call.SerialNumber, onu.SerialNumber)
Matteo Scandoloe811ae92019-12-10 17:50:14 -080071 }
David Bainbridge103cf022019-12-16 20:11:35 +000072 cancel()
Matteo Scandoloe811ae92019-12-10 17:50:14 -080073}
74
75// test that if the discovery indication is not acknowledge we'll keep sending new ones
76func Test_Onu_DiscoverIndication_retry_on_discovery(t *testing.T) {
77 onu := createTestOnu()
78 stream := &mockStream{
79 CallCount: 0,
Matteo Scandolo4a036262020-08-17 15:56:13 -070080 Calls: make(map[int]*openolt.Indication),
Matteo Scandoloe811ae92019-12-10 17:50:14 -080081 fail: false,
82 channel: make(chan int, 10),
83 }
David Bainbridge103cf022019-12-16 20:11:35 +000084 ctx, cancel := context.WithCancel(context.TODO())
85 go onu.ProcessOnuMessages(ctx, stream, nil)
Matteo Scandoloe811ae92019-12-10 17:50:14 -080086 onu.InternalState.SetState("initialized")
Shrey Baid688b4242020-07-10 20:40:10 +053087 _ = onu.InternalState.Event("discover")
Matteo Scandoloe811ae92019-12-10 17:50:14 -080088
89 select {
Shrey Baid688b4242020-07-10 20:40:10 +053090 default:
Matteo Scandoloe811ae92019-12-10 17:50:14 -080091 case <-time.After(400 * time.Millisecond):
92 assert.Equal(t, stream.CallCount, 4)
93 }
David Bainbridge103cf022019-12-16 20:11:35 +000094 cancel()
Matteo Scandoloe811ae92019-12-10 17:50:14 -080095}
96
97// test that if the discovery indication is not acknowledge we'll send a new one
98func Test_Onu_DiscoverIndication_retry_on_discovery_stops(t *testing.T) {
99 onu := createTestOnu()
100 onu.DiscoveryRetryDelay = 500 * time.Millisecond
101 stream := &mockStream{
102 CallCount: 0,
Matteo Scandolo4a036262020-08-17 15:56:13 -0700103 Calls: make(map[int]*openolt.Indication),
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800104 fail: false,
105 channel: make(chan int, 10),
106 }
David Bainbridge103cf022019-12-16 20:11:35 +0000107 ctx, cancel := context.WithCancel(context.TODO())
108 go onu.ProcessOnuMessages(ctx, stream, nil)
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800109 onu.InternalState.SetState("initialized")
Shrey Baid688b4242020-07-10 20:40:10 +0530110 _ = onu.InternalState.Event("discover")
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800111
112 go func() {
113 for calls := range stream.channel {
114 if calls == 2 {
115 onu.InternalState.SetState("enabled")
116 }
117 }
118 }()
119
120 select {
Shrey Baid688b4242020-07-10 20:40:10 +0530121 default:
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800122 case <-time.After(1 * time.Second):
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800123 assert.Equal(t, stream.CallCount, 2)
124 }
David Bainbridge103cf022019-12-16 20:11:35 +0000125 cancel()
Matteo Scandoloe811ae92019-12-10 17:50:14 -0800126}