| 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" |
| Abhay Kumar | be5b5bd | 2025-12-09 08:47:09 +0000 | [diff] [blame^] | 26 | grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" |
| 27 | grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus" |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| Abhay Kumar | be5b5bd | 2025-12-09 08:47:09 +0000 | [diff] [blame^] | 29 | |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 30 | "google.golang.org/grpc" |
| Abhay Kumar | fe505f2 | 2025-11-10 14:16:31 +0000 | [diff] [blame] | 31 | "google.golang.org/grpc/credentials/insecure" |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 32 | ) |
| 33 | |
| vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 34 | // GrpcMaxSize Max size of grpc message |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 35 | const GrpcMaxSize int = 17455678 |
| 36 | |
| Akash Soni | ef452f1 | 2024-12-12 18:20:28 +0530 | [diff] [blame] | 37 | func (vpa *VPAgent) establishConnectionToVoltha(ctx context.Context) error { |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 38 | if vpa.volthaConnection != nil { |
| mgouda | bb017dc | 2025-10-29 19:53:34 +0530 | [diff] [blame] | 39 | _ = vpa.volthaConnection.Close() |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | vpa.volthaConnection = nil |
| 43 | vpa.volthaClient.Clear() |
| 44 | try := 1 |
| Abhay Kumar | be5b5bd | 2025-12-09 08:47:09 +0000 | [diff] [blame^] | 45 | grpc_prometheus.EnableClientHandlingTimeHistogram() |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 46 | for vpa.ConnectionMaxRetries == 0 || try < vpa.ConnectionMaxRetries { |
| Abhay Kumar | be5b5bd | 2025-12-09 08:47:09 +0000 | [diff] [blame^] | 47 | conn, err := grpc.NewClient(vpa.VolthaAPIEndPoint, |
| 48 | grpc.WithTransportCredentials(insecure.NewCredentials()), |
| 49 | grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(GrpcMaxSize)), |
| 50 | grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient( |
| 51 | grpc_prometheus.StreamClientInterceptor, |
| 52 | )), |
| 53 | grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient( |
| 54 | grpc_prometheus.UnaryClientInterceptor, |
| 55 | ))) |
| 56 | |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 57 | if err == nil { |
| 58 | svc := voltha.NewVolthaServiceClient(conn) |
| 59 | if svc != nil { |
| 60 | if _, err = svc.GetVoltha(context.Background(), &empty.Empty{}); err == nil { |
| 61 | logger.Debugw(ctx, "Established connection to Voltha", |
| 62 | log.Fields{ |
| 63 | "VolthaApiEndPoint": vpa.VolthaAPIEndPoint, |
| 64 | }) |
| 65 | vpa.volthaConnection = conn |
| 66 | vpa.volthaClient.Set(svc) |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 67 | vpa.events <- vpaEventVolthaConnected |
| 68 | return nil |
| 69 | } |
| 70 | } |
| 71 | } |
| Akash Soni | 634d9bf | 2023-07-10 12:11:10 +0530 | [diff] [blame] | 72 | logger.Errorw(ctx, "Failed to connect to voltha", |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 73 | log.Fields{ |
| 74 | "VolthaApiEndPoint": vpa.VolthaAPIEndPoint, |
| 75 | "error": err.Error(), |
| 76 | }) |
| 77 | if vpa.ConnectionMaxRetries == 0 || try < vpa.ConnectionMaxRetries { |
| 78 | if vpa.ConnectionMaxRetries != 0 { |
| 79 | try++ |
| 80 | } |
| 81 | time.Sleep(vpa.ConnectionRetryDelay) |
| 82 | } |
| 83 | } |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 84 | return errors.New("failed-to-connect-to-voltha") |
| 85 | } |
| 86 | |
| 87 | // CloseConnectionToVoltha closes the grpc connection to VOLTHA |
| 88 | func (vpa *VPAgent) CloseConnectionToVoltha() { |
| vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 89 | // Close the grpc connection to voltha |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 90 | logger.Debug(ctx, "Closing voltha grpc connection") |
| mgouda | bb017dc | 2025-10-29 19:53:34 +0530 | [diff] [blame] | 91 | _ = vpa.volthaConnection.Close() |
| Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 92 | } |