blob: ffeafec1e24e232fff2785a18015aabf6a7d1dae [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. Bainbridgeaea73cd2020-01-27 10:44:50 -080023 "github.com/opencord/voltha-lib-go/v3/pkg/log"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080024 "google.golang.org/grpc"
25)
26
27func (ofa *OFAgent) streamPacketOut(ctx context.Context) {
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080028 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. Bainbridge157bdab2020-01-16 14:38:05 -080037 }
38 opt := grpc.EmptyCallOption{}
39 streamCtx, streamDone := context.WithCancel(context.Background())
David Bainbridgef8ce7d22020-04-08 12:49:41 -070040 outClient, err := ofa.volthaClient.Get().StreamPacketsOut(streamCtx, opt)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080041 defer streamDone()
42 if err != nil {
43 logger.Errorw("streamPacketOut Error creating packetout stream ", log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080044 return
David K. Bainbridge157bdab2020-01-16 14:38:05 -080045 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080046top:
David K. Bainbridge157bdab2020-01-16 14:38:05 -080047 for {
48 select {
49 case <-ctx.Done():
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080050 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080051 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. Bainbridge9cb404e2020-01-28 14:32:29 -080056 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. Bainbridge157bdab2020-01-16 14:38:05 -080062 }
63 }
64}