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" |
Maninder | 12b909f | 2020-10-23 14:23:36 +0530 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 27 | "github.com/opencord/voltha-lib-go/v4/pkg/probe" |
| 28 | "github.com/opencord/voltha-protos/v4/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 { |
| 38 | ofa.volthaConnection.Close() |
| 39 | } |
| 40 | |
| 41 | ofa.volthaConnection = nil |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 42 | ofa.volthaClient.Clear() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 43 | try := 1 |
| 44 | for ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries { |
Girish Kumar | 2ed051b | 2020-07-28 16:35:25 +0000 | [diff] [blame] | 45 | // 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 Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 49 | grpc_opentracing.StreamClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
Girish Kumar | 2ed051b | 2020-07-28 16:35:25 +0000 | [diff] [blame] | 50 | )), |
| 51 | grpc.WithUnaryInterceptor(grpc_middleware.ChainUnaryClient( |
Girish Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 52 | grpc_opentracing.UnaryClientInterceptor(grpc_opentracing.WithTracer(log.ActiveTracerProxy{})), |
Girish Kumar | 2ed051b | 2020-07-28 16:35:25 +0000 | [diff] [blame] | 53 | ))) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 54 | if err == nil { |
| 55 | svc := voltha.NewVolthaServiceClient(conn) |
| 56 | if svc != nil { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 57 | 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] | 58 | logger.Debugw(ctx, "Established connection to Voltha", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 59 | log.Fields{ |
| 60 | "VolthaApiEndPoint": ofa.VolthaApiEndPoint, |
| 61 | }) |
| 62 | ofa.volthaConnection = conn |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 63 | ofa.volthaClient.Set(svc) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 64 | if p != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 65 | p.UpdateStatus(ctx, "voltha", probe.ServiceStatusRunning) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 66 | } |
| 67 | ofa.events <- ofaEventVolthaConnected |
| 68 | return nil |
| 69 | } |
| 70 | } |
| 71 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 72 | logger.Warnw(ctx, "Failed to connect to voltha", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 73 | 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 Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 85 | p.UpdateStatus(ctx, "voltha", probe.ServiceStatusFailed) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 86 | } |
| 87 | return errors.New("failed-to-connect-to-voltha") |
| 88 | } |