David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package ofagent |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 21 | "time" |
| 22 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 23 | "github.com/golang/protobuf/ptypes/empty" |
Girish Kumar | 2ed051b | 2020-07-28 16:35:25 +0000 | [diff] [blame] | 24 | grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" |
| 25 | grpc_opentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing" |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 26 | "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. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 29 | "google.golang.org/grpc" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 30 | ) |
| 31 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 32 | func (ofa *OFAgent) establishConnectionToVoltha(ctx context.Context, p *probe.Probe) error { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 33 | if p != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 34 | p.UpdateStatus(ctx, "voltha", probe.ServiceStatusPreparing) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | if ofa.volthaConnection != nil { |
Andrey Pozolotin | 536ee58 | 2021-05-28 16:31:44 +0300 | [diff] [blame] | 38 | 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. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | ofa.volthaConnection = nil |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 45 | ofa.volthaClient.Clear() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 46 | try := 1 |
| 47 | for ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries { |
Girish Kumar | 2ed051b | 2020-07-28 16:35:25 +0000 | [diff] [blame] | 48 | // 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 Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 52 | grpc_opentracing.StreamClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
Girish Kumar | 2ed051b | 2020-07-28 16:35:25 +0000 | [diff] [blame] | 53 | )), |
| 54 | grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient( |
Girish Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 55 | grpc_opentracing.UnaryClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
Girish Kumar | 2ed051b | 2020-07-28 16:35:25 +0000 | [diff] [blame] | 56 | ))) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 57 | if err == nil { |
| 58 | svc := voltha.NewVolthaServiceClient(conn) |
| 59 | if svc != nil { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 60 | if _, err = svc.GetVoltha(log.WithSpanFromContext(context.Background(), ctx), &empty.Empty{}); err == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 61 | logger.Debugw(ctx, "Established connection to Voltha", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 62 | log.Fields{ |
| 63 | "VolthaApiEndPoint": ofa.VolthaApiEndPoint, |
| 64 | }) |
| 65 | ofa.volthaConnection = conn |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 66 | ofa.volthaClient.Set(svc) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 67 | if p != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 68 | p.UpdateStatus(ctx, "voltha", probe.ServiceStatusRunning) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 69 | } |
| 70 | ofa.events <- ofaEventVolthaConnected |
| 71 | return nil |
| 72 | } |
| 73 | } |
| 74 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 75 | logger.Warnw(ctx, "Failed to connect to voltha", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 76 | 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 Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 88 | p.UpdateStatus(ctx, "voltha", probe.ServiceStatusFailed) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 89 | } |
| 90 | return errors.New("failed-to-connect-to-voltha") |
| 91 | } |