blob: 6fe654622f796644fd71899f5ef2d9a2c90b73e9 [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) {
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())
43 outClient, err := ofa.volthaClient.Get().StreamPacketsOut(log.WithSpanFromContext(streamCtx, ctx), opt)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080044 defer streamDone()
45 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000046 logger.Errorw(ctx, "streamPacketOut Error creating packetout stream ", log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080047 return
David K. Bainbridge157bdab2020-01-16 14:38:05 -080048 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080049top:
David K. Bainbridge157bdab2020-01-16 14:38:05 -080050 for {
51 select {
52 case <-ctx.Done():
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080053 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080054 case ofPacketOut := <-ofa.packetOutChannel:
55 if logger.V(log.DebugLevel) {
56 js, _ := json.Marshal(ofPacketOut)
Rohan Agrawalc32d9932020-06-15 11:01:47 +000057 logger.Debugw(ctx, "streamPacketOut Receive PacketOut from Channel", log.Fields{"PacketOut": js})
David K. Bainbridge157bdab2020-01-16 14:38:05 -080058 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080059 if err := outClient.Send(ofPacketOut); err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000060 logger.Errorw(ctx, "packet-out-send-error",
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080061 log.Fields{"error": err.Error()})
62 break top
63 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000064 logger.Debug(ctx, "packet-out-send")
David K. Bainbridge157bdab2020-01-16 14:38:05 -080065 }
66 }
67}