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 | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 24 | "sync" |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 25 | "testing" |
| 26 | "time" |
| 27 | ) |
| 28 | |
| 29 | var ld voltha.LogicalDevice |
| 30 | var olt voltha.Device |
| 31 | var onusOnPort4 []voltha.Device |
| 32 | var onusOnPort5 []voltha.Device |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 33 | var logicalDeviceId string |
| 34 | var oltDeviceId string |
| 35 | var numCalled int |
| 36 | var lock sync.RWMutex |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 37 | |
| 38 | const ( |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 39 | maxOnuOnPort4 int = 256 |
| 40 | maxOnuOnPort5 int = 256 |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 41 | ) |
| 42 | |
| 43 | func init() { |
| 44 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 45 | logicalDeviceId = "ld" |
| 46 | oltDeviceId = "olt" |
| 47 | lock = sync.RWMutex{} |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 48 | |
| 49 | // Setup ONUs on OLT port 4 |
| 50 | onusOnPort4 = make([]voltha.Device, 0) |
| 51 | var onu voltha.Device |
| 52 | var id string |
| 53 | oltPeerPort := uint32(4) |
| 54 | for i := 0; i < maxOnuOnPort4; i++ { |
| 55 | id := fmt.Sprintf("onu%d", i) |
| 56 | onu = voltha.Device{Id: id, ParentId: oltDeviceId} |
| 57 | ponPort := voltha.Port{PortNo: 1, DeviceId: onu.Id, Type: voltha.Port_PON_ONU} |
| 58 | ponPort.Peers = make([]*voltha.Port_PeerPort, 0) |
| 59 | peerPort := voltha.Port_PeerPort{DeviceId: oltDeviceId, PortNo: oltPeerPort} |
| 60 | ponPort.Peers = append(ponPort.Peers, &peerPort) |
| 61 | uniPort := voltha.Port{PortNo: 2, DeviceId: onu.Id, Type: voltha.Port_ETHERNET_UNI} |
| 62 | onu.Ports = make([]*voltha.Port, 0) |
| 63 | onu.Ports = append(onu.Ports, &ponPort) |
| 64 | onu.Ports = append(onu.Ports, &uniPort) |
| 65 | onusOnPort4 = append(onusOnPort4, onu) |
| 66 | } |
| 67 | |
| 68 | // Setup ONUs on OLT port 5 |
| 69 | onusOnPort5 = make([]voltha.Device, 0) |
| 70 | oltPeerPort = uint32(5) |
| 71 | for i := 0; i < maxOnuOnPort5; i++ { |
| 72 | id := fmt.Sprintf("onu%d", i+maxOnuOnPort4) |
| 73 | onu = voltha.Device{Id: id, ParentId: oltDeviceId} |
| 74 | ponPort := voltha.Port{PortNo: 1, DeviceId: onu.Id, Type: voltha.Port_PON_ONU} |
| 75 | ponPort.Peers = make([]*voltha.Port_PeerPort, 0) |
| 76 | peerPort := voltha.Port_PeerPort{DeviceId: oltDeviceId, PortNo: oltPeerPort} |
| 77 | ponPort.Peers = append(ponPort.Peers, &peerPort) |
| 78 | uniPort := voltha.Port{PortNo: 2, DeviceId: onu.Id, Type: voltha.Port_ETHERNET_UNI} |
| 79 | onu.Ports = make([]*voltha.Port, 0) |
| 80 | onu.Ports = append(onu.Ports, &ponPort) |
| 81 | onu.Ports = append(onu.Ports, &uniPort) |
| 82 | onusOnPort5 = append(onusOnPort5, onu) |
| 83 | } |
| 84 | |
| 85 | // Setup OLT |
| 86 | // Setup the OLT device |
| 87 | olt = voltha.Device{Id: oltDeviceId, ParentId: logicalDeviceId} |
| 88 | p1 := voltha.Port{PortNo: 2, DeviceId: oltDeviceId, Type: voltha.Port_ETHERNET_NNI} |
| 89 | p2 := voltha.Port{PortNo: 3, DeviceId: oltDeviceId, Type: voltha.Port_ETHERNET_NNI} |
| 90 | p3 := voltha.Port{PortNo: 4, DeviceId: oltDeviceId, Type: voltha.Port_PON_OLT} |
| 91 | p4 := voltha.Port{PortNo: 5, DeviceId: oltDeviceId, Type: voltha.Port_PON_OLT} |
| 92 | p3.Peers = make([]*voltha.Port_PeerPort, 0) |
| 93 | for _, onu := range onusOnPort4 { |
| 94 | peerPort := voltha.Port_PeerPort{DeviceId: onu.Id, PortNo: p3.PortNo} |
| 95 | p3.Peers = append(p3.Peers, &peerPort) |
| 96 | } |
| 97 | p4.Peers = make([]*voltha.Port_PeerPort, 0) |
| 98 | for _, onu := range onusOnPort5 { |
| 99 | peerPort := voltha.Port_PeerPort{DeviceId: onu.Id, PortNo: p4.PortNo} |
| 100 | p4.Peers = append(p4.Peers, &peerPort) |
| 101 | } |
| 102 | olt.Ports = make([]*voltha.Port, 0) |
| 103 | olt.Ports = append(olt.Ports, &p1) |
| 104 | olt.Ports = append(olt.Ports, &p2) |
| 105 | olt.Ports = append(olt.Ports, &p3) |
| 106 | olt.Ports = append(olt.Ports, &p4) |
| 107 | |
| 108 | // Setup the logical device |
| 109 | ld = voltha.LogicalDevice{Id: logicalDeviceId} |
| 110 | ld.Ports = make([]*voltha.LogicalPort, 0) |
| 111 | ofpPortNo := 1 |
| 112 | //Add olt ports |
| 113 | for i, port := range olt.Ports { |
| 114 | if port.Type == voltha.Port_ETHERNET_NNI { |
| 115 | id = fmt.Sprintf("nni-%d", i) |
| 116 | lp := voltha.LogicalPort{Id: id, DeviceId: olt.Id, DevicePortNo: port.PortNo, OfpPort: &openflow_13.OfpPort{PortNo: uint32(ofpPortNo)}, RootPort: true} |
| 117 | ld.Ports = append(ld.Ports, &lp) |
| 118 | ofpPortNo = ofpPortNo + 1 |
| 119 | } |
| 120 | } |
| 121 | //Add onu ports on port 4 |
| 122 | for i, onu := range onusOnPort4 { |
| 123 | for _, port := range onu.Ports { |
| 124 | if port.Type == voltha.Port_ETHERNET_UNI { |
| 125 | id = fmt.Sprintf("uni-%d", i) |
| 126 | lp := voltha.LogicalPort{Id: id, DeviceId: onu.Id, DevicePortNo: port.PortNo, OfpPort: &openflow_13.OfpPort{PortNo: uint32(ofpPortNo)}, RootPort: false} |
| 127 | ld.Ports = append(ld.Ports, &lp) |
| 128 | ofpPortNo = ofpPortNo + 1 |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | //Add onu ports on port 5 |
| 133 | for i, onu := range onusOnPort5 { |
| 134 | for _, port := range onu.Ports { |
| 135 | if port.Type == voltha.Port_ETHERNET_UNI { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 136 | id = fmt.Sprintf("uni-%d", i+len(onusOnPort4)) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 137 | lp := voltha.LogicalPort{Id: id, DeviceId: onu.Id, DevicePortNo: port.PortNo, OfpPort: &openflow_13.OfpPort{PortNo: uint32(ofpPortNo)}, RootPort: false} |
| 138 | ld.Ports = append(ld.Ports, &lp) |
| 139 | ofpPortNo = ofpPortNo + 1 |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func GetDeviceHelper(id string) (*voltha.Device, error) { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 146 | lock.Lock() |
| 147 | numCalled += 1 |
| 148 | lock.Unlock() |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 149 | if id == "olt" { |
| 150 | return &olt, nil |
| 151 | } |
| 152 | for _, onu := range onusOnPort4 { |
| 153 | if onu.Id == id { |
| 154 | return &onu, nil |
| 155 | } |
| 156 | } |
| 157 | for _, onu := range onusOnPort5 { |
| 158 | if onu.Id == id { |
| 159 | return &onu, nil |
| 160 | } |
| 161 | } |
| 162 | return nil, errors.New("Not-found") |
| 163 | } |
| 164 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 165 | func TestGetRoutesOneShot(t *testing.T) { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 166 | |
| 167 | getDevice := GetDeviceHelper |
| 168 | |
| 169 | // Create a device graph and computes Routes |
| 170 | start := time.Now() |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 171 | dg := NewDeviceGraph(logicalDeviceId, getDevice) |
| 172 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 173 | dg.ComputeRoutes(ld.Ports) |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 174 | fmt.Println("Total num called:", numCalled) |
| 175 | fmt.Println("Total Time creating graph & compute Routes in one shot:", time.Since(start)) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 176 | assert.NotNil(t, dg.GGraph) |
| 177 | assert.EqualValues(t, (maxOnuOnPort4*4 + maxOnuOnPort5*4), len(dg.Routes)) |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 178 | dg.Print() |
| 179 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 180 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 181 | func TestGetRoutesAddPort(t *testing.T) { |
| 182 | |
| 183 | getDevice := GetDeviceHelper |
| 184 | |
| 185 | // Create a device graph and computes Routes |
| 186 | start := time.Now() |
| 187 | dg := NewDeviceGraph(logicalDeviceId, getDevice) |
| 188 | for _, lp := range ld.Ports { |
| 189 | dg.AddPort(lp) |
| 190 | } |
| 191 | |
| 192 | fmt.Println("Total num called:", numCalled) |
| 193 | fmt.Println("Total Time creating graph & compute Routes per port:", time.Since(start)) |
| 194 | assert.NotNil(t, dg.GGraph) |
| 195 | assert.EqualValues(t, (maxOnuOnPort4*4 + maxOnuOnPort5*4), len(dg.Routes)) |
| 196 | dg.Print() |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 197 | } |