blob: 154942b6106f12bee4bda40c20bab7c459de0d57 [file] [log] [blame]
Matteo Scandolo10f965c2019-09-24 10:40:46 -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 Scandolof9d43412021-01-12 11:11:34 -080020 "github.com/opencord/bbsim/internal/bbsim/types"
Matteo Scandolo4a036262020-08-17 15:56:13 -070021 "github.com/opencord/bbsim/internal/common"
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070022 "net"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070023 "testing"
Shrey Baid688b4242020-07-10 20:40:10 +053024
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070025 "github.com/opencord/voltha-protos/v4/go/openolt"
Shrey Baid688b4242020-07-10 20:40:10 +053026 "gotest.tools/assert"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070027)
28
Matteo Scandolo4a036262020-08-17 15:56:13 -070029func createMockOlt(numPon int, numOnu int, services []ServiceIf) *OltDevice {
Shrey Baid688b4242020-07-10 20:40:10 +053030 olt := &OltDevice{
Matteo Scandolo10f965c2019-09-24 10:40:46 -070031 ID: 0,
32 }
33
34 for i := 0; i < numPon; i++ {
35 pon := PonPort{
36 ID: uint32(i),
37 }
38
39 for j := 0; j < numOnu; j++ {
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070040 onuId := uint32(i + j)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070041 onu := Onu{
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070042 ID: onuId,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070043 PonPort: &pon,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070044 PonPortID: pon.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -070045 }
Matteo Scandolo4a036262020-08-17 15:56:13 -070046
47 for k, s := range services {
48 service := s.(*Service)
49 service.HwAddress = net.HardwareAddr{0x2e, 0x60, byte(olt.ID), byte(pon.ID), byte(onuId), byte(k)}
50 service.Onu = &onu
51 onu.Services = append(onu.Services, service)
52 }
53
Matteo Scandolo10f965c2019-09-24 10:40:46 -070054 onu.SerialNumber = onu.NewSN(olt.ID, pon.ID, onu.ID)
Matteo Scandolo27428702019-10-11 16:21:16 -070055 pon.Onus = append(pon.Onus, &onu)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070056 }
Matteo Scandolo27428702019-10-11 16:21:16 -070057 olt.Pons = append(olt.Pons, &pon)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070058 }
59 return olt
60}
61
Matteo Scandolo4a036262020-08-17 15:56:13 -070062// check the creation of an OLT with a single Service
63func TestCreateOLT(t *testing.T) {
64
65 common.Services = []common.ServiceYaml{
66 {Name: "hsia", CTag: 900, CTagAllocation: common.TagAllocationUnique.String(), STag: 900, STagAllocation: common.TagAllocationShared.String(), NeedsEapol: true, NeedsDchp: true, NeedsIgmp: true},
67 }
68
69 common.Config = &common.GlobalConfig{
70 Olt: common.OltConfig{
71 ID: 1,
72 PonPorts: 2,
73 OnusPonPort: 2,
74 },
75 }
76
77 olt := CreateOLT(*common.Config, common.Services, true)
78
79 assert.Equal(t, len(olt.Pons), int(common.Config.Olt.PonPorts))
80
81 // count the ONUs
82 onus := 0
83 for _, p := range olt.Pons {
84 onus = onus + len(p.Onus)
85 }
86
87 assert.Equal(t, onus, int(common.Config.Olt.PonPorts*common.Config.Olt.OnusPonPort))
88
89 // count the services
90 services := 0
91 for _, p := range olt.Pons {
92 for _, o := range p.Onus {
93 services = services + len(o.Services)
94 }
95 }
96
97 assert.Equal(t, services, int(common.Config.Olt.PonPorts)*int(common.Config.Olt.OnusPonPort)*len(common.Services))
98
99 s1 := olt.Pons[0].Onus[0].Services[0].(*Service)
100
101 assert.Equal(t, s1.Name, "hsia")
102 assert.Equal(t, s1.CTag, 900)
103 assert.Equal(t, s1.STag, 900)
104 assert.Equal(t, s1.HwAddress.String(), "2e:60:01:00:01:00")
105 assert.Equal(t, olt.Pons[0].Onus[0].ID, uint32(1))
106
107 s2 := olt.Pons[0].Onus[1].Services[0].(*Service)
108 assert.Equal(t, s2.CTag, 901)
109 assert.Equal(t, s2.STag, 900)
110 assert.Equal(t, s2.HwAddress.String(), "2e:60:01:00:02:00")
111 assert.Equal(t, olt.Pons[0].Onus[1].ID, uint32(2))
112
113 s3 := olt.Pons[1].Onus[0].Services[0].(*Service)
114 assert.Equal(t, s3.CTag, 902)
115 assert.Equal(t, s3.STag, 900)
116 assert.Equal(t, s3.HwAddress.String(), "2e:60:01:01:01:00")
117 assert.Equal(t, olt.Pons[1].Onus[0].ID, uint32(1))
118
119 s4 := olt.Pons[1].Onus[1].Services[0].(*Service)
120 assert.Equal(t, s4.CTag, 903)
121 assert.Equal(t, s4.STag, 900)
122 assert.Equal(t, s4.HwAddress.String(), "2e:60:01:01:02:00")
123 assert.Equal(t, olt.Pons[1].Onus[1].ID, uint32(2))
124}
125
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700126func Test_Olt_FindOnuBySn_Success(t *testing.T) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700127
128 numPon := 4
129 numOnu := 4
130
Matteo Scandolo4a036262020-08-17 15:56:13 -0700131 olt := createMockOlt(numPon, numOnu, []ServiceIf{})
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700132
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700133 onu, err := olt.FindOnuBySn("BBSM00000303")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700134
135 assert.Equal(t, err, nil)
136 assert.Equal(t, onu.Sn(), "BBSM00000303")
137 assert.Equal(t, onu.ID, uint32(3))
138 assert.Equal(t, onu.PonPortID, uint32(3))
139}
140
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700141func Test_Olt_FindOnuBySn_Error(t *testing.T) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700142
143 numPon := 1
144 numOnu := 4
145
Matteo Scandolo4a036262020-08-17 15:56:13 -0700146 olt := createMockOlt(numPon, numOnu, []ServiceIf{})
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700147
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700148 _, err := olt.FindOnuBySn("BBSM00000303")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700149
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700150 assert.Equal(t, err.Error(), "cannot-find-onu-by-serial-number-BBSM00000303")
151}
152
153func Test_Olt_FindOnuByMacAddress_Success(t *testing.T) {
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700154 numPon := 4
155 numOnu := 4
156
Matteo Scandolo4a036262020-08-17 15:56:13 -0700157 services := []ServiceIf{
158 &Service{Name: "hsia"},
159 &Service{Name: "voip"},
160 &Service{Name: "vod"},
161 }
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700162
Matteo Scandolo4a036262020-08-17 15:56:13 -0700163 olt := createMockOlt(numPon, numOnu, services)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700164
Matteo Scandolo4a036262020-08-17 15:56:13 -0700165 mac := net.HardwareAddr{0x2e, 0x60, byte(olt.ID), byte(3), byte(6), byte(1)}
166 s, err := olt.FindServiceByMacAddress(mac)
167
168 assert.NilError(t, err)
169
170 service := s.(*Service)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700171
172 assert.Equal(t, err, nil)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700173 assert.Equal(t, service.Onu.Sn(), "BBSM00000306")
174 assert.Equal(t, service.Onu.ID, uint32(6))
175 assert.Equal(t, service.Onu.PonPortID, uint32(3))
176
177 assert.Equal(t, service.Name, "voip")
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700178}
179
180func Test_Olt_FindOnuByMacAddress_Error(t *testing.T) {
181
182 numPon := 1
183 numOnu := 4
184
Matteo Scandolo4a036262020-08-17 15:56:13 -0700185 olt := createMockOlt(numPon, numOnu, []ServiceIf{})
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700186
187 mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)}
188
Matteo Scandolo4a036262020-08-17 15:56:13 -0700189 _, err := olt.FindServiceByMacAddress(mac)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700190
Matteo Scandolo4a036262020-08-17 15:56:13 -0700191 assert.Equal(t, err.Error(), "cannot-find-service-by-mac-address-2e:60:70:13:03:03")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700192}
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700193
194func Test_Olt_GetOnuByFlowId(t *testing.T) {
195 numPon := 4
196 numOnu := 4
197
Matteo Scandolo4a036262020-08-17 15:56:13 -0700198 olt := createMockOlt(numPon, numOnu, []ServiceIf{})
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700199
200 // Add the flows to onus (to be found)
201 onu1, _ := olt.FindOnuBySn("BBSM00000303")
202 flow1 := openolt.Flow{
Shrey Baid688b4242020-07-10 20:40:10 +0530203 FlowId: 64,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700204 Classifier: &openolt.Classifier{},
205 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800206 msg1 := types.OnuFlowUpdateMessage{
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700207 OnuID: onu1.ID,
208 PonPortID: onu1.PonPortID,
209 Flow: &flow1,
210 }
211 onu1.handleFlowAdd(msg1)
212
213 onu2, _ := olt.FindOnuBySn("BBSM00000103")
214 flow2 := openolt.Flow{
Shrey Baid688b4242020-07-10 20:40:10 +0530215 FlowId: 72,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700216 Classifier: &openolt.Classifier{},
217 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800218 msg2 := types.OnuFlowUpdateMessage{
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700219 OnuID: onu2.ID,
220 PonPortID: onu2.PonPortID,
221 Flow: &flow2,
222 }
223 onu2.handleFlowAdd(msg2)
224
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700225 found, err := olt.GetOnuByFlowId(flow1.FlowId)
226
227 assert.Equal(t, err, nil)
228 assert.Equal(t, found.Sn(), onu1.Sn())
Shrey Baid688b4242020-07-10 20:40:10 +0530229}