khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 1 | /* |
| 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 | package graph |
| 17 | |
| 18 | import ( |
| 19 | "errors" |
| 20 | "fmt" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 21 | "github.com/opencord/voltha-protos/go/openflow_13" |
| 22 | "github.com/opencord/voltha-protos/go/voltha" |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 23 | "github.com/stretchr/testify/assert" |
khenaidoo | cfe03b9 | 2019-06-03 20:06:31 -0400 | [diff] [blame] | 24 | "math/rand" |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 25 | "strconv" |
| 26 | "strings" |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 27 | "sync" |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 28 | "testing" |
| 29 | "time" |
| 30 | ) |
| 31 | |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 32 | var ( |
| 33 | ld voltha.LogicalDevice |
| 34 | olt voltha.Device |
| 35 | onus map[int][]voltha.Device |
| 36 | logicalDeviceId string |
| 37 | oltDeviceId string |
| 38 | numCalled int |
| 39 | lock sync.RWMutex |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | func init() { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 43 | logicalDeviceId = "ld" |
| 44 | oltDeviceId = "olt" |
| 45 | lock = sync.RWMutex{} |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 46 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 47 | |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 48 | func setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu int) { |
| 49 | // Create the OLT and add the NNI ports |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 50 | olt = voltha.Device{Id: oltDeviceId, ParentId: logicalDeviceId} |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 51 | olt.Ports = make([]*voltha.Port, 0) |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 52 | for nniPort := 1; nniPort < numNNIPort+1; nniPort++ { |
| 53 | p := voltha.Port{PortNo: uint32(nniPort), DeviceId: oltDeviceId, Type: voltha.Port_ETHERNET_NNI} |
| 54 | olt.Ports = append(olt.Ports, &p) |
| 55 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 56 | |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 57 | // 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) |
| 63 | oltPonPort := voltha.Port{PortNo: uint32(pPortNo), DeviceId: oltDeviceId, Type: voltha.Port_PON_OLT} |
| 64 | oltPonPort.Peers = make([]*voltha.Port_PeerPort, 0) |
| 65 | for i := 0; i < numOnuPerOltPonPort; i++ { |
| 66 | id := fmt.Sprintf("%d-onu-%d", pPortNo, i) |
| 67 | onu = voltha.Device{Id: id, ParentId: oltDeviceId, ParentPortNo: uint32(pPortNo)} |
| 68 | ponPort := voltha.Port{PortNo: 1, DeviceId: onu.Id, Type: voltha.Port_PON_ONU} |
| 69 | ponPort.Peers = make([]*voltha.Port_PeerPort, 0) |
| 70 | peerPort := voltha.Port_PeerPort{DeviceId: oltDeviceId, PortNo: oltPeerPort} |
| 71 | 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 |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 87 | ld = voltha.LogicalDevice{Id: logicalDeviceId} |
| 88 | ld.Ports = make([]*voltha.LogicalPort, 0) |
| 89 | ofpPortNo := 1 |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 90 | var id string |
| 91 | //Add olt NNI ports |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 92 | 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 | } |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 100 | //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 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func GetDeviceHelper(id string) (*voltha.Device, error) { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 116 | lock.Lock() |
| 117 | numCalled += 1 |
| 118 | lock.Unlock() |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 119 | if id == "olt" { |
| 120 | return &olt, nil |
| 121 | } |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 122 | // 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 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | return nil, errors.New("Not-found") |
| 135 | } |
| 136 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 137 | func TestGetRoutesOneShot(t *testing.T) { |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 138 | numNNIPort := 1 |
| 139 | numPonPortOnOlt := 1 |
| 140 | numOnuPerOltPonPort := 64 |
| 141 | numUniPerOnu := 1 |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 142 | |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 143 | setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 144 | getDevice := GetDeviceHelper |
| 145 | |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 146 | 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)) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 147 | // Create a device graph and computes Routes |
| 148 | start := time.Now() |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 149 | dg := NewDeviceGraph(logicalDeviceId, getDevice) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 150 | dg.ComputeRoutes(ld.Ports) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 151 | assert.NotNil(t, dg.GGraph) |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 152 | 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)) |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 154 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 155 | |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 156 | func TestGetRoutesPerPort(t *testing.T) { |
| 157 | numNNIPort := 1 |
| 158 | numPonPortOnOlt := 1 |
| 159 | numOnuPerOltPonPort := 64 |
| 160 | numUniPerOnu := 1 |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 161 | |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 162 | setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu) |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 163 | getDevice := GetDeviceHelper |
| 164 | |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 165 | 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 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 167 | // Create a device graph and computes Routes |
| 168 | start := time.Now() |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 169 | var pt time.Time |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 170 | dg := NewDeviceGraph(logicalDeviceId, getDevice) |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 171 | for k, lp := range ld.Ports { |
| 172 | if k == len(ld.Ports)-1 { |
| 173 | pt = time.Now() |
| 174 | } |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 175 | dg.AddPort(lp) |
| 176 | } |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 177 | assert.NotNil(t, dg.GGraph) |
khenaidoo | f934a1e | 2019-05-01 21:44:09 -0400 | [diff] [blame] | 178 | 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 | |
| 182 | func 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 |
| 196 | dg := NewDeviceGraph(logicalDeviceId, getDevice) |
| 197 | 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 | |
| 208 | func 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 |
| 223 | dg := NewDeviceGraph(logicalDeviceId, getDevice) |
| 224 | 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 | |
| 235 | func 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 |
| 250 | dg := NewDeviceGraph(logicalDeviceId, getDevice) |
| 251 | 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 | |
| 262 | func 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 |
| 277 | dg := NewDeviceGraph(logicalDeviceId, getDevice) |
| 278 | 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)) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 287 | } |
khenaidoo | cfe03b9 | 2019-06-03 20:06:31 -0400 | [diff] [blame] | 288 | |
| 289 | func 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 | } |