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 | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v7/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) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 28 | span, ctx := log.CreateChildSpan(ctx, "stream-packet-out") |
| 29 | defer span.Finish() |
| 30 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 31 | logger.Debug(ctx, "packet-out-started") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 32 | // If we exit, assume disconnected |
| 33 | defer func() { |
| 34 | ofa.events <- ofaEventVolthaDisconnected |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 35 | logger.Debug(ctx, "packet-out-finished") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 36 | }() |
| 37 | if ofa.volthaClient == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 38 | logger.Error(ctx, "no-voltha-connection") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 39 | return |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 40 | } |
| 41 | opt := grpc.EmptyCallOption{} |
Girish Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 42 | streamCtx, streamDone := context.WithCancel(context.Background()) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 43 | defer streamDone() |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 44 | vc := ofa.volthaClient.Get() |
| 45 | if vc == nil { |
| 46 | logger.Error(ctx, "No client found to create packetout stream") |
| 47 | return |
| 48 | } |
| 49 | outClient, err := vc.StreamPacketsOut(log.WithSpanFromContext(streamCtx, ctx), opt) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 50 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 51 | logger.Errorw(ctx, "streamPacketOut Error creating packetout stream ", log.Fields{"error": err}) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 52 | return |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 53 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 54 | top: |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 55 | for { |
| 56 | select { |
| 57 | case <-ctx.Done(): |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 58 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 59 | case ofPacketOut := <-ofa.packetOutChannel: |
| 60 | if logger.V(log.DebugLevel) { |
| 61 | js, _ := json.Marshal(ofPacketOut) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 62 | logger.Debugw(ctx, "streamPacketOut Receive PacketOut from Channel", log.Fields{"PacketOut": js}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 63 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 64 | if err := outClient.Send(ofPacketOut); err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 65 | logger.Errorw(ctx, "packet-out-send-error", |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 66 | log.Fields{"error": err.Error()}) |
| 67 | break top |
| 68 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 69 | logger.Debug(ctx, "packet-out-send") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | } |