blob: 1e47034035a3fb967e4903e821c5f118f5b921dc [file] [log] [blame]
Matteo Scandolo813402b2019-10-23 19:24:52 -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 (
20 "context"
21 "errors"
22 "github.com/google/gopacket/layers"
23 "github.com/looplab/fsm"
24 "github.com/opencord/voltha-protos/go/openolt"
25 "github.com/opencord/voltha-protos/go/tech_profile"
26 "google.golang.org/grpc"
27 "gotest.tools/assert"
28 "net"
29 "testing"
30)
31
32type FlowAddSpy struct {
33 CallCount int
34 Calls map[int]*openolt.Flow
35}
36
37type mockClient struct {
38 FlowAddSpy
39 fail bool
40}
41
42func (s *mockClient) DisableOlt(ctx context.Context, in *openolt.Empty, opts ...grpc.CallOption) (*openolt.Empty, error) {
43 return nil, errors.New("unimplemented-in-mock-client")
44}
45func (s *mockClient) ReenableOlt(ctx context.Context, in *openolt.Empty, opts ...grpc.CallOption) (*openolt.Empty, error) {
46 return nil, errors.New("unimplemented-in-mock-client")
47}
48func (s *mockClient) ActivateOnu(ctx context.Context, in *openolt.Onu, opts ...grpc.CallOption) (*openolt.Empty, error) {
49 return nil, errors.New("unimplemented-in-mock-client")
50}
51func (s *mockClient) DeactivateOnu(ctx context.Context, in *openolt.Onu, opts ...grpc.CallOption) (*openolt.Empty, error) {
52 return nil, errors.New("unimplemented-in-mock-client")
53}
54func (s *mockClient) DeleteOnu(ctx context.Context, in *openolt.Onu, opts ...grpc.CallOption) (*openolt.Empty, error) {
55 return nil, errors.New("unimplemented-in-mock-client")
56}
57func (s *mockClient) OmciMsgOut(ctx context.Context, in *openolt.OmciMsg, opts ...grpc.CallOption) (*openolt.Empty, error) {
58 return nil, errors.New("unimplemented-in-mock-client")
59}
60func (s *mockClient) OnuPacketOut(ctx context.Context, in *openolt.OnuPacket, opts ...grpc.CallOption) (*openolt.Empty, error) {
61 return nil, errors.New("unimplemented-in-mock-client")
62}
63func (s *mockClient) UplinkPacketOut(ctx context.Context, in *openolt.UplinkPacket, opts ...grpc.CallOption) (*openolt.Empty, error) {
64 return nil, errors.New("unimplemented-in-mock-client")
65}
66func (s *mockClient) FlowAdd(ctx context.Context, in *openolt.Flow, opts ...grpc.CallOption) (*openolt.Empty, error) {
67 s.FlowAddSpy.CallCount++
68 if s.fail {
69 return nil, errors.New("fake-error")
70 }
71 s.FlowAddSpy.Calls[s.FlowAddSpy.CallCount] = in
72 return &openolt.Empty{}, nil
73}
74func (s *mockClient) FlowRemove(ctx context.Context, in *openolt.Flow, opts ...grpc.CallOption) (*openolt.Empty, error) {
75 return nil, errors.New("unimplemented-in-mock-client")
76}
77func (s *mockClient) HeartbeatCheck(ctx context.Context, in *openolt.Empty, opts ...grpc.CallOption) (*openolt.Heartbeat, error) {
78 return nil, errors.New("unimplemented-in-mock-client")
79}
80func (s *mockClient) EnablePonIf(ctx context.Context, in *openolt.Interface, opts ...grpc.CallOption) (*openolt.Empty, error) {
81 return nil, errors.New("unimplemented-in-mock-client")
82}
83func (s *mockClient) DisablePonIf(ctx context.Context, in *openolt.Interface, opts ...grpc.CallOption) (*openolt.Empty, error) {
84 return nil, errors.New("unimplemented-in-mock-client")
85}
86func (s *mockClient) GetDeviceInfo(ctx context.Context, in *openolt.Empty, opts ...grpc.CallOption) (*openolt.DeviceInfo, error) {
87 return nil, errors.New("unimplemented-in-mock-client")
88}
89func (s *mockClient) Reboot(ctx context.Context, in *openolt.Empty, opts ...grpc.CallOption) (*openolt.Empty, error) {
90 return nil, errors.New("unimplemented-in-mock-client")
91}
92func (s *mockClient) CollectStatistics(ctx context.Context, in *openolt.Empty, opts ...grpc.CallOption) (*openolt.Empty, error) {
93 return nil, errors.New("unimplemented-in-mock-client")
94}
95func (s *mockClient) CreateTrafficSchedulers(ctx context.Context, in *tech_profile.TrafficSchedulers, opts ...grpc.CallOption) (*openolt.Empty, error) {
96 return nil, errors.New("unimplemented-in-mock-client")
97}
98func (s *mockClient) RemoveTrafficSchedulers(ctx context.Context, in *tech_profile.TrafficSchedulers, opts ...grpc.CallOption) (*openolt.Empty, error) {
99 return nil, errors.New("unimplemented-in-mock-client")
100}
101func (s *mockClient) CreateTrafficQueues(ctx context.Context, in *tech_profile.TrafficQueues, opts ...grpc.CallOption) (*openolt.Empty, error) {
102 return nil, errors.New("unimplemented-in-mock-client")
103}
104func (s *mockClient) RemoveTrafficQueues(ctx context.Context, in *tech_profile.TrafficQueues, opts ...grpc.CallOption) (*openolt.Empty, error) {
105 return nil, errors.New("unimplemented-in-mock-client")
106}
107func (s *mockClient) EnableIndication(ctx context.Context, in *openolt.Empty, opts ...grpc.CallOption) (openolt.Openolt_EnableIndicationClient, error) {
108 return nil, errors.New("unimplemented-in-mock-client")
109}
110
111func createMockOnu(id uint32, ponPortId uint32, sTag int, cTag int) Onu {
112 o := Onu{
113 ID: id,
114 PonPortID: ponPortId,
115 STag: sTag,
116 CTag: cTag,
117 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(ponPortId), byte(id)},
118 PortNo: 0,
119 }
120 o.SerialNumber = o.NewSN(0, ponPortId, o.ID)
121 return o
122}
123
124func Test_Onu_SendEapolFlow(t *testing.T) {
125 onu := createMockOnu(1, 1, 900, 900)
126
127 client := &mockClient{
128 FlowAddSpy: FlowAddSpy{
129 Calls: make(map[int]*openolt.Flow),
130 },
131 fail: false,
132 }
133
134 onu.sendEapolFlow(client)
135 assert.Equal(t, client.FlowAddSpy.CallCount, 1)
136
137 assert.Equal(t, client.FlowAddSpy.Calls[1].AccessIntfId, int32(onu.PonPortID))
138 assert.Equal(t, client.FlowAddSpy.Calls[1].OnuId, int32(onu.ID))
139 assert.Equal(t, client.FlowAddSpy.Calls[1].UniId, int32(0))
140 assert.Equal(t, client.FlowAddSpy.Calls[1].FlowId, onu.ID)
141 assert.Equal(t, client.FlowAddSpy.Calls[1].FlowType, "downstream")
142 assert.Equal(t, client.FlowAddSpy.Calls[1].PortNo, onu.ID)
143}
144
145// validates that when an ONU receives an EAPOL flow for UNI 0
146// it transition to auth_started state
147func Test_HandleFlowUpdateEapolFromGem(t *testing.T) {
148
149 onu := createMockOnu(1, 1, 900, 900)
150
151 onu.InternalState = fsm.NewFSM(
152 "gem_port_added",
153 fsm.Events{
154 {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added"}, Dst: "auth_started"},
155 },
156 fsm.Callbacks{},
157 )
158
159 flow := openolt.Flow{
160 AccessIntfId: int32(onu.PonPortID),
161 OnuId: int32(onu.ID),
162 UniId: int32(0),
163 FlowId: uint32(onu.ID),
164 FlowType: "downstream",
165 AllocId: int32(0),
166 NetworkIntfId: int32(0),
167 Classifier: &openolt.Classifier{
168 EthType: uint32(layers.EthernetTypeEAPOL),
169 OVid: 4091,
170 },
171 Action: &openolt.Action{},
172 Priority: int32(100),
173 PortNo: uint32(onu.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
174 }
175
176 msg := OnuFlowUpdateMessage{
177 PonPortID: 1,
178 OnuID: 1,
179 Flow: &flow,
180 }
181
182 onu.handleFlowUpdate(msg)
183 assert.Equal(t, onu.InternalState.Current(), "auth_started")
184}
185
186// validates that when an ONU receives an EAPOL flow for UNI that is not 0
187// no action is taken
188func Test_HandleFlowUpdateEapolFromGemIgnore(t *testing.T) {
189
190 onu := createMockOnu(1, 1, 900, 900)
191
192 onu.InternalState = fsm.NewFSM(
193 "gem_port_added",
194 fsm.Events{
195 {Name: "start_auth", Src: []string{"eapol_flow_received", "gem_port_added"}, Dst: "auth_started"},
196 },
197 fsm.Callbacks{},
198 )
199
200 flow := openolt.Flow{
201 AccessIntfId: int32(onu.PonPortID),
202 OnuId: int32(onu.ID),
203 UniId: int32(1),
204 FlowId: uint32(onu.ID),
205 FlowType: "downstream",
206 AllocId: int32(0),
207 NetworkIntfId: int32(0),
208 Classifier: &openolt.Classifier{
209 EthType: uint32(layers.EthernetTypeEAPOL),
210 OVid: 4091,
211 },
212 Action: &openolt.Action{},
213 Priority: int32(100),
214 PortNo: uint32(onu.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
215 }
216
217 msg := OnuFlowUpdateMessage{
218 PonPortID: 1,
219 OnuID: 1,
220 Flow: &flow,
221 }
222
223 onu.handleFlowUpdate(msg)
224 assert.Equal(t, onu.InternalState.Current(), "gem_port_added")
225}
226
227// validates that when an ONU receives an EAPOL flow for UNI 0
228// it transition to auth_started state
229func Test_HandleFlowUpdateEapolFromEnabled(t *testing.T) {
230
231 onu := createMockOnu(1, 1, 900, 900)
232
233 onu.InternalState = fsm.NewFSM(
234 "enabled",
235 fsm.Events{
236 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
237 },
238 fsm.Callbacks{},
239 )
240
241 flow := openolt.Flow{
242 AccessIntfId: int32(onu.PonPortID),
243 OnuId: int32(onu.ID),
244 UniId: int32(0),
245 FlowId: uint32(onu.ID),
246 FlowType: "downstream",
247 AllocId: int32(0),
248 NetworkIntfId: int32(0),
249 Classifier: &openolt.Classifier{
250 EthType: uint32(layers.EthernetTypeEAPOL),
251 OVid: 4091,
252 },
253 Action: &openolt.Action{},
254 Priority: int32(100),
255 PortNo: uint32(onu.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
256 }
257
258 msg := OnuFlowUpdateMessage{
259 PonPortID: 1,
260 OnuID: 1,
261 Flow: &flow,
262 }
263
264 onu.handleFlowUpdate(msg)
265 assert.Equal(t, onu.InternalState.Current(), "eapol_flow_received")
266}
267
268// validates that when an ONU receives an EAPOL flow for UNI that is not 0
269// no action is taken
270func Test_HandleFlowUpdateEapolFromEnabledIgnore(t *testing.T) {
271
272 onu := createMockOnu(1, 1, 900, 900)
273
274 onu.InternalState = fsm.NewFSM(
275 "enabled",
276 fsm.Events{
277 {Name: "receive_eapol_flow", Src: []string{"enabled", "gem_port_added"}, Dst: "eapol_flow_received"},
278 },
279 fsm.Callbacks{},
280 )
281
282 flow := openolt.Flow{
283 AccessIntfId: int32(onu.PonPortID),
284 OnuId: int32(onu.ID),
285 UniId: int32(1),
286 FlowId: uint32(onu.ID),
287 FlowType: "downstream",
288 AllocId: int32(0),
289 NetworkIntfId: int32(0),
290 Classifier: &openolt.Classifier{
291 EthType: uint32(layers.EthernetTypeEAPOL),
292 OVid: 4091,
293 },
294 Action: &openolt.Action{},
295 Priority: int32(100),
296 PortNo: uint32(onu.ID), // NOTE we are using this to map an incoming packetIndication to an ONU
297 }
298
299 msg := OnuFlowUpdateMessage{
300 PonPortID: 1,
301 OnuID: 1,
302 Flow: &flow,
303 }
304
305 onu.handleFlowUpdate(msg)
306 assert.Equal(t, onu.InternalState.Current(), "enabled")
307}
308
309// TODO add tests for DHCP flow