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