blob: 8aa258b349782d9bafe43b538065d5c192f6d2e5 [file] [log] [blame]
Matteo Scandolo99f18462019-10-28 14:14:28 -07001/*
Joey Armstrong3881b732022-12-27 07:55:37 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolo99f18462019-10-28 14:14:28 -07003
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 (
Elia Battiston536a22b2022-01-10 10:33:15 +010020 "testing"
21
22 "github.com/opencord/bbsim/internal/bbsim/responders/eapol"
Matteo Scandolod15c0b42021-03-22 11:38:13 -070023 omcilib "github.com/opencord/bbsim/internal/common/omci"
Andrea Campanella10426e22021-10-15 17:58:04 +020024 me "github.com/opencord/omci-lib-go/v2/generated"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010025
26 "gotest.tools/assert"
Matteo Scandolo99f18462019-10-28 14:14:28 -070027)
28
29func Test_Onu_StateMachine_enable(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070030 onu := createTestOnu()
Matteo Scandolo9f4bf4f2021-06-22 09:41:02 +020031 assert.Equal(t, onu.InternalState.Current(), OnuStateInitialized)
32 _ = onu.InternalState.Event(OnuTxDiscover)
33 assert.Equal(t, onu.InternalState.Current(), OnuStateDiscovered)
34 _ = onu.InternalState.Event(OnuTxEnable)
Matteo Scandolocedde462021-03-09 17:37:16 -080035 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo99f18462019-10-28 14:14:28 -070036}
37
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070038func Test_Onu_StateMachine_disable(t *testing.T) {
39 onu := createTestOnu()
Matteo Scandolocedde462021-03-09 17:37:16 -080040 onu.InternalState.SetState(OnuStateEnabled)
41 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070042
Andrea Campanellacc9b1662022-01-26 17:47:48 +010043 onu.Flows = []FlowKey{
yasin saplic07b9522022-01-27 11:23:54 +000044 {ID: 1},
45 {ID: 2},
Andrea Campanellacc9b1662022-01-26 17:47:48 +010046 }
Matteo Scandolod15c0b42021-03-22 11:38:13 -070047 key := omcilib.OnuAlarmInfoMapKey{
48 MeInstance: 257,
49 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
50 }
51 onu.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{SequenceNo: 1, AlarmBitMap: [28]byte{}}
52 onu.PonPort.storeOnuId(onu.ID, onu.SerialNumber)
Girish Gowdra574834a2022-02-04 15:15:15 -080053 onu.PonPort.storeAllocId(1, 1024, 0x8001, 1024, onu.SerialNumber)
Matteo Scandolod15c0b42021-03-22 11:38:13 -070054 onu.PonPort.storeGemPort(1, onu.SerialNumber)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070055
Matteo Scandolocedde462021-03-09 17:37:16 -080056 _ = onu.InternalState.Event(OnuTxDisable)
57 assert.Equal(t, onu.InternalState.Current(), OnuStateDisabled)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070058
Matteo Scandolod15c0b42021-03-22 11:38:13 -070059 assert.Equal(t, len(onu.onuAlarmsInfo), 0)
Andrea Campanellacc9b1662022-01-26 17:47:48 +010060 assert.Equal(t, len(onu.Flows), 0)
Matteo Scandolod15c0b42021-03-22 11:38:13 -070061 assert.Equal(t, len(onu.PonPort.AllocatedOnuIds), 0)
62 assert.Equal(t, len(onu.PonPort.AllocatedAllocIds), 0)
63 assert.Equal(t, len(onu.PonPort.AllocatedGemPorts), 0)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070064}
65
Matteo Scandolo9f4bf4f2021-06-22 09:41:02 +020066func Test_Onu_StateMachine_pon_disable(t *testing.T) {
67 onu := createTestOnu()
68 var err error
69
70 onu.InternalState.SetState(OnuStateEnabled)
71 err = onu.InternalState.Event(OnuTxPonDisable)
72 assert.NilError(t, err)
73 assert.Equal(t, OnuStatePonDisabled, onu.InternalState.Current())
74
75 onu.InternalState.SetState(OnuStateImageDownloadComplete)
76 err = onu.InternalState.Event(OnuTxPonDisable)
77 assert.NilError(t, err)
78 assert.Equal(t, OnuStatePonDisabled, onu.InternalState.Current())
79}
80
81func Test_Onu_StateMachine_software_image(t *testing.T) {
82 onu := createTestOnu()
83 var err error
84
85 // happy path
86 onu.InternalState.SetState(OnuStateEnabled)
87 err = onu.InternalState.Event(OnuTxStartImageDownload)
88 assert.NilError(t, err)
89 assert.Equal(t, OnuStateImageDownloadStarted, onu.InternalState.Current())
90
91 err = onu.InternalState.Event(OnuTxProgressImageDownload)
92 assert.NilError(t, err)
93 assert.Equal(t, OnuStateImageDownloadInProgress, onu.InternalState.Current())
94
95 err = onu.InternalState.Event(OnuTxCompleteImageDownload)
96 assert.NilError(t, err)
97 assert.Equal(t, OnuStateImageDownloadComplete, onu.InternalState.Current())
98
99 err = onu.InternalState.Event(OnuTxActivateImage)
100 assert.NilError(t, err)
101 assert.Equal(t, OnuStateImageActivated, onu.InternalState.Current())
102
103 // after image activate we get an ONU reboot, thus the state is back to Enabled before committing
104 onu.InternalState.SetState(OnuStateEnabled)
105 err = onu.InternalState.Event(OnuTxCommitImage)
106 assert.NilError(t, err)
107 assert.Equal(t, OnuStateImageCommitted, onu.InternalState.Current())
108
109 // but we should be able to start a new download
110 err = onu.InternalState.Event(OnuTxStartImageDownload)
111 assert.NilError(t, err)
112 assert.Equal(t, OnuStateImageDownloadStarted, onu.InternalState.Current())
113}
114
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800115// check that I can go to auth_started only if
116// - the GemPort is set
117// - the eapolFlow is received
118func Test_Onu_StateMachine_eapol_no_flow(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700119 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700120 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700121
Matteo Scandolocedde462021-03-09 17:37:16 -0800122 onu.InternalState.SetState(OnuStateEnabled)
123 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800124
125 // fail as no EapolFlow has been received
Elia Battiston536a22b2022-01-10 10:33:15 +0100126 err := onu.InternalState.Event(eapol.EventStartAuth)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800127 if err == nil {
128 t.Fatal("can't start EAPOL without EapolFlow")
129 }
Matteo Scandolocedde462021-03-09 17:37:16 -0800130 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800131 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-eapol-flow-is-missing")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700132}
133
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800134func Test_Onu_StateMachine_eapol_no_gem(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700135 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700136 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700137
Matteo Scandolocedde462021-03-09 17:37:16 -0800138 onu.InternalState.SetState(OnuStateEnabled)
139 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800140
Matteo Scandolo4a036262020-08-17 15:56:13 -0700141 // fail has no GemPort has been set
Elia Battiston536a22b2022-01-10 10:33:15 +0100142 err := onu.InternalState.Event(eapol.EventStartAuth)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800143 if err == nil {
144 t.Fatal("can't start EAPOL without GemPort")
145 }
Matteo Scandolocedde462021-03-09 17:37:16 -0800146 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800147 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-gemport-is-missing")
148
149}
150
151func Test_Onu_StateMachine_eapol_start(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700152 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800153 onu := createTestOnu()
154
Matteo Scandolocedde462021-03-09 17:37:16 -0800155 onu.InternalState.SetState(OnuStateEnabled)
156 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800157
158 // succeed
Elia Battiston536a22b2022-01-10 10:33:15 +0100159 _ = onu.InternalState.Event(eapol.EventStartAuth)
160 assert.Equal(t, onu.InternalState.Current(), eapol.StateAuthStarted)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700161}
162
163func Test_Onu_StateMachine_eapol_states(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700164 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700165 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700166
Elia Battiston536a22b2022-01-10 10:33:15 +0100167 onu.InternalState.SetState(eapol.StateAuthStarted)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700168
Elia Battiston536a22b2022-01-10 10:33:15 +0100169 assert.Equal(t, onu.InternalState.Current(), eapol.StateAuthStarted)
170 _ = onu.InternalState.Event(eapol.EventStartSent)
171 assert.Equal(t, onu.InternalState.Current(), eapol.StateStartSent)
172 _ = onu.InternalState.Event(eapol.EventResponseIdentitySent)
173 assert.Equal(t, onu.InternalState.Current(), eapol.StateResponseIdentitySent)
174 _ = onu.InternalState.Event(eapol.EventResponseChallengeSent)
175 assert.Equal(t, onu.InternalState.Current(), eapol.StateResponseChallengeSent)
176 _ = onu.InternalState.Event(eapol.EventResponseSuccessReceived)
177 assert.Equal(t, onu.InternalState.Current(), eapol.StateResponseSuccessReceived)
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800178
179 // test that we can retrigger EAPOL
Elia Battiston536a22b2022-01-10 10:33:15 +0100180 states := []string{eapol.StateStartSent, eapol.StateResponseIdentitySent, eapol.StateResponseChallengeSent, eapol.StateResponseSuccessReceived, eapol.StateAuthFailed, "dhcp_ack_received", "dhcp_failed"}
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800181 for _, state := range states {
182 onu.InternalState.SetState(state)
Elia Battiston536a22b2022-01-10 10:33:15 +0100183 err := onu.InternalState.Event(eapol.EventStartAuth)
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800184 assert.Equal(t, err, nil)
Elia Battiston536a22b2022-01-10 10:33:15 +0100185 assert.Equal(t, onu.InternalState.Current(), eapol.StateAuthStarted)
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800186 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700187}
188
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800189// if auth is set to true we can't go from enabled to dhcp_started
190func Test_Onu_StateMachine_dhcp_no_auth(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700191 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800192 onu := createTestOnu()
193
Matteo Scandolocedde462021-03-09 17:37:16 -0800194 onu.InternalState.SetState(OnuStateEnabled)
195 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800196
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800197 err := onu.InternalState.Event("start_dhcp")
198 if err == nil {
199 t.Fail()
200 }
Matteo Scandolocedde462021-03-09 17:37:16 -0800201 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800202 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-authentication-is-required")
203}
204
205// if the DHCP flow has not been received we can't start authentication
206func Test_Onu_StateMachine_dhcp_no_flow(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700207 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800208 onu := createTestOnu()
209
Elia Battiston536a22b2022-01-10 10:33:15 +0100210 onu.InternalState.SetState(eapol.StateResponseSuccessReceived)
211 assert.Equal(t, onu.InternalState.Current(), eapol.StateResponseSuccessReceived)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800212
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800213 err := onu.InternalState.Event("start_dhcp")
214 if err == nil {
215 t.Fail()
216 }
Elia Battiston536a22b2022-01-10 10:33:15 +0100217 assert.Equal(t, onu.InternalState.Current(), eapol.StateResponseSuccessReceived)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800218 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")
219}
220
221// if the ONU does not have a GemPort we can't start DHCP
222func Test_Onu_StateMachine_dhcp_no_gem(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700223 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800224 onu := createTestOnu()
225
Elia Battiston536a22b2022-01-10 10:33:15 +0100226 onu.InternalState.SetState(eapol.StateResponseSuccessReceived)
227 assert.Equal(t, onu.InternalState.Current(), eapol.StateResponseSuccessReceived)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800228
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800229 err := onu.InternalState.Event("start_dhcp")
230 if err == nil {
231 t.Fail()
232 }
Elia Battiston536a22b2022-01-10 10:33:15 +0100233 assert.Equal(t, onu.InternalState.Current(), eapol.StateResponseSuccessReceived)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800234 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-gemport-is-missing")
235}
236
Matteo Scandolo99f18462019-10-28 14:14:28 -0700237func Test_Onu_StateMachine_dhcp_start(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700238 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700239 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700240
Elia Battiston536a22b2022-01-10 10:33:15 +0100241 onu.InternalState.SetState(eapol.StateResponseSuccessReceived)
242 assert.Equal(t, onu.InternalState.Current(), eapol.StateResponseSuccessReceived)
Matteo Scandolo99f18462019-10-28 14:14:28 -0700243
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800244 // default transition
Shrey Baid688b4242020-07-10 20:40:10 +0530245 _ = onu.InternalState.Event("start_dhcp")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700246 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
247}
248
Matteo Scandolo99f18462019-10-28 14:14:28 -0700249func Test_Onu_StateMachine_dhcp_states(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700250 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700251 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700252
Matteo Scandolo99f18462019-10-28 14:14:28 -0700253 onu.InternalState.SetState("dhcp_started")
254
255 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530256 _ = onu.InternalState.Event("dhcp_discovery_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700257 assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530258 _ = onu.InternalState.Event("dhcp_request_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700259 assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530260 _ = onu.InternalState.Event("dhcp_ack_received")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700261 assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received")
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800262
263 // test that we can retrigger DHCP
Elia Battiston536a22b2022-01-10 10:33:15 +0100264 states := []string{eapol.StateResponseSuccessReceived, "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"}
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800265 for _, state := range states {
266 onu.InternalState.SetState(state)
267 err := onu.InternalState.Event("start_dhcp")
268 assert.Equal(t, err, nil)
269 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
270 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700271}