blob: ab32dd0d704d337d309915396e1cecf09d5ac078 [file] [log] [blame]
Matteo Scandolo99f18462019-10-28 14:14:28 -07001/*
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 (
Matteo Scandolod15c0b42021-03-22 11:38:13 -070020 omcilib "github.com/opencord/bbsim/internal/common/omci"
Andrea Campanella10426e22021-10-15 17:58:04 +020021 me "github.com/opencord/omci-lib-go/v2/generated"
Matteo Scandolo99f18462019-10-28 14:14:28 -070022 "testing"
Zdravko Bozakov681364d2019-11-10 14:28:46 +010023
24 "gotest.tools/assert"
Matteo Scandolo99f18462019-10-28 14:14:28 -070025)
26
27func Test_Onu_StateMachine_enable(t *testing.T) {
Matteo Scandoloc1147092019-10-29 09:38:33 -070028 onu := createTestOnu()
Matteo Scandolo9f4bf4f2021-06-22 09:41:02 +020029 assert.Equal(t, onu.InternalState.Current(), OnuStateInitialized)
30 _ = onu.InternalState.Event(OnuTxDiscover)
31 assert.Equal(t, onu.InternalState.Current(), OnuStateDiscovered)
32 _ = onu.InternalState.Event(OnuTxEnable)
Matteo Scandolocedde462021-03-09 17:37:16 -080033 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo99f18462019-10-28 14:14:28 -070034}
35
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070036func Test_Onu_StateMachine_disable(t *testing.T) {
37 onu := createTestOnu()
Matteo Scandolocedde462021-03-09 17:37:16 -080038 onu.InternalState.SetState(OnuStateEnabled)
39 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070040
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070041 onu.Flows = []FlowKey{
Shrey Baid688b4242020-07-10 20:40:10 +053042 {ID: 1, Direction: "upstream"},
43 {ID: 2, Direction: "downstream"},
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070044 }
Matteo Scandolod15c0b42021-03-22 11:38:13 -070045 key := omcilib.OnuAlarmInfoMapKey{
46 MeInstance: 257,
47 MeClassID: me.PhysicalPathTerminationPointEthernetUniClassID,
48 }
49 onu.onuAlarmsInfo[key] = omcilib.OnuAlarmInfo{SequenceNo: 1, AlarmBitMap: [28]byte{}}
50 onu.PonPort.storeOnuId(onu.ID, onu.SerialNumber)
51 onu.PonPort.storeAllocId(1, onu.SerialNumber)
52 onu.PonPort.storeGemPort(1, onu.SerialNumber)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070053
Matteo Scandolocedde462021-03-09 17:37:16 -080054 _ = onu.InternalState.Event(OnuTxDisable)
55 assert.Equal(t, onu.InternalState.Current(), OnuStateDisabled)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070056
Matteo Scandolod15c0b42021-03-22 11:38:13 -070057 assert.Equal(t, len(onu.onuAlarmsInfo), 0)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070058 assert.Equal(t, len(onu.Flows), 0)
Matteo Scandolod15c0b42021-03-22 11:38:13 -070059 assert.Equal(t, len(onu.PonPort.AllocatedOnuIds), 0)
60 assert.Equal(t, len(onu.PonPort.AllocatedAllocIds), 0)
61 assert.Equal(t, len(onu.PonPort.AllocatedGemPorts), 0)
Matteo Scandolo47ef64b2020-04-20 14:16:07 -070062}
63
Matteo Scandolo9f4bf4f2021-06-22 09:41:02 +020064func Test_Onu_StateMachine_pon_disable(t *testing.T) {
65 onu := createTestOnu()
66 var err error
67
68 onu.InternalState.SetState(OnuStateEnabled)
69 err = onu.InternalState.Event(OnuTxPonDisable)
70 assert.NilError(t, err)
71 assert.Equal(t, OnuStatePonDisabled, onu.InternalState.Current())
72
73 onu.InternalState.SetState(OnuStateImageDownloadComplete)
74 err = onu.InternalState.Event(OnuTxPonDisable)
75 assert.NilError(t, err)
76 assert.Equal(t, OnuStatePonDisabled, onu.InternalState.Current())
77}
78
79func Test_Onu_StateMachine_software_image(t *testing.T) {
80 onu := createTestOnu()
81 var err error
82
83 // happy path
84 onu.InternalState.SetState(OnuStateEnabled)
85 err = onu.InternalState.Event(OnuTxStartImageDownload)
86 assert.NilError(t, err)
87 assert.Equal(t, OnuStateImageDownloadStarted, onu.InternalState.Current())
88
89 err = onu.InternalState.Event(OnuTxProgressImageDownload)
90 assert.NilError(t, err)
91 assert.Equal(t, OnuStateImageDownloadInProgress, onu.InternalState.Current())
92
93 err = onu.InternalState.Event(OnuTxCompleteImageDownload)
94 assert.NilError(t, err)
95 assert.Equal(t, OnuStateImageDownloadComplete, onu.InternalState.Current())
96
97 err = onu.InternalState.Event(OnuTxActivateImage)
98 assert.NilError(t, err)
99 assert.Equal(t, OnuStateImageActivated, onu.InternalState.Current())
100
101 // after image activate we get an ONU reboot, thus the state is back to Enabled before committing
102 onu.InternalState.SetState(OnuStateEnabled)
103 err = onu.InternalState.Event(OnuTxCommitImage)
104 assert.NilError(t, err)
105 assert.Equal(t, OnuStateImageCommitted, onu.InternalState.Current())
106
107 // but we should be able to start a new download
108 err = onu.InternalState.Event(OnuTxStartImageDownload)
109 assert.NilError(t, err)
110 assert.Equal(t, OnuStateImageDownloadStarted, onu.InternalState.Current())
111}
112
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800113// check that I can go to auth_started only if
114// - the GemPort is set
115// - the eapolFlow is received
116func Test_Onu_StateMachine_eapol_no_flow(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700117 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700118 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700119
Matteo Scandolocedde462021-03-09 17:37:16 -0800120 onu.InternalState.SetState(OnuStateEnabled)
121 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800122
123 // fail as no EapolFlow has been received
124 err := onu.InternalState.Event("start_auth")
125 if err == nil {
126 t.Fatal("can't start EAPOL without EapolFlow")
127 }
Matteo Scandolocedde462021-03-09 17:37:16 -0800128 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800129 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 -0700130}
131
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800132func Test_Onu_StateMachine_eapol_no_gem(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700133 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700134 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700135
Matteo Scandolocedde462021-03-09 17:37:16 -0800136 onu.InternalState.SetState(OnuStateEnabled)
137 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800138
Matteo Scandolo4a036262020-08-17 15:56:13 -0700139 // fail has no GemPort has been set
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800140 err := onu.InternalState.Event("start_auth")
141 if err == nil {
142 t.Fatal("can't start EAPOL without GemPort")
143 }
Matteo Scandolocedde462021-03-09 17:37:16 -0800144 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800145 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-auth-started-as-gemport-is-missing")
146
147}
148
149func Test_Onu_StateMachine_eapol_start(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700150 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800151 onu := createTestOnu()
152
Matteo Scandolocedde462021-03-09 17:37:16 -0800153 onu.InternalState.SetState(OnuStateEnabled)
154 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800155
156 // succeed
Shrey Baid688b4242020-07-10 20:40:10 +0530157 _ = onu.InternalState.Event("start_auth")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700158 assert.Equal(t, onu.InternalState.Current(), "auth_started")
159}
160
161func Test_Onu_StateMachine_eapol_states(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700162 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700163 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700164
165 onu.InternalState.SetState("auth_started")
166
167 assert.Equal(t, onu.InternalState.Current(), "auth_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530168 _ = onu.InternalState.Event("eap_start_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700169 assert.Equal(t, onu.InternalState.Current(), "eap_start_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530170 _ = onu.InternalState.Event("eap_response_identity_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700171 assert.Equal(t, onu.InternalState.Current(), "eap_response_identity_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530172 _ = onu.InternalState.Event("eap_response_challenge_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700173 assert.Equal(t, onu.InternalState.Current(), "eap_response_challenge_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530174 _ = onu.InternalState.Event("eap_response_success_received")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700175 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800176
177 // test that we can retrigger EAPOL
178 states := []string{"eap_start_sent", "eap_response_identity_sent", "eap_response_challenge_sent", "eap_response_success_received", "auth_failed", "dhcp_ack_received", "dhcp_failed"}
179 for _, state := range states {
180 onu.InternalState.SetState(state)
181 err := onu.InternalState.Event("start_auth")
182 assert.Equal(t, err, nil)
183 assert.Equal(t, onu.InternalState.Current(), "auth_started")
184 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700185}
186
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800187// if auth is set to true we can't go from enabled to dhcp_started
188func Test_Onu_StateMachine_dhcp_no_auth(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700189 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800190 onu := createTestOnu()
191
Matteo Scandolocedde462021-03-09 17:37:16 -0800192 onu.InternalState.SetState(OnuStateEnabled)
193 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800194
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800195 err := onu.InternalState.Event("start_dhcp")
196 if err == nil {
197 t.Fail()
198 }
Matteo Scandolocedde462021-03-09 17:37:16 -0800199 assert.Equal(t, onu.InternalState.Current(), OnuStateEnabled)
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800200 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-authentication-is-required")
201}
202
203// if the DHCP flow has not been received we can't start authentication
204func Test_Onu_StateMachine_dhcp_no_flow(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700205 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800206 onu := createTestOnu()
207
208 onu.InternalState.SetState("eap_response_success_received")
209 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
210
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800211 err := onu.InternalState.Event("start_dhcp")
212 if err == nil {
213 t.Fail()
214 }
215 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
216 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-dhcp-flow-is-missing")
217}
218
219// if the ONU does not have a GemPort we can't start DHCP
220func Test_Onu_StateMachine_dhcp_no_gem(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700221 t.Skip("Needs to be moved in the Service struct")
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800222 onu := createTestOnu()
223
224 onu.InternalState.SetState("eap_response_success_received")
225 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
226
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800227 err := onu.InternalState.Event("start_dhcp")
228 if err == nil {
229 t.Fail()
230 }
231 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
232 assert.Equal(t, err.Error(), "transition canceled with error: cannot-go-to-dhcp-started-as-gemport-is-missing")
233}
234
Matteo Scandolo99f18462019-10-28 14:14:28 -0700235func Test_Onu_StateMachine_dhcp_start(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700236 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700237 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700238
239 onu.InternalState.SetState("eap_response_success_received")
240 assert.Equal(t, onu.InternalState.Current(), "eap_response_success_received")
241
Matteo Scandolo5ff80082019-12-20 13:20:57 -0800242 // default transition
Shrey Baid688b4242020-07-10 20:40:10 +0530243 _ = onu.InternalState.Event("start_dhcp")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700244 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
245}
246
Matteo Scandolo99f18462019-10-28 14:14:28 -0700247func Test_Onu_StateMachine_dhcp_states(t *testing.T) {
Matteo Scandolo4a036262020-08-17 15:56:13 -0700248 t.Skip("Needs to be moved in the Service struct")
Matteo Scandoloc1147092019-10-29 09:38:33 -0700249 onu := createTestOnu()
Matteo Scandolo99f18462019-10-28 14:14:28 -0700250
Matteo Scandolo99f18462019-10-28 14:14:28 -0700251 onu.InternalState.SetState("dhcp_started")
252
253 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
Shrey Baid688b4242020-07-10 20:40:10 +0530254 _ = onu.InternalState.Event("dhcp_discovery_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700255 assert.Equal(t, onu.InternalState.Current(), "dhcp_discovery_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530256 _ = onu.InternalState.Event("dhcp_request_sent")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700257 assert.Equal(t, onu.InternalState.Current(), "dhcp_request_sent")
Shrey Baid688b4242020-07-10 20:40:10 +0530258 _ = onu.InternalState.Event("dhcp_ack_received")
Matteo Scandolo99f18462019-10-28 14:14:28 -0700259 assert.Equal(t, onu.InternalState.Current(), "dhcp_ack_received")
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800260
261 // test that we can retrigger DHCP
Matteo Scandolo5e081b52019-11-21 14:34:25 -0800262 states := []string{"eap_response_success_received", "dhcp_discovery_sent", "dhcp_request_sent", "dhcp_ack_received", "dhcp_failed"}
263 for _, state := range states {
264 onu.InternalState.SetState(state)
265 err := onu.InternalState.Event("start_dhcp")
266 assert.Equal(t, err, nil)
267 assert.Equal(t, onu.InternalState.Current(), "dhcp_started")
268 }
Matteo Scandolo99f18462019-10-28 14:14:28 -0700269}