VOL-2625 - fix SCA issues

Change-Id: If32cc66371f53040dc242679934a642a9488221a
diff --git a/cmd/ofagent/main.go b/cmd/ofagent/main.go
index f34148e..b7aabd1 100644
--- a/cmd/ofagent/main.go
+++ b/cmd/ofagent/main.go
@@ -56,13 +56,19 @@
 		printBanner()
 	}
 
-	log.SetLogLevel(log.DebugLevel)
-	log.SetDefaultLogger(log.JSON, log.DebugLevel,
-		log.Fields{
-			"component": "ofagent",
-		})
+	if err := log.SetLogLevel(log.DebugLevel); err != nil {
+		log.Errorw("set-log-level", log.Fields{
+			"level": log.DebugLevel,
+			"error": err})
+	}
+	if _, err := log.SetDefaultLogger(log.JSON, log.DebugLevel, log.Fields{"component": "ofagent"}); err != nil {
+		log.Errorw("set-default-log-level", log.Fields{
+			"level": log.DebugLevel,
+			"error": err})
 
-	logLevel := log.WarnLevel
+	}
+
+	var logLevel int
 	switch strings.ToLower(config.LogLevel) {
 	case "fatal":
 		logLevel = log.FatalLevel
@@ -82,12 +88,14 @@
 				"value":     config.LogLevel,
 			})
 		config.LogLevel = "WARN"
-		logLevel = log.FatalLevel
+		logLevel = log.WarnLevel
 	}
-	log.SetDefaultLogger(log.JSON, logLevel,
-		log.Fields{
-			"component": "ofagent",
-		})
+	if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"component": "ofagent"}); err != nil {
+		log.Errorw("set-default-log-level", log.Fields{
+			"level": logLevel,
+			"error": err})
+
+	}
 	log.SetAllLogLevel(logLevel)
 
 	log.Infow("ofagent-startup-configuration",
diff --git a/internal/pkg/ofagent/changeEvent.go b/internal/pkg/ofagent/changeEvent.go
index 20c55de..8972e1c 100644
--- a/internal/pkg/ofagent/changeEvent.go
+++ b/internal/pkg/ofagent/changeEvent.go
@@ -117,7 +117,9 @@
 			ofDesc.SetState(ofp.PortState(desc.GetState()))
 			ofDesc.SetSupported(ofp.PortFeatures(desc.GetSupported()))
 			ofPortStatus.SetDesc(*ofDesc)
-			ofa.getOFClient(deviceID).SendMessage(ofPortStatus)
+			if err := ofa.getOFClient(deviceID).SendMessage(ofPortStatus); err != nil {
+				log.Errorw("handle-change-events-send-message", log.Fields{"error": err})
+			}
 		}
 	}
 
diff --git a/internal/pkg/ofagent/ofagent.go b/internal/pkg/ofagent/ofagent.go
index 25041ab..c81fd58 100644
--- a/internal/pkg/ofagent/ofagent.go
+++ b/internal/pkg/ofagent/ofagent.go
@@ -130,11 +130,9 @@
 		case <-ctx.Done():
 			if volthaDone != nil {
 				volthaDone()
-				volthaDone = nil
 			}
 			if hdlDone != nil {
 				hdlDone()
-				hdlDone = nil
 			}
 			return
 		case event := <-ofa.events:
@@ -150,7 +148,12 @@
 				// Kick off process to attempt to establish
 				// connection to voltha
 				state = ofaStateConnecting
-				go ofa.establishConnectionToVoltha(p)
+				go func() {
+					if err := ofa.establishConnectionToVoltha(p); err != nil {
+						logger.Errorw("voltha-connection-failed", log.Fields{"error": err})
+						panic(err)
+					}
+				}()
 
 			case ofaEventVolthaConnected:
 				logger.Debug("ofagent-voltha-connect-event")
@@ -194,11 +197,15 @@
 					}
 					volthaDone()
 					volthaDone = nil
-					volthaCtx = nil
 				}
 				if state != ofaStateConnecting {
 					state = ofaStateConnecting
-					go ofa.establishConnectionToVoltha(p)
+					go func() {
+						if err := ofa.establishConnectionToVoltha(p); err != nil {
+							log.Errorw("voltha-connection-failed", log.Fields{"error": err})
+							panic(err)
+						}
+					}()
 				}
 
 			case ofaEventError:
