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" |
David K. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 25 | "github.com/opencord/voltha-lib-go/v3/pkg/probe" |
| 26 | "github.com/opencord/voltha-protos/v3/go/voltha" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 27 | "google.golang.org/grpc" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | func (ofa *OFAgent) establishConnectionToVoltha(p *probe.Probe) error { |
| 31 | if p != nil { |
| 32 | p.UpdateStatus("voltha", probe.ServiceStatusPreparing) |
| 33 | } |
| 34 | |
| 35 | if ofa.volthaConnection != nil { |
| 36 | ofa.volthaConnection.Close() |
| 37 | } |
| 38 | |
| 39 | ofa.volthaConnection = nil |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 40 | ofa.volthaClient.Clear() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 41 | try := 1 |
| 42 | for ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries { |
| 43 | conn, err := grpc.Dial(ofa.VolthaApiEndPoint, grpc.WithInsecure()) |
| 44 | if err == nil { |
| 45 | svc := voltha.NewVolthaServiceClient(conn) |
| 46 | if svc != nil { |
| 47 | if _, err = svc.GetVoltha(context.Background(), &empty.Empty{}); err == nil { |
| 48 | logger.Debugw("Established connection to Voltha", |
| 49 | log.Fields{ |
| 50 | "VolthaApiEndPoint": ofa.VolthaApiEndPoint, |
| 51 | }) |
| 52 | ofa.volthaConnection = conn |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 53 | ofa.volthaClient.Set(svc) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 54 | if p != nil { |
| 55 | p.UpdateStatus("voltha", probe.ServiceStatusRunning) |
| 56 | } |
| 57 | ofa.events <- ofaEventVolthaConnected |
| 58 | return nil |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | logger.Warnw("Failed to connect to voltha", |
| 63 | log.Fields{ |
| 64 | "VolthaApiEndPoint": ofa.VolthaApiEndPoint, |
| 65 | "error": err.Error(), |
| 66 | }) |
| 67 | if ofa.ConnectionMaxRetries == 0 || try < ofa.ConnectionMaxRetries { |
| 68 | if ofa.ConnectionMaxRetries != 0 { |
| 69 | try += 1 |
| 70 | } |
| 71 | time.Sleep(ofa.ConnectionRetryDelay) |
| 72 | } |
| 73 | } |
| 74 | if p != nil { |
| 75 | p.UpdateStatus("voltha", probe.ServiceStatusFailed) |
| 76 | } |
| 77 | return errors.New("failed-to-connect-to-voltha") |
| 78 | } |