Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 16 | package vpagent |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "io" |
| 21 | |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 22 | "voltha-go-controller/log" |
Akash Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 23 | |
| 24 | "github.com/golang/protobuf/ptypes/empty" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 25 | "google.golang.org/grpc" |
| 26 | ) |
| 27 | |
| 28 | func (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 Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 36 | logger.Fatal(ctx, "no-voltha-connection") |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 37 | 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 | |
| 55 | top: |
| 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 Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 64 | 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 Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 77 | logger.Warnw(ctx, "Ignoring unhandled error", log.Fields{"err": err}) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 78 | continue |
| 79 | } |
| 80 | vpa.changeEventChannel <- ce |
| 81 | logger.Debug(ctx, "receive-change-event-queued") |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func (vpa *VPAgent) handleChangeEvents(ctx context.Context) { |
| 87 | logger.Debug(ctx, "handle-change-event-started") |
| 88 | |
| 89 | top: |
| 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 Soni | 53da285 | 2023-03-15 00:31:31 +0530 | [diff] [blame] | 98 | if err := vpc.ChangeEvent(changeEvent); err != nil { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 99 | 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 | } |