David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 the original author or authors. |
| 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 | */ |
| 16 | |
| 17 | package ofagent |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
David K. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 23 | "google.golang.org/grpc" |
| 24 | ) |
| 25 | |
| 26 | func (ofa *OFAgent) streamPacketOut(ctx context.Context) { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 27 | logger.Debug("packet-out-started") |
| 28 | // If we exit, assume disconnected |
| 29 | defer func() { |
| 30 | ofa.events <- ofaEventVolthaDisconnected |
| 31 | logger.Debug("packet-out-finished") |
| 32 | }() |
| 33 | if ofa.volthaClient == nil { |
| 34 | logger.Error("no-voltha-connection") |
| 35 | return |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 36 | } |
| 37 | opt := grpc.EmptyCallOption{} |
| 38 | streamCtx, streamDone := context.WithCancel(context.Background()) |
| 39 | outClient, err := ofa.volthaClient.StreamPacketsOut(streamCtx, opt) |
| 40 | defer streamDone() |
| 41 | if err != nil { |
| 42 | logger.Errorw("streamPacketOut Error creating packetout stream ", log.Fields{"error": err}) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 43 | return |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 44 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 45 | top: |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 46 | for { |
| 47 | select { |
| 48 | case <-ctx.Done(): |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 49 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 50 | case ofPacketOut := <-ofa.packetOutChannel: |
| 51 | if logger.V(log.DebugLevel) { |
| 52 | js, _ := json.Marshal(ofPacketOut) |
| 53 | logger.Debugw("streamPacketOut Receive PacketOut from Channel", log.Fields{"PacketOut": js}) |
| 54 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 55 | if err := outClient.Send(ofPacketOut); err != nil { |
| 56 | logger.Errorw("packet-out-send-error", |
| 57 | log.Fields{"error": err.Error()}) |
| 58 | break top |
| 59 | } |
| 60 | logger.Debug("packet-out-send") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | } |