blob: 47d8b998c703ff7b3469268088d04dd4f61c1c91 [file] [log] [blame]
khenaidoo89b0e942018-10-21 21:11:33 -04001/*
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 */
16package graph
17
18import (
19 "errors"
20 "fmt"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080021 "github.com/opencord/voltha-protos/v3/go/openflow_13"
22 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoo89b0e942018-10-21 21:11:33 -040023 "github.com/stretchr/testify/assert"
khenaidoocfe03b92019-06-03 20:06:31 -040024 "math/rand"
khenaidoof934a1e2019-05-01 21:44:09 -040025 "strconv"
26 "strings"
khenaidoo910204f2019-04-08 17:56:40 -040027 "sync"
khenaidoo89b0e942018-10-21 21:11:33 -040028 "testing"
29 "time"
30)
31
khenaidoof934a1e2019-05-01 21:44:09 -040032var (
33 ld voltha.LogicalDevice
34 olt voltha.Device
35 onus map[int][]voltha.Device
npujar1d86a522019-11-14 17:11:16 +053036 logicalDeviceID string
37 oltDeviceID string
khenaidoof934a1e2019-05-01 21:44:09 -040038 numCalled int
39 lock sync.RWMutex
khenaidoo89b0e942018-10-21 21:11:33 -040040)
41
42func init() {
npujar1d86a522019-11-14 17:11:16 +053043 logicalDeviceID = "ld"
44 oltDeviceID = "olt"
khenaidoo910204f2019-04-08 17:56:40 -040045 lock = sync.RWMutex{}
khenaidoof934a1e2019-05-01 21:44:09 -040046}
khenaidoo89b0e942018-10-21 21:11:33 -040047
khenaidoof934a1e2019-05-01 21:44:09 -040048func setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu int) {
49 // Create the OLT and add the NNI ports
npujar1d86a522019-11-14 17:11:16 +053050 olt = voltha.Device{Id: oltDeviceID, ParentId: logicalDeviceID}
khenaidoo89b0e942018-10-21 21:11:33 -040051 olt.Ports = make([]*voltha.Port, 0)
khenaidoof934a1e2019-05-01 21:44:09 -040052 for nniPort := 1; nniPort < numNNIPort+1; nniPort++ {
npujar1d86a522019-11-14 17:11:16 +053053 p := voltha.Port{PortNo: uint32(nniPort), DeviceId: oltDeviceID, Type: voltha.Port_ETHERNET_NNI}
khenaidoof934a1e2019-05-01 21:44:09 -040054 olt.Ports = append(olt.Ports, &p)
55 }
khenaidoo89b0e942018-10-21 21:11:33 -040056
khenaidoof934a1e2019-05-01 21:44:09 -040057 // Create the ONUs and associate them with the OLT
58 onus = make(map[int][]voltha.Device)
59 for pPortNo := numNNIPort + 1; pPortNo < numPonPortOnOlt+numNNIPort+1; pPortNo++ {
60 onusOnPon := make([]voltha.Device, 0)
61 var onu voltha.Device
62 oltPeerPort := uint32(pPortNo)
npujar1d86a522019-11-14 17:11:16 +053063 oltPonPort := voltha.Port{PortNo: uint32(pPortNo), DeviceId: oltDeviceID, Type: voltha.Port_PON_OLT}
khenaidoof934a1e2019-05-01 21:44:09 -040064 oltPonPort.Peers = make([]*voltha.Port_PeerPort, 0)
65 for i := 0; i < numOnuPerOltPonPort; i++ {
66 id := fmt.Sprintf("%d-onu-%d", pPortNo, i)
npujar1d86a522019-11-14 17:11:16 +053067 onu = voltha.Device{Id: id, ParentId: oltDeviceID, ParentPortNo: uint32(pPortNo)}
khenaidoof934a1e2019-05-01 21:44:09 -040068 ponPort := voltha.Port{PortNo: 1, DeviceId: onu.Id, Type: voltha.Port_PON_ONU}
69 ponPort.Peers = make([]*voltha.Port_PeerPort, 0)
npujar1d86a522019-11-14 17:11:16 +053070 peerPort := voltha.Port_PeerPort{DeviceId: oltDeviceID, PortNo: oltPeerPort}
khenaidoof934a1e2019-05-01 21:44:09 -040071 ponPort.Peers = append(ponPort.Peers, &peerPort)
72 onu.Ports = make([]*voltha.Port, 0)
73 onu.Ports = append(onu.Ports, &ponPort)
74 for j := 2; j < numUniPerOnu+2; j++ {
75 uniPort := voltha.Port{PortNo: uint32(j), DeviceId: onu.Id, Type: voltha.Port_ETHERNET_UNI}
76 onu.Ports = append(onu.Ports, &uniPort)
77 }
78 onusOnPon = append(onusOnPon, onu)
79 oltPeerPort := voltha.Port_PeerPort{DeviceId: onu.Id, PortNo: 1}
80 oltPonPort.Peers = append(oltPonPort.Peers, &oltPeerPort)
81 }
82 onus[pPortNo] = onusOnPon
83 olt.Ports = append(olt.Ports, &oltPonPort)
84 }
85
86 // Create the logical device
npujar1d86a522019-11-14 17:11:16 +053087 ld = voltha.LogicalDevice{Id: logicalDeviceID}
khenaidoo89b0e942018-10-21 21:11:33 -040088 ld.Ports = make([]*voltha.LogicalPort, 0)
89 ofpPortNo := 1
khenaidoof934a1e2019-05-01 21:44:09 -040090 var id string
91 //Add olt NNI ports
khenaidoo89b0e942018-10-21 21:11:33 -040092 for i, port := range olt.Ports {
93 if port.Type == voltha.Port_ETHERNET_NNI {
94 id = fmt.Sprintf("nni-%d", i)
95 lp := voltha.LogicalPort{Id: id, DeviceId: olt.Id, DevicePortNo: port.PortNo, OfpPort: &openflow_13.OfpPort{PortNo: uint32(ofpPortNo)}, RootPort: true}
96 ld.Ports = append(ld.Ports, &lp)
97 ofpPortNo = ofpPortNo + 1
98 }
99 }
khenaidoof934a1e2019-05-01 21:44:09 -0400100 //Add onu UNI ports
101 for _, onusOnPort := range onus {
102 for _, onu := range onusOnPort {
103 for j, port := range onu.Ports {
104 if port.Type == voltha.Port_ETHERNET_UNI {
105 id = fmt.Sprintf("%s:uni-%d", onu.Id, j)
106 lp := voltha.LogicalPort{Id: id, DeviceId: onu.Id, DevicePortNo: port.PortNo, OfpPort: &openflow_13.OfpPort{PortNo: uint32(ofpPortNo)}, RootPort: false}
107 ld.Ports = append(ld.Ports, &lp)
108 ofpPortNo = ofpPortNo + 1
109 }
khenaidoo89b0e942018-10-21 21:11:33 -0400110 }
111 }
112 }
113}
114
115func GetDeviceHelper(id string) (*voltha.Device, error) {
khenaidoo910204f2019-04-08 17:56:40 -0400116 lock.Lock()
npujar1d86a522019-11-14 17:11:16 +0530117 numCalled++
khenaidoo910204f2019-04-08 17:56:40 -0400118 lock.Unlock()
khenaidoo89b0e942018-10-21 21:11:33 -0400119 if id == "olt" {
120 return &olt, nil
121 }
khenaidoof934a1e2019-05-01 21:44:09 -0400122 // Extract the olt pon port from the id ("<ponport>-onu-<onu number>")
123 res := strings.Split(id, "-")
124 if len(res) == 3 {
125 if ponPort, err := strconv.Atoi(res[0]); err == nil {
126 for _, onu := range onus[ponPort] {
127 if onu.Id == id {
128 return &onu, nil
129 }
130 }
131
khenaidoo89b0e942018-10-21 21:11:33 -0400132 }
133 }
134 return nil, errors.New("Not-found")
135}
136
khenaidoo910204f2019-04-08 17:56:40 -0400137func TestGetRoutesOneShot(t *testing.T) {
khenaidoof934a1e2019-05-01 21:44:09 -0400138 numNNIPort := 1
139 numPonPortOnOlt := 1
140 numOnuPerOltPonPort := 64
141 numUniPerOnu := 1
khenaidoo89b0e942018-10-21 21:11:33 -0400142
khenaidoof934a1e2019-05-01 21:44:09 -0400143 setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu)
khenaidoo89b0e942018-10-21 21:11:33 -0400144 getDevice := GetDeviceHelper
145
khenaidoof934a1e2019-05-01 21:44:09 -0400146 fmt.Println(fmt.Sprintf("Test: Computing all routes. LogicalPorts:%d, NNI:%d, Pon/OLT:%d, ONU/Pon:%d, Uni/Onu:%d", len(ld.Ports), numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu))
khenaidoo89b0e942018-10-21 21:11:33 -0400147 // Create a device graph and computes Routes
148 start := time.Now()
npujar1d86a522019-11-14 17:11:16 +0530149 dg := NewDeviceGraph(logicalDeviceID, getDevice)
khenaidoo89b0e942018-10-21 21:11:33 -0400150 dg.ComputeRoutes(ld.Ports)
khenaidoo89b0e942018-10-21 21:11:33 -0400151 assert.NotNil(t, dg.GGraph)
khenaidoof934a1e2019-05-01 21:44:09 -0400152 fmt.Println(fmt.Sprintf("Total Time:%dms Total Routes:%d", time.Since(start)/time.Millisecond, len(dg.Routes)))
153 assert.EqualValues(t, (2 * numNNIPort * numPonPortOnOlt * numOnuPerOltPonPort * numUniPerOnu), len(dg.Routes))
khenaidoo910204f2019-04-08 17:56:40 -0400154}
khenaidoo89b0e942018-10-21 21:11:33 -0400155
khenaidoof934a1e2019-05-01 21:44:09 -0400156func TestGetRoutesPerPort(t *testing.T) {
157 numNNIPort := 1
158 numPonPortOnOlt := 1
159 numOnuPerOltPonPort := 64
160 numUniPerOnu := 1
khenaidoo910204f2019-04-08 17:56:40 -0400161
khenaidoof934a1e2019-05-01 21:44:09 -0400162 setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu)
khenaidoo910204f2019-04-08 17:56:40 -0400163 getDevice := GetDeviceHelper
164
khenaidoof934a1e2019-05-01 21:44:09 -0400165 fmt.Println(fmt.Sprintf("Test: Compute routes per port. LogicalPorts:%d, NNI:%d, Pon/OLT:%d, ONU/Pon:%d, Uni/Onu:%d", len(ld.Ports), numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu))
166
khenaidoo910204f2019-04-08 17:56:40 -0400167 // Create a device graph and computes Routes
168 start := time.Now()
khenaidoof934a1e2019-05-01 21:44:09 -0400169 var pt time.Time
npujar1d86a522019-11-14 17:11:16 +0530170 dg := NewDeviceGraph(logicalDeviceID, getDevice)
khenaidoof934a1e2019-05-01 21:44:09 -0400171 for k, lp := range ld.Ports {
172 if k == len(ld.Ports)-1 {
173 pt = time.Now()
174 }
khenaidoo910204f2019-04-08 17:56:40 -0400175 dg.AddPort(lp)
176 }
khenaidoo910204f2019-04-08 17:56:40 -0400177 assert.NotNil(t, dg.GGraph)
khenaidoof934a1e2019-05-01 21:44:09 -0400178 fmt.Println(fmt.Sprintf("Total Time:%dms. Total Routes:%d. LastPort_Time:%dms", time.Since(start)/time.Millisecond, len(dg.Routes), time.Since(pt)/time.Millisecond))
179 assert.EqualValues(t, (2 * numNNIPort * numPonPortOnOlt * numOnuPerOltPonPort * numUniPerOnu), len(dg.Routes))
180}
181
182func TestGetRoutesPerPortMultipleUNIs(t *testing.T) {
183 numNNIPort := 1
184 numPonPortOnOlt := 1
185 numOnuPerOltPonPort := 64
186 numUniPerOnu := 5
187
188 setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu)
189 getDevice := GetDeviceHelper
190
191 fmt.Println(fmt.Sprintf("Test: Compute routes per port - multiple UNIs. LogicalPorts:%d, NNI:%d, Pon/OLT:%d, ONU/Pon:%d, Uni/Onu:%d", len(ld.Ports), numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu))
192
193 // Create a device graph and computes Routes
194 start := time.Now()
195 var pt time.Time
npujar1d86a522019-11-14 17:11:16 +0530196 dg := NewDeviceGraph(logicalDeviceID, getDevice)
khenaidoof934a1e2019-05-01 21:44:09 -0400197 for k, lp := range ld.Ports {
198 if k == len(ld.Ports)-1 {
199 pt = time.Now()
200 }
201 dg.AddPort(lp)
202 }
203 assert.NotNil(t, dg.GGraph)
204 fmt.Println(fmt.Sprintf("Total Time:%dms. Total Routes:%d. LastPort_Time:%dms", time.Since(start)/time.Millisecond, len(dg.Routes), time.Since(pt)/time.Millisecond))
205 assert.EqualValues(t, (2 * numNNIPort * numPonPortOnOlt * numOnuPerOltPonPort * numUniPerOnu), len(dg.Routes))
206}
207
208func TestGetRoutesPerPortNoUNI(t *testing.T) {
209 numNNIPort := 1
210 numPonPortOnOlt := 1
211 numOnuPerOltPonPort := 1
212 numUniPerOnu := 0
213
214 setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu)
215 getDevice := GetDeviceHelper
216 assert.EqualValues(t, 1, len(ld.Ports))
217
218 fmt.Println(fmt.Sprintf("Test: Compute routes per port - no UNI. LogicalPorts:%d, NNI:%d, Pon/OLT:%d, ONU/Pon:%d, Uni/Onu:%d", len(ld.Ports), numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu))
219
220 // Create a device graph and computes Routes
221 start := time.Now()
222 var pt time.Time
npujar1d86a522019-11-14 17:11:16 +0530223 dg := NewDeviceGraph(logicalDeviceID, getDevice)
khenaidoof934a1e2019-05-01 21:44:09 -0400224 for k, lp := range ld.Ports {
225 if k == len(ld.Ports)-1 {
226 pt = time.Now()
227 }
228 dg.AddPort(lp)
229 }
230 assert.NotNil(t, dg.GGraph)
231 fmt.Println(fmt.Sprintf("Total Time:%dms. Total Routes:%d. LastPort_Time:%dms", time.Since(start)/time.Millisecond, len(dg.Routes), time.Since(pt)/time.Millisecond))
232 assert.EqualValues(t, 0, len(dg.Routes))
233}
234
235func TestGetRoutesPerPortNoONU(t *testing.T) {
236 numNNIPort := 1
237 numPonPortOnOlt := 1
238 numOnuPerOltPonPort := 0
239 numUniPerOnu := 0
240
241 setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu)
242 getDevice := GetDeviceHelper
243 assert.EqualValues(t, 1, len(ld.Ports))
244
245 fmt.Println(fmt.Sprintf("Test: Compute routes per port - no ONU. LogicalPorts:%d, NNI:%d, Pon/OLT:%d, ONU/Pon:%d, Uni/Onu:%d", len(ld.Ports), numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu))
246
247 // Create a device graph and computes Routes
248 start := time.Now()
249 var pt time.Time
npujar1d86a522019-11-14 17:11:16 +0530250 dg := NewDeviceGraph(logicalDeviceID, getDevice)
khenaidoof934a1e2019-05-01 21:44:09 -0400251 for k, lp := range ld.Ports {
252 if k == len(ld.Ports)-1 {
253 pt = time.Now()
254 }
255 dg.AddPort(lp)
256 }
257 assert.NotNil(t, dg.GGraph)
258 fmt.Println(fmt.Sprintf("Total Time:%dms. Total Routes:%d. LastPort_Time:%dms", time.Since(start)/time.Millisecond, len(dg.Routes), time.Since(pt)/time.Millisecond))
259 assert.EqualValues(t, 0, len(dg.Routes))
260}
261
262func TestGetRoutesPerPortNoNNI(t *testing.T) {
263 numNNIPort := 0
264 numPonPortOnOlt := 1
265 numOnuPerOltPonPort := 1
266 numUniPerOnu := 1
267
268 setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu)
269 getDevice := GetDeviceHelper
270 assert.EqualValues(t, 1, len(ld.Ports))
271
272 fmt.Println(fmt.Sprintf("Test: Compute routes per port - no NNI. LogicalPorts:%d, NNI:%d, Pon/OLT:%d, ONU/Pon:%d, Uni/Onu:%d", len(ld.Ports), numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu))
273
274 // Create a device graph and computes Routes
275 start := time.Now()
276 var pt time.Time
npujar1d86a522019-11-14 17:11:16 +0530277 dg := NewDeviceGraph(logicalDeviceID, getDevice)
khenaidoof934a1e2019-05-01 21:44:09 -0400278 for k, lp := range ld.Ports {
279 if k == len(ld.Ports)-1 {
280 pt = time.Now()
281 }
282 dg.AddPort(lp)
283 }
284 assert.NotNil(t, dg.GGraph)
285 fmt.Println(fmt.Sprintf("Total Time:%dms. Total Routes:%d. LastPort_Time:%dms", time.Since(start)/time.Millisecond, len(dg.Routes), time.Since(pt)/time.Millisecond))
286 assert.EqualValues(t, 0, len(dg.Routes))
khenaidoo89b0e942018-10-21 21:11:33 -0400287}
khenaidoocfe03b92019-06-03 20:06:31 -0400288
289func TestReverseRoute(t *testing.T) {
290 // Test the typical use case - 2 hops in a route
291 route := make([]RouteHop, 2)
292 route[0].DeviceID = "d1"
293 route[0].Ingress = 1
294 route[0].Egress = 2
295 route[1].DeviceID = "d2"
296 route[1].Ingress = 10
297 route[1].Egress = 15
298
299 reverseRoute := getReverseRoute(route)
300 assert.Equal(t, 2, len(reverseRoute))
301 assert.Equal(t, "d2", reverseRoute[0].DeviceID)
302 assert.Equal(t, "d1", reverseRoute[1].DeviceID)
303 assert.Equal(t, uint32(15), reverseRoute[0].Ingress)
304 assert.Equal(t, uint32(10), reverseRoute[0].Egress)
305 assert.Equal(t, uint32(2), reverseRoute[1].Ingress)
306 assert.Equal(t, uint32(1), reverseRoute[1].Egress)
307
308 fmt.Println("Reverse of two hops successful.")
309
310 //Test 3 hops in a route
311 route = make([]RouteHop, 3)
312 route[0].DeviceID = "d1"
313 route[0].Ingress = 1
314 route[0].Egress = 2
315 route[1].DeviceID = "d2"
316 route[1].Ingress = 10
317 route[1].Egress = 15
318 route[2].DeviceID = "d3"
319 route[2].Ingress = 20
320 route[2].Egress = 25
321 reverseRoute = getReverseRoute(route)
322 assert.Equal(t, 3, len(reverseRoute))
323 assert.Equal(t, "d3", reverseRoute[0].DeviceID)
324 assert.Equal(t, "d2", reverseRoute[1].DeviceID)
325 assert.Equal(t, "d1", reverseRoute[2].DeviceID)
326 assert.Equal(t, uint32(25), reverseRoute[0].Ingress)
327 assert.Equal(t, uint32(20), reverseRoute[0].Egress)
328 assert.Equal(t, uint32(15), reverseRoute[1].Ingress)
329 assert.Equal(t, uint32(10), reverseRoute[1].Egress)
330 assert.Equal(t, uint32(2), reverseRoute[2].Ingress)
331 assert.Equal(t, uint32(1), reverseRoute[2].Egress)
332
333 fmt.Println("Reverse of three hops successful.")
334
335 // Test any number of hops in a route
336 numRoutes := rand.Intn(100)
337 route = make([]RouteHop, numRoutes)
338 deviceIds := make([]string, numRoutes)
339 ingressNos := make([]uint32, numRoutes)
340 egressNos := make([]uint32, numRoutes)
341 for i := 0; i < numRoutes; i++ {
342 deviceIds[i] = fmt.Sprintf("d-%d", i)
343 ingressNos[i] = rand.Uint32()
344 egressNos[i] = rand.Uint32()
345 }
346 for i := 0; i < numRoutes; i++ {
347 route[i].DeviceID = deviceIds[i]
348 route[i].Ingress = ingressNos[i]
349 route[i].Egress = egressNos[i]
350 }
351 reverseRoute = getReverseRoute(route)
352 assert.Equal(t, numRoutes, len(reverseRoute))
353 for i, j := 0, numRoutes-1; j >= 0; i, j = i+1, j-1 {
354 assert.Equal(t, deviceIds[j], reverseRoute[i].DeviceID)
355 assert.Equal(t, egressNos[j], reverseRoute[i].Ingress)
356 assert.Equal(t, ingressNos[j], reverseRoute[i].Egress)
357 }
358
359 fmt.Println(fmt.Sprintf("Reverse of %d hops successful.", numRoutes))
360
361 reverseOfReverse := getReverseRoute(reverseRoute)
362 assert.Equal(t, route, reverseOfReverse)
363 fmt.Println("Reverse of reverse successful.")
364}