blob: a518638741530c81786e239fe06539282a6319ab [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"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080029 "google.golang.org/grpc"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080030)
31
Rohan Agrawalc32d9932020-06-15 11:01:47 +000032func (ofa *OFAgent) establishConnectionToVoltha(ctx context.Context, p *probe.Probe) error {
David K. Bainbridge157bdab2020-01-16 14:38:05 -080033 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000034 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusPreparing)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080035 }
36
37 if ofa.volthaConnection != nil {
38 ofa.volthaConnection.Close()
39 }
40
41 ofa.volthaConnection = nil
David Bainbridgef8ce7d22020-04-08 12:49:41 -070042 ofa.volthaClient.Clear()
David K. Bainbridge157bdab2020-01-16 14:38:05 -080043 try := 1
44 for ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
Girish Kumar2ed051b2020-07-28 16:35:25 +000045 // Use Intercepters to automatically inject and publish Open Tracing Spans by this GRPC client
46 conn, err := grpc.Dial(ofa.VolthaApiEndPoint,
47 grpc.WithInsecure(),
48 grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(
Girish Kumarcd402012020-08-18 12:17:38 +000049 grpc_opentracing.StreamClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumar2ed051b2020-07-28 16:35:25 +000050 )),
51 grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
Girish Kumarcd402012020-08-18 12:17:38 +000052 grpc_opentracing.UnaryClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumar2ed051b2020-07-28 16:35:25 +000053 )))
David K. Bainbridge157bdab2020-01-16 14:38:05 -080054 if err == nil {
55 svc := voltha.NewVolthaServiceClient(conn)
56 if svc != nil {
Girish Kumar01e0c632020-08-10 16:48:56 +000057 if _, err = svc.GetVoltha(log.WithSpanFromContext(context.Background(), ctx), &empty.Empty{}); err == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000058 logger.Debugw(ctx, "Established connection to Voltha",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080059 log.Fields{
60 "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
61 })
62 ofa.volthaConnection = conn
David Bainbridgef8ce7d22020-04-08 12:49:41 -070063 ofa.volthaClient.Set(svc)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080064 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000065 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusRunning)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080066 }
67 ofa.events <- ofaEventVolthaConnected
68 return nil
69 }
70 }
71 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000072 logger.Warnw(ctx, "Failed to connect to voltha",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080073 log.Fields{
74 "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
75 "error": err.Error(),
76 })
77 if ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
78 if ofa.ConnectionMaxRetries != 0 {
79 try += 1
80 }
81 time.Sleep(ofa.ConnectionRetryDelay)
82 }
83 }
84 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000085 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusFailed)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080086 }
87 return errors.New("failed-to-connect-to-voltha")
88}