blob: f369ab33f84cde66c5548115127e6a509c4482d0 [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"
Girish Kumar2ed051b2020-07-28 16:35:25 +000024 grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
25 grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080026 "github.com/opencord/voltha-lib-go/v3/pkg/log"
27 "github.com/opencord/voltha-lib-go/v3/pkg/probe"
28 "github.com/opencord/voltha-protos/v3/go/voltha"
Girish Kumar2ed051b2020-07-28 16:35:25 +000029 "github.com/opentracing/opentracing-go"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080030 "google.golang.org/grpc"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080031)
32
Rohan Agrawalc32d9932020-06-15 11:01:47 +000033func (ofa *OFAgent) establishConnectionToVoltha(ctx context.Context, p *probe.Probe) error {
David K. Bainbridge157bdab2020-01-16 14:38:05 -080034 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000035 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusPreparing)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080036 }
37
38 if ofa.volthaConnection != nil {
39 ofa.volthaConnection.Close()
40 }
41
42 ofa.volthaConnection = nil
David Bainbridgef8ce7d22020-04-08 12:49:41 -070043 ofa.volthaClient.Clear()
David K. Bainbridge157bdab2020-01-16 14:38:05 -080044 try := 1
45 for ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
Girish Kumar2ed051b2020-07-28 16:35:25 +000046 // Use Intercepters to automatically inject and publish Open Tracing Spans by this GRPC client
47 conn, err := grpc.Dial(ofa.VolthaApiEndPoint,
48 grpc.WithInsecure(),
49 grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(
50 grpc_opentracing.StreamClientInterceptor(grpc_opentracing.WithTracer(opentracing.GlobalTracer())),
51 )),
52 grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
53 grpc_opentracing.UnaryClientInterceptor(grpc_opentracing.WithTracer(opentracing.GlobalTracer())),
54 )))
David K. Bainbridge157bdab2020-01-16 14:38:05 -080055 if err == nil {
56 svc := voltha.NewVolthaServiceClient(conn)
57 if svc != nil {
58 if _, err = svc.GetVoltha(context.Background(), &empty.Empty{}); err == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000059 logger.Debugw(ctx, "Established connection to Voltha",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080060 log.Fields{
61 "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
62 })
63 ofa.volthaConnection = conn
David Bainbridgef8ce7d22020-04-08 12:49:41 -070064 ofa.volthaClient.Set(svc)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080065 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000066 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusRunning)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080067 }
68 ofa.events <- ofaEventVolthaConnected
69 return nil
70 }
71 }
72 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000073 logger.Warnw(ctx, "Failed to connect to voltha",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080074 log.Fields{
75 "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
76 "error": err.Error(),
77 })
78 if ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
79 if ofa.ConnectionMaxRetries != 0 {
80 try += 1
81 }
82 time.Sleep(ofa.ConnectionRetryDelay)
83 }
84 }
85 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000086 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusFailed)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080087 }
88 return errors.New("failed-to-connect-to-voltha")
89}