Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 1 | /* |
Joey Armstrong | 2c03936 | 2024-02-04 18:51:52 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 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 | |
| 17 | package devices |
| 18 | |
| 19 | import ( |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 20 | "context" |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 21 | "errors" |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 22 | "testing" |
| 23 | "time" |
| 24 | |
David K. Bainbridge | c415efe | 2021-08-19 13:05:21 +0000 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v5/go/openolt" |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 26 | "google.golang.org/grpc" |
| 27 | "gotest.tools/assert" |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type mockStream struct { |
| 31 | grpc.ServerStream |
| 32 | CallCount int |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 33 | Calls map[int]*openolt.Indication |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 34 | channel chan int |
| 35 | fail bool |
| 36 | } |
| 37 | |
| 38 | func (s *mockStream) Send(ind *openolt.Indication) error { |
| 39 | s.CallCount++ |
| 40 | if s.fail { |
| 41 | return errors.New("fake-error") |
| 42 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 43 | s.Calls[s.CallCount] = ind |
| 44 | go func() { |
| 45 | s.channel <- s.CallCount |
| 46 | }() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 47 | return nil |
| 48 | } |
| 49 | |
Matteo Scandolo | 9ddb3a9 | 2021-04-14 16:16:20 -0700 | [diff] [blame] | 50 | func (s *mockStream) Context() context.Context { |
| 51 | return context.Background() |
| 52 | } |
| 53 | |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 54 | // test that we're sending a Discovery indication to VOLTHA |
| 55 | func Test_Onu_DiscoverIndication_send_on_discovery(t *testing.T) { |
| 56 | onu := createTestOnu() |
| 57 | stream := &mockStream{ |
| 58 | CallCount: 0, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 59 | Calls: make(map[int]*openolt.Indication), |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 60 | fail: false, |
| 61 | channel: make(chan int, 10), |
| 62 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 63 | ctx, cancel := context.WithCancel(context.TODO()) |
| 64 | go onu.ProcessOnuMessages(ctx, stream, nil) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 65 | onu.InternalState.SetState(OnuTxInitialize) |
| 66 | _ = onu.InternalState.Event(OnuTxDiscover) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 67 | |
| 68 | select { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 69 | default: |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 70 | case <-time.After(90 * time.Millisecond): |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 71 | call := stream.Calls[1].GetOnuDiscInd() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 72 | assert.Equal(t, stream.CallCount, 1) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 73 | assert.Equal(t, call.IntfId, onu.PonPortID) |
| 74 | assert.Equal(t, call.SerialNumber, onu.SerialNumber) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 75 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 76 | cancel() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | // test that if the discovery indication is not acknowledge we'll keep sending new ones |
| 80 | func Test_Onu_DiscoverIndication_retry_on_discovery(t *testing.T) { |
| 81 | onu := createTestOnu() |
| 82 | stream := &mockStream{ |
| 83 | CallCount: 0, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 84 | Calls: make(map[int]*openolt.Indication), |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 85 | fail: false, |
| 86 | channel: make(chan int, 10), |
| 87 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 88 | ctx, cancel := context.WithCancel(context.TODO()) |
| 89 | go onu.ProcessOnuMessages(ctx, stream, nil) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 90 | onu.InternalState.SetState(OnuStateInitialized) |
| 91 | _ = onu.InternalState.Event(OnuTxDiscover) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 92 | |
| 93 | select { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 94 | default: |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 95 | case <-time.After(400 * time.Millisecond): |
| 96 | assert.Equal(t, stream.CallCount, 4) |
| 97 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 98 | cancel() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // test that if the discovery indication is not acknowledge we'll send a new one |
| 102 | func 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 Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 107 | Calls: make(map[int]*openolt.Indication), |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 108 | fail: false, |
| 109 | channel: make(chan int, 10), |
| 110 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 111 | ctx, cancel := context.WithCancel(context.TODO()) |
| 112 | go onu.ProcessOnuMessages(ctx, stream, nil) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 113 | onu.InternalState.SetState(OnuStateInitialized) |
| 114 | _ = onu.InternalState.Event(OnuTxDiscover) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 115 | |
| 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 Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 125 | default: |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 126 | case <-time.After(1 * time.Second): |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 127 | assert.Equal(t, stream.CallCount, 2) |
| 128 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 129 | cancel() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 130 | } |