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-lib-go/v7/pkg/probe" |
| 27 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 28 | "google.golang.org/grpc" |
| 29 | ) |
| 30 | |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 31 | // GrpcMaxSize Max size of grpc message |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 32 | const GrpcMaxSize int = 17455678 |
| 33 | |
| 34 | func (vpa *VPAgent) establishConnectionToVoltha(ctx context.Context, p *probe.Probe) error { |
| 35 | if p != nil { |
| 36 | p.UpdateStatus(ctx, "voltha", probe.ServiceStatusPreparing) |
| 37 | } |
| 38 | |
| 39 | if vpa.volthaConnection != nil { |
| 40 | vpa.volthaConnection.Close() |
| 41 | } |
| 42 | |
| 43 | vpa.volthaConnection = nil |
| 44 | vpa.volthaClient.Clear() |
| 45 | try := 1 |
| 46 | for vpa.ConnectionMaxRetries == 0 || try < vpa.ConnectionMaxRetries { |
| 47 | conn, err := grpc.Dial(vpa.VolthaAPIEndPoint, grpc.WithInsecure(), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(GrpcMaxSize))) |
| 48 | if err == nil { |
| 49 | svc := voltha.NewVolthaServiceClient(conn) |
| 50 | if svc != nil { |
| 51 | if _, err = svc.GetVoltha(context.Background(), &empty.Empty{}); err == nil { |
| 52 | logger.Debugw(ctx, "Established connection to Voltha", |
| 53 | log.Fields{ |
| 54 | "VolthaApiEndPoint": vpa.VolthaAPIEndPoint, |
| 55 | }) |
| 56 | vpa.volthaConnection = conn |
| 57 | vpa.volthaClient.Set(svc) |
| 58 | if p != nil { |
| 59 | p.UpdateStatus(ctx, "voltha", probe.ServiceStatusRunning) |
| 60 | } |
| 61 | vpa.events <- vpaEventVolthaConnected |
| 62 | return nil |
| 63 | } |
| 64 | } |
| 65 | } |
Akash Soni | 6168f31 | 2023-05-18 20:57:33 +0530 | [diff] [blame] | 66 | logger.Fatalw(ctx, "Failed to connect to voltha", |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 67 | log.Fields{ |
| 68 | "VolthaApiEndPoint": vpa.VolthaAPIEndPoint, |
| 69 | "error": err.Error(), |
| 70 | }) |
| 71 | if vpa.ConnectionMaxRetries == 0 || try < vpa.ConnectionMaxRetries { |
| 72 | if vpa.ConnectionMaxRetries != 0 { |
| 73 | try++ |
| 74 | } |
| 75 | time.Sleep(vpa.ConnectionRetryDelay) |
| 76 | } |
| 77 | } |
| 78 | if p != nil { |
| 79 | p.UpdateStatus(ctx, "voltha", probe.ServiceStatusFailed) |
| 80 | } |
| 81 | return errors.New("failed-to-connect-to-voltha") |
| 82 | } |
| 83 | |
| 84 | // CloseConnectionToVoltha closes the grpc connection to VOLTHA |
| 85 | func (vpa *VPAgent) CloseConnectionToVoltha() { |
vinokuma | 926cb3e | 2023-03-29 11:41:06 +0530 | [diff] [blame] | 86 | // Close the grpc connection to voltha |
Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 87 | logger.Debug(ctx, "Closing voltha grpc connection") |
| 88 | vpa.volthaConnection.Close() |
| 89 | } |