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 | "errors" |
| 21 | "time" |
| 22 | |
Tinoj Joseph | 1d10832 | 2022-07-13 10:07:39 +0530 | [diff] [blame] | 23 | "voltha-go-controller/log" |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 24 | |
| 25 | "github.com/golang/protobuf/ptypes/empty" |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 27 | "google.golang.org/grpc" |
| 28 | ) |
| 29 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 30 | // GrpcMaxSize Max size of grpc message |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 31 | const GrpcMaxSize int = 17455678 |
| 32 | |
Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame^] | 33 | func (vpa *VPAgent) establishConnectionToVoltha(ctx context.Context) error { |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 34 | if vpa.volthaConnection != nil { |
| 35 | vpa.volthaConnection.Close() |
| 36 | } |
| 37 | |
| 38 | vpa.volthaConnection = nil |
| 39 | vpa.volthaClient.Clear() |
| 40 | try := 1 |
| 41 | for vpa.ConnectionMaxRetries == 0 || try < vpa.ConnectionMaxRetries { |
| 42 | conn, err := grpc.Dial(vpa.VolthaAPIEndPoint, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(GrpcMaxSize))) |
| 43 | if err == nil { |
| 44 | svc := voltha.NewVolthaServiceClient(conn) |
| 45 | if svc != nil { |
| 46 | if _, err = svc.GetVoltha(context.Background(), &empty.Empty{}); err == nil { |
| 47 | logger.Debugw(ctx, "Established connection to Voltha", |
| 48 | log.Fields{ |
| 49 | "VolthaApiEndPoint": vpa.VolthaAPIEndPoint, |
| 50 | }) |
| 51 | vpa.volthaConnection = conn |
| 52 | vpa.volthaClient.Set(svc) |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 53 | vpa.events <- vpaEventVolthaConnected |
| 54 | return nil |
| 55 | } |
| 56 | } |
| 57 | } |
Akash Soni | 634d9bf | 2023-07-10 12:11:10 +0530 | [diff] [blame] | 58 | logger.Errorw(ctx, "Failed to connect to voltha", |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 59 | log.Fields{ |
| 60 | "VolthaApiEndPoint": vpa.VolthaAPIEndPoint, |
| 61 | "error": err.Error(), |
| 62 | }) |
| 63 | if vpa.ConnectionMaxRetries == 0 || try < vpa.ConnectionMaxRetries { |
| 64 | if vpa.ConnectionMaxRetries != 0 { |
| 65 | try++ |
| 66 | } |
| 67 | time.Sleep(vpa.ConnectionRetryDelay) |
| 68 | } |
| 69 | } |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 70 | return errors.New("failed-to-connect-to-voltha") |
| 71 | } |
| 72 | |
| 73 | // CloseConnectionToVoltha closes the grpc connection to VOLTHA |
| 74 | func (vpa *VPAgent) CloseConnectionToVoltha() { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 75 | // Close the grpc connection to voltha |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 76 | logger.Debug(ctx, "Closing voltha grpc connection") |
| 77 | vpa.volthaConnection.Close() |
| 78 | } |