Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 1 | package sbi |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/golang/protobuf/ptypes/empty" |
| 6 | "github.com/google/gopacket" |
| 7 | "github.com/google/gopacket/layers" |
| 8 | "github.com/opencord/voltha/ponsim/v2/common" |
| 9 | "github.com/opencord/voltha/ponsim/v2/core" |
| 10 | "github.com/opencord/voltha/protos/go/ponsim" |
| 11 | "github.com/sirupsen/logrus" |
| 12 | "io" |
| 13 | ) |
| 14 | |
| 15 | type PonSimCommonHandler struct { |
| 16 | device core.PonSimInterface |
| 17 | } |
| 18 | |
| 19 | /* |
| 20 | NewPonSimCommonHandler instantiates a handler for common GRPC servicing methods |
| 21 | */ |
| 22 | func NewPonSimCommonHandler(device core.PonSimInterface) *PonSimCommonHandler { |
| 23 | var handler *PonSimCommonHandler |
| 24 | |
| 25 | handler = &PonSimCommonHandler{device: device} |
| 26 | |
| 27 | return handler |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | ProcessData handles and forwards streaming INGRESS/EGRESS packets |
| 32 | */ |
| 33 | func (h *PonSimCommonHandler) ProcessData(stream ponsim.PonSimCommon_ProcessDataServer) error { |
| 34 | common.Logger().WithFields(logrus.Fields{ |
| 35 | "handler": h, |
| 36 | }).Debug("Processing data") |
| 37 | |
| 38 | var err error |
| 39 | var data *ponsim.IncomingData |
| 40 | |
| 41 | for { |
| 42 | |
| 43 | if data, err = stream.Recv(); err == io.EOF { |
| 44 | common.Logger().WithFields(logrus.Fields{ |
| 45 | "handler": h, |
| 46 | }).Warn("Streaming channel was closed") |
| 47 | return stream.SendAndClose(&empty.Empty{}) |
| 48 | } else if err != nil { |
| 49 | common.Logger().WithFields(logrus.Fields{ |
| 50 | "handler": h, |
| 51 | "error": err.Error(), |
| 52 | }).Warn("Error occurred with stream") |
| 53 | return err |
| 54 | } |
| 55 | |
| 56 | frame := gopacket.NewPacket(data.Payload, layers.LayerTypeEthernet, gopacket.Default) |
| 57 | |
| 58 | h.device.Forward( |
| 59 | context.Background(), |
| 60 | int(data.Port), |
| 61 | frame, |
| 62 | ) |
| 63 | common.Logger().WithFields(logrus.Fields{ |
| 64 | "handler": h, |
| 65 | "frame": frame, |
| 66 | "port": data.Port, |
| 67 | }).Debug("Retrieved and forwarded packet") |
| 68 | |
| 69 | } |
| 70 | |
| 71 | return nil |
| 72 | } |