blob: e4f562b5c167d1593b291bb9b1acccc1fbf0d178 [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 "errors"
21 "time"
22
Tinoj Joseph1d108322022-07-13 10:07:39 +053023 "voltha-go-controller/log"
vinokuma926cb3e2023-03-29 11:41:06 +053024
25 "github.com/golang/protobuf/ptypes/empty"
Naveen Sampath04696f72022-06-13 15:19:14 +053026 "github.com/opencord/voltha-protos/v5/go/voltha"
27 "google.golang.org/grpc"
Abhay Kumarfe505f22025-11-10 14:16:31 +000028 "google.golang.org/grpc/credentials/insecure"
Naveen Sampath04696f72022-06-13 15:19:14 +053029)
30
vinokuma926cb3e2023-03-29 11:41:06 +053031// GrpcMaxSize Max size of grpc message
Naveen Sampath04696f72022-06-13 15:19:14 +053032const GrpcMaxSize int = 17455678
33
Akash Sonief452f12024-12-12 18:20:28 +053034func (vpa *VPAgent) establishConnectionToVoltha(ctx context.Context) error {
Naveen Sampath04696f72022-06-13 15:19:14 +053035 if vpa.volthaConnection != nil {
mgoudabb017dc2025-10-29 19:53:34 +053036 _ = vpa.volthaConnection.Close()
Naveen Sampath04696f72022-06-13 15:19:14 +053037 }
38
39 vpa.volthaConnection = nil
40 vpa.volthaClient.Clear()
41 try := 1
42 for vpa.ConnectionMaxRetries == 0 || try < vpa.ConnectionMaxRetries {
Abhay Kumarfe505f22025-11-10 14:16:31 +000043 conn, err := grpc.NewClient(vpa.VolthaAPIEndPoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(GrpcMaxSize)))
Naveen Sampath04696f72022-06-13 15:19:14 +053044 if err == nil {
45 svc := voltha.NewVolthaServiceClient(conn)
46 if svc != nil {
47 if _, err = svc.GetVoltha(context.Background(), &empty.Empty{}); err == nil {
48 logger.Debugw(ctx, "Established connection to Voltha",
49 log.Fields{
50 "VolthaApiEndPoint": vpa.VolthaAPIEndPoint,
51 })
52 vpa.volthaConnection = conn
53 vpa.volthaClient.Set(svc)
Naveen Sampath04696f72022-06-13 15:19:14 +053054 vpa.events <- vpaEventVolthaConnected
55 return nil
56 }
57 }
58 }
Akash Soni634d9bf2023-07-10 12:11:10 +053059 logger.Errorw(ctx, "Failed to connect to voltha",
Naveen Sampath04696f72022-06-13 15:19:14 +053060 log.Fields{
61 "VolthaApiEndPoint": vpa.VolthaAPIEndPoint,
62 "error": err.Error(),
63 })
64 if vpa.ConnectionMaxRetries == 0 || try < vpa.ConnectionMaxRetries {
65 if vpa.ConnectionMaxRetries != 0 {
66 try++
67 }
68 time.Sleep(vpa.ConnectionRetryDelay)
69 }
70 }
Naveen Sampath04696f72022-06-13 15:19:14 +053071 return errors.New("failed-to-connect-to-voltha")
72}
73
74// CloseConnectionToVoltha closes the grpc connection to VOLTHA
75func (vpa *VPAgent) CloseConnectionToVoltha() {
vinokuma926cb3e2023-03-29 11:41:06 +053076 // Close the grpc connection to voltha
Naveen Sampath04696f72022-06-13 15:19:14 +053077 logger.Debug(ctx, "Closing voltha grpc connection")
mgoudabb017dc2025-10-29 19:53:34 +053078 _ = vpa.volthaConnection.Close()
Naveen Sampath04696f72022-06-13 15:19:14 +053079}