blob: dbb908bbaf241d050f751693c66fe4abdab0899f [file] [log] [blame]
David K. Bainbridge157bdab2020-01-16 14:38:05 -08001/*
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
17package ofagent
18
19import (
20 "context"
21 "encoding/json"
David Bainbridgef8ce7d22020-04-08 12:49:41 -070022
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000023 "github.com/opencord/voltha-lib-go/v7/pkg/log"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080024 "google.golang.org/grpc"
25)
26
27func (ofa *OFAgent) streamPacketOut(ctx context.Context) {
Girish Kumar01e0c632020-08-10 16:48:56 +000028 span, ctx := log.CreateChildSpan(ctx, "stream-packet-out")
29 defer span.Finish()
30
Rohan Agrawalc32d9932020-06-15 11:01:47 +000031 logger.Debug(ctx, "packet-out-started")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080032 // If we exit, assume disconnected
33 defer func() {
34 ofa.events <- ofaEventVolthaDisconnected
Rohan Agrawalc32d9932020-06-15 11:01:47 +000035 logger.Debug(ctx, "packet-out-finished")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080036 }()
37 if ofa.volthaClient == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000038 logger.Error(ctx, "no-voltha-connection")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080039 return
David K. Bainbridge157bdab2020-01-16 14:38:05 -080040 }
41 opt := grpc.EmptyCallOption{}
Girish Kumarcd402012020-08-18 12:17:38 +000042 streamCtx, streamDone := context.WithCancel(context.Background())
David K. Bainbridge157bdab2020-01-16 14:38:05 -080043 defer streamDone()
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000044 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. Bainbridge157bdab2020-01-16 14:38:05 -080050 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000051 logger.Errorw(ctx, "streamPacketOut Error creating packetout stream ", log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080052 return
David K. Bainbridge157bdab2020-01-16 14:38:05 -080053 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080054top:
David K. Bainbridge157bdab2020-01-16 14:38:05 -080055 for {
56 select {
57 case <-ctx.Done():
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080058 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080059 case ofPacketOut := <-ofa.packetOutChannel:
60 if logger.V(log.DebugLevel) {
61 js, _ := json.Marshal(ofPacketOut)
Rohan Agrawalc32d9932020-06-15 11:01:47 +000062 logger.Debugw(ctx, "streamPacketOut Receive PacketOut from Channel", log.Fields{"PacketOut": js})
David K. Bainbridge157bdab2020-01-16 14:38:05 -080063 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080064 if err := outClient.Send(ofPacketOut); err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000065 logger.Errorw(ctx, "packet-out-send-error",
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080066 log.Fields{"error": err.Error()})
67 break top
68 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000069 logger.Debug(ctx, "packet-out-send")
David K. Bainbridge157bdab2020-01-16 14:38:05 -080070 }
71 }
72}