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