diff --git a/internal/pkg/ofagent/packetIn.go b/internal/pkg/ofagent/packetIn.go
index 175a9c8..0f5ad71 100644
--- a/internal/pkg/ofagent/packetIn.go
+++ b/internal/pkg/ofagent/packetIn.go
@@ -166,7 +166,11 @@
 			ofPacketIn.SetTableId(uint8(packetIn.GetTableId()))
 			ofPacketIn.SetTotalLen(uint16(len(ofPacketIn.GetData())))
 			ofc := ofa.getOFClient(deviceID)
-			ofc.SendMessage(ofPacketIn)
+			if err := ofc.SendMessage(ofPacketIn); err != nil {
+				logger.Errorw("send-message-failed", log.Fields{
+					"device-id": deviceID,
+					"error":     err})
+			}
 
 		}
 	}
diff --git a/internal/pkg/openflow/barrier.go b/internal/pkg/openflow/barrier.go
index a72d936..f371cc8 100644
--- a/internal/pkg/openflow/barrier.go
+++ b/internal/pkg/openflow/barrier.go
@@ -34,5 +34,7 @@
 	reply := ofp.NewBarrierReply()
 	reply.SetVersion(4)
 	reply.SetXid(request.GetXid())
-	ofc.SendMessage(reply)
+	if err := ofc.SendMessage(reply); err != nil {
+		log.Errorw("barrier-request-send-message", log.Fields{"error": err})
+	}
 }
