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