blob: 0b1dab0542274443e95f4104ad7c13ff097fcb8e [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. Bainbridgee05cf0c2021-08-19 03:16:50 +000026 "github.com/opencord/voltha-lib-go/v7/pkg/log"
27 "github.com/opencord/voltha-lib-go/v7/pkg/probe"
28 "github.com/opencord/voltha-protos/v5/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 {
Andrey Pozolotin536ee582021-05-28 16:31:44 +030038 err := ofa.volthaConnection.Close()
39 if err != nil {
40 logger.Errorw(ctx, "failed-connection-close-proceeding-setting-to-nil", log.Fields{"error": err})
41 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -080042 }
43
44 ofa.volthaConnection = nil
David Bainbridgef8ce7d22020-04-08 12:49:41 -070045 ofa.volthaClient.Clear()
David K. Bainbridge157bdab2020-01-16 14:38:05 -080046 try := 1
47 for ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
Girish Kumar2ed051b2020-07-28 16:35:25 +000048 // Use Intercepters to automatically inject and publish Open Tracing Spans by this GRPC client
49 conn, err := grpc.Dial(ofa.VolthaApiEndPoint,
50 grpc.WithInsecure(),
51 grpc.WithStreamInterceptor(grpc_middleware.ChainStreamClient(
Girish Kumarcd402012020-08-18 12:17:38 +000052 grpc_opentracing.StreamClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumar2ed051b2020-07-28 16:35:25 +000053 )),
54 grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient(
Girish Kumarcd402012020-08-18 12:17:38 +000055 grpc_opentracing.UnaryClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})),
Girish Kumar2ed051b2020-07-28 16:35:25 +000056 )))
David K. Bainbridge157bdab2020-01-16 14:38:05 -080057 if err == nil {
58 svc := voltha.NewVolthaServiceClient(conn)
59 if svc != nil {
Girish Kumar01e0c632020-08-10 16:48:56 +000060 if _, err = svc.GetVoltha(log.WithSpanFromContext(context.Background(), ctx), &empty.Empty{}); err == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000061 logger.Debugw(ctx, "Established connection to Voltha",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080062 log.Fields{
63 "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
64 })
65 ofa.volthaConnection = conn
David Bainbridgef8ce7d22020-04-08 12:49:41 -070066 ofa.volthaClient.Set(svc)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080067 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000068 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusRunning)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080069 }
70 ofa.events <- ofaEventVolthaConnected
71 return nil
72 }
73 }
74 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000075 logger.Warnw(ctx, "Failed to connect to voltha",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080076 log.Fields{
77 "VolthaApiEndPoint": ofa.VolthaApiEndPoint,
78 "error": err.Error(),
79 })
80 if ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries {
81 if ofa.ConnectionMaxRetries != 0 {
82 try += 1
83 }
84 time.Sleep(ofa.ConnectionRetryDelay)
85 }
86 }
87 if p != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000088 p.UpdateStatus(ctx, "voltha", probe.ServiceStatusFailed)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080089 }
90 return errors.New("failed-to-connect-to-voltha")
91}