VOL-1900 lint warning fixes rw_core
Change-Id: Icaa84d7ce24163da90c91ff2babcbb78ff4e9141
diff --git a/rw_core/graph/device_graph.go b/rw_core/graph/device_graph.go
index 229a3a4..4c89134 100644
--- a/rw_core/graph/device_graph.go
+++ b/rw_core/graph/device_graph.go
@@ -17,26 +17,31 @@
package graph
import (
- "errors"
"fmt"
- "github.com/gyuho/goraph"
- "github.com/opencord/voltha-lib-go/v2/pkg/log"
- "github.com/opencord/voltha-protos/v2/go/voltha"
"strconv"
"strings"
"sync"
+
+ "github.com/gyuho/goraph"
+ "github.com/opencord/voltha-lib-go/v2/pkg/log"
+ "github.com/opencord/voltha-protos/v2/go/voltha"
)
func init() {
- log.AddPackage(log.JSON, log.WarnLevel, nil)
+ _, err := log.AddPackage(log.JSON, log.WarnLevel, nil)
+ if err != nil {
+ log.Errorw("unable-to-register-package-to-the-log-map", log.Fields{"error": err})
+ }
}
+// RouteHop represent route hop attributes
type RouteHop struct {
DeviceID string
Ingress uint32
Egress uint32
}
+// OFPortLink represent of port link attributes
type OFPortLink struct {
Ingress uint32
Egress uint32
@@ -47,10 +52,12 @@
path []RouteHop
}
+// GetDeviceFunc returns device function
type GetDeviceFunc func(id string) (*voltha.Device, error)
+// DeviceGraph represent device graph attributes
type DeviceGraph struct {
- logicalDeviceId string
+ logicalDeviceID string
GGraph goraph.Graph
getDeviceFromModel GetDeviceFunc
logicalPorts []*voltha.LogicalPort
@@ -68,9 +75,10 @@
portsAdded map[string]string
}
-func NewDeviceGraph(logicalDeviceId string, getDevice GetDeviceFunc) *DeviceGraph {
+// NewDeviceGraph creates device graph instance
+func NewDeviceGraph(logicalDeviceID string, getDevice GetDeviceFunc) *DeviceGraph {
var dg DeviceGraph
- dg.logicalDeviceId = logicalDeviceId
+ dg.logicalDeviceID = logicalDeviceID
dg.GGraph = goraph.NewGraph()
dg.getDeviceFromModel = getDevice
dg.graphBuildLock = sync.RWMutex{}
@@ -126,14 +134,14 @@
// Set the root, non-root ports and boundary ports
for _, lp := range lps {
- portId := concatDeviceIdPortId(lp.DeviceId, lp.DevicePortNo)
+ portID := concatDeviceIDPortID(lp.DeviceId, lp.DevicePortNo)
if lp.RootPort {
- dg.rootPortsString[portId] = lp.OfpPort.PortNo
+ dg.rootPortsString[portID] = lp.OfpPort.PortNo
dg.RootPorts[lp.OfpPort.PortNo] = lp.OfpPort.PortNo
} else {
- dg.nonRootPortsString[portId] = lp.OfpPort.PortNo
+ dg.nonRootPortsString[portID] = lp.OfpPort.PortNo
}
- dg.boundaryPorts[portId] = lp.OfpPort.PortNo
+ dg.boundaryPorts[portID] = lp.OfpPort.PortNo
}
// Build the graph
@@ -158,14 +166,14 @@
dg.graphBuildLock.Lock()
defer dg.graphBuildLock.Unlock()
- portId := concatDeviceIdPortId(lp.DeviceId, lp.DevicePortNo)
+ portID := concatDeviceIDPortID(lp.DeviceId, lp.DevicePortNo)
// If the port is already part of the boundary ports, do nothing
- if dg.portExist(portId) {
+ if dg.portExist(portID) {
return
}
// Add the port to the set of boundary ports
- dg.boundaryPorts[portId] = lp.OfpPort.PortNo
+ dg.boundaryPorts[portID] = lp.OfpPort.PortNo
// Add the device where this port is located to the device graph. If the device is already added then
// only the missing port will be added
@@ -174,20 +182,21 @@
if lp.RootPort {
// Compute the route from this root port to all non-root ports
- dg.rootPortsString[portId] = lp.OfpPort.PortNo
+ dg.rootPortsString[portID] = lp.OfpPort.PortNo
dg.RootPorts[lp.OfpPort.PortNo] = lp.OfpPort.PortNo
dg.Routes = dg.buildPathsToAllNonRootPorts(lp)
} else {
// Compute the route from this port to all root ports
- dg.nonRootPortsString[portId] = lp.OfpPort.PortNo
+ dg.nonRootPortsString[portID] = lp.OfpPort.PortNo
dg.Routes = dg.buildPathsToAllRootPorts(lp)
}
dg.Print()
}
+// Print prints routes
func (dg *DeviceGraph) Print() error {
- log.Debugw("Print", log.Fields{"graph": dg.logicalDeviceId, "boundaryPorts": dg.boundaryPorts})
+ log.Debugw("Print", log.Fields{"graph": dg.logicalDeviceID, "boundaryPorts": dg.boundaryPorts})
if level, err := log.GetPackageLogLevel(); err == nil && level == log.DebugLevel {
output := ""
routeNumber := 1
@@ -199,28 +208,29 @@
}
val = val[:len(val)-1]
output += fmt.Sprintf("%d:{%s=>%s} ", routeNumber, key, fmt.Sprintf("[%s]", val))
- routeNumber += 1
+ routeNumber++
}
if len(dg.Routes) == 0 {
- log.Debugw("no-routes-found", log.Fields{"lDeviceId": dg.logicalDeviceId, "Graph": dg.GGraph.String()})
+ log.Debugw("no-routes-found", log.Fields{"lDeviceId": dg.logicalDeviceID, "Graph": dg.GGraph.String()})
} else {
- log.Debugw("graph_routes", log.Fields{"lDeviceId": dg.logicalDeviceId, "Routes": output})
+ log.Debugw("graph_routes", log.Fields{"lDeviceId": dg.logicalDeviceID, "Routes": output})
}
}
return nil
}
+// IsUpToDate returns true if device is up to date
func (dg *DeviceGraph) IsUpToDate(ld *voltha.LogicalDevice) bool {
if ld != nil {
if len(dg.boundaryPorts) != len(ld.Ports) {
return false
}
- var portId string
+ var portID string
var val uint32
var exist bool
for _, lp := range ld.Ports {
- portId = concatDeviceIdPortId(lp.DeviceId, lp.DevicePortNo)
- if val, exist = dg.boundaryPorts[portId]; !exist || val != lp.OfpPort.PortNo {
+ portID = concatDeviceIDPortID(lp.DeviceId, lp.DevicePortNo)
+ if val, exist = dg.boundaryPorts[portID]; !exist || val != lp.OfpPort.PortNo {
return false
}
}
@@ -243,16 +253,17 @@
dg.cachedDevicesLock.RUnlock()
}
// Not cached
- if d, err := dg.getDeviceFromModel(id); err != nil {
+ d, err := dg.getDeviceFromModel(id)
+ if err != nil {
log.Errorw("device-not-found", log.Fields{"deviceId": id, "error": err})
return nil, err
- } else { // cache it
- dg.cachedDevicesLock.Lock()
- dg.cachedDevices[id] = d
- dg.cachedDevicesLock.Unlock()
- //log.Debugw("getDevice - returned from model", log.Fields{"deviceId": id})
- return d, nil
}
+ // cache it
+ dg.cachedDevicesLock.Lock()
+ dg.cachedDevices[id] = d
+ dg.cachedDevicesLock.Unlock()
+ //log.Debugw("getDevice - returned from model", log.Fields{"deviceId": id})
+ return d, nil
}
// addDevice adds a device to a device graph and setup edges that represent the device connections to its peers
@@ -270,25 +281,36 @@
(*devicesAdded)[device.Id] = device.Id
}
- var portId string
- var peerPortId string
+ var portID string
+ var peerPortID string
for _, port := range device.Ports {
- portId = concatDeviceIdPortId(device.Id, port.PortNo)
- if _, exist := (*portsAdded)[portId]; !exist {
- (*portsAdded)[portId] = portId
- g.AddNode(goraph.NewNode(portId))
- g.AddEdge(goraph.StringID(device.Id), goraph.StringID(portId), 1)
- g.AddEdge(goraph.StringID(portId), goraph.StringID(device.Id), 1)
+ portID = concatDeviceIDPortID(device.Id, port.PortNo)
+ if _, exist := (*portsAdded)[portID]; !exist {
+ (*portsAdded)[portID] = portID
+ g.AddNode(goraph.NewNode(portID))
+ err := g.AddEdge(goraph.StringID(device.Id), goraph.StringID(portID), 1)
+ if err != nil {
+ log.Errorw("unable-to-add-edge", log.Fields{"error": err})
+ }
+ err = g.AddEdge(goraph.StringID(portID), goraph.StringID(device.Id), 1)
+ if err != nil {
+ log.Errorw("unable-to-add-edge", log.Fields{"error": err})
+ }
}
for _, peer := range port.Peers {
if _, exist := (*devicesAdded)[peer.DeviceId]; !exist {
d, _ := dg.getDevice(peer.DeviceId, true)
g = dg.addDevice(d, g, devicesAdded, portsAdded, boundaryPorts)
}
- peerPortId = concatDeviceIdPortId(peer.DeviceId, peer.PortNo)
- g.AddEdge(goraph.StringID(portId), goraph.StringID(peerPortId), 1)
- g.AddEdge(goraph.StringID(peerPortId), goraph.StringID(portId), 1)
-
+ peerPortID = concatDeviceIDPortID(peer.DeviceId, peer.PortNo)
+ err := g.AddEdge(goraph.StringID(portID), goraph.StringID(peerPortID), 1)
+ if err != nil {
+ log.Errorw("unable-to-add-edge", log.Fields{"error": err})
+ }
+ err = g.AddEdge(goraph.StringID(peerPortID), goraph.StringID(portID), 1)
+ if err != nil {
+ log.Errorw("unable-to-add-edge", log.Fields{"error": err})
+ }
}
}
return g
@@ -306,13 +328,13 @@
// on the logical device
func (dg *DeviceGraph) buildPathsToAllRootPorts(lp *voltha.LogicalPort) map[OFPortLink][]RouteHop {
paths := dg.Routes
- source := concatDeviceIdPortId(lp.DeviceId, lp.DevicePortNo)
+ source := concatDeviceIDPortID(lp.DeviceId, lp.DevicePortNo)
sourcePort := lp.OfpPort.PortNo
ch := make(chan *ofPortLinkToPath)
numBuildRequest := 0
for target, targetPort := range dg.rootPortsString {
go dg.buildRoute(source, target, sourcePort, targetPort, ch)
- numBuildRequest += 1
+ numBuildRequest++
}
responseReceived := 0
forloop:
@@ -320,18 +342,16 @@
if responseReceived == numBuildRequest {
break
}
- select {
- case res, ok := <-ch:
- if !ok {
- log.Debug("channel closed")
- break forloop
- }
- if res != nil && len(res.path) > 0 {
- paths[res.link] = res.path
- paths[OFPortLink{Ingress: res.link.Egress, Egress: res.link.Ingress}] = getReverseRoute(res.path)
- }
+ res, ok := <-ch
+ if !ok {
+ log.Debug("channel closed")
+ break forloop
}
- responseReceived += 1
+ if res != nil && len(res.path) > 0 {
+ paths[res.link] = res.path
+ paths[OFPortLink{Ingress: res.link.Egress, Egress: res.link.Ingress}] = getReverseRoute(res.path)
+ }
+ responseReceived++
}
return paths
}
@@ -340,13 +360,13 @@
// on the logical device
func (dg *DeviceGraph) buildPathsToAllNonRootPorts(lp *voltha.LogicalPort) map[OFPortLink][]RouteHop {
paths := dg.Routes
- source := concatDeviceIdPortId(lp.DeviceId, lp.DevicePortNo)
+ source := concatDeviceIDPortID(lp.DeviceId, lp.DevicePortNo)
sourcePort := lp.OfpPort.PortNo
ch := make(chan *ofPortLinkToPath)
numBuildRequest := 0
for target, targetPort := range dg.nonRootPortsString {
go dg.buildRoute(source, target, sourcePort, targetPort, ch)
- numBuildRequest += 1
+ numBuildRequest++
}
responseReceived := 0
forloop:
@@ -354,31 +374,29 @@
if responseReceived == numBuildRequest {
break
}
- select {
- case res, ok := <-ch:
- if !ok {
- log.Debug("channel closed")
- break forloop
- }
- if res != nil && len(res.path) > 0 {
- paths[res.link] = res.path
- paths[OFPortLink{Ingress: res.link.Egress, Egress: res.link.Ingress}] = getReverseRoute(res.path)
- }
+ res, ok := <-ch
+ if !ok {
+ log.Debug("channel closed")
+ break forloop
}
- responseReceived += 1
+ if res != nil && len(res.path) > 0 {
+ paths[res.link] = res.path
+ paths[OFPortLink{Ingress: res.link.Egress, Egress: res.link.Ingress}] = getReverseRoute(res.path)
+ }
+ responseReceived++
}
return paths
}
//buildRoute builds a route between a source and a target logical port
-func (dg *DeviceGraph) buildRoute(sourceId, targetId string, sourcePort, targetPort uint32, ch chan *ofPortLinkToPath) {
+func (dg *DeviceGraph) buildRoute(sourceID, targetID string, sourcePort, targetPort uint32, ch chan *ofPortLinkToPath) {
var pathIds []goraph.ID
path := make([]RouteHop, 0)
var err error
var hop RouteHop
var result *ofPortLinkToPath
- if sourceId == targetId {
+ if sourceID == targetID {
ch <- result
return
}
@@ -394,8 +412,8 @@
return
}
- if pathIds, _, err = goraph.Dijkstra(dg.GGraph, goraph.StringID(sourceId), goraph.StringID(targetId)); err != nil {
- log.Errorw("no-path", log.Fields{"sourceId": sourceId, "targetId": targetId, "error": err})
+ if pathIds, _, err = goraph.Dijkstra(dg.GGraph, goraph.StringID(sourceID), goraph.StringID(targetID)); err != nil {
+ log.Errorw("no-path", log.Fields{"sourceId": sourceID, "targetId": targetID, "error": err})
ch <- result
return
}
@@ -403,19 +421,19 @@
ch <- result
return
}
- var deviceId string
+ var deviceID string
var ingressPort uint32
var egressPort uint32
for i := 0; i < len(pathIds); i = i + 3 {
- if deviceId, ingressPort, err = splitIntoDeviceIdPortId(pathIds[i].String()); err != nil {
- log.Errorw("id-error", log.Fields{"sourceId": sourceId, "targetId": targetId, "error": err})
+ if deviceID, ingressPort, err = splitIntoDeviceIDPortID(pathIds[i].String()); err != nil {
+ log.Errorw("id-error", log.Fields{"sourceId": sourceID, "targetId": targetID, "error": err})
break
}
- if _, egressPort, err = splitIntoDeviceIdPortId(pathIds[i+2].String()); err != nil {
- log.Errorw("id-error", log.Fields{"sourceId": sourceId, "targetId": targetId, "error": err})
+ if _, egressPort, err = splitIntoDeviceIDPortID(pathIds[i+2].String()); err != nil {
+ log.Errorw("id-error", log.Fields{"sourceId": sourceID, "targetId": targetID, "error": err})
break
}
- hop = RouteHop{Ingress: ingressPort, DeviceID: deviceId, Egress: egressPort}
+ hop = RouteHop{Ingress: ingressPort, DeviceID: deviceID, Egress: egressPort}
path = append(path, hop)
}
result = &ofPortLinkToPath{link: OFPortLink{Ingress: sourcePort, Egress: targetPort}, path: path}
@@ -430,7 +448,7 @@
for source, sourcePort := range dg.boundaryPorts {
for target, targetPort := range dg.boundaryPorts {
go dg.buildRoute(source, target, sourcePort, targetPort, ch)
- numBuildRequest += 1
+ numBuildRequest++
}
}
responseReceived := 0
@@ -439,17 +457,15 @@
if responseReceived == numBuildRequest {
break
}
- select {
- case res, ok := <-ch:
- if !ok {
- log.Debug("channel closed")
- break forloop
- }
- if res != nil && len(res.path) > 0 {
- paths[res.link] = res.path
- }
+ res, ok := <-ch
+ if !ok {
+ log.Debug("channel closed")
+ break forloop
}
- responseReceived += 1
+ if res != nil && len(res.path) > 0 {
+ paths[res.link] = res.path
+ }
+ responseReceived++
}
return paths
}
@@ -467,21 +483,21 @@
}
//concatDeviceIdPortId formats a portid using the device id and the port number
-func concatDeviceIdPortId(deviceId string, portNo uint32) string {
- return fmt.Sprintf("%s:%d", deviceId, portNo)
+func concatDeviceIDPortID(deviceID string, portNo uint32) string {
+ return fmt.Sprintf("%s:%d", deviceID, portNo)
}
// splitIntoDeviceIdPortId extracts the device id and port number from the portId
-func splitIntoDeviceIdPortId(id string) (string, uint32, error) {
+func splitIntoDeviceIDPortID(id string) (string, uint32, error) {
result := strings.Split(id, ":")
if len(result) != 2 {
- return "", 0, errors.New(fmt.Sprintf("invalid-id-%s", id))
+ return "", 0, fmt.Errorf("invalid-id-%s", id)
}
- if temp, err := strconv.ParseInt(result[1], 10, 32); err != nil {
- return "", 0, errors.New(fmt.Sprintf("invalid-id-%s-%s", id, err.Error()))
- } else {
- return result[0], uint32(temp), nil
+ temp, err := strconv.ParseInt(result[1], 10, 32)
+ if err != nil {
+ return "", 0, fmt.Errorf("invalid-id-%s-%s", id, err.Error())
}
+ return result[0], uint32(temp), nil
}
//getReverseRoute returns the reverse of the route
diff --git a/rw_core/graph/device_graph_test.go b/rw_core/graph/device_graph_test.go
index cb777fb..a4214e9 100644
--- a/rw_core/graph/device_graph_test.go
+++ b/rw_core/graph/device_graph_test.go
@@ -33,24 +33,24 @@
ld voltha.LogicalDevice
olt voltha.Device
onus map[int][]voltha.Device
- logicalDeviceId string
- oltDeviceId string
+ logicalDeviceID string
+ oltDeviceID string
numCalled int
lock sync.RWMutex
)
func init() {
- logicalDeviceId = "ld"
- oltDeviceId = "olt"
+ logicalDeviceID = "ld"
+ oltDeviceID = "olt"
lock = sync.RWMutex{}
}
func setupDevices(numNNIPort, numPonPortOnOlt, numOnuPerOltPonPort, numUniPerOnu int) {
// Create the OLT and add the NNI ports
- olt = voltha.Device{Id: oltDeviceId, ParentId: logicalDeviceId}
+ olt = voltha.Device{Id: oltDeviceID, ParentId: logicalDeviceID}
olt.Ports = make([]*voltha.Port, 0)
for nniPort := 1; nniPort < numNNIPort+1; nniPort++ {
- p := voltha.Port{PortNo: uint32(nniPort), DeviceId: oltDeviceId, Type: voltha.Port_ETHERNET_NNI}
+ p := voltha.Port{PortNo: uint32(nniPort), DeviceId: oltDeviceID, Type: voltha.Port_ETHERNET_NNI}
olt.Ports = append(olt.Ports, &p)
}
@@ -60,14 +60,14 @@
onusOnPon := make([]voltha.Device, 0)
var onu voltha.Device
oltPeerPort := uint32(pPortNo)
- oltPonPort := voltha.Port{PortNo: uint32(pPortNo), DeviceId: oltDeviceId, Type: voltha.Port_PON_OLT}
+ oltPonPort := voltha.Port{PortNo: uint32(pPortNo), DeviceId: oltDeviceID, Type: voltha.Port_PON_OLT}
oltPonPort.Peers = make([]*voltha.Port_PeerPort, 0)
for i := 0; i < numOnuPerOltPonPort; i++ {
id := fmt.Sprintf("%d-onu-%d", pPortNo, i)
- onu = voltha.Device{Id: id, ParentId: oltDeviceId, ParentPortNo: uint32(pPortNo)}
+ onu = voltha.Device{Id: id, ParentId: oltDeviceID, ParentPortNo: uint32(pPortNo)}
ponPort := voltha.Port{PortNo: 1, DeviceId: onu.Id, Type: voltha.Port_PON_ONU}
ponPort.Peers = make([]*voltha.Port_PeerPort, 0)
- peerPort := voltha.Port_PeerPort{DeviceId: oltDeviceId, PortNo: oltPeerPort}
+ peerPort := voltha.Port_PeerPort{DeviceId: oltDeviceID, PortNo: oltPeerPort}
ponPort.Peers = append(ponPort.Peers, &peerPort)
onu.Ports = make([]*voltha.Port, 0)
onu.Ports = append(onu.Ports, &ponPort)
@@ -84,7 +84,7 @@
}
// Create the logical device
- ld = voltha.LogicalDevice{Id: logicalDeviceId}
+ ld = voltha.LogicalDevice{Id: logicalDeviceID}
ld.Ports = make([]*voltha.LogicalPort, 0)
ofpPortNo := 1
var id string
@@ -114,7 +114,7 @@
func GetDeviceHelper(id string) (*voltha.Device, error) {
lock.Lock()
- numCalled += 1
+ numCalled++
lock.Unlock()
if id == "olt" {
return &olt, nil
@@ -146,7 +146,7 @@
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))
// Create a device graph and computes Routes
start := time.Now()
- dg := NewDeviceGraph(logicalDeviceId, getDevice)
+ dg := NewDeviceGraph(logicalDeviceID, getDevice)
dg.ComputeRoutes(ld.Ports)
assert.NotNil(t, dg.GGraph)
fmt.Println(fmt.Sprintf("Total Time:%dms Total Routes:%d", time.Since(start)/time.Millisecond, len(dg.Routes)))
@@ -167,7 +167,7 @@
// Create a device graph and computes Routes
start := time.Now()
var pt time.Time
- dg := NewDeviceGraph(logicalDeviceId, getDevice)
+ dg := NewDeviceGraph(logicalDeviceID, getDevice)
for k, lp := range ld.Ports {
if k == len(ld.Ports)-1 {
pt = time.Now()
@@ -193,7 +193,7 @@
// Create a device graph and computes Routes
start := time.Now()
var pt time.Time
- dg := NewDeviceGraph(logicalDeviceId, getDevice)
+ dg := NewDeviceGraph(logicalDeviceID, getDevice)
for k, lp := range ld.Ports {
if k == len(ld.Ports)-1 {
pt = time.Now()
@@ -220,7 +220,7 @@
// Create a device graph and computes Routes
start := time.Now()
var pt time.Time
- dg := NewDeviceGraph(logicalDeviceId, getDevice)
+ dg := NewDeviceGraph(logicalDeviceID, getDevice)
for k, lp := range ld.Ports {
if k == len(ld.Ports)-1 {
pt = time.Now()
@@ -247,7 +247,7 @@
// Create a device graph and computes Routes
start := time.Now()
var pt time.Time
- dg := NewDeviceGraph(logicalDeviceId, getDevice)
+ dg := NewDeviceGraph(logicalDeviceID, getDevice)
for k, lp := range ld.Ports {
if k == len(ld.Ports)-1 {
pt = time.Now()
@@ -274,7 +274,7 @@
// Create a device graph and computes Routes
start := time.Now()
var pt time.Time
- dg := NewDeviceGraph(logicalDeviceId, getDevice)
+ dg := NewDeviceGraph(logicalDeviceID, getDevice)
for k, lp := range ld.Ports {
if k == len(ld.Ports)-1 {
pt = time.Now()