VOL-2408 - Refactor / Use voltha-lib-go Logging
Change-Id: I6e7d9eaba49c104cd50bb5692a2ff9733014fac4
diff --git a/openflow/barrier.go b/openflow/barrier.go
index 60f5a2f..cb999d6 100644
--- a/openflow/barrier.go
+++ b/openflow/barrier.go
@@ -18,13 +18,19 @@
import (
"encoding/json"
+
+ "github.com/opencord/ofagent-go/settings"
+
ofp "github.com/donNewtonAlpha/goloxi/of13"
- "log"
+ l "github.com/opencord/voltha-lib-go/v2/pkg/log"
)
-func handleBarrierRequest(request *ofp.BarrierRequest, client *Client) {
- jsonRequest, _ := json.Marshal(request)
- log.Printf("handleBarrierRequest called with %s", jsonRequest)
+func handleBarrierRequest(request *ofp.BarrierRequest, DeviceID string, client *Client) {
+
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(request)
+ logger.Debugw("handleBarrierRequest called with %s", l.Fields{"DeviceID": DeviceID, "request": js})
+ }
reply := ofp.NewBarrierReply()
reply.SetVersion(4)
reply.SetXid(request.GetXid())
diff --git a/openflow/echo.go b/openflow/echo.go
index 8e6c0c5..528f845 100644
--- a/openflow/echo.go
+++ b/openflow/echo.go
@@ -18,13 +18,17 @@
import (
"encoding/json"
+
ofp "github.com/donNewtonAlpha/goloxi/of13"
- "log"
+ "github.com/opencord/ofagent-go/settings"
+ l "github.com/opencord/voltha-lib-go/v2/pkg/log"
)
-func handleEchoRequest(request *ofp.EchoRequest, client *Client) {
- jsonMessage, _ := json.Marshal(request)
- log.Printf("handleEchoRequest called with %s", jsonMessage)
+func handleEchoRequest(request *ofp.EchoRequest, DeviceID string, client *Client) {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(request)
+ logger.Debugw("handleEchoRequest called", l.Fields{"DeviceID": DeviceID, "request": js})
+ }
reply := ofp.NewEchoReply()
reply.SetXid(request.GetXid())
reply.SetVersion(request.GetVersion())
diff --git a/openflow/feature.go b/openflow/feature.go
index beb134d..ebfca92 100644
--- a/openflow/feature.go
+++ b/openflow/feature.go
@@ -19,20 +19,29 @@
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/common"
pb "github.com/opencord/voltha-protos/v2/go/voltha"
)
-func handleFeatureRequest(request *ofp.FeaturesRequest, deviceId string, client *Client) error {
- message, _ := json.Marshal(request)
- log.Printf("handleFeatureRequest called with %s\n ", message)
+func handleFeatureRequest(request *ofp.FeaturesRequest, DeviceID string, client *Client) error {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(request)
+ logger.Debugw("handleFeatureRequest called", l.Fields{"DeviceID": DeviceID, "request": js})
+ }
var grpcClient = *getGrpcClient()
- var id = common.ID{Id: deviceId}
+ var id = common.ID{Id: DeviceID}
logicalDevice, err := grpcClient.GetLogicalDevice(context.Background(), &id)
reply := createFeaturesRequestReply(request.GetXid(), logicalDevice)
+
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(reply)
+ logger.Debugw("handleFeatureRequestReturn", l.Fields{"DeviceID": DeviceID, "reply": js})
+ }
err = client.SendMessage(reply)
return err
}
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})
+ }
}
diff --git a/openflow/getConfig.go b/openflow/getConfig.go
index 2ba4ded..3cce700 100644
--- a/openflow/getConfig.go
+++ b/openflow/getConfig.go
@@ -18,16 +18,24 @@
import (
"encoding/json"
+
ofp "github.com/donNewtonAlpha/goloxi/of13"
- "log"
+ "github.com/opencord/ofagent-go/settings"
+ l "github.com/opencord/voltha-lib-go/v2/pkg/log"
)
-func handleGetConfigRequest(request *ofp.GetConfigRequest, client *Client) {
- jsonReq, _ := json.Marshal(request)
- log.Printf("handleGetConfigRequest called with %s", jsonReq)
+func handleGetConfigRequest(request *ofp.GetConfigRequest, DeviceID string, client *Client) {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(request)
+ logger.Debugw("handleGetConfigRequest called", l.Fields{"DeviceID": DeviceID, "request": js})
+ }
reply := ofp.NewGetConfigReply()
reply.SetVersion(4)
reply.SetXid(request.GetXid())
reply.SetMissSendLen(ofp.OFPCMLNoBuffer)
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(reply)
+ logger.Debugw("handleGetConfigRequest reply", l.Fields{"DeviceID": DeviceID, "reply": js})
+ }
client.SendMessage(reply)
}
diff --git a/openflow/meter.go b/openflow/meter.go
index 7e9d816..ea1f552 100644
--- a/openflow/meter.go
+++ b/openflow/meter.go
@@ -17,18 +17,22 @@
import (
"encoding/json"
+
ofp "github.com/donNewtonAlpha/goloxi/of13"
+ "github.com/opencord/ofagent-go/settings"
+ l "github.com/opencord/voltha-lib-go/v2/pkg/log"
"github.com/opencord/voltha-protos/v2/go/openflow_13"
"golang.org/x/net/context"
- "log"
)
-func handleMeterModRequest(request *ofp.MeterMod, client *Client) {
- jsonRequest, _ := json.Marshal(request)
- log.Printf("handleMeterModRequest called with %s", jsonRequest)
+func handleMeterModRequest(request *ofp.MeterMod, DeviceID string, client *Client) {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(request)
+ logger.Debugw("handleMeterModRequest called", l.Fields{"DeviceID": DeviceID, "request": js})
+ }
var meterModUpdate openflow_13.MeterModUpdate
- meterModUpdate.Id = client.DeviceId
+ meterModUpdate.Id = DeviceID
var meterMod openflow_13.OfpMeterMod
meterMod.MeterId = request.MeterId
meterMod.Flags = uint32(request.Flags)
@@ -78,12 +82,13 @@
meterMod.Bands = bands
meterModUpdate.MeterMod = &meterMod
grpcClient := *getGrpcClient()
- meterModJS, _ := json.Marshal(meterModUpdate)
- log.Printf("METER MOD UPDATE %s", meterModJS)
- empty, err := grpcClient.UpdateLogicalDeviceMeterTable(context.Background(), &meterModUpdate)
- if err != nil {
- log.Printf("ERROR DOING METER MOD ADD %v", err)
+ if settings.GetDebug(DeviceID) {
+ meterModJS, _ := json.Marshal(meterModUpdate)
+ logger.Debugw("handleMeterModUpdate sending request",
+ l.Fields{"DeviceID": DeviceID, "MeterModRequest": meterModJS})
}
- emptyJs, _ := json.Marshal(empty)
- log.Printf("METER MOD RESPONSE %s", emptyJs)
+ _, err := grpcClient.UpdateLogicalDeviceMeterTable(context.Background(), &meterModUpdate)
+ if err != nil {
+ logger.Errorw("Error calling UpdateLogicalDeviceMeterTable", l.Fields{"DeviceID": DeviceID, "error": err})
+ }
}
diff --git a/openflow/ofError.go b/openflow/ofError.go
index 2ca5b81..1f90c34 100644
--- a/openflow/ofError.go
+++ b/openflow/ofError.go
@@ -18,12 +18,16 @@
import (
"encoding/json"
+
ofp "github.com/donNewtonAlpha/goloxi/of13"
- "log"
+ "github.com/opencord/ofagent-go/settings"
+ l "github.com/opencord/voltha-lib-go/v2/pkg/log"
)
-func handleErrMsg(message *ofp.ErrorMsg) {
- jsonMessage, _ := json.Marshal(message)
- log.Printf("handleErrMsg called with %s", jsonMessage)
+func handleErrMsg(message *ofp.ErrorMsg, DeviceID string) {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(message)
+ logger.Debugw("handleErrMsg called", l.Fields{"DeviceID": DeviceID, "request": js})
+ }
//Not sure yet what if anything to do here
}
diff --git a/openflow/openflowClient.go b/openflow/openflowClient.go
index 29cd423..4925f4f 100644
--- a/openflow/openflowClient.go
+++ b/openflow/openflowClient.go
@@ -23,67 +23,91 @@
"github.com/donNewtonAlpha/goloxi"
ofp "github.com/donNewtonAlpha/goloxi/of13"
+ "github.com/opencord/ofagent-go/settings"
+ l "github.com/opencord/voltha-lib-go/v2/pkg/log"
pb "github.com/opencord/voltha-protos/v2/go/voltha"
- "log"
"net"
)
-var conn net.Conn
var volthaClient *pb.VolthaServiceClient
var packetOutChannel chan pb.PacketOut
+var logger, _ = l.AddPackage(l.JSON, l.DebugLevel, nil)
+//Client structure to hold fields of Openflow Client
type Client struct {
OfServerAddress string
Port uint16
- DeviceId string
+ DeviceID string
KeepRunning bool
+ conn net.Conn
}
-func NewClient(ofServerAddress string, port uint16, deviceId string, keepRunning bool) *Client {
- client := Client{OfServerAddress: ofServerAddress, Port: port, DeviceId: deviceId, KeepRunning: keepRunning}
- go client.Start()
- return &client
-}
+//NewClient contstructs a new Openflow Client and then starts up
+func NewClient(ofServerAddress string, port uint16, deviceID string, keepRunning bool) *Client {
-func (client *Client) End() {
- client.KeepRunning = false
-}
-func (client *Client) Start() {
+ client := Client{OfServerAddress: ofServerAddress, Port: port, DeviceID: deviceID, KeepRunning: keepRunning}
addressLine := fmt.Sprintf("%s:%d", client.OfServerAddress, client.Port)
raddr, e := net.ResolveTCPAddr("tcp", addressLine)
if e != nil {
- errMessage := fmt.Errorf("Unable to resolve %s", addressLine)
- fmt.Println(errMessage)
- return
+ logger.Fatalw("openflowClient Start unable to resolve address", l.Fields{"Address": addressLine})
}
+ connection, e := net.DialTCP("tcp", nil, raddr)
+ client.conn = connection
+ if e != nil {
+ logger.Fatalf("openflowClient Unable to connect to voltha @ %v exiting", raddr)
+ }
+ client.sayHello()
+ return &client
+}
+
+//End - set keepRunning to false so start loop exits
+func (client *Client) End() {
+ client.KeepRunning = false
+}
+
+//Start run loop for the openflow client
+func (client *Client) Start() {
+
for {
if client.KeepRunning {
- connection, e := net.DialTCP("tcp", nil, raddr)
- conn = connection
- if e != nil {
- log.Fatalf("unable to connect to %v", raddr)
- }
- defer conn.Close()
- client.sayHello()
+ defer client.conn.Close()
for {
+ defer func() {
+
+ if r := recover(); r != nil {
+ err := r.(error)
+ fmt.Printf("Caught error in client.Start() %v \n ", err)
+ }
+ }()
+
if !client.KeepRunning {
return
}
buf := make([]byte, 1500)
- read, e := conn.Read(buf)
+ read, e := client.conn.Read(buf)
+ if settings.GetDebug(client.DeviceID) {
+ fmt.Printf("conn.Read read %d bytes\n", read)
+ }
+ if read < 8 {
+ continue
+ }
+
if e != nil {
- log.Printf("Connection has died %v", e)
+ logger.Errorw("Voltha connection has died", l.Fields{"DeviceID": client.DeviceID, "Error": e})
break
}
decoder := goloxi.NewDecoder(buf)
header, e := ofp.DecodeHeader(decoder)
if e != nil {
- log.Printf("decodeheader threw error %v", e)
+ js, _ := json.Marshal(decoder)
+ logger.Errorw("DecodeHeader threw error", l.Fields{"DeviceID": client.DeviceID, "Decoder": js, "Error": e})
}
- message, _ := json.Marshal(header)
- log.Printf("message %s\n", message)
+ if settings.GetDebug(client.DeviceID) {
+ js, _ := json.Marshal(header)
+ logger.Debugw("Header Decode", l.Fields{"DeviceID": client.DeviceID, "Header": js})
+ }
client.parseHeader(header, buf) //first one is ready
len := header.GetLength()
if len < uint16(read) {
@@ -92,48 +116,51 @@
newBuf := buf[len:]
decoder = goloxi.NewDecoder(newBuf)
newHeader, e := ofp.DecodeHeader(decoder)
- message, _ := json.Marshal(newHeader)
- log.Printf("message %s\n", message)
if e != nil {
-
+ js, _ := json.Marshal(decoder)
+ logger.Errorw("DecodeHeader threw error", l.Fields{"DeviceID": client.DeviceID, "Decoder": js, "Error": e})
+ }
+ if e != nil {
+ js, _ := json.Marshal(decoder)
+ logger.Errorw("DecodeHeader threw error", l.Fields{"DeviceID": client.DeviceID, "Decoder": js, "Error": e})
+ }
+ if settings.GetDebug(client.DeviceID) {
+ js, _ := json.Marshal(header)
+ logger.Debugw("Header Decode", l.Fields{"DeviceID": client.DeviceID, "Header": js})
}
len = newHeader.GetLength()
client.parseHeader(newHeader, newBuf)
if read == int(len) {
break
- } else {
}
buf = newBuf
}
}
- if e != nil {
- log.Println("Decode Header error ", e)
- }
}
}
break
}
}
func (client *Client) sayHello() {
-
hello := ofp.NewHello()
hello.Xid = uint32(GetXid())
var elements []ofp.IHelloElem
-
elem := ofp.NewHelloElemVersionbitmap()
elem.SetType(ofp.OFPHETVersionbitmap)
elem.SetLength(8)
var bitmap = ofp.Uint32{Value: 16}
bitmaps := []*ofp.Uint32{&bitmap}
-
elem.SetBitmaps(bitmaps)
-
elements = append(elements, elem)
-
hello.SetElements(elements)
+ if settings.GetDebug(client.DeviceID) {
+ js, _ := json.Marshal(hello)
+ logger.Debugw("sayHello Called", l.Fields{"DeviceID": client.DeviceID, "HelloMessage": js})
+ }
e := client.SendMessage(hello)
if e != nil {
- log.Fatal(e)
+ logger.Fatalw("Failed saying hello to Openflow Server, unable to proceed",
+ l.Fields{"DeviceID": client.DeviceID, "Error": e})
}
}
func (client *Client) parseHeader(header ofp.IHeader, buf []byte) {
@@ -142,105 +169,104 @@
//x := header.(*ofp.Hello)
case ofp.OFPTError:
errMsg := header.(*ofp.ErrorMsg)
- go handleErrMsg(errMsg)
+ go handleErrMsg(errMsg, client.DeviceID)
case ofp.OFPTEchoRequest:
echoReq := header.(*ofp.EchoRequest)
- go handleEchoRequest(echoReq, client)
+ go handleEchoRequest(echoReq, client.DeviceID, client)
case ofp.OFPTEchoReply:
case ofp.OFPTExperimenter:
case ofp.OFPTFeaturesRequest:
featReq := header.(*ofp.FeaturesRequest)
- go handleFeatureRequest(featReq, client.DeviceId, client)
+ go handleFeatureRequest(featReq, client.DeviceID, client)
case ofp.OFPTFeaturesReply:
case ofp.OFPTGetConfigRequest:
configReq := header.(*ofp.GetConfigRequest)
- go handleGetConfigRequest(configReq, client)
+ go handleGetConfigRequest(configReq, client.DeviceID, client)
case ofp.OFPTGetConfigReply:
case ofp.OFPTSetConfig:
setConf := header.(*ofp.SetConfig)
- go handleSetConfig(setConf)
+ go handleSetConfig(setConf, client.DeviceID)
case ofp.OFPTPacketIn:
case ofp.OFPTFlowRemoved:
case ofp.OFPTPortStatus:
case ofp.OFPTPacketOut:
packetOut := header.(*ofp.PacketOut)
- go handlePacketOut(packetOut, client.DeviceId)
+ go handlePacketOut(packetOut, client.DeviceID)
case ofp.OFPTFlowMod:
flowModType := uint8(buf[25])
switch flowModType {
case ofp.OFPFCAdd:
flowAdd := header.(*ofp.FlowAdd)
- go handleFlowAdd(flowAdd, client.DeviceId)
+ go handleFlowAdd(flowAdd, client.DeviceID)
case ofp.OFPFCModify:
flowMod := header.(*ofp.FlowMod)
- go handleFlowMod(flowMod, client.DeviceId)
+ go handleFlowMod(flowMod, client.DeviceID)
case ofp.OFPFCModifyStrict:
flowModStrict := header.(*ofp.FlowModifyStrict)
- go handleFlowModStrict(flowModStrict, client.DeviceId)
+ go handleFlowModStrict(flowModStrict, client.DeviceID)
case ofp.OFPFCDelete:
flowDelete := header.(*ofp.FlowDelete)
- go handleFlowDelete(flowDelete, client.DeviceId)
+ go handleFlowDelete(flowDelete, client.DeviceID)
case ofp.OFPFCDeleteStrict:
flowDeleteStrict := header.(*ofp.FlowDeleteStrict)
- go handleFlowDeleteStrict(flowDeleteStrict, client.DeviceId)
+ go handleFlowDeleteStrict(flowDeleteStrict, client.DeviceID)
}
case ofp.OFPTStatsRequest:
var statType = uint16(buf[8])<<8 + uint16(buf[9])
- log.Println("statsType", statType)
- go handleStatsRequest(header, statType, client.DeviceId, client)
+ go handleStatsRequest(header, statType, client.DeviceID, client)
case ofp.OFPTBarrierRequest:
barRequest := header.(*ofp.BarrierRequest)
- go handleBarrierRequest(barRequest, client)
+ go handleBarrierRequest(barRequest, client.DeviceID, client)
case ofp.OFPTRoleRequest:
roleReq := header.(*ofp.RoleRequest)
- go handleRoleRequest(roleReq, client)
+ go handleRoleRequest(roleReq, client.DeviceID, client)
case ofp.OFPTMeterMod:
meterMod := header.(*ofp.MeterMod)
- go handleMeterModRequest(meterMod, client)
+ go handleMeterModRequest(meterMod, client.DeviceID, client)
}
}
-//openFlowMessage created to allow for a single SendMessage
-type OpenFlowMessage interface {
+//Message created to allow for a single SendMessage
+type Message interface {
Serialize(encoder *goloxi.Encoder) error
}
-func (client *Client) SendMessage(message OpenFlowMessage) error {
- jsonMessage, _ := json.Marshal(message)
- log.Printf("send message called %s\n", jsonMessage)
+//SendMessage sends message to openflow server
+func (client *Client) SendMessage(message Message) error {
+ if settings.GetDebug(client.DeviceID) {
+ js, _ := json.Marshal(message)
+ logger.Debugw("SendMessage called", l.Fields{"DeviceID": client.DeviceID, "Message": js})
+ }
enc := goloxi.NewEncoder()
message.Serialize(enc)
- jMessage, _ := json.Marshal(message)
- bytes := enc.Bytes()
- log.Printf("message after serialize %d for message %s", bytes, jMessage)
-
for {
- if conn == nil {
- log.Println("CONN IS NIL")
+ if client.conn == nil {
+ logger.Warnln("SendMessage Connection is Nil sleeping for 10 milliseconds")
time.Sleep(10 * time.Millisecond)
} else {
break
}
}
- _, err := conn.Write(bytes)
+ bytes := enc.Bytes()
+ _, err := client.conn.Write(bytes)
if err != nil {
- log.Printf("SendMessage had error %v \n %s\n********", err, jMessage)
+ jMessage, _ := json.Marshal(message)
+ logger.Errorw("SendMessage failed sending message", l.Fields{"DeviceID": client.DeviceID, "Error": err, "Message": jMessage})
return err
- } else {
- log.Printf("message after send %s", jMessage)
}
return nil
}
+
+//SetGrpcClient store a reference to the grpc client connection
func SetGrpcClient(client *pb.VolthaServiceClient) {
volthaClient = client
}
func getGrpcClient() *pb.VolthaServiceClient {
return volthaClient
}
-func test(hello ofp.IHello) {
- fmt.Println("works")
-}
+
+//SetPacketOutChannel - store the channel to send packet outs to grpc connection
func SetPacketOutChannel(pktOutChan chan pb.PacketOut) {
packetOutChannel = pktOutChan
}
diff --git a/openflow/packet.go b/openflow/packet.go
index 3229623..046d8a3 100644
--- a/openflow/packet.go
+++ b/openflow/packet.go
@@ -18,14 +18,18 @@
import (
"encoding/json"
+
ofp "github.com/donNewtonAlpha/goloxi/of13"
+ "github.com/opencord/ofagent-go/settings"
+ l "github.com/opencord/voltha-lib-go/v2/pkg/log"
pb "github.com/opencord/voltha-protos/v2/go/voltha"
- "log"
)
-func handlePacketOut(packetOut *ofp.PacketOut, deviceId string) {
- jsonMessage, _ := json.Marshal(packetOut)
- log.Printf("handlePacketOut called with %s", jsonMessage)
+func handlePacketOut(packetOut *ofp.PacketOut, DeviceID string) {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(packetOut)
+ logger.Debugw("handlePacketOut called", l.Fields{"DeviceID": DeviceID, "packetOut": js})
+ }
pktOut := pb.OfpPacketOut{}
pktOut.BufferId = packetOut.GetBufferId()
pktOut.InPort = uint32(packetOut.GetInPort())
@@ -34,21 +38,16 @@
for i := 0; i < len(inActions); i++ {
action := inActions[i]
newAction := extractAction(action)
- js, _ := json.Marshal(newAction)
- log.Printf("PACKET_OUT ACTION %s", js)
- /*var newAction = pb.OfpAction{}
- newAction.Type = pb.OfpActionType(action.GetType())
- action.
-
- */
actions = append(actions, newAction)
}
pktOut.Actions = actions
pktOut.Data = packetOut.GetData()
- js, _ := json.Marshal(pktOut)
- log.Printf("PacketOut %s", js)
pbPacketOut := pb.PacketOut{}
pbPacketOut.PacketOut = &pktOut
- pbPacketOut.Id = deviceId
+ pbPacketOut.Id = DeviceID
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(pbPacketOut)
+ logger.Debugw("handlePacketOut sending", l.Fields{"DeviceID": DeviceID, "packetOut": js})
+ }
packetOutChannel <- pbPacketOut
}
diff --git a/openflow/parseGrpcReturn.go b/openflow/parseGrpcReturn.go
index 072bdcf..30ec672 100644
--- a/openflow/parseGrpcReturn.go
+++ b/openflow/parseGrpcReturn.go
@@ -17,17 +17,21 @@
import (
"encoding/json"
- "log"
- "unsafe"
"github.com/donNewtonAlpha/goloxi"
ofp "github.com/donNewtonAlpha/goloxi/of13"
+ "github.com/opencord/ofagent-go/settings"
+ 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"
)
-func parseOxm(ofbField *openflow_13.OfpOxmOfbField) (goloxi.IOxm, uint16) {
- log.Printf("PARSE OXM")
+func parseOxm(ofbField *openflow_13.OfpOxmOfbField, DeviceID string) (goloxi.IOxm, uint16) {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(ofbField)
+ logger.Debugw("parseOxm called", l.Fields{"DeviceID": DeviceID, "ofbField": js})
+ }
+
switch ofbField.Type {
case pb.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT:
ofpInPort := ofp.NewOxmInPort()
@@ -68,8 +72,6 @@
} else {
ofpVlanVid.Value = uint16(0)
}
- js, _ := json.Marshal(ofpVlanVid)
- log.Printf("PARSE OXM VLAN VID %s", js)
return ofpVlanVid, 2
case pb.OxmOfbFieldTypes_OFPXMT_OFB_METADATA:
ofpMetadata := ofp.NewOxmMetadata()
@@ -77,11 +79,16 @@
ofpMetadata.Value = val.TableMetadata
return ofpMetadata, 8
default:
- log.Printf("handleFlowStatsRequest Unhandled OxmField %v", ofbField.Type)
+ js, _ := json.Marshal(ofbField)
+ logger.Warnw("ParseOXM Unhandled OxmField", l.Fields{"DeviceID": DeviceID, "OfbField": js})
}
return nil, 0
}
-func parseInstructions(ofpInstruction *openflow_13.OfpInstruction) (ofp.IInstruction, uint16) {
+func parseInstructions(ofpInstruction *openflow_13.OfpInstruction, DeviceID string) (ofp.IInstruction, uint16) {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(ofpInstruction)
+ logger.Debugw("parseInstructions called", l.Fields{"DeviceID": DeviceID, "Instruction": js})
+ }
instType := ofpInstruction.Type
data := ofpInstruction.GetData()
switch instType {
@@ -111,29 +118,28 @@
var actions []goloxi.IAction
for i := 0; i < len(ofpActions); i++ {
ofpAction := ofpActions[i]
- action, actionSize := parseAction(ofpAction)
- js, _ := json.Marshal(action)
- log.Printf("ACTION size(%d) value %s", actionSize, js)
+ action, actionSize := parseAction(ofpAction, DeviceID)
actions = append(actions, action)
instructionSize += actionSize
- mySize := unsafe.Sizeof(*instruction)
- log.Printf("Calculated Size %d Measured size %d", instructionSize, mySize)
}
instruction.Actions = actions
instruction.SetLen(instructionSize)
- js, _ := json.Marshal(instruction)
- log.Printf("INSTRUCTION %s", js)
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(instruction)
+ l.Debugw("parseInstructions returning", l.Fields{"DeviceID": DeviceID,
+ "Size": instructionSize, "ParsedInstruction": js})
+ }
return instruction, instructionSize
}
//shouldn't have reached here :<
- js, _ := json.Marshal(ofpInstruction)
- log.Printf("Parse Instruction Failed ofpInstruction : %s", js)
return nil, 0
}
-func parseAction(ofpAction *openflow_13.OfpAction) (goloxi.IAction, uint16) {
- js, _ := json.Marshal(ofpAction)
- log.Printf("ACTION BEFORE %s", js)
+func parseAction(ofpAction *openflow_13.OfpAction, DeviceID string) (goloxi.IAction, uint16) {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(ofpAction)
+ logger.Debugw("parseAction called", l.Fields{"DeviceID": DeviceID, "Action": js})
+ }
switch ofpAction.Type {
case openflow_13.OfpActionType_OFPAT_OUTPUT:
ofpOutputAction := ofpAction.GetOutput()
@@ -155,13 +161,13 @@
ofpActionSetField := ofpAction.GetSetField()
setFieldAction := ofp.NewActionSetField()
- iOxm, _ := parseOxm(ofpActionSetField.GetField().GetOfbField())
+ iOxm, _ := parseOxm(ofpActionSetField.GetField().GetOfbField(), DeviceID)
setFieldAction.Field = iOxm
setFieldAction.Len = 16
return setFieldAction, 16
default:
js, _ := json.Marshal(ofpAction)
- log.Printf("UNKNOWN ACTION %s", js)
+ logger.Warnw("parseAction unknow action", l.Fields{"DeviceID": DeviceID, "Action": js})
}
return nil, 0
}
diff --git a/openflow/role.go b/openflow/role.go
index 0620321..3ca3e93 100644
--- a/openflow/role.go
+++ b/openflow/role.go
@@ -18,14 +18,17 @@
import (
"encoding/json"
+
ofp "github.com/donNewtonAlpha/goloxi/of13"
- "log"
+ "github.com/opencord/ofagent-go/settings"
+ l "github.com/opencord/voltha-lib-go/v2/pkg/log"
)
-func handleRoleRequest(request *ofp.RoleRequest, client *Client) {
- jsonMessage, _ := json.Marshal(request)
- log.Printf("handleRoleRequest called with %s", jsonMessage)
-
+func handleRoleRequest(request *ofp.RoleRequest, DeviceID string, client *Client) {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(request)
+ logger.Debugw("handleRoleRequest called", l.Fields{"DeviceID": client.DeviceID, "request": js})
+ }
reply := ofp.NewRoleReply()
reply.SetXid(request.GetXid())
reply.SetVersion(request.GetVersion())
diff --git a/openflow/setConfig.go b/openflow/setConfig.go
index 69a6ab2..3c789fd 100644
--- a/openflow/setConfig.go
+++ b/openflow/setConfig.go
@@ -18,11 +18,16 @@
import (
"encoding/json"
+
ofp "github.com/donNewtonAlpha/goloxi/of13"
- "log"
+ "github.com/opencord/ofagent-go/settings"
+ l "github.com/opencord/voltha-lib-go/v2/pkg/log"
)
-func handleSetConfig(request *ofp.SetConfig) {
- jsonMessage, _ := json.Marshal(request)
- log.Printf("handleSetConfig %s\n", jsonMessage)
+func handleSetConfig(request *ofp.SetConfig, DeviceID string) {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(request)
+ logger.Debugw("handleSetConfig called", l.Fields{"DeviceID": DeviceID, "request": js})
+
+ }
}
diff --git a/openflow/stats.go b/openflow/stats.go
index 54a23de..8b12294 100644
--- a/openflow/stats.go
+++ b/openflow/stats.go
@@ -20,10 +20,12 @@
"context"
"encoding/json"
- "log"
"net"
+
"unsafe"
+ "github.com/opencord/ofagent-go/settings"
+ l "github.com/opencord/voltha-lib-go/v2/pkg/log"
"github.com/opencord/voltha-protos/v2/go/openflow_13"
"github.com/donNewtonAlpha/goloxi"
@@ -31,11 +33,12 @@
"github.com/opencord/voltha-protos/v2/go/common"
)
-func handleStatsRequest(request ofp.IHeader, statType uint16, deviceId string, client *Client) error {
- message, _ := json.Marshal(request)
- log.Printf("handleStatsRequest called with %s\n ", message)
- var id = common.ID{Id: deviceId}
-
+func handleStatsRequest(request ofp.IHeader, statType uint16, DeviceID string, client *Client) error {
+ if settings.GetDebug(DeviceID) {
+ js, _ := json.Marshal(request)
+ logger.Debugw("handleStatsRequest called", l.Fields{"DeviceID": DeviceID, "StatType": statType, "rquest": js})
+ }
+ var id = common.ID{Id: DeviceID}
switch statType {
case ofp.OFPSTDesc:
statsReq := request.(*ofp.DescStatsRequest)
@@ -43,43 +46,62 @@
if err != nil {
return err
}
- client.SendMessage(response)
-
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTFlow:
statsReq := request.(*ofp.FlowStatsRequest)
- response, err := handleFlowStatsRequest(statsReq, id)
+ response, err := handleFlowStatsRequest(statsReq, id, DeviceID)
if err != nil {
return err
}
response.Length = uint16(unsafe.Sizeof(*response))
- jResponse, _ := json.Marshal(response)
- log.Printf("HANDLE FLOW STATS REQUEST response\n\n\n %s \n\n\n", jResponse)
- err = client.SendMessage(response)
- if err != nil {
- return err
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
}
+ return client.SendMessage(response)
case ofp.OFPSTAggregate:
statsReq := request.(*ofp.AggregateStatsRequest)
- aggregateStatsReply, err := handleAggregateStatsRequest(statsReq, id)
+ response, err := handleAggregateStatsRequest(statsReq, id)
if err != nil {
return err
}
- client.SendMessage(aggregateStatsReply)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTTable:
statsReq := request.(*ofp.TableStatsRequest)
- tableStatsReply, e := handleTableStatsRequest(statsReq, id)
+ response, e := handleTableStatsRequest(statsReq, id)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
if e != nil {
return e
}
- client.SendMessage(tableStatsReply)
+ return client.SendMessage(response)
case ofp.OFPSTPort:
statsReq := request.(*ofp.PortStatsRequest)
response, err := handlePortStatsRequest(statsReq, id)
if err != nil {
return err
}
- client.SendMessage(response)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTQueue:
statsReq := request.(*ofp.QueueStatsRequest)
@@ -87,14 +109,23 @@
if err != nil {
return err
}
- client.SendMessage(response)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTGroup:
statsReq := request.(*ofp.GroupStatsRequest)
response, err := handleGroupStatsRequest(statsReq, id)
if err != nil {
return err
}
-
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
client.SendMessage(response)
case ofp.OFPSTGroupDesc:
statsReq := request.(*ofp.GroupDescStatsRequest)
@@ -102,7 +133,12 @@
if err != nil {
return err
}
- client.SendMessage(response)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTGroupFeatures:
statsReq := request.(*ofp.GroupFeaturesStatsRequest)
@@ -110,43 +146,72 @@
if err != nil {
return err
}
- client.SendMessage(response)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTMeter:
statsReq := request.(*ofp.MeterStatsRequest)
response, err := handleMeterStatsRequest(statsReq, id)
if err != nil {
- log.Printf("ERROR HANDLE METER STATS REQUEST %v", err)
return err
}
- client.SendMessage(response)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTMeterConfig:
statsReq := request.(*ofp.MeterConfigStatsRequest)
response, err := handleMeterConfigStatsRequest(statsReq, id)
if err != nil {
return err
}
- client.SendMessage(response)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTMeterFeatures:
statsReq := request.(*ofp.MeterFeaturesStatsRequest)
response, err := handleMeterFeatureStatsRequest(statsReq)
if err != nil {
return err
}
- client.SendMessage(response)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTTableFeatures:
statsReq := request.(*ofp.TableFeaturesStatsRequest)
response, err := handleTableFeaturesStatsRequest(statsReq, id)
if err != nil {
return err
}
- client.SendMessage(response)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTPortDesc:
statsReq := request.(*ofp.PortDescStatsRequest)
- response, err := handlePortDescStatsRequest(statsReq, deviceId)
+ response, err := handlePortDescStatsRequest(statsReq, DeviceID)
if err != nil {
return err
}
- client.SendMessage(response)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
case ofp.OFPSTExperimenter:
statsReq := request.(*ofp.ExperimenterStatsRequest)
@@ -154,7 +219,12 @@
if err != nil {
return err
}
- client.SendMessage(response)
+ if settings.GetDebug(DeviceID) {
+ reqJs, _ := json.Marshal(statsReq)
+ resJs, _ := json.Marshal(response)
+ logger.Debugw("HandleStatsRequest GRPC", l.Fields{"DeviceID": DeviceID, "Req": reqJs, "Res": resJs})
+ }
+ return client.SendMessage(response)
}
return nil
}
@@ -179,8 +249,7 @@
response.SetDpDesc(PadString(desc.GetDpDesc(), 256))
return response, nil
}
-func handleFlowStatsRequest(request *ofp.FlowStatsRequest, id common.ID) (*ofp.FlowStatsReply, error) {
- log.Println("****************************************\n***********************************")
+func handleFlowStatsRequest(request *ofp.FlowStatsRequest, id common.ID, DeviceID string) (*ofp.FlowStatsReply, error) {
response := ofp.NewFlowStatsReply()
response.SetXid(request.GetXid())
response.SetVersion(4)
@@ -188,18 +257,13 @@
client := *getGrpcClient()
resp, err := client.ListLogicalDeviceFlows(context.Background(), &id)
if err != nil {
- log.Printf("err in handleFlowStatsRequest calling ListLogicalDeviceFlows %v", err)
return nil, err
}
- js, _ := json.Marshal(resp)
- log.Printf("HandleFlowStats response: %s", js)
var flow []*ofp.FlowStatsEntry
items := resp.GetItems()
-
for i := 0; i < len(items); i++ {
item := items[i]
entry := ofp.NewFlowStatsEntry()
-
entry.SetTableId(uint8(item.GetTableId()))
entry.SetDurationSec(item.GetDurationSec())
entry.SetDurationNsec(item.GetDurationNsec())
@@ -223,40 +287,30 @@
oxmField := oxFields[i]
field := oxmField.GetField()
ofbField := field.(*openflow_13.OfpOxmField_OfbField).OfbField
- iOxm, oxmSize := parseOxm(ofbField)
- log.Printf("OXMSIZE %d", oxmSize)
+ iOxm, oxmSize := parseOxm(ofbField, DeviceID)
fields = append(fields, iOxm)
if oxmSize > 0 {
size += 4 //header for oxm
}
size += oxmSize
- log.Printf("SIZE %d", size)
}
- log.Printf("entrySize %d += size %d", entrySize, size)
- log.Printf("new entrySize %d", entrySize)
match.OxmList = fields
match.Length = uint16(size)
//account for 8 byte alignment
- log.Printf("Size was %d", size)
if size%8 != 0 {
size = ((size / 8) + 1) * 8
}
-
- log.Printf("Size is %d", size)
entrySize += size
entry.SetMatch(*match)
var instructions []ofp.IInstruction
ofpInstructions := item.Instructions
for i := 0; i < len(ofpInstructions); i++ {
- instruction, size := parseInstructions(ofpInstructions[i])
+ instruction, size := parseInstructions(ofpInstructions[i], DeviceID)
instructions = append(instructions, instruction)
entrySize += size
}
entry.Instructions = instructions
- log.Printf("entrysize was %d", entrySize)
- entrySizeMeasured := uint16(unsafe.Sizeof(*entry))
- log.Printf("entrysize measure %d", entrySizeMeasured)
entry.Length = entrySize
entrySize = 0
flow = append(flow, entry)
@@ -420,7 +474,6 @@
return response, nil
}
func handlePortStatsRequest(request *ofp.PortStatsRequest, id common.ID) (*ofp.PortStatsReply, error) {
- log.Println("HERE")
response := ofp.NewPortStatsReply()
response.SetXid(request.GetXid())
response.SetVersion(request.GetVersion())
@@ -429,11 +482,8 @@
reply, err := client.ListLogicalDevicePorts(context.Background(), &id)
//reply,err := client.GetLogicalDevicePort(context.Background(),&id)
if err != nil {
- log.Printf("error calling ListDevicePorts %v", err)
return nil, err
}
- js, _ := json.Marshal(reply)
- log.Printf("PORT STATS REPLY %s", js)
ports := reply.GetItems()
var entries []*ofp.PortStatsEntry
if request.GetPortNo() == 0xffffffff { //all ports
@@ -451,19 +501,16 @@
}
}
response.SetEntries(entries)
- js, _ = json.Marshal(response)
- log.Printf("handlePortStatsResponse %s", js)
return response, nil
-
}
-func handlePortDescStatsRequest(request *ofp.PortDescStatsRequest, deviceId string) (*ofp.PortDescStatsReply, error) {
+func handlePortDescStatsRequest(request *ofp.PortDescStatsRequest, DeviceID string) (*ofp.PortDescStatsReply, error) {
response := ofp.NewPortDescStatsReply()
response.SetVersion(request.GetVersion())
response.SetXid(request.GetXid())
response.SetFlags(ofp.StatsReplyFlags(request.GetFlags()))
var grpcClient = *getGrpcClient()
- var id = common.ID{Id: deviceId}
+ var id = common.ID{Id: DeviceID}
logicalDevice, err := grpcClient.GetLogicalDevice(context.Background(), &id)
if err != nil {
return nil, err
diff --git a/openflow/utils.go b/openflow/utils.go
index 7994fd2..d87c338 100644
--- a/openflow/utils.go
+++ b/openflow/utils.go
@@ -17,9 +17,7 @@
package openflow
import (
- "encoding/json"
"fmt"
- "log"
"strings"
"sync"
@@ -62,8 +60,6 @@
outputAction.Output = &output
ofpAction.Action = &outputAction
ofpAction.Type = openflow_13.OfpActionType_OFPAT_OUTPUT
- js, _ := json.Marshal(outputAction)
- log.Printf("EXTRACT ACTION %s", js)
case ofp.OFPATCopyTtlOut: //CopyTtltOut
case ofp.OFPATCopyTtlIn: //CopyTtlIn
case ofp.OFPATSetMplsTtl: //SetMplsTtl
@@ -71,9 +67,6 @@
case ofp.OFPATPushVLAN: //PushVlan
var pushVlan openflow_13.OfpAction_Push
loxiPushAction := action.(*ofp.ActionPushVlan)
- fields := loxiPushAction.GetActionFields()
- fieldsJS, _ := json.Marshal(fields)
- log.Printf("\n\nPushVlan fields %s\n\n", fieldsJS)
var push openflow_13.OfpActionPush
push.Ethertype = uint32(loxiPushAction.Ethertype) //TODO This should be available in the fields
pushVlan.Push = &push
@@ -98,6 +91,7 @@
loxiSetField := action.(*ofp.ActionSetField)
oxmName := loxiSetField.Field.GetOXMName()
switch oxmName {
+ //TODO handle set field sith other fields
case "vlan_vid":
ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID
var vlanVid openflow_13.OfpOxmOfbField_VlanVid
@@ -105,8 +99,6 @@
vlanVid.VlanVid = uint32(VlanVid)
ofpOxmOfbField.Value = &vlanVid
- default:
- log.Printf("UNHANDLED SET FIELD %s", oxmName)
}
ofpOxmField_OfbField.OfbField = &ofpOxmOfbField
ofpOxmField.Field = &ofpOxmField_OfbField