blob: 4d9279bbd73572619c4d42182e307bb1a4827a27 [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"
David Bainbridgef8ce7d22020-04-08 12:49:41 -070021 "time"
22
David K. Bainbridge157bdab2020-01-16 14:38:05 -080023 "github.com/golang/protobuf/ptypes/empty"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080024 "github.com/opencord/voltha-lib-go/v3/pkg/log"
25 "github.com/opencord/voltha-lib-go/v3/pkg/probe"
26 "github.com/opencord/voltha-protos/v3/go/voltha"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080027 "google.golang.org/grpc"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080028)
29
Rohan Agrawalc32d9932020-06-15 11:01:47 +000030func (ofa *OFAgent) establishConnectionToVoltha(ctx context.Context, p *probe.Probe) error {
David K. Bainbridge157bdab2020-01-16 14:38:05 -080031 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000032 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusPreparing)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080033 }
34
35 if ofa.volthaConnection != nil {
36 ofa.volthaConnection.Close()
37 }
38
39 ofa.volthaConnection = nil
David Bainbridgef8ce7d22020-04-08 12:49:41 -070040 ofa.volthaClient.Clear()
David K. Bainbridge157bdab2020-01-16 14:38:05 -080041 try := 1
42 for ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
43 conn, err := grpc.Dial(ofa.VolthaApiEndPoint, grpc.WithInsecure())
44 if err == nil {
45 svc := voltha.NewVolthaServiceClient(conn)
46 if svc != nil {
47 if _, err = svc.GetVoltha(context.Background(), &empty.Empty{}); err == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000048 logger.Debugw(ctx, "Established connection to Voltha",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080049 log.Fields{
50 "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
51 })
52 ofa.volthaConnection = conn
David Bainbridgef8ce7d22020-04-08 12:49:41 -070053 ofa.volthaClient.Set(svc)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080054 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000055 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusRunning)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080056 }
57 ofa.events <- ofaEventVolthaConnected
58 return nil
59 }
60 }
61 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000062 logger.Warnw(ctx, "Failed to connect to voltha",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080063 log.Fields{
64 "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
65 "error": err.Error(),
66 })
67 if ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
68 if ofa.ConnectionMaxRetries != 0 {
69 try += 1
70 }
71 time.Sleep(ofa.ConnectionRetryDelay)
72 }
73 }
74 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000075 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusFailed)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080076 }
77 return errors.New("failed-to-connect-to-voltha")
78}