blob: e975396746044521dd8eac5afb2fdd9119e09cbf [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16package vpagent
17
18import (
19 "context"
20 "io"
21
Tinoj Joseph1d108322022-07-13 10:07:39 +053022 "voltha-go-controller/log"
Akash Soni53da2852023-03-15 00:31:31 +053023
24 "github.com/golang/protobuf/ptypes/empty"
Naveen Sampath04696f72022-06-13 15:19:14 +053025 "google.golang.org/grpc"
26)
27
28func (vpa *VPAgent) receiveChangeEvents(ctx context.Context) {
29 logger.Debug(ctx, "receive-change-events-started")
30 // If we exit, assume disconnected
31 defer func() {
32 vpa.events <- vpaEventVolthaDisconnected
33 logger.Debug(ctx, "receive-change-events-finished")
34 }()
35 if vpa.volthaClient == nil {
Akash Soni6168f312023-05-18 20:57:33 +053036 logger.Fatal(ctx, "no-voltha-connection")
Naveen Sampath04696f72022-06-13 15:19:14 +053037 return
38 }
39 opt := grpc.EmptyCallOption{}
40 streamCtx, streamDone := context.WithCancel(context.Background())
41 defer streamDone()
42 vServiceClient := vpa.volthaClient.Get()
43 if vServiceClient == nil {
44 logger.Error(ctx, "Failed to get Voltha Service Client")
45 return
46 }
47
48 stream, err := vServiceClient.ReceiveChangeEvents(streamCtx, &empty.Empty{}, opt)
49 if err != nil {
50 logger.Errorw(ctx, "Unable to establish Receive Change Event Stream",
51 log.Fields{"error": err})
52 return
53 }
54
55top:
56 for {
57 select {
58 case <-ctx.Done():
59 logger.Errorw(ctx, "Context Done", log.Fields{"Context": ctx})
60 break top
61 default:
62 ce, err := stream.Recv()
63 if err == io.EOF {
Naveen Sampath04696f72022-06-13 15:19:14 +053064 stream, err = vServiceClient.ReceiveChangeEvents(streamCtx, &empty.Empty{}, opt)
65 if err != nil {
66 logger.Errorw(ctx, "Unable to establish Receive Change Event Stream",
67 log.Fields{"error": err})
68 return
69 }
70 continue
71 }
72 if isConnCanceled(err) {
73 logger.Errorw(ctx, "error receiving change event",
74 log.Fields{"error": err})
75 break top
76 } else if err != nil {
Akash Soni6168f312023-05-18 20:57:33 +053077 logger.Warnw(ctx, "Ignoring unhandled error", log.Fields{"err": err})
Naveen Sampath04696f72022-06-13 15:19:14 +053078 continue
79 }
80 vpa.changeEventChannel <- ce
81 logger.Debug(ctx, "receive-change-event-queued")
82 }
83 }
84}
85
86func (vpa *VPAgent) handleChangeEvents(ctx context.Context) {
87 logger.Debug(ctx, "handle-change-event-started")
88
89top:
90 for {
91 select {
92 case <-ctx.Done():
93 logger.Errorw(ctx, "Context Done", log.Fields{"Context": ctx})
94 break top
95 case changeEvent := <-vpa.changeEventChannel:
96 logger.Debugw(ctx, "Change Event", log.Fields{"Device": changeEvent.Id})
97 if vpc := vpa.getVPClient(changeEvent.Id); vpc != nil {
Akash Soni53da2852023-03-15 00:31:31 +053098 if err := vpc.ChangeEvent(changeEvent); err != nil {
Naveen Sampath04696f72022-06-13 15:19:14 +053099 logger.Errorw(ctx, "error handling Change Event", log.Fields{"Error": err, "Device": changeEvent.Id})
100 }
101 }
102 }
103 }
104
105 logger.Debug(ctx, "handle-change-event-finsihed")
106}