Move common/utils.go to device/utils.go.

The utility functions ConvB2S(), OnuToSn(), LoggerWithOnu() in common/utils.go strongly depend on ONU instance specification.
Then, I consider it should be in device package that contains ONU instance , not utils package.

Change-Id: I7fd2b21c5f7c51be8ec4a5c161da9c93e3a3f582
diff --git a/common/logger/logger.go b/common/logger/logger.go
index cb7229f..6efe0f1 100644
--- a/common/logger/logger.go
+++ b/common/logger/logger.go
@@ -17,9 +17,10 @@
 package logger
 
 import (
+	"time"
+
 	lkh "github.com/gfremex/logrus-kafka-hook"
 	log "github.com/sirupsen/logrus"
-	"time"
 )
 
 var (
@@ -34,17 +35,17 @@
 	myLogger = logger.WithField("topics", []string{"bbsim.log"})
 
 	var logLevel = log.DebugLevel
-	switch level{
-		case "TRACE":
-			logLevel = log.TraceLevel
-		case "INFO":
-			logLevel = log.InfoLevel
-		case "WARN":
-			logLevel = log.WarnLevel
-		case "ERROR":
-			logLevel = log.ErrorLevel
-		default:
-			logLevel = log.DebugLevel
+	switch level {
+	case "TRACE":
+		logLevel = log.TraceLevel
+	case "INFO":
+		logLevel = log.InfoLevel
+	case "WARN":
+		logLevel = log.WarnLevel
+	case "ERROR":
+		logLevel = log.ErrorLevel
+	default:
+		logLevel = log.DebugLevel
 	}
 	logger.Println("Setting Log Level ", logLevel)
 	logger.SetLevel(logLevel)
diff --git a/core/api_service.go b/core/api_service.go
index a826594..1800e9e 100644
--- a/core/api_service.go
+++ b/core/api_service.go
@@ -22,15 +22,13 @@
 	"net/http"
 	"sync"
 
-	"gerrit.opencord.org/voltha-bbsim/device"
-
 	pb "gerrit.opencord.org/voltha-bbsim/api"
+	"gerrit.opencord.org/voltha-bbsim/common/logger"
+	"gerrit.opencord.org/voltha-bbsim/device"
 	"github.com/grpc-ecosystem/grpc-gateway/runtime"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/codes"
 	"google.golang.org/grpc/status"
-
-	"gerrit.opencord.org/voltha-bbsim/common/logger"
 )
 
 // Response Constants
diff --git a/core/core_server.go b/core/core_server.go
index 4c4fcab..69c0bc8 100644
--- a/core/core_server.go
+++ b/core/core_server.go
@@ -26,7 +26,6 @@
 
 	pb "gerrit.opencord.org/voltha-bbsim/api"
 	"gerrit.opencord.org/voltha-bbsim/common/logger"
-	"gerrit.opencord.org/voltha-bbsim/common/utils"
 	"gerrit.opencord.org/voltha-bbsim/device"
 	flowHandler "gerrit.opencord.org/voltha-bbsim/flow"
 	openolt "gerrit.opencord.org/voltha-bbsim/protos"
@@ -169,7 +168,7 @@
 	for i := 0; i < MaxOnusPerPon; i++ {
 		oltid := s.Olt.ID
 		intfid := uint32(1)
-		sn := utils.ConvB2S(device.NewSN(oltid, intfid, uint32(i)))
+		sn := device.ConvB2S(device.NewSN(oltid, intfid, uint32(i)))
 		s.CtagMap[sn] = uint32(900 + i) // This is hard coded for BBWF
 	}
 
@@ -573,18 +572,18 @@
 				logger.Error("Failed to GetOnuByID:%d", onuid)
 				continue
 			}
-			sn := utils.ConvB2S(onu.SerialNumber.VendorSpecific)
+			sn := device.ConvB2S(onu.SerialNumber.VendorSpecific)
 			if ctag, ok := s.CtagMap[sn]; ok == true {
 				tagpkt, err := PushVLAN(pkt, uint16(ctag), onu)
 				if err != nil {
-					utils.LoggerWithOnu(onu).WithFields(log.Fields{
+					device.LoggerWithOnu(onu).WithFields(log.Fields{
 						"gemId": gemid,
 					}).Error("Fail to tag C-tag")
 				} else {
 					pkt = tagpkt
 				}
 			} else {
-				utils.LoggerWithOnu(onu).WithFields(log.Fields{
+				device.LoggerWithOnu(onu).WithFields(log.Fields{
 					"gemId":   gemid,
 					"cTagMap": s.CtagMap,
 				}).Error("Could not find onuid in CtagMap", onuid, sn, s.CtagMap)
@@ -609,7 +608,7 @@
 			intfid := nnipkt.Info.intfid
 			onu, _ := s.GetOnuByID(onuid, intfid)
 
-			utils.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
+			device.LoggerWithOnu(onu).Info("Received packet from NNI in grpc Server.")
 
 			pkt := nnipkt.Pkt
 			data = &openolt.Indication_PktInd{PktInd: &openolt.PacketIndication{IntfType: "nni", IntfId: intfid, Pkt: pkt.Data()}}
@@ -637,12 +636,12 @@
 		pkt, _ := layerEth.(*layers.Ethernet)
 		ethtype := pkt.EthernetType
 		if ethtype == layers.EthernetTypeEAPOL {
-			utils.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
+			device.LoggerWithOnu(onu).Info("Received downstream packet is EAPOL.")
 			eapolPkt := byteMsg{IntfId: intfid, OnuId: onuid, Byte: rawpkt.Data()}
 			s.eapolOut <- &eapolPkt
 			return nil
 		} else if layerDHCP := rawpkt.Layer(layers.LayerTypeDHCPv4); layerDHCP != nil {
-			utils.LoggerWithOnu(onu).WithFields(log.Fields{
+			device.LoggerWithOnu(onu).WithFields(log.Fields{
 				"payload": layerDHCP.LayerPayload(),
 				"type":    layerDHCP.LayerType().String(),
 			}).Info("Received downstream packet is DHCP.")
@@ -653,7 +652,7 @@
 			s.dhcpOut <- &dhcpPkt
 			return nil
 		} else {
-			utils.LoggerWithOnu(onu).Info("WARNING: Received packet is not EAPOL or DHCP")
+			device.LoggerWithOnu(onu).Warn("WARNING: Received packet is not EAPOL or DHCP")
 			return nil
 		}
 		ioinfo, err := s.identifyUniIoinfo("inside", intfid, onuid)
@@ -664,7 +663,7 @@
 		SendUni(handle, rawpkt, onu)
 		return nil
 	}
-	utils.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
+	device.LoggerWithOnu(onu).Info("WARNING: Received packet does not have layerEth")
 	return nil
 }
 
@@ -771,7 +770,7 @@
 }
 
 func stringifySerialNumber(serialNum *openolt.SerialNumber) string {
-	return string(serialNum.VendorId) + utils.ConvB2S(serialNum.VendorSpecific)
+	return string(serialNum.VendorId) + device.ConvB2S(serialNum.VendorSpecific)
 }
 
 func getOpenoltSerialNumber(SerialNumber string) (*openolt.SerialNumber, error) {
diff --git a/core/grpc_service.go b/core/grpc_service.go
index 2d216d8..edf7d1b 100644
--- a/core/grpc_service.go
+++ b/core/grpc_service.go
@@ -20,7 +20,6 @@
 	"net"
 
 	"gerrit.opencord.org/voltha-bbsim/common/logger"
-	"gerrit.opencord.org/voltha-bbsim/common/utils"
 	"gerrit.opencord.org/voltha-bbsim/device"
 	flowHandler "gerrit.opencord.org/voltha-bbsim/flow"
 	openolt "gerrit.opencord.org/voltha-bbsim/protos"
@@ -187,12 +186,12 @@
 		logger.Error("Failed in OnuPacketOut, %v", err)
 		return new(openolt.Empty), err
 	}
-	utils.LoggerWithOnu(onu).Debugf("OLT %d receives OnuPacketOut () to IF-ID:%d ONU-ID %d.", s.Olt.ID, packet.IntfId, packet.OnuId)
+	device.LoggerWithOnu(onu).Debugf("OLT %d receives OnuPacketOut () to IF-ID:%d ONU-ID %d.", s.Olt.ID, packet.IntfId, packet.OnuId)
 	onuid := packet.OnuId
 	intfid := packet.IntfId
 	rawpkt := gopacket.NewPacket(packet.Pkt, layers.LayerTypeEthernet, gopacket.Default)
 	if err := s.onuPacketOut(intfid, onuid, rawpkt); err != nil {
-		utils.LoggerWithOnu(onu).WithField("error", err).Errorf("OnuPacketOut Error ")
+		device.LoggerWithOnu(onu).WithField("error", err).Errorf("OnuPacketOut Error ")
 		return new(openolt.Empty), err
 	}
 	return new(openolt.Empty), nil
@@ -234,7 +233,7 @@
 	if err == nil {
 		onu.GemportID = uint16(flow.GemportId)
 
-		utils.LoggerWithOnu(onu).WithFields(log.Fields{
+		device.LoggerWithOnu(onu).WithFields(log.Fields{
 			"olt":   s.Olt.ID,
 			"c_tag": flow.Action.IVid,
 		}).Debug("OLT receives FlowAdd().")
@@ -293,7 +292,7 @@
 	} else {
 		// Delete flowID from onu
 		onu.DeleteFlowID(flow.FlowId)
-		utils.LoggerWithOnu(onu).WithFields(log.Fields{
+		device.LoggerWithOnu(onu).WithFields(log.Fields{
 			"olt":   s.Olt.ID,
 			"c_tag": flow.Action.IVid,
 		}).Debug("OLT receives FlowRemove().")
diff --git a/core/io_worker.go b/core/io_worker.go
index c59b903..00ecda3 100644
--- a/core/io_worker.go
+++ b/core/io_worker.go
@@ -18,14 +18,14 @@
 
 import (
 	"errors"
+	"net"
+	"strconv"
+
 	"gerrit.opencord.org/voltha-bbsim/common/logger"
-	"gerrit.opencord.org/voltha-bbsim/common/utils"
 	"gerrit.opencord.org/voltha-bbsim/device"
 	"github.com/google/gopacket"
 	"github.com/google/gopacket/layers"
 	"github.com/google/gopacket/pcap"
-	"net"
-	"strconv"
 )
 
 // RecvWorker receives the packet and forwards to the channel
@@ -46,9 +46,9 @@
 func SendUni(handle *pcap.Handle, packet gopacket.Packet, onu *device.Onu) {
 	err := handle.WritePacketData(packet.Data())
 	if err != nil {
-		utils.LoggerWithOnu(onu).Errorf("Error in send packet to UNI-IF: %v e:%v", *handle, err)
+		device.LoggerWithOnu(onu).Errorf("Error in send packet to UNI-IF: %v e:%v", *handle, err)
 	}
-	utils.LoggerWithOnu(onu).Debugf("Successfully send packet to UNI-IF: %v", *handle)
+	device.LoggerWithOnu(onu).Debugf("Successfully send packet to UNI-IF: %v", *handle)
 }
 
 // SendNni sends packaet to NNI interface
@@ -117,7 +117,7 @@
 			layers.LayerTypeEthernet,
 			gopacket.Default,
 		)
-		utils.LoggerWithOnu(onu).Debugf("Push the 802.1Q header (VID: %d)", vid)
+		device.LoggerWithOnu(onu).Debugf("Push the 802.1Q header (VID: %d)", vid)
 		return ret, nil
 	}
 	return nil, errors.New("failed to push vlan")
@@ -177,7 +177,7 @@
 		snapshotLen int32 = 1518
 		promiscuous       = false
 		err         error
-		timeout           = pcap.BlockForever
+		timeout     = pcap.BlockForever
 	)
 	handle, err := pcap.OpenLive(device, snapshotLen, promiscuous, timeout)
 	if err != nil {
diff --git a/core/omci.go b/core/omci.go
index a83c07a..2566f2a 100644
--- a/core/omci.go
+++ b/core/omci.go
@@ -18,6 +18,7 @@
 
 import (
 	"context"
+
 	"gerrit.opencord.org/voltha-bbsim/common/logger"
 	openolt "gerrit.opencord.org/voltha-bbsim/protos"
 	omci "github.com/opencord/omci-sim"
@@ -43,7 +44,6 @@
 					omciIn <- resp
 					s.handleOmciAction(resp.Pkt, resp.IntfId, resp.OnuId)
 
-
 				case *omci.OmciError:
 					// Error in processing omci message. Log and carry on.
 					logger.Debug("%s", err.Msg)
diff --git a/core/openolt_service.go b/core/openolt_service.go
index dc48577..489e185 100644
--- a/core/openolt_service.go
+++ b/core/openolt_service.go
@@ -20,7 +20,6 @@
 	"time"
 
 	"gerrit.opencord.org/voltha-bbsim/common/logger"
-	"gerrit.opencord.org/voltha-bbsim/common/utils"
 	"gerrit.opencord.org/voltha-bbsim/device"
 	openolt "gerrit.opencord.org/voltha-bbsim/protos"
 )
@@ -88,24 +87,24 @@
 		logger.Error("Failed to send ONUDiscInd [id: %d]: %v", onu.OnuID, err)
 		return err
 	}
-	utils.LoggerWithOnu(onu).Info("sendONUDiscInd Onuid")
+	device.LoggerWithOnu(onu).Info("sendONUDiscInd Onuid")
 	return nil
 }
 
 func sendOnuInd(stream openolt.Openolt_EnableIndicationServer, onu *device.Onu, delay int, operState string, adminState string) error {
 	time.Sleep(time.Duration(delay) * time.Millisecond)
 	data := &openolt.Indication_OnuInd{OnuInd: &openolt.OnuIndication{
-		IntfId:         onu.IntfID,
-		OnuId:          onu.OnuID,
-		OperState:      operState,
-		AdminState:     adminState,
-		SerialNumber:   onu.SerialNumber,
+		IntfId:       onu.IntfID,
+		OnuId:        onu.OnuID,
+		OperState:    operState,
+		AdminState:   adminState,
+		SerialNumber: onu.SerialNumber,
 	}}
 	if err := stream.Send(&openolt.Indication{Data: data}); err != nil {
 		logger.Error("Failed to send ONUInd [id: %d]: %v", onu.OnuID, err)
 		return err
 	}
-	utils.LoggerWithOnu(onu).Info("sendONUInd Onuid")
+	device.LoggerWithOnu(onu).Info("sendONUInd Onuid")
 	return nil
 }
 
diff --git a/device/device_olt.go b/device/device_olt.go
index 5f34a03..b3dc2c3 100644
--- a/device/device_olt.go
+++ b/device/device_olt.go
@@ -48,8 +48,8 @@
 	Name               string
 	InternalState      DeviceState
 	OperState          string
-	NniIntfs		   []nniIntf
-	PonIntfs		   []ponIntf
+	NniIntfs           []nniIntf
+	PonIntfs           []ponIntf
 	HeartbeatSignature uint32
 	mu                 *sync.Mutex
 }
@@ -186,4 +186,4 @@
 func (olt *Olt) UpdatePonPortState(portID uint32, alarmState AlarmState, operState string) {
 	olt.PonIntfs[portID].AlarmState = alarmState
 	olt.PonIntfs[portID].OperState = operState
-}
\ No newline at end of file
+}
diff --git a/common/utils/utils.go b/device/utils.go
similarity index 69%
rename from common/utils/utils.go
rename to device/utils.go
index 3bfd0de..bf5bfaa 100644
--- a/common/utils/utils.go
+++ b/device/utils.go
@@ -14,32 +14,23 @@
  * limitations under the License.
  */
 
-package utils
+package device
 
 import (
-	"strconv"
+	"fmt"
 
 	"gerrit.opencord.org/voltha-bbsim/common/logger"
-	"gerrit.opencord.org/voltha-bbsim/device"
 	log "github.com/sirupsen/logrus"
+	"strconv"
 )
 
-// ConvB2S converts byte array to string
-func ConvB2S(b []byte) string {
-	s := ""
-	for _, i := range b {
-		s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
-	}
-	return s
+func OnuToSn(onu *Onu) string {
+	// FIXME
+	// see https://github.com/opencord/voltha/blob/master/voltha/adapters/openolt/openolt_device.py#L929-L943
+	return string(onu.SerialNumber.VendorId) + "00000" + fmt.Sprint(onu.IntfID) + "0" + fmt.Sprintf("%x", onu.OnuID-1)
 }
 
-// OnuToSn returns serial number in string format for given ONU
-func OnuToSn(onu *device.Onu) string {
-	return string(onu.SerialNumber.VendorId) + ConvB2S(onu.SerialNumber.VendorSpecific)
-}
-
-// LoggerWithOnu method logs ONU fields
-func LoggerWithOnu(onu *device.Onu) *log.Entry {
+func LoggerWithOnu(onu *Onu) *log.Entry {
 
 	if onu == nil {
 		logger.Warn("utils.LoggerWithOnu has been called without Onu")
@@ -48,8 +39,16 @@
 
 	return logger.GetLogger().WithFields(log.Fields{
 		"serial_number": OnuToSn(onu),
-		"interfaceID":   onu.IntfID,
-		"onuID":         onu.OnuID,
-		"oltID":         onu.OltID,
+		"interfaceId":   onu.IntfID,
+		"onuId":         onu.OnuID,
+		"oltId":         onu.OltID,
 	})
 }
+
+func ConvB2S(b []byte) string {
+	s := ""
+	for _, i := range b {
+		s = s + strconv.FormatInt(int64(i/16), 16) + strconv.FormatInt(int64(i%16), 16)
+	}
+	return s
+}