Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022-present Open Networking Foundation |
| 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 | |
| 17 | package clients |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "crypto/tls" |
| 22 | "fmt" |
| 23 | "time" |
| 24 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 25 | vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc" |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 27 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 28 | "google.golang.org/grpc" |
| 29 | "google.golang.org/grpc/backoff" |
| 30 | "google.golang.org/grpc/connectivity" |
| 31 | "google.golang.org/grpc/credentials" |
| 32 | ) |
| 33 | |
| 34 | const ( |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 35 | nbiInitialBackoffInterval = time.Second |
| 36 | nbiMaxBackoffInterval = time.Second * 10 |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 37 | ) |
| 38 | |
| 39 | //Used to keep track of a connection to a grpc endpoint of the northbound api |
| 40 | type VolthaNbiClient struct { |
| 41 | conn *grpc.ClientConn |
| 42 | Service voltha.VolthaServiceClient |
| 43 | endpoint string |
| 44 | } |
| 45 | |
| 46 | // Creates a new voltha northbound client |
| 47 | func NewVolthaNbiClient(endpoint string) *VolthaNbiClient { |
| 48 | return &VolthaNbiClient{ |
| 49 | endpoint: endpoint, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // Dials the grpc connection to the endpoint and sets the service as running |
| 54 | func (c *VolthaNbiClient) Connect(ctx context.Context, useTls bool, verifyTls bool) error { |
| 55 | var opts []grpc.DialOption |
| 56 | |
| 57 | backoffConfig := backoff.DefaultConfig |
| 58 | backoffConfig.MaxDelay = nbiMaxBackoffInterval |
| 59 | |
| 60 | opts = append(opts, |
| 61 | grpc.WithConnectParams( |
| 62 | grpc.ConnectParams{ |
| 63 | Backoff: backoffConfig, |
| 64 | }, |
| 65 | ), |
| 66 | ) |
| 67 | |
| 68 | if useTls { |
| 69 | //TODO: should this be expanded with the ability to provide certificates? |
| 70 | creds := credentials.NewTLS(&tls.Config{InsecureSkipVerify: !verifyTls}) |
| 71 | opts = append(opts, grpc.WithTransportCredentials(creds)) |
| 72 | } else { |
| 73 | opts = append(opts, grpc.WithInsecure()) |
| 74 | } |
| 75 | |
| 76 | logger.Debugw(ctx, "connecting-to-voltha-nbi-grpc", log.Fields{"endpoint": c.endpoint}) |
| 77 | |
| 78 | var err error |
| 79 | c.conn, err = grpc.DialContext(ctx, c.endpoint, opts...) |
| 80 | |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | |
| 85 | //Wait for the connection to be successful, with periodic updates on its status |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 86 | backoff := vgrpc.NewBackoff(nbiInitialBackoffInterval, nbiMaxBackoffInterval, vgrpc.DefaultBackoffMaxElapsedTime) |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 87 | for { |
| 88 | if state := c.conn.GetState(); state == connectivity.Ready { |
| 89 | break |
| 90 | } else { |
| 91 | logger.Warnw(ctx, "voltha-nbi-grpc-not-ready", log.Fields{"state": state}) |
| 92 | } |
| 93 | |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 94 | if err := backoff.Backoff(ctx); err != nil { |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 95 | return fmt.Errorf("voltha-nbi-connection-stopped-due-to-context-done") |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | c.Service = voltha.NewVolthaServiceClient(c.conn) |
| 100 | |
| 101 | logger.Debug(ctx, "voltha-nbi-grpc-connected") |
| 102 | |
| 103 | return nil |
| 104 | } |
| 105 | |
| 106 | // Closes the connection and cleans up |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 107 | func (c *VolthaNbiClient) Close(ctx context.Context) { |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 108 | c.conn.Close() |
| 109 | c.Service = nil |
Elia Battiston | ac8d23f | 2022-03-14 17:54:56 +0100 | [diff] [blame] | 110 | |
| 111 | logger.Debug(ctx, "closed-voltha-nbi-grpc-connection") |
Elia Battiston | be9edc1 | 2022-03-09 11:35:58 +0100 | [diff] [blame] | 112 | } |