blob: d4b6a7389b797bc22344d44ad35b249f09d588dd [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) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000028 logger.Debug(ctx, "packet-out-started")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080029 // If we exit, assume disconnected
30 defer func() {
31 ofa.events <- ofaEventVolthaDisconnected
Rohan Agrawalc32d9932020-06-15 11:01:47 +000032 logger.Debug(ctx, "packet-out-finished")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080033 }()
34 if ofa.volthaClient == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000035 logger.Error(ctx, "no-voltha-connection")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080036 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 {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000043 logger.Errorw(ctx, "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)
Rohan Agrawalc32d9932020-06-15 11:01:47 +000054 logger.Debugw(ctx, "streamPacketOut Receive PacketOut from Channel", log.Fields{"PacketOut": js})
David K. Bainbridge157bdab2020-01-16 14:38:05 -080055 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080056 if err := outClient.Send(ofPacketOut); err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000057 logger.Errorw(ctx, "packet-out-send-error",
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080058 log.Fields{"error": err.Error()})
59 break top
60 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000061 logger.Debug(ctx, "packet-out-send")
David K. Bainbridge157bdab2020-01-16 14:38:05 -080062 }
63 }
64}