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 | |
| 17 | package graph |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | "github.com/gyuho/goraph" |
| 23 | "github.com/opencord/voltha-go/common/log" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 24 | "github.com/opencord/voltha-protos/go/voltha" |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 25 | "strconv" |
| 26 | "strings" |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 27 | "sync" |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | func init() { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 31 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | type RouteHop struct { |
| 35 | DeviceID string |
| 36 | Ingress uint32 |
| 37 | Egress uint32 |
| 38 | } |
| 39 | |
| 40 | type OFPortLink struct { |
| 41 | Ingress uint32 |
| 42 | Egress uint32 |
| 43 | } |
| 44 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 45 | type ofPortLinkToPath struct { |
| 46 | link OFPortLink |
| 47 | path []RouteHop |
| 48 | } |
| 49 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 50 | type GetDeviceFunc func(id string) (*voltha.Device, error) |
| 51 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 52 | type DeviceGraph struct { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 53 | logicalDeviceId string |
| 54 | GGraph goraph.Graph |
| 55 | getDeviceFromModel GetDeviceFunc |
| 56 | logicalPorts []*voltha.LogicalPort |
| 57 | rootPortsString map[string]uint32 |
| 58 | nonRootPortsString map[string]uint32 |
| 59 | RootPorts map[uint32]uint32 |
| 60 | rootPortsLock sync.RWMutex |
| 61 | Routes map[OFPortLink][]RouteHop |
| 62 | graphBuildLock sync.RWMutex |
| 63 | boundaryPorts map[string]uint32 |
| 64 | boundaryPortsLock sync.RWMutex |
| 65 | cachedDevices map[string]*voltha.Device |
| 66 | cachedDevicesLock sync.RWMutex |
| 67 | devicesAdded map[string]string |
| 68 | portsAdded map[string]string |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 69 | } |
| 70 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 71 | func NewDeviceGraph(logicalDeviceId string, getDevice GetDeviceFunc) *DeviceGraph { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 72 | var dg DeviceGraph |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 73 | dg.logicalDeviceId = logicalDeviceId |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 74 | dg.GGraph = goraph.NewGraph() |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 75 | dg.getDeviceFromModel = getDevice |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 76 | dg.graphBuildLock = sync.RWMutex{} |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 77 | dg.cachedDevicesLock = sync.RWMutex{} |
| 78 | dg.rootPortsLock = sync.RWMutex{} |
| 79 | dg.devicesAdded = make(map[string]string) |
| 80 | dg.portsAdded = make(map[string]string) |
| 81 | dg.rootPortsString = make(map[string]uint32) |
| 82 | dg.nonRootPortsString = make(map[string]uint32) |
| 83 | dg.RootPorts = make(map[uint32]uint32) |
| 84 | dg.boundaryPorts = make(map[string]uint32) |
| 85 | dg.Routes = make(map[OFPortLink][]RouteHop) |
| 86 | dg.cachedDevices = make(map[string]*voltha.Device) |
| 87 | log.Debug("new device graph created ...") |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 88 | return &dg |
| 89 | } |
| 90 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 91 | //IsRootPort returns true if the port is a root port on a logical device |
| 92 | func (dg *DeviceGraph) IsRootPort(port uint32) bool { |
| 93 | dg.rootPortsLock.RLock() |
| 94 | defer dg.rootPortsLock.RUnlock() |
| 95 | _, exist := dg.RootPorts[port] |
| 96 | return exist |
| 97 | } |
| 98 | |
| 99 | //GetDeviceNodeIds retrieves all the nodes in the device graph |
| 100 | func (dg *DeviceGraph) GetDeviceNodeIds() map[string]string { |
| 101 | dg.graphBuildLock.RLock() |
| 102 | defer dg.graphBuildLock.RUnlock() |
| 103 | nodeIds := make(map[string]string) |
| 104 | nodesMap := dg.GGraph.GetNodes() |
| 105 | for id, node := range nodesMap { |
| 106 | if len(strings.Split(node.String(), ":")) != 2 { // not port node |
| 107 | nodeIds[id.String()] = id.String() |
| 108 | } |
| 109 | } |
| 110 | return nodeIds |
| 111 | } |
| 112 | |
| 113 | //ComputeRoutes creates a device graph from the logical ports and then calculates all the routes |
| 114 | //between the logical ports. This will clear up the graph and routes if there were any. |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 115 | func (dg *DeviceGraph) ComputeRoutes(lps []*voltha.LogicalPort) { |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 116 | if dg == nil { |
| 117 | return |
| 118 | } |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 119 | dg.graphBuildLock.Lock() |
| 120 | defer dg.graphBuildLock.Unlock() |
Stephane Barbarie | c53a275 | 2019-03-08 17:50:10 -0500 | [diff] [blame] | 121 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 122 | // Clear the graph |
| 123 | dg.reset() |
| 124 | |
| 125 | dg.logicalPorts = lps |
| 126 | |
| 127 | // Set the root, non-root ports and boundary ports |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 128 | for _, lp := range lps { |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 129 | portId := concatDeviceIdPortId(lp.DeviceId, lp.DevicePortNo) |
| 130 | if lp.RootPort { |
| 131 | dg.rootPortsString[portId] = lp.OfpPort.PortNo |
| 132 | dg.RootPorts[lp.OfpPort.PortNo] = lp.OfpPort.PortNo |
| 133 | } else { |
| 134 | dg.nonRootPortsString[portId] = lp.OfpPort.PortNo |
| 135 | } |
| 136 | dg.boundaryPorts[portId] = lp.OfpPort.PortNo |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 137 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 138 | |
| 139 | // Build the graph |
| 140 | var device *voltha.Device |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 141 | for _, logicalPort := range dg.logicalPorts { |
| 142 | device, _ = dg.getDevice(logicalPort.DeviceId) |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 143 | dg.GGraph = dg.addDevice(device, dg.GGraph, &dg.devicesAdded, &dg.portsAdded, dg.boundaryPorts) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 144 | } |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 145 | |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 146 | dg.Routes = dg.buildRoutes() |
| 147 | } |
| 148 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 149 | // AddPort adds a port to the graph. If the graph is empty it will just invoke ComputeRoutes function |
| 150 | func (dg *DeviceGraph) AddPort(lp *voltha.LogicalPort) { |
| 151 | // If the graph does not exist invoke ComputeRoutes. |
| 152 | if len(dg.boundaryPorts) == 0 { |
| 153 | dg.ComputeRoutes([]*voltha.LogicalPort{lp}) |
| 154 | return |
| 155 | } |
| 156 | |
| 157 | dg.graphBuildLock.Lock() |
| 158 | defer dg.graphBuildLock.Unlock() |
| 159 | |
| 160 | portId := concatDeviceIdPortId(lp.DeviceId, lp.DevicePortNo) |
| 161 | |
| 162 | // If the port is already part of the boundary ports, do nothing |
| 163 | if dg.portExist(portId) { |
| 164 | fmt.Println("port exists") |
| 165 | return |
| 166 | } |
| 167 | // Add the device where this port is located to the device graph. If the device is already added then |
| 168 | // only the missing port will be added |
| 169 | device, _ := dg.getDevice(lp.DeviceId) |
| 170 | dg.GGraph = dg.addDevice(device, dg.GGraph, &dg.devicesAdded, &dg.portsAdded, dg.boundaryPorts) |
| 171 | |
| 172 | if lp.RootPort { |
| 173 | // Compute the route from this root port to all non-root ports |
| 174 | dg.rootPortsString[portId] = lp.OfpPort.PortNo |
| 175 | dg.RootPorts[lp.OfpPort.PortNo] = lp.OfpPort.PortNo |
| 176 | dg.Routes = dg.buildPathsToAllNonRootPorts(lp) |
| 177 | } else { |
| 178 | // Compute the route from this port to all root ports |
| 179 | dg.nonRootPortsString[portId] = lp.OfpPort.PortNo |
| 180 | dg.Routes = dg.buildPathsToAllRootPorts(lp) |
| 181 | } |
| 182 | |
| 183 | dg.Print() |
| 184 | } |
| 185 | |
| 186 | func (dg *DeviceGraph) Print() error { |
| 187 | if level, err := log.GetPackageLogLevel(); err == nil && level == log.DebugLevel { |
| 188 | output := "" |
| 189 | routeNumber := 1 |
| 190 | for k, v := range dg.Routes { |
| 191 | key := fmt.Sprintf("LP:%d->LP:%d", k.Ingress, k.Egress) |
| 192 | val := "" |
| 193 | for _, i := range v { |
| 194 | val += fmt.Sprintf("{%d->%s->%d},", i.Ingress, i.DeviceID, i.Egress) |
| 195 | } |
| 196 | val = val[:len(val)-1] |
| 197 | output += fmt.Sprintf("%d:{%s=>%s} ", routeNumber, key, fmt.Sprintf("[%s]", val)) |
| 198 | routeNumber += 1 |
| 199 | } |
| 200 | log.Debugw("graph_routes", log.Fields{"lDeviceId": dg.logicalDeviceId, "Routes": output}) |
| 201 | } |
| 202 | return nil |
| 203 | } |
| 204 | |
| 205 | //getDevice returns the device either from the local cache (default) or from the model. |
| 206 | //TODO: Set a cache timeout such that we do not use invalid data. The full device lifecycle should also |
| 207 | //be taken in consideration |
| 208 | func (dg *DeviceGraph) getDevice(id string) (*voltha.Device, error) { |
| 209 | dg.cachedDevicesLock.RLock() |
| 210 | if d, exist := dg.cachedDevices[id]; exist { |
| 211 | dg.cachedDevicesLock.RUnlock() |
| 212 | //log.Debugw("getDevice - returned from cache", log.Fields{"deviceId": id}) |
| 213 | return d, nil |
| 214 | } |
| 215 | dg.cachedDevicesLock.RUnlock() |
| 216 | // Not cached |
| 217 | if d, err := dg.getDeviceFromModel(id); err != nil { |
| 218 | log.Errorw("device-not-found", log.Fields{"deviceId": id, "error": err}) |
| 219 | return nil, err |
| 220 | } else { // cache it |
| 221 | dg.cachedDevicesLock.Lock() |
| 222 | dg.cachedDevices[id] = d |
| 223 | dg.cachedDevicesLock.Unlock() |
| 224 | //log.Debugw("getDevice - returned from model", log.Fields{"deviceId": id}) |
| 225 | return d, nil |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // addDevice adds a device to a device graph and setup edges that represent the device connections to its peers |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 230 | func (dg *DeviceGraph) addDevice(device *voltha.Device, g goraph.Graph, devicesAdded *map[string]string, portsAdded *map[string]string, |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 231 | boundaryPorts map[string]uint32) goraph.Graph { |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 232 | |
| 233 | if device == nil { |
| 234 | return g |
| 235 | } |
| 236 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 237 | if _, exist := (*devicesAdded)[device.Id]; !exist { |
| 238 | g.AddNode(goraph.NewNode(device.Id)) |
| 239 | (*devicesAdded)[device.Id] = device.Id |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 240 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 241 | |
| 242 | var portId string |
| 243 | var peerPortId string |
| 244 | for _, port := range device.Ports { |
| 245 | portId = concatDeviceIdPortId(device.Id, port.PortNo) |
| 246 | if _, exist := (*portsAdded)[portId]; !exist { |
| 247 | (*portsAdded)[portId] = portId |
| 248 | g.AddNode(goraph.NewNode(portId)) |
| 249 | g.AddEdge(goraph.StringID(device.Id), goraph.StringID(portId), 1) |
| 250 | g.AddEdge(goraph.StringID(portId), goraph.StringID(device.Id), 1) |
| 251 | } |
| 252 | for _, peer := range port.Peers { |
| 253 | if _, exist := (*devicesAdded)[peer.DeviceId]; !exist { |
| 254 | d, _ := dg.getDevice(peer.DeviceId) |
| 255 | g = dg.addDevice(d, g, devicesAdded, portsAdded, boundaryPorts) |
| 256 | } else { |
| 257 | peerPortId = concatDeviceIdPortId(peer.DeviceId, peer.PortNo) |
| 258 | g.AddEdge(goraph.StringID(portId), goraph.StringID(peerPortId), 1) |
| 259 | g.AddEdge(goraph.StringID(peerPortId), goraph.StringID(portId), 1) |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | return g |
| 264 | } |
| 265 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 266 | //portExist returns true if the port ID is already part of the boundary ports map. |
| 267 | func (dg *DeviceGraph) portExist(id string) bool { |
| 268 | dg.boundaryPortsLock.RLock() |
| 269 | defer dg.boundaryPortsLock.RUnlock() |
| 270 | _, exist := dg.boundaryPorts[id] |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 271 | return exist |
| 272 | } |
| 273 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 274 | // buildPathsToAllRootPorts builds all the paths from the non-root logical port to all root ports |
| 275 | // on the logical device |
| 276 | func (dg *DeviceGraph) buildPathsToAllRootPorts(lp *voltha.LogicalPort) map[OFPortLink][]RouteHop { |
| 277 | paths := dg.Routes |
| 278 | source := concatDeviceIdPortId(lp.DeviceId, lp.DevicePortNo) |
| 279 | sourcePort := lp.OfpPort.PortNo |
| 280 | ch := make(chan *ofPortLinkToPath) |
| 281 | numBuildRequest := 0 |
| 282 | for target, targetPort := range dg.rootPortsString { |
| 283 | go dg.buildRoute(source, target, sourcePort, targetPort, ch) |
| 284 | numBuildRequest += 1 |
| 285 | } |
| 286 | responseReceived := 0 |
| 287 | forloop: |
| 288 | for { |
| 289 | if responseReceived == numBuildRequest { |
| 290 | break |
| 291 | } |
| 292 | select { |
| 293 | case res, ok := <-ch: |
| 294 | if !ok { |
| 295 | log.Debug("channel closed") |
| 296 | break forloop |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 297 | } |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 298 | if res != nil && len(res.path) > 0 { |
| 299 | paths[res.link] = res.path |
| 300 | paths[OFPortLink{Ingress: res.link.Egress, Egress: res.link.Ingress}] = getReverseRoute(res.path) |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 301 | } |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 302 | } |
| 303 | responseReceived += 1 |
| 304 | } |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 305 | return paths |
| 306 | } |
| 307 | |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 308 | // buildPathsToAllNonRootPorts builds all the paths from the root logical port to all non-root ports |
| 309 | // on the logical device |
| 310 | func (dg *DeviceGraph) buildPathsToAllNonRootPorts(lp *voltha.LogicalPort) map[OFPortLink][]RouteHop { |
| 311 | paths := dg.Routes |
| 312 | source := concatDeviceIdPortId(lp.DeviceId, lp.DevicePortNo) |
| 313 | sourcePort := lp.OfpPort.PortNo |
| 314 | ch := make(chan *ofPortLinkToPath) |
| 315 | numBuildRequest := 0 |
| 316 | for target, targetPort := range dg.nonRootPortsString { |
| 317 | go dg.buildRoute(source, target, sourcePort, targetPort, ch) |
| 318 | numBuildRequest += 1 |
| 319 | } |
| 320 | responseReceived := 0 |
| 321 | forloop: |
| 322 | for { |
| 323 | if responseReceived == numBuildRequest { |
| 324 | break |
| 325 | } |
| 326 | select { |
| 327 | case res, ok := <-ch: |
| 328 | if !ok { |
| 329 | log.Debug("channel closed") |
| 330 | break forloop |
| 331 | } |
| 332 | if res != nil && len(res.path) > 0 { |
| 333 | paths[res.link] = res.path |
| 334 | paths[OFPortLink{Ingress: res.link.Egress, Egress: res.link.Ingress}] = getReverseRoute(res.path) |
| 335 | } |
| 336 | } |
| 337 | responseReceived += 1 |
| 338 | } |
| 339 | return paths |
| 340 | } |
| 341 | |
| 342 | //buildRoute builds a route between a source and a target logical port |
| 343 | func (dg *DeviceGraph) buildRoute(sourceId, targetId string, sourcePort, targetPort uint32, ch chan *ofPortLinkToPath) { |
| 344 | var pathIds []goraph.ID |
| 345 | path := make([]RouteHop, 0) |
| 346 | var err error |
| 347 | var hop RouteHop |
| 348 | var result *ofPortLinkToPath |
| 349 | |
| 350 | if sourceId == targetId { |
| 351 | ch <- result |
| 352 | return |
| 353 | } |
| 354 | //Ignore Root - Root Routes |
| 355 | if dg.IsRootPort(sourcePort) && dg.IsRootPort(targetPort) { |
| 356 | ch <- result |
| 357 | return |
| 358 | } |
| 359 | |
| 360 | //Ignore non-Root - non-Root Routes |
| 361 | if !dg.IsRootPort(sourcePort) && !dg.IsRootPort(targetPort) { |
| 362 | ch <- result |
| 363 | return |
| 364 | } |
| 365 | |
| 366 | if pathIds, _, err = goraph.Dijkstra(dg.GGraph, goraph.StringID(sourceId), goraph.StringID(targetId)); err != nil { |
| 367 | log.Errorw("no-path", log.Fields{"sourceId": sourceId, "targetId": targetId, "error": err}) |
| 368 | ch <- result |
| 369 | return |
| 370 | } |
| 371 | if len(pathIds)%3 != 0 { |
| 372 | ch <- result |
| 373 | return |
| 374 | } |
| 375 | var deviceId string |
| 376 | var ingressPort uint32 |
| 377 | var egressPort uint32 |
| 378 | for i := 0; i < len(pathIds); i = i + 3 { |
| 379 | if deviceId, ingressPort, err = splitIntoDeviceIdPortId(pathIds[i].String()); err != nil { |
| 380 | log.Errorw("id-error", log.Fields{"sourceId": sourceId, "targetId": targetId, "error": err}) |
| 381 | break |
| 382 | } |
| 383 | if _, egressPort, err = splitIntoDeviceIdPortId(pathIds[i+2].String()); err != nil { |
| 384 | log.Errorw("id-error", log.Fields{"sourceId": sourceId, "targetId": targetId, "error": err}) |
| 385 | break |
| 386 | } |
| 387 | hop = RouteHop{Ingress: ingressPort, DeviceID: deviceId, Egress: egressPort} |
| 388 | path = append(path, hop) |
| 389 | } |
| 390 | result = &ofPortLinkToPath{link: OFPortLink{Ingress: sourcePort, Egress: targetPort}, path: path} |
| 391 | ch <- result |
| 392 | } |
| 393 | |
| 394 | //buildRoutes build all routes between all the ports on the logical device |
| 395 | func (dg *DeviceGraph) buildRoutes() map[OFPortLink][]RouteHop { |
| 396 | paths := make(map[OFPortLink][]RouteHop) |
| 397 | ch := make(chan *ofPortLinkToPath) |
| 398 | numBuildRequest := 0 |
| 399 | for source, sourcePort := range dg.boundaryPorts { |
| 400 | for target, targetPort := range dg.boundaryPorts { |
| 401 | go dg.buildRoute(source, target, sourcePort, targetPort, ch) |
| 402 | numBuildRequest += 1 |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 403 | } |
| 404 | } |
khenaidoo | 910204f | 2019-04-08 17:56:40 -0400 | [diff] [blame] | 405 | responseReceived := 0 |
| 406 | forloop: |
| 407 | for { |
| 408 | if responseReceived == numBuildRequest { |
| 409 | break |
| 410 | } |
| 411 | select { |
| 412 | case res, ok := <-ch: |
| 413 | if !ok { |
| 414 | log.Debug("channel closed") |
| 415 | break forloop |
| 416 | } |
| 417 | if res != nil && len(res.path) > 0 { |
| 418 | paths[res.link] = res.path |
| 419 | } |
| 420 | } |
| 421 | responseReceived += 1 |
| 422 | } |
| 423 | return paths |
| 424 | } |
| 425 | |
| 426 | // reset cleans up the device graph |
| 427 | func (dg *DeviceGraph) reset() { |
| 428 | dg.devicesAdded = make(map[string]string) |
| 429 | dg.portsAdded = make(map[string]string) |
| 430 | dg.rootPortsString = make(map[string]uint32) |
| 431 | dg.nonRootPortsString = make(map[string]uint32) |
| 432 | dg.RootPorts = make(map[uint32]uint32) |
| 433 | dg.boundaryPorts = make(map[string]uint32) |
| 434 | dg.Routes = make(map[OFPortLink][]RouteHop) |
| 435 | dg.cachedDevices = make(map[string]*voltha.Device) |
| 436 | } |
| 437 | |
| 438 | //concatDeviceIdPortId formats a portid using the device id and the port number |
| 439 | func concatDeviceIdPortId(deviceId string, portNo uint32) string { |
| 440 | return fmt.Sprintf("%s:%d", deviceId, portNo) |
| 441 | } |
| 442 | |
| 443 | // splitIntoDeviceIdPortId extracts the device id and port number from the portId |
| 444 | func splitIntoDeviceIdPortId(id string) (string, uint32, error) { |
| 445 | result := strings.Split(id, ":") |
| 446 | if len(result) != 2 { |
| 447 | return "", 0, errors.New(fmt.Sprintf("invalid-id-%s", id)) |
| 448 | } |
| 449 | if temp, err := strconv.ParseInt(result[1], 10, 32); err != nil { |
| 450 | return "", 0, errors.New(fmt.Sprintf("invalid-id-%s-%s", id, err.Error())) |
| 451 | } else { |
| 452 | return result[0], uint32(temp), nil |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | //getReverseRoute returns the reverse of the route in param |
| 457 | func getReverseRoute(route []RouteHop) []RouteHop { |
| 458 | reverse := make([]RouteHop, len(route)) |
| 459 | for i, j := 0, len(route)-1; i < j; i, j = i+1, j-1 { |
| 460 | reverse[i], reverse[j] = route[j], route[i] |
| 461 | } |
| 462 | return reverse |
khenaidoo | 89b0e94 | 2018-10-21 21:11:33 -0400 | [diff] [blame] | 463 | } |