| /* |
| * Copyright 2018-present Open Networking Foundation |
| |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package devices |
| |
| import ( |
| "github.com/opencord/voltha-protos/v2/go/openolt" |
| "gotest.tools/assert" |
| "net" |
| "testing" |
| ) |
| |
| func createMockOlt(numPon int, numOnu int) OltDevice { |
| olt := OltDevice{ |
| ID: 0, |
| } |
| |
| for i := 0; i < numPon; i++ { |
| pon := PonPort{ |
| ID: uint32(i), |
| } |
| |
| for j := 0; j < numOnu; j++ { |
| onuId := uint32(i + j) |
| onu := Onu{ |
| ID: onuId, |
| PonPort: &pon, |
| PonPortID: pon.ID, |
| HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(onuId)}, |
| } |
| onu.SerialNumber = onu.NewSN(olt.ID, pon.ID, onu.ID) |
| pon.Onus = append(pon.Onus, &onu) |
| } |
| olt.Pons = append(olt.Pons, &pon) |
| } |
| return olt |
| } |
| |
| func Test_Olt_FindOnuBySn_Success(t *testing.T) { |
| |
| numPon := 4 |
| numOnu := 4 |
| |
| olt := createMockOlt(numPon, numOnu) |
| |
| onu, err := olt.FindOnuBySn("BBSM00000303") |
| |
| assert.Equal(t, err, nil) |
| assert.Equal(t, onu.Sn(), "BBSM00000303") |
| assert.Equal(t, onu.ID, uint32(3)) |
| assert.Equal(t, onu.PonPortID, uint32(3)) |
| } |
| |
| func Test_Olt_FindOnuBySn_Error(t *testing.T) { |
| |
| numPon := 1 |
| numOnu := 4 |
| |
| olt := createMockOlt(numPon, numOnu) |
| |
| _, err := olt.FindOnuBySn("BBSM00000303") |
| |
| assert.Equal(t, err.Error(), "cannot-find-onu-by-serial-number-BBSM00000303") |
| } |
| |
| func Test_Olt_FindOnuByMacAddress_Success(t *testing.T) { |
| |
| numPon := 4 |
| numOnu := 4 |
| |
| olt := createMockOlt(numPon, numOnu) |
| |
| mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)} |
| |
| onu, err := olt.FindOnuByMacAddress(mac) |
| |
| assert.Equal(t, err, nil) |
| assert.Equal(t, onu.Sn(), "BBSM00000303") |
| assert.Equal(t, onu.ID, uint32(3)) |
| assert.Equal(t, onu.PonPortID, uint32(3)) |
| } |
| |
| func Test_Olt_FindOnuByMacAddress_Error(t *testing.T) { |
| |
| numPon := 1 |
| numOnu := 4 |
| |
| olt := createMockOlt(numPon, numOnu) |
| |
| mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)} |
| |
| _, err := olt.FindOnuByMacAddress(mac) |
| |
| assert.Equal(t, err.Error(), "cannot-find-onu-by-mac-address-2e:60:70:13:03:03") |
| } |
| |
| func Test_Olt_GetOnuByFlowId(t *testing.T) { |
| numPon := 4 |
| numOnu := 4 |
| |
| olt := createMockOlt(numPon, numOnu) |
| |
| // Add the flows to onus (to be found) |
| onu1, _ := olt.FindOnuBySn("BBSM00000303") |
| flow1 := openolt.Flow{ |
| FlowId: 64, |
| Classifier: &openolt.Classifier{}, |
| } |
| msg1 := OnuFlowUpdateMessage{ |
| OnuID: onu1.ID, |
| PonPortID: onu1.PonPortID, |
| Flow: &flow1, |
| } |
| onu1.handleFlowAdd(msg1) |
| |
| onu2, _ := olt.FindOnuBySn("BBSM00000103") |
| flow2 := openolt.Flow{ |
| FlowId: 72, |
| Classifier: &openolt.Classifier{}, |
| } |
| msg2 := OnuFlowUpdateMessage{ |
| OnuID: onu2.ID, |
| PonPortID: onu2.PonPortID, |
| Flow: &flow2, |
| } |
| onu2.handleFlowAdd(msg2) |
| |
| |
| |
| found, err := olt.GetOnuByFlowId(flow1.FlowId) |
| |
| assert.Equal(t, err, nil) |
| assert.Equal(t, found.Sn(), onu1.Sn()) |
| } |