diff --git a/internal/pkg/openflow/client.go b/internal/pkg/openflow/client.go
index 97df5d2..d921400 100644
--- a/internal/pkg/openflow/client.go
+++ b/internal/pkg/openflow/client.go
@@ -205,7 +205,12 @@
 				if state == ofcStateCreated {
 					state = ofcStateStarted
 					logger.Debug("STARTED MORE THAN ONCE")
-					go ofc.establishConnectionToController()
+					go func() {
+						if err := ofc.establishConnectionToController(); err != nil {
+							logger.Errorw("controller-connection-failed", log.Fields{"error": err})
+							panic(err)
+						}
+					}()
 				} else {
 					logger.Errorw("illegal-state-transition",
 						log.Fields{
@@ -239,7 +244,12 @@
 						ofDone()
 						ofDone = nil
 					}
-					go ofc.establishConnectionToController()
+					go func() {
+						if err := ofc.establishConnectionToController(); err != nil {
+							log.Errorw("controller-connection-failed", log.Fields{"error": err})
+							panic(err)
+						}
+					}()
 				} else {
 					logger.Errorw("illegal-state-transition",
 						log.Fields{
@@ -275,7 +285,6 @@
 		log.Debugw("closing-child-processes",
 			log.Fields{"device-id": ofc.DeviceID})
 		ofDone()
-		ofDone = nil
 	}
 
 	// If the connection is open, then close it
@@ -351,6 +360,7 @@
 			decoder := goloxi.NewDecoder(messageBuf)
 			msg, err := ofp.DecodeHeader(decoder)
 			if err != nil {
+				// nolint: staticcheck
 				js, _ := json.Marshal(decoder)
 				logger.Errorw("failed-to-decode",
 					log.Fields{
@@ -408,7 +418,11 @@
 	case ofp.OFPTEchoReply:
 	case ofp.OFPTExperimenter:
 	case ofp.OFPTFeaturesRequest:
-		go ofc.handleFeatureRequest(header.(*ofp.FeaturesRequest))
+		go func() {
+			if err := ofc.handleFeatureRequest(header.(*ofp.FeaturesRequest)); err != nil {
+				logger.Errorw("handle-feature-request", log.Fields{"error": err})
+			}
+		}()
 	case ofp.OFPTFeaturesReply:
 	case ofp.OFPTGetConfigRequest:
 		go ofc.handleGetConfigRequest(header.(*ofp.GetConfigRequest))
@@ -440,7 +454,11 @@
 			ofc.handleFlowDeleteStrict(header.(*ofp.FlowDeleteStrict))
 		}
 	case ofp.OFPTStatsRequest:
-		go ofc.handleStatsRequest(header, header.(ofp.IStatsRequest).GetStatsType())
+		go func() {
+			if err := ofc.handleStatsRequest(header, header.(ofp.IStatsRequest).GetStatsType()); err != nil {
+				logger.Errorw("ofpt-stats-request", log.Fields{"error": err})
+			}
+		}()
 	case ofp.OFPTBarrierRequest:
 		/* See note above at case ofp.OFPTFlowMod:*/
 		ofc.handleBarrierRequest(header.(*ofp.BarrierRequest))
@@ -462,7 +480,10 @@
 		return errors.New("no-connection")
 	}
 	enc := goloxi.NewEncoder()
-	msg.Serialize(enc)
+	if err := msg.Serialize(enc); err != nil {
+		return err
+	}
+
 	bytes := enc.Bytes()
 	if _, err := ofc.conn.Write(bytes); err != nil {
 		logger.Errorw("unable-to-send-message-to-controller",
diff --git a/internal/pkg/openflow/echo.go b/internal/pkg/openflow/echo.go
index bb166fa..74f2dc8 100644
--- a/internal/pkg/openflow/echo.go
+++ b/internal/pkg/openflow/echo.go
@@ -33,5 +33,7 @@
 	reply := ofp.NewEchoReply()
 	reply.SetXid(request.GetXid())
 	reply.SetVersion(request.GetVersion())
-	ofc.SendMessage(reply)
+	if err := ofc.SendMessage(reply); err != nil {
+		log.Errorw("handle-echo-request-send-message", log.Fields{"error": err})
+	}
 }
diff --git a/internal/pkg/openflow/feature.go b/internal/pkg/openflow/feature.go
index f456175..5eb8fa0 100644
--- a/internal/pkg/openflow/feature.go
+++ b/internal/pkg/openflow/feature.go
@@ -37,6 +37,9 @@
 	}
 	var id = common.ID{Id: ofc.DeviceID}
 	logicalDevice, err := ofc.VolthaClient.GetLogicalDevice(context.Background(), &id)
+	if err != nil {
+		return err
+	}
 	reply := ofp.NewFeaturesReply()
 	reply.SetVersion(4)
 	reply.SetXid(request.GetXid())
diff --git a/internal/pkg/openflow/getConfig.go b/internal/pkg/openflow/getConfig.go
index 1d92c9c..da5c473 100644
--- a/internal/pkg/openflow/getConfig.go
+++ b/internal/pkg/openflow/getConfig.go
@@ -41,5 +41,7 @@
 				"device-id": ofc.DeviceID,
 				"reply":     js})
 	}
-	ofc.SendMessage(reply)
+	if err := ofc.SendMessage(reply); err != nil {
+		log.Errorw("handle-get-config-request-send-message", log.Fields{"error": err})
+	}
 }
diff --git a/internal/pkg/openflow/role.go b/internal/pkg/openflow/role.go
index 4258c26..3a123e8 100644
--- a/internal/pkg/openflow/role.go
+++ b/internal/pkg/openflow/role.go
@@ -35,5 +35,9 @@
 	reply.SetVersion(request.GetVersion())
 	reply.SetRole(request.GetRole())
 	reply.SetGenerationId(request.GetGenerationId())
-	ofc.SendMessage(reply)
+	if err := ofc.SendMessage(reply); err != nil {
+		logger.Errorw("handle-role-request-send-message", log.Fields{
+			"device-id": ofc.DeviceID,
+			"error":     err})
+	}
 }
diff --git a/internal/pkg/openflow/stats.go b/internal/pkg/openflow/stats.go
index 97d389b..613b476 100644
--- a/internal/pkg/openflow/stats.go
+++ b/internal/pkg/openflow/stats.go
@@ -152,7 +152,7 @@
 					"request":   reqJs,
 					"response":  resJs})
 		}
-		ofc.SendMessage(response)
+		return ofc.SendMessage(response)
 	case ofp.OFPSTGroupDesc:
 		statsReq := request.(*ofp.GroupDescStatsRequest)
 		response, err := ofc.handleGroupStatsDescRequest(statsReq)
@@ -370,7 +370,6 @@
 		}
 		entry.Instructions = instructions
 		entry.Length = entrySize
-		entrySize = 0
 		flow = append(flow, entry)
 	}
 	response.SetEntries(flow)