blob: 928d19c50253da985df972c812a649e37b6cec90 [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 K. Bainbridgeaea73cd2020-01-27 10:44:50 -080022 "github.com/opencord/voltha-lib-go/v3/pkg/log"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080023 "google.golang.org/grpc"
24)
25
26func (ofa *OFAgent) streamPacketOut(ctx context.Context) {
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080027 logger.Debug("packet-out-started")
28 // If we exit, assume disconnected
29 defer func() {
30 ofa.events <- ofaEventVolthaDisconnected
31 logger.Debug("packet-out-finished")
32 }()
33 if ofa.volthaClient == nil {
34 logger.Error("no-voltha-connection")
35 return
David K. Bainbridge157bdab2020-01-16 14:38:05 -080036 }
37 opt := grpc.EmptyCallOption{}
38 streamCtx, streamDone := context.WithCancel(context.Background())
39 outClient, err := ofa.volthaClient.StreamPacketsOut(streamCtx, opt)
40 defer streamDone()
41 if err != nil {
42 logger.Errorw("streamPacketOut Error creating packetout stream ", log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080043 return
David K. Bainbridge157bdab2020-01-16 14:38:05 -080044 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080045top:
David K. Bainbridge157bdab2020-01-16 14:38:05 -080046 for {
47 select {
48 case <-ctx.Done():
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080049 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080050 case ofPacketOut := <-ofa.packetOutChannel:
51 if logger.V(log.DebugLevel) {
52 js, _ := json.Marshal(ofPacketOut)
53 logger.Debugw("streamPacketOut Receive PacketOut from Channel", log.Fields{"PacketOut": js})
54 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080055 if err := outClient.Send(ofPacketOut); err != nil {
56 logger.Errorw("packet-out-send-error",
57 log.Fields{"error": err.Error()})
58 break top
59 }
60 logger.Debug("packet-out-send")
David K. Bainbridge157bdab2020-01-16 14:38:05 -080061 }
62 }
63}