Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 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 | |
Matteo Scandolo | 4f4ac79 | 2020-10-01 16:33:21 -0700 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v4/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 | |
| 50 | // test that we're sending a Discovery indication to VOLTHA |
| 51 | func Test_Onu_DiscoverIndication_send_on_discovery(t *testing.T) { |
| 52 | onu := createTestOnu() |
| 53 | stream := &mockStream{ |
| 54 | CallCount: 0, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 55 | Calls: make(map[int]*openolt.Indication), |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 56 | fail: false, |
| 57 | channel: make(chan int, 10), |
| 58 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 59 | ctx, cancel := context.WithCancel(context.TODO()) |
| 60 | go onu.ProcessOnuMessages(ctx, stream, nil) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 61 | onu.InternalState.SetState(OnuTxInitialize) |
| 62 | _ = onu.InternalState.Event(OnuTxDiscover) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 63 | |
| 64 | select { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 65 | default: |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 66 | case <-time.After(90 * time.Millisecond): |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 67 | call := stream.Calls[1].GetOnuDiscInd() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 68 | assert.Equal(t, stream.CallCount, 1) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 69 | assert.Equal(t, call.IntfId, onu.PonPortID) |
| 70 | assert.Equal(t, call.SerialNumber, onu.SerialNumber) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 71 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 72 | cancel() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // test that if the discovery indication is not acknowledge we'll keep sending new ones |
| 76 | func Test_Onu_DiscoverIndication_retry_on_discovery(t *testing.T) { |
| 77 | onu := createTestOnu() |
| 78 | stream := &mockStream{ |
| 79 | CallCount: 0, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 80 | Calls: make(map[int]*openolt.Indication), |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 81 | fail: false, |
| 82 | channel: make(chan int, 10), |
| 83 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 84 | ctx, cancel := context.WithCancel(context.TODO()) |
| 85 | go onu.ProcessOnuMessages(ctx, stream, nil) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 86 | onu.InternalState.SetState(OnuStateInitialized) |
| 87 | _ = onu.InternalState.Event(OnuTxDiscover) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 88 | |
| 89 | select { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 90 | default: |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 91 | case <-time.After(400 * time.Millisecond): |
| 92 | assert.Equal(t, stream.CallCount, 4) |
| 93 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 94 | cancel() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | // test that if the discovery indication is not acknowledge we'll send a new one |
| 98 | func 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 Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 103 | Calls: make(map[int]*openolt.Indication), |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 104 | fail: false, |
| 105 | channel: make(chan int, 10), |
| 106 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 107 | ctx, cancel := context.WithCancel(context.TODO()) |
| 108 | go onu.ProcessOnuMessages(ctx, stream, nil) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 109 | onu.InternalState.SetState(OnuStateInitialized) |
| 110 | _ = onu.InternalState.Event(OnuTxDiscover) |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 111 | |
| 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 Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 121 | default: |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 122 | case <-time.After(1 * time.Second): |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 123 | assert.Equal(t, stream.CallCount, 2) |
| 124 | } |
David Bainbridge | 103cf02 | 2019-12-16 20:11:35 +0000 | [diff] [blame] | 125 | cancel() |
Matteo Scandolo | e811ae9 | 2019-12-10 17:50:14 -0800 | [diff] [blame] | 126 | } |