blob: 0081ffa085ca78898a01e06ab751a2de6481a7fa [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 Scandolo4a036262020-08-17 15:56:13 -070020 "github.com/opencord/bbsim/internal/common"
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070021 "net"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070022 "testing"
Shrey Baid688b4242020-07-10 20:40:10 +053023
Matteo Scandolo4f4ac792020-10-01 16:33:21 -070024 "github.com/opencord/voltha-protos/v4/go/openolt"
Shrey Baid688b4242020-07-10 20:40:10 +053025 "gotest.tools/assert"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070026)
27
Matteo Scandolo4a036262020-08-17 15:56:13 -070028func createMockOlt(numPon int, numOnu int, services []ServiceIf) *OltDevice {
Shrey Baid688b4242020-07-10 20:40:10 +053029 olt := &OltDevice{
Matteo Scandolo10f965c2019-09-24 10:40:46 -070030 ID: 0,
31 }
32
33 for i := 0; i < numPon; i++ {
34 pon := PonPort{
35 ID: uint32(i),
36 }
37
38 for j := 0; j < numOnu; j++ {
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070039 onuId := uint32(i + j)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070040 onu := Onu{
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070041 ID: onuId,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070042 PonPort: &pon,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070043 PonPortID: pon.ID,
Matteo Scandolo10f965c2019-09-24 10:40:46 -070044 }
Matteo Scandolo4a036262020-08-17 15:56:13 -070045
46 for k, s := range services {
47 service := s.(*Service)
48 service.HwAddress = net.HardwareAddr{0x2e, 0x60, byte(olt.ID), byte(pon.ID), byte(onuId), byte(k)}
49 service.Onu = &onu
50 onu.Services = append(onu.Services, service)
51 }
52
Matteo Scandolo10f965c2019-09-24 10:40:46 -070053 onu.SerialNumber = onu.NewSN(olt.ID, pon.ID, onu.ID)
Matteo Scandolo27428702019-10-11 16:21:16 -070054 pon.Onus = append(pon.Onus, &onu)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070055 }
Matteo Scandolo27428702019-10-11 16:21:16 -070056 olt.Pons = append(olt.Pons, &pon)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070057 }
58 return olt
59}
60
Matteo Scandolo4a036262020-08-17 15:56:13 -070061// check the creation of an OLT with a single Service
62func TestCreateOLT(t *testing.T) {
63
64 common.Services = []common.ServiceYaml{
65 {Name: "hsia", CTag: 900, CTagAllocation: common.TagAllocationUnique.String(), STag: 900, STagAllocation: common.TagAllocationShared.String(), NeedsEapol: true, NeedsDchp: true, NeedsIgmp: true},
66 }
67
68 common.Config = &common.GlobalConfig{
69 Olt: common.OltConfig{
70 ID: 1,
71 PonPorts: 2,
72 OnusPonPort: 2,
73 },
74 }
75
76 olt := CreateOLT(*common.Config, common.Services, true)
77
78 assert.Equal(t, len(olt.Pons), int(common.Config.Olt.PonPorts))
79
80 // count the ONUs
81 onus := 0
82 for _, p := range olt.Pons {
83 onus = onus + len(p.Onus)
84 }
85
86 assert.Equal(t, onus, int(common.Config.Olt.PonPorts*common.Config.Olt.OnusPonPort))
87
88 // count the services
89 services := 0
90 for _, p := range olt.Pons {
91 for _, o := range p.Onus {
92 services = services + len(o.Services)
93 }
94 }
95
96 assert.Equal(t, services, int(common.Config.Olt.PonPorts)*int(common.Config.Olt.OnusPonPort)*len(common.Services))
97
98 s1 := olt.Pons[0].Onus[0].Services[0].(*Service)
99
100 assert.Equal(t, s1.Name, "hsia")
101 assert.Equal(t, s1.CTag, 900)
102 assert.Equal(t, s1.STag, 900)
103 assert.Equal(t, s1.HwAddress.String(), "2e:60:01:00:01:00")
104 assert.Equal(t, olt.Pons[0].Onus[0].ID, uint32(1))
105
106 s2 := olt.Pons[0].Onus[1].Services[0].(*Service)
107 assert.Equal(t, s2.CTag, 901)
108 assert.Equal(t, s2.STag, 900)
109 assert.Equal(t, s2.HwAddress.String(), "2e:60:01:00:02:00")
110 assert.Equal(t, olt.Pons[0].Onus[1].ID, uint32(2))
111
112 s3 := olt.Pons[1].Onus[0].Services[0].(*Service)
113 assert.Equal(t, s3.CTag, 902)
114 assert.Equal(t, s3.STag, 900)
115 assert.Equal(t, s3.HwAddress.String(), "2e:60:01:01:01:00")
116 assert.Equal(t, olt.Pons[1].Onus[0].ID, uint32(1))
117
118 s4 := olt.Pons[1].Onus[1].Services[0].(*Service)
119 assert.Equal(t, s4.CTag, 903)
120 assert.Equal(t, s4.STag, 900)
121 assert.Equal(t, s4.HwAddress.String(), "2e:60:01:01:02:00")
122 assert.Equal(t, olt.Pons[1].Onus[1].ID, uint32(2))
123}
124
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700125func Test_Olt_FindOnuBySn_Success(t *testing.T) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700126
127 numPon := 4
128 numOnu := 4
129
Matteo Scandolo4a036262020-08-17 15:56:13 -0700130 olt := createMockOlt(numPon, numOnu, []ServiceIf{})
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700131
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700132 onu, err := olt.FindOnuBySn("BBSM00000303")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700133
134 assert.Equal(t, err, nil)
135 assert.Equal(t, onu.Sn(), "BBSM00000303")
136 assert.Equal(t, onu.ID, uint32(3))
137 assert.Equal(t, onu.PonPortID, uint32(3))
138}
139
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700140func Test_Olt_FindOnuBySn_Error(t *testing.T) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700141
142 numPon := 1
143 numOnu := 4
144
Matteo Scandolo4a036262020-08-17 15:56:13 -0700145 olt := createMockOlt(numPon, numOnu, []ServiceIf{})
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700146
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700147 _, err := olt.FindOnuBySn("BBSM00000303")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700148
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700149 assert.Equal(t, err.Error(), "cannot-find-onu-by-serial-number-BBSM00000303")
150}
151
152func Test_Olt_FindOnuByMacAddress_Success(t *testing.T) {
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700153 numPon := 4
154 numOnu := 4
155
Matteo Scandolo4a036262020-08-17 15:56:13 -0700156 services := []ServiceIf{
157 &Service{Name: "hsia"},
158 &Service{Name: "voip"},
159 &Service{Name: "vod"},
160 }
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700161
Matteo Scandolo4a036262020-08-17 15:56:13 -0700162 olt := createMockOlt(numPon, numOnu, services)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700163
Matteo Scandolo4a036262020-08-17 15:56:13 -0700164 mac := net.HardwareAddr{0x2e, 0x60, byte(olt.ID), byte(3), byte(6), byte(1)}
165 s, err := olt.FindServiceByMacAddress(mac)
166
167 assert.NilError(t, err)
168
169 service := s.(*Service)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700170
171 assert.Equal(t, err, nil)
Matteo Scandolo4a036262020-08-17 15:56:13 -0700172 assert.Equal(t, service.Onu.Sn(), "BBSM00000306")
173 assert.Equal(t, service.Onu.ID, uint32(6))
174 assert.Equal(t, service.Onu.PonPortID, uint32(3))
175
176 assert.Equal(t, service.Name, "voip")
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700177}
178
179func Test_Olt_FindOnuByMacAddress_Error(t *testing.T) {
180
181 numPon := 1
182 numOnu := 4
183
Matteo Scandolo4a036262020-08-17 15:56:13 -0700184 olt := createMockOlt(numPon, numOnu, []ServiceIf{})
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700185
186 mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)}
187
Matteo Scandolo4a036262020-08-17 15:56:13 -0700188 _, err := olt.FindServiceByMacAddress(mac)
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -0700189
Matteo Scandolo4a036262020-08-17 15:56:13 -0700190 assert.Equal(t, err.Error(), "cannot-find-service-by-mac-address-2e:60:70:13:03:03")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700191}
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700192
193func Test_Olt_GetOnuByFlowId(t *testing.T) {
194 numPon := 4
195 numOnu := 4
196
Matteo Scandolo4a036262020-08-17 15:56:13 -0700197 olt := createMockOlt(numPon, numOnu, []ServiceIf{})
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700198
199 // Add the flows to onus (to be found)
200 onu1, _ := olt.FindOnuBySn("BBSM00000303")
201 flow1 := openolt.Flow{
Shrey Baid688b4242020-07-10 20:40:10 +0530202 FlowId: 64,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700203 Classifier: &openolt.Classifier{},
204 }
205 msg1 := OnuFlowUpdateMessage{
206 OnuID: onu1.ID,
207 PonPortID: onu1.PonPortID,
208 Flow: &flow1,
209 }
210 onu1.handleFlowAdd(msg1)
211
212 onu2, _ := olt.FindOnuBySn("BBSM00000103")
213 flow2 := openolt.Flow{
Shrey Baid688b4242020-07-10 20:40:10 +0530214 FlowId: 72,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700215 Classifier: &openolt.Classifier{},
216 }
217 msg2 := OnuFlowUpdateMessage{
218 OnuID: onu2.ID,
219 PonPortID: onu2.PonPortID,
220 Flow: &flow2,
221 }
222 onu2.handleFlowAdd(msg2)
223
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700224 found, err := olt.GetOnuByFlowId(flow1.FlowId)
225
226 assert.Equal(t, err, nil)
227 assert.Equal(t, found.Sn(), onu1.Sn())
Shrey Baid688b4242020-07-10 20:40:10 +0530228}