Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Stephane Barbarie | 3559506 | 2018-02-08 08:34:39 -0500 | [diff] [blame] | 16 | package sbi |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "github.com/golang/protobuf/ptypes/empty" |
| 21 | "github.com/google/gopacket" |
| 22 | "github.com/google/gopacket/layers" |
| 23 | "github.com/opencord/voltha/ponsim/v2/common" |
| 24 | "github.com/opencord/voltha/ponsim/v2/core" |
| 25 | "github.com/opencord/voltha/protos/go/ponsim" |
| 26 | "github.com/sirupsen/logrus" |
| 27 | "io" |
| 28 | ) |
| 29 | |
| 30 | type PonSimCommonHandler struct { |
| 31 | device core.PonSimInterface |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | NewPonSimCommonHandler instantiates a handler for common GRPC servicing methods |
| 36 | */ |
| 37 | func NewPonSimCommonHandler(device core.PonSimInterface) *PonSimCommonHandler { |
| 38 | var handler *PonSimCommonHandler |
| 39 | |
| 40 | handler = &PonSimCommonHandler{device: device} |
| 41 | |
| 42 | return handler |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | ProcessData handles and forwards streaming INGRESS/EGRESS packets |
| 47 | */ |
| 48 | func (h *PonSimCommonHandler) ProcessData(stream ponsim.PonSimCommon_ProcessDataServer) error { |
| 49 | common.Logger().WithFields(logrus.Fields{ |
| 50 | "handler": h, |
| 51 | }).Debug("Processing data") |
| 52 | |
| 53 | var err error |
| 54 | var data *ponsim.IncomingData |
| 55 | |
| 56 | for { |
| 57 | |
| 58 | if data, err = stream.Recv(); err == io.EOF { |
| 59 | common.Logger().WithFields(logrus.Fields{ |
| 60 | "handler": h, |
| 61 | }).Warn("Streaming channel was closed") |
| 62 | return stream.SendAndClose(&empty.Empty{}) |
| 63 | } else if err != nil { |
| 64 | common.Logger().WithFields(logrus.Fields{ |
| 65 | "handler": h, |
| 66 | "error": err.Error(), |
| 67 | }).Warn("Error occurred with stream") |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | frame := gopacket.NewPacket(data.Payload, layers.LayerTypeEthernet, gopacket.Default) |
| 72 | |
| 73 | h.device.Forward( |
| 74 | context.Background(), |
| 75 | int(data.Port), |
| 76 | frame, |
| 77 | ) |
| 78 | common.Logger().WithFields(logrus.Fields{ |
| 79 | "handler": h, |
| 80 | "frame": frame, |
| 81 | "port": data.Port, |
| 82 | }).Debug("Retrieved and forwarded packet") |
| 83 | |
| 84 | } |
| 85 | |
| 86 | return nil |
| 87 | } |