VOL-2408 - Refactor / Use voltha-lib-go Logging

Change-Id: I6e7d9eaba49c104cd50bb5692a2ff9733014fac4
diff --git a/openflow/flowMod.go b/openflow/flowMod.go
index 53ec8c9..eb6ba02 100644
--- a/openflow/flowMod.go
+++ b/openflow/flowMod.go
@@ -19,9 +19,11 @@
 import (
 	"context"
 	"encoding/json"
-	"log"
+
+	"github.com/opencord/ofagent-go/settings"
 
 	ofp "github.com/donNewtonAlpha/goloxi/of13"
+	l "github.com/opencord/voltha-lib-go/v2/pkg/log"
 	"github.com/opencord/voltha-protos/v2/go/openflow_13"
 	pb "github.com/opencord/voltha-protos/v2/go/voltha"
 )
@@ -69,18 +71,19 @@
 	"ipv6_exthdr":    39,
 }
 
-func handleFlowAdd(flowAdd *ofp.FlowAdd, deviceId string) {
-	js, _ := json.Marshal(flowAdd)
-	log.Printf("handleFlowAdd called with %s", js)
+func handleFlowAdd(flowAdd *ofp.FlowAdd, DeviceID string) {
+	if settings.GetDebug(DeviceID) {
+		js, _ := json.Marshal(flowAdd)
+		logger.Debugw("handleFlowAdd called", l.Fields{"DeviceID": DeviceID, "params": js})
+	}
 
 	var flowUpdate openflow_13.FlowTableUpdate
-	flowUpdate.Id = deviceId
+	flowUpdate.Id = DeviceID
 	var flowMod pb.OfpFlowMod
 	flowMod.Cookie = flowAdd.Cookie
 	flowMod.CookieMask = flowAdd.CookieMask
 	flowMod.TableId = uint32(flowAdd.TableId)
-	//flowMod.Command = pb.OfpFlowModCommand_OFPFC_ADD
-	flowMod.Command = 0
+	flowMod.Command = pb.OfpFlowModCommand_OFPFC_ADD
 	flowMod.IdleTimeout = uint32(flowAdd.IdleTimeout)
 	flowMod.HardTimeout = uint32(flowAdd.HardTimeout)
 	flowMod.Priority = uint32(flowAdd.Priority)
@@ -88,8 +91,6 @@
 	flowMod.OutPort = uint32(flowAdd.OutPort)
 	flowMod.OutGroup = uint32(flowAdd.OutGroup)
 	flowMod.Flags = uint32(flowAdd.Flags)
-	js, _ = json.Marshal(flowMod)
-	log.Printf("HANDLE FLOW ADD FLOW_MOD %s", js)
 	inMatch := flowAdd.Match
 	var flowMatch pb.OfpMatch
 	flowMatch.Type = pb.OfpMatchType(inMatch.GetType())
@@ -97,23 +98,15 @@
 	inOxmList := inMatch.GetOxmList()
 	for i := 0; i < len(inOxmList); i++ {
 		oxmField := inOxmList[i]
-		j, _ := json.Marshal(oxmField)
-
 		name := oxmMap[oxmField.GetOXMName()]
-		log.Printf("\n\n\n %s  %d  %s\n\n\n", j, name, oxmField.GetOXMName())
-
 		val := oxmField.GetOXMValue()
 		var ofpOxmField pb.OfpOxmField
 		ofpOxmField.OxmClass = ofp.OFPXMCOpenflowBasic
 		var field pb.OfpOxmOfbField
-
 		field.Type = pb.OxmOfbFieldTypes(name)
-		log.Println("****\nFieldType: " + openflow_13.OxmOfbFieldTypes_name[name] + "\n\n\n\n")
-
 		var x openflow_13.OfpOxmField_OfbField
 		x.OfbField = &field
 		ofpOxmField.Field = &x
-
 		switch pb.OxmOfbFieldTypes(name) {
 		case pb.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT:
 			port := val.(ofp.Port)
@@ -144,7 +137,6 @@
 			udpSrc := val.(uint16)
 			var value pb.OfpOxmOfbField_UdpSrc
 			value.UdpSrc = uint32(udpSrc)
-			log.Printf("udpSrc %v", udpSrc)
 			field.Value = &value
 		case pb.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST:
 			udpDst := val.(uint16)
@@ -224,37 +216,47 @@
 	flowMod.Instructions = instructions
 	flowUpdate.FlowMod = &flowMod
 	grpcClient := *getGrpcClient()
-	flowUpdateJs, _ := json.Marshal(flowUpdate)
-	log.Printf("FLOW UPDATE %s", flowUpdateJs)
-	empty, err := grpcClient.UpdateLogicalDeviceFlowTable(context.Background(), &flowUpdate)
-	if err != nil {
-		log.Printf("ERROR DOING FLOW MOD ADD %v", err)
+	if settings.GetDebug(DeviceID) {
+		flowUpdateJs, _ := json.Marshal(flowUpdate)
+		logger.Debugf("FlowUpdate being sent to Voltha", l.Fields{"DeviceID": DeviceID, "FlowModRequest": flowUpdateJs})
 	}
-	emptyJs, _ := json.Marshal(empty)
-	log.Printf("FLOW MOD RESPONSE %s", emptyJs)
-
+	_, err := grpcClient.UpdateLogicalDeviceFlowTable(context.Background(), &flowUpdate)
+	if err != nil {
+		logger.Errorw("Error calling FlowUpdate ", l.Fields{"DeviceID": DeviceID, "error": err})
+	}
 }
 
-func handleFlowMod(flowMod *ofp.FlowMod, deviceId string) {
-	js, _ := json.Marshal(flowMod)
-	log.Printf("handleFlowMod called with %s", js)
+func handleFlowMod(flowMod *ofp.FlowMod, DeviceID string) {
+	if settings.GetDebug(DeviceID) {
+		js, _ := json.Marshal(flowMod)
+		logger.Debugw("handleMod called", l.Fields{"DeviceID": DeviceID, "params": js})
+	}
+	logger.Error("handleFlowMod not implemented")
 }
 
-func handleFlowModStrict(flowModStrict *ofp.FlowModifyStrict, deviceId string) {
-	js, _ := json.Marshal(flowModStrict)
-	log.Printf("handleFlowModStrict called with %s", js)
+func handleFlowModStrict(flowModStrict *ofp.FlowModifyStrict, DeviceID string) {
+	if settings.GetDebug(DeviceID) {
+		js, _ := json.Marshal(flowModStrict)
+		logger.Debugw("handleFlowModStrict called", l.Fields{"DeviceID": DeviceID, "params": js})
+	}
+	logger.Error("handleFlowModStrict not implemented")
 }
-func handleFlowDelete(flowDelete *ofp.FlowDelete, deviceId string) {
-	js, _ := json.Marshal(flowDelete)
-	log.Printf("handleFlowDelete called with %s", js)
+func handleFlowDelete(flowDelete *ofp.FlowDelete, DeviceID string) {
+	if settings.GetDebug(DeviceID) {
+		js, _ := json.Marshal(flowDelete)
+		logger.Debugw("handleFlowDelete called", l.Fields{"DeviceID": DeviceID, "params": js})
+	}
+	logger.Error("handleFlowDelete not implemented")
 
 }
-func handleFlowDeleteStrict(flowDeleteStrict *ofp.FlowDeleteStrict, deviceId string) {
-	js, _ := json.Marshal(flowDeleteStrict)
-	log.Printf("handleFlowDeleteStrict called with %s", js)
+func handleFlowDeleteStrict(flowDeleteStrict *ofp.FlowDeleteStrict, DeviceID string) {
+	if settings.GetDebug(DeviceID) {
+		js, _ := json.Marshal(flowDeleteStrict)
+		logger.Debugw("handleFlowAdd called", l.Fields{"DeviceID": DeviceID, "params": js})
+	}
 
 	var flowUpdate openflow_13.FlowTableUpdate
-	flowUpdate.Id = deviceId
+	flowUpdate.Id = DeviceID
 	var flowMod pb.OfpFlowMod
 	flowMod.Cookie = flowDeleteStrict.Cookie
 	flowMod.CookieMask = flowDeleteStrict.CookieMask
@@ -274,18 +276,12 @@
 	inOxmList := inMatch.GetOxmList()
 	for i := 0; i < len(inOxmList); i++ {
 		oxmField := inOxmList[i]
-		j, _ := json.Marshal(oxmField)
-
 		name := oxmMap[oxmField.GetOXMName()]
-		log.Printf("\n\n\n %s  %d  %s\n\n\n", j, name, oxmField.GetOXMName())
-
 		val := oxmField.GetOXMValue()
 		var ofpOxmField pb.OfpOxmField
 		ofpOxmField.OxmClass = ofp.OFPXMCOpenflowBasic
 		var field pb.OfpOxmOfbField
-
 		field.Type = pb.OxmOfbFieldTypes(name)
-		log.Println("****\nFieldType: " + openflow_13.OxmOfbFieldTypes_name[name] + "\n\n\n\n")
 
 		var x openflow_13.OfpOxmField_OfbField
 		x.OfbField = &field
@@ -321,7 +317,6 @@
 			udpSrc := val.(uint16)
 			var value pb.OfpOxmOfbField_UdpSrc
 			value.UdpSrc = uint32(udpSrc)
-			log.Printf("udpSrc %v", udpSrc)
 			field.Value = &value
 		case pb.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST:
 			udpDst := val.(uint16)
@@ -340,78 +335,14 @@
 	}
 	flowMatch.OxmFields = oxmList
 	flowMod.Match = &flowMatch
-	/*var instructions []*pb.OfpInstruction
-	ofpInstructions := flowDeleteStrict.GetInstructions()
-	for i := 0; i < len(ofpInstructions); i++ {
-		var instruction pb.OfpInstruction
-		ofpInstruction := ofpInstructions[i]
-		instructionType := ofpInstruction.GetType()
-		instruction.Type = uint32(instructionType)
-		switch instructionType {
-		case ofp.OFPITGotoTable:
-			goToTable := ofpInstruction.(ofp.IInstructionGotoTable)
-			var ofpGoToTable openflow_13.OfpInstruction_GotoTable
-			var oGoToTable openflow_13.OfpInstructionGotoTable
-			ofpGoToTable.GotoTable = &oGoToTable
-			ofpGoToTable.GotoTable.TableId = uint32(goToTable.GetTableId())
-			instruction.Data = &ofpGoToTable
-		case ofp.OFPITWriteMetadata:
-			writeMetaData := ofpInstruction.(ofp.IInstructionWriteMetadata)
-			var ofpWriteMetadata openflow_13.OfpInstruction_WriteMetadata
-			var writeMetadata openflow_13.OfpInstructionWriteMetadata
-			ofpWriteMetadata.WriteMetadata = &writeMetadata
-			ofpWriteMetadata.WriteMetadata.Metadata = writeMetaData.GetMetadata()
-			ofpWriteMetadata.WriteMetadata.MetadataMask = writeMetaData.GetMetadataMask()
-			instruction.Data = &ofpWriteMetadata
-		case ofp.OFPITWriteActions:
-			writeAction := ofpInstruction.(ofp.IInstructionWriteActions)
-			var ofpInstructionActions openflow_13.OfpInstruction_Actions
-			var ofpActions []*openflow_13.OfpAction
-			actions := writeAction.GetActions()
-			for i := 0; i < len(actions); i++ {
-				action := actions[i]
-				ofpAction := extractAction(action)
-				ofpActions = append(ofpActions, ofpAction)
-			}
-			instruction.Data = &ofpInstructionActions
-		case ofp.OFPITApplyActions:
-			applyAction := ofpInstruction.(ofp.IInstructionApplyActions)
-			var ofpInstructionActions openflow_13.OfpInstruction_Actions
-			var ofpActions []*openflow_13.OfpAction
-			actions := applyAction.GetActions()
-			for i := 0; i < len(actions); i++ {
-				action := actions[i]
-				ofpAction := extractAction(action)
-				ofpActions = append(ofpActions, ofpAction)
-			}
-			var actionsHolder openflow_13.OfpInstructionActions
-			actionsHolder.Actions = ofpActions
-			ofpInstructionActions.Actions = &actionsHolder
-			instruction.Data = &ofpInstructionActions
-		case ofp.OFPITMeter:
-			var instructionMeter = ofpInstruction.(ofp.IInstructionMeter)
-			var meterInstruction openflow_13.OfpInstruction_Meter
-			var meter openflow_13.OfpInstructionMeter
-
-			meter.MeterId = instructionMeter.GetMeterId()
-			meterInstruction.Meter = &meter
-			instruction.Data = &meterInstruction
-		}
-		instructions = append(instructions, &instruction)
-	}
-
-	flowMod.Instructions = instructions
-
-	*/
 	flowUpdate.FlowMod = &flowMod
 	grpcClient := *getGrpcClient()
-	flowUpdateJs, _ := json.Marshal(flowUpdate)
-	log.Printf("FLOW UPDATE %s", flowUpdateJs)
-	empty, err := grpcClient.UpdateLogicalDeviceFlowTable(context.Background(), &flowUpdate)
-	if err != nil {
-		log.Printf("ERROR DOING FLOW MOD ADD %v", err)
+	if settings.GetDebug(DeviceID) {
+		flowUpdateJs, _ := json.Marshal(flowUpdate)
+		logger.Debugf("FlowUpdate being sent to Voltha", l.Fields{"DeviceID": DeviceID, "FlowModRequest": flowUpdateJs})
 	}
-	emptyJs, _ := json.Marshal(empty)
-	log.Printf("FLOW MOD RESPONSE %s", emptyJs)
-
+	_, err := grpcClient.UpdateLogicalDeviceFlowTable(context.Background(), &flowUpdate)
+	if err != nil {
+		logger.Errorw("Error calling FlowUpdate ", l.Fields{"DeviceID": DeviceID, "error": err})
+	}
 }