blob: 54a4ed8a4c51a598d2493ecf5f5f5f1f090a1f5f [file] [log] [blame]
David K. Bainbridge157bdab2020-01-16 14:38:05 -08001/*
2 Copyright 2020 the original author or authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16package ofagent
17
18import (
19 "context"
20 "errors"
21 "github.com/golang/protobuf/ptypes/empty"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080022 "github.com/opencord/voltha-lib-go/v3/pkg/log"
23 "github.com/opencord/voltha-lib-go/v3/pkg/probe"
24 "github.com/opencord/voltha-protos/v3/go/voltha"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080025 "google.golang.org/grpc"
26 "time"
27)
28
29func (ofa *OFAgent) establishConnectionToVoltha(p *probe.Probe) error {
30 if p != nil {
31 p.UpdateStatus("voltha", probe.ServiceStatusPreparing)
32 }
33
34 if ofa.volthaConnection != nil {
35 ofa.volthaConnection.Close()
36 }
37
38 ofa.volthaConnection = nil
39 ofa.volthaClient = nil
40 try := 1
41 for ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
42 conn, err := grpc.Dial(ofa.VolthaApiEndPoint, grpc.WithInsecure())
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("Established connection to Voltha",
48 log.Fields{
49 "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
50 })
51 ofa.volthaConnection = conn
52 ofa.volthaClient = svc
53 if p != nil {
54 p.UpdateStatus("voltha", probe.ServiceStatusRunning)
55 }
56 ofa.events <- ofaEventVolthaConnected
57 return nil
58 }
59 }
60 }
61 logger.Warnw("Failed to connect to voltha",
62 log.Fields{
63 "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
64 "error": err.Error(),
65 })
66 if ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
67 if ofa.ConnectionMaxRetries != 0 {
68 try += 1
69 }
70 time.Sleep(ofa.ConnectionRetryDelay)
71 }
72 }
73 if p != nil {
74 p.UpdateStatus("voltha", probe.ServiceStatusFailed)
75 }
76 return errors.New("failed-to-connect-to-voltha")
77}