VOL-2487 - correct meter state size calculation

Also:
- modified OF read loop to use bufio as opposed to custome buffered io
- cleaned up the OF client state machine processing, including more
  logging

Change-Id: If16f57f6f30a6fe6fecab85b5bf638402f2b7693
diff --git a/internal/pkg/openflow/stats.go b/internal/pkg/openflow/stats.go
index 68d2625..17db448 100644
--- a/internal/pkg/openflow/stats.go
+++ b/internal/pkg/openflow/stats.go
@@ -48,7 +48,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-desc",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -65,7 +65,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-flow",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -82,7 +82,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-aggregate",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -95,7 +95,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-table",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -114,7 +114,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-port",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -130,7 +130,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-queue",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -146,7 +146,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-group",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -162,7 +162,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-group-desc",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -179,7 +179,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-group-features",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -195,7 +195,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-meter",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -211,7 +211,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-meter-config",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -227,7 +227,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-meter-features",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -243,7 +243,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-table-features",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -259,7 +259,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-port-desc",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -276,7 +276,7 @@
 		if logger.V(log.DebugLevel) {
 			reqJs, _ := json.Marshal(statsReq)
 			resJs, _ := json.Marshal(response)
-			logger.Debugw("handle-stats-request",
+			logger.Debugw("handle-stats-request-experimenter",
 				log.Fields{
 					"device-id": ofc.DeviceID,
 					"request":   reqJs,
@@ -465,9 +465,10 @@
 	if err != nil {
 		return nil, err
 	}
-	size := uint16(40)
+	size := uint16(5) // size of stats header
 	var meterStats []*ofp.MeterStats
 	for _, item := range resp.Items {
+		entrySize := uint16(40) // size of entry header
 		meterStat := ofp.NewMeterStats()
 		stats := item.Stats
 		meterStat.DurationNsec = stats.DurationNsec
@@ -481,13 +482,15 @@
 			bandStat.ByteBandCount = bStat.ByteBandCount
 			bandStat.PacketBandCount = bStat.PacketBandCount
 			bandStats = append(bandStats, bandStat)
-			size += 16
+			entrySize += uint16(16) // size of each band stat
 		}
 		meterStat.SetBandStats(bandStats)
-		meterStat.Len = size
+		meterStat.Len = entrySize
 		meterStats = append(meterStats, meterStat)
+		size += entrySize
 	}
 	response.SetEntries(meterStats)
+	response.SetLength(size)
 	return response, nil
 }