Skip to content

Commit

Permalink
[Refacoring] Remove Code Smell, apply CamelCase, etc (lf-edge#265)
Browse files Browse the repository at this point in the history
Signed-off-by: Taras Drozdovskyi <t.drozdovsky@gmail.com>

- To reduce the number of warnings that found by SonarCloud and Lint tools, refactoring was performed. 
- This allows us to meet the requirements of the CII Best Practice: elimination of warnings found by the Analysis tools.
  • Loading branch information
tdrozdovsky authored Feb 18, 2021
1 parent 690fde0 commit 988e157
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 73 deletions.
2 changes: 1 addition & 1 deletion cmd/edge-orchestration/capi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,5 @@ func PrintLog(cMsg *C.char) (count C.int) {
}

func main() {

// Do nothing because it is only to build static lWatcher interface
}
4 changes: 2 additions & 2 deletions src/common/commandvalidator/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type commands struct {
mutex *sync.Mutex
}

const NOT_FOUND_REGISTERED_SERVICE = "not found registered service"
const notFoundRegisteredService = "not found registered service"

var commandList commands

Expand All @@ -51,7 +51,7 @@ func (c *commands) GetServiceFileName(serviceName string) (string, error) {

val, ok := c.serviceInfos[serviceName]
if !ok {
return "", errors.New(NOT_FOUND_REGISTERED_SERVICE)
return "", errors.New(notFoundRegisteredService)
}

return val, nil
Expand Down
20 changes: 10 additions & 10 deletions src/common/commandvalidator/commandvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
)

const (
NOT_MATCHED_SERVICE_WITH_EXECUTABLE = "not matched service with executable"
NOT_ALLOWED_EXECUTABLE_SERVICE = "not allowed executable service"
NOT_FOUND_EXECUTABLE_FILE = "not found executable file"
FOUND_INJECTION_COMMAND = "found injection command"
ALREADY_REGISTERED = "already registered service name"
notMatchedServiceWithExecutable = "not matched service with executable"
notAllowedExecutableService = "not allowed executable service"
notFoundExecutableFile = "not found executable file"
foundInjectionCommand = "found injection command"
alreadyRegisteredServiceName = "already registered service name"
)

type ICommandValidator interface {
Expand All @@ -54,12 +54,12 @@ func (CommandValidator) AddWhiteCommand(serviceInfo configuremgrtypes.ServiceInf
}

if blacklist.IsBlack(command) {
return errors.New(NOT_ALLOWED_EXECUTABLE_SERVICE)
return errors.New(notAllowedExecutableService)
}

_, err = commands.GetInstance().GetServiceFileName(serviceInfo.ServiceName)
if err == nil {
return errors.New(ALREADY_REGISTERED)
return errors.New(alreadyRegisteredServiceName)
}

commands.GetInstance().StoreServiceInfo(serviceInfo.ServiceName, command)
Expand All @@ -69,7 +69,7 @@ func (CommandValidator) AddWhiteCommand(serviceInfo configuremgrtypes.ServiceInf
func (CommandValidator) CheckCommand(serviceName string, command []string) error {
fullCommand := strings.Join(command, " ")
if injectionchecker.HasInjectionOperator(fullCommand) {
return errors.New(FOUND_INJECTION_COMMAND)
return errors.New(foundInjectionCommand)
}

expected, err := commands.GetInstance().GetServiceFileName(serviceName)
Expand All @@ -83,7 +83,7 @@ func (CommandValidator) CheckCommand(serviceName string, command []string) error
}

if expected != realCommand {
return errors.New(NOT_MATCHED_SERVICE_WITH_EXECUTABLE)
return errors.New(notMatchedServiceWithExecutable)
}

return nil
Expand All @@ -94,7 +94,7 @@ func getExecutableName(str string) (string, error) {
commandList := strings.Split(str, "/")
switch {
case str == "":
return "", errors.New(NOT_FOUND_EXECUTABLE_FILE)
return "", errors.New(notFoundExecutableFile)
case len(commandList) == 1:
command = commandList[0]
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type requesters struct {
mutex *sync.Mutex
}

const NOT_FOUND_REGISTERED_SERVICE = "not found registered service"
const notFoundRegisteredService = "not found registered service"

var requesterList requesters

Expand All @@ -48,7 +48,7 @@ func init() {
func (r requesters) GetRequester(serviceName string) ([]string, error) {
val, ok := r.requesterInfos[serviceName]
if !ok {
return nil, errors.New(NOT_FOUND_REGISTERED_SERVICE)
return nil, errors.New(notFoundRegisteredService)
}

return val, nil
Expand Down
4 changes: 2 additions & 2 deletions src/common/requestervalidator/requestervalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type IRequesterValidator interface {
CheckRequester(serviceName, requester string) error
}

const NOT_ALLOWED_SERVICE_EXECUTION = "not allowed service execution"
const notAllowedServiceExecution = "not allowed service execution"

type RequesterValidator struct{}

Expand All @@ -43,7 +43,7 @@ func (r RequesterValidator) CheckRequester(serviceName, requester string) error
if allowed := common.HasElem(stored, requester); allowed {
return nil
}
return errors.New(NOT_ALLOWED_SERVICE_EXECUTION)
return errors.New(notAllowedServiceExecution)
}

func (RequesterValidator) GetRequester(serviceName string) ([]string, error) {
Expand Down
1 change: 1 addition & 0 deletions src/controller/configuremgr/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ func GetInstance(configPath string) *ConfigureMgr {

// Watch implements Watcher interface with ConfigureMgr struct
func (cfgMgr ConfigureMgr) Watch(notifier configuremgr.Notifier) {
// Do nothing because it is Watcher interface
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import (
"github.com/lf-edge/edge-home-orchestration-go/src/restinterface/tls"

rafftls "github.com/raff/tls-ext"
"github.com/raff/tls-psk"
psk "github.com/raff/tls-psk"
)

type networkUtilImpl struct{}

var networkUtilIns networkUtilImpl

func init() {

// Do nothing because there is no need to initialize anything
}

//NetworkUtil interface declares the network methods
Expand Down
4 changes: 2 additions & 2 deletions src/controller/discoverymgr/mnedc/tunmgr/tun.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ package tunmgr

import (
"fmt"
"github.com/lf-edge/edge-home-orchestration-go/src/common/logmgr"
"net"
"os/exec"
"strings"

"github.com/lf-edge/edge-home-orchestration-go/src/common/logmgr"
"github.com/songgao/water"
)

Expand All @@ -36,7 +36,7 @@ var (
)

func init() {

// Do nothing because there is no need to initialize anything
}

//Tun interface declares methods related to setting tun network interface
Expand Down
26 changes: 13 additions & 13 deletions src/controller/scoringmgr/scoringmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
)

const (
logPrefix = "scoringmgr"
INVALID_SCORE = 0.0
logPrefix = "scoringmgr"
InvalidScore = 0.0
)

// Scoring is the interface to apply application specific scoring functions
Expand Down Expand Up @@ -71,36 +71,36 @@ func (ScoringImpl) GetResource(ID string) (resource map[string]interface{}, err
resource = make(map[string]interface{})
cpuUsage, err := resourceIns.GetResource(resourceutil.CPUUsage)
if err != nil {
resource["error"] = INVALID_SCORE
resource["error"] = InvalidScore
return
}
resource["cpuUsage"] = cpuUsage

cpuCount, err := resourceIns.GetResource(resourceutil.CPUCount)
if err != nil {
resource["error"] = INVALID_SCORE
resource["error"] = InvalidScore
return
}
resource["cpuCount"] = cpuCount

cpuFreq, err := resourceIns.GetResource(resourceutil.CPUFreq)
if err != nil {
resource["error"] = INVALID_SCORE
resource["error"] = InvalidScore
return
}
resource["cpuFreq"] = cpuFreq

netBandwidth, err := resourceIns.GetResource(resourceutil.NetBandwidth)
if err != nil {
resource["error"] = INVALID_SCORE
resource["error"] = InvalidScore
return
}
resource["netBandwidth"] = netBandwidth

resourceIns.SetDeviceID(ID)
rtt, err := resourceIns.GetResource(resourceutil.NetRTT)
if err != nil {
resource["error"] = INVALID_SCORE
resource["error"] = InvalidScore
return
}
resource["rtt"] = rtt
Expand All @@ -111,7 +111,7 @@ func (ScoringImpl) GetResource(ID string) (resource map[string]interface{}, err
// GetScoreWithResource provides score value of an edge device
func (ScoringImpl) GetScoreWithResource(resource map[string]interface{}) (scoreValue float64, err error) {
if _, found := resource["error"]; found {
return INVALID_SCORE, errors.New("Resource Not Found")
return InvalidScore, errors.New("Resource Not Found")
}

cpuScore := cpuScore(resource["cpuUsage"].(float64), resource["cpuCount"].(float64), resource["cpuFreq"].(float64))
Expand All @@ -123,28 +123,28 @@ func (ScoringImpl) GetScoreWithResource(resource map[string]interface{}) (scoreV
func calculateScore(ID string) float64 {
cpuUsage, err := resourceIns.GetResource(resourceutil.CPUUsage)
if err != nil {
return INVALID_SCORE
return InvalidScore
}
cpuCount, err := resourceIns.GetResource(resourceutil.CPUCount)
if err != nil {
return INVALID_SCORE
return InvalidScore
}
cpuFreq, err := resourceIns.GetResource(resourceutil.CPUFreq)
if err != nil {
return INVALID_SCORE
return InvalidScore
}
cpuScore := cpuScore(cpuUsage, cpuCount, cpuFreq)

netBandwidth, err := resourceIns.GetResource(resourceutil.NetBandwidth)
if err != nil {
return INVALID_SCORE
return InvalidScore
}
netScore := netScore(netBandwidth)

resourceIns.SetDeviceID(ID)
rtt, err := resourceIns.GetResource(resourceutil.NetRTT)
if err != nil {
return INVALID_SCORE
return InvalidScore
}
renderingScore := renderingScore(rtt)

Expand Down
18 changes: 9 additions & 9 deletions src/orchestrationapi/orchestrationapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ type ResponseService struct {
}

const (
ERROR_NONE = "ERROR_NONE"
INVALID_PARAMETER = "INVALID_PARAMETER"
SERVICE_NOT_FOUND = "SERVICE_NOT_FOUND"
INTERNAL_SERVER_ERROR = "INTERNAL_SERVER_ERROR"
NOT_ALLOWED_COMMAND = "NOT_ALLOWED_COMMAND"
ErrorNone = "ERROR_NONE"
InvalidParameter = "INVALID_PARAMETER"
ServiceNotFound = "SERVICE_NOT_FOUND"
InternalServerError = "INTERNAL_SERVER_ERROR"
NotAllowedCommand = "NOT_ALLOWED_COMMAND"
)

var (
Expand All @@ -123,7 +123,7 @@ func (orcheEngine *orcheImpl) RequestService(serviceInfo ReqeustService) Respons

if orcheEngine.Ready == false {
return ResponseService{
Message: INTERNAL_SERVER_ERROR,
Message: InternalServerError,
ServiceName: serviceInfo.ServiceName,
RemoteTargetInfo: TargetInfo{},
}
Expand Down Expand Up @@ -162,7 +162,7 @@ func (orcheEngine *orcheImpl) RequestService(serviceInfo ReqeustService) Respons
}

errorResp := ResponseService{
Message: SERVICE_NOT_FOUND,
Message: ServiceNotFound,
ServiceName: serviceInfo.ServiceName,
RemoteTargetInfo: TargetInfo{},
}
Expand All @@ -184,7 +184,7 @@ func (orcheEngine *orcheImpl) RequestService(serviceInfo ReqeustService) Respons

if len(deviceScores) <= 0 {
return errorResp
} else if deviceScores[0].score == scoringmgr.INVALID_SCORE {
} else if deviceScores[0].score == scoringmgr.InvalidScore {
return errorResp
}

Expand Down Expand Up @@ -238,7 +238,7 @@ func (orcheEngine *orcheImpl) RequestService(serviceInfo ReqeustService) Respons
log.Println("[orchestrationapi] ", deviceScores)

return ResponseService{
Message: ERROR_NONE,
Message: ErrorNone,
ServiceName: serviceInfo.ServiceName,
RemoteTargetInfo: TargetInfo{
ExecutionType: deviceScores[0].execType,
Expand Down
6 changes: 3 additions & 3 deletions src/orchestrationapi/orchestrationapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestRequestService(t *testing.T) {
oche.Ready = true

res := oche.RequestService(requestServiceInfo)
if res.Message != ERROR_NONE {
if res.Message != ErrorNone {
t.Error("unexpected handle")
}
})
Expand All @@ -119,7 +119,7 @@ func TestRequestService(t *testing.T) {

oche := getOrcheImple()
res := oche.RequestService(requestServiceInfo)
if res.Message != INTERNAL_SERVER_ERROR {
if res.Message != InternalServerError {
t.Error("unexpected Error")
}
})
Expand All @@ -139,7 +139,7 @@ func TestRequestService(t *testing.T) {
oche.Ready = true

res := oche.RequestService(requestServiceInfo)
if res.Message == ERROR_NONE {
if res.Message == ErrorNone {
t.Error("unexpected Error")
}
})
Expand Down
10 changes: 5 additions & 5 deletions src/restinterface/externalhandler/externalhandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (h *Handler) APIV1RequestServicePost(w http.ResponseWriter, r *http.Request
if isParseRequesterFromPort != true {
serviceRequester, ok := appCommand["ServiceRequester"].(string)
if !ok {
responseMsg = orchestrationapi.INVALID_PARAMETER
responseMsg = orchestrationapi.InvalidParameter
responseName = ""
goto SEND_RESP
}
Expand All @@ -179,15 +179,15 @@ func (h *Handler) APIV1RequestServicePost(w http.ResponseWriter, r *http.Request

name, ok = appCommand["ServiceName"].(string)
if !ok {
responseMsg = orchestrationapi.INVALID_PARAMETER
responseMsg = orchestrationapi.InvalidParameter
responseName = ""
goto SEND_RESP
}
serviceInfos.ServiceName = name

executeEnvs, ok = appCommand["ServiceInfo"].([]interface{})
if !ok {
responseMsg = orchestrationapi.INVALID_PARAMETER
responseMsg = orchestrationapi.InvalidParameter
responseName = name
goto SEND_RESP
}
Expand All @@ -197,15 +197,15 @@ func (h *Handler) APIV1RequestServicePost(w http.ResponseWriter, r *http.Request
tmp := executeEnv.(map[string]interface{})
exeType, ok := tmp["ExecutionType"].(string)
if !ok {
responseMsg = orchestrationapi.INVALID_PARAMETER
responseMsg = orchestrationapi.InvalidParameter
responseName = name
goto SEND_RESP
}
serviceInfos.ServiceInfo[idx].ExecutionType = exeType

exeCmd, ok := tmp["ExecCmd"].([]interface{})
if !ok {
responseMsg = orchestrationapi.INVALID_PARAMETER
responseMsg = orchestrationapi.InvalidParameter
responseName = name
goto SEND_RESP
}
Expand Down
Loading

0 comments on commit 988e157

Please sign in to comment.