Prince Pereira | c1c21d6 | 2021-04-22 08:38:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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 grpc holds utils for grpc client implementation |
| 18 | package grpc |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | |
| 23 | "github.com/opencord/device-management-interface/go/dmi" |
| 24 | "github.com/opencord/opendevice-manager/pkg/config" |
| 25 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 26 | "google.golang.org/grpc/credentials" |
| 27 | |
| 28 | g "google.golang.org/grpc" |
| 29 | ) |
| 30 | |
| 31 | // logger represents the log object |
| 32 | var logger log.CLogger |
| 33 | |
| 34 | // init function for the package |
| 35 | func init() { |
| 36 | logger = config.Initlog() |
| 37 | } |
| 38 | |
| 39 | // Client holds the parameters for grpc |
| 40 | type Client struct { |
| 41 | uri string |
| 42 | conn *g.ClientConn |
| 43 | hwMgmtSvcClient dmi.NativeHWManagementServiceClient |
| 44 | } |
| 45 | |
| 46 | // NewClient returns a new Grpc Client |
| 47 | func NewClient(uri string) *Client { |
| 48 | c := new(Client) |
| 49 | c.uri = uri |
| 50 | return c |
| 51 | } |
| 52 | |
| 53 | func (c *Client) getDialOpts(ctx context.Context) []g.DialOption { |
| 54 | |
| 55 | coreFlags := config.NewCoreFlags() |
| 56 | var opts []g.DialOption |
| 57 | |
| 58 | if coreFlags.SecureConnection { |
| 59 | logger.Info(ctx, "Trying-to-establish-secure-connection") |
| 60 | |
| 61 | creds, err := credentials.NewClientTLSFromFile(coreFlags.CertsPath.RootCaCrt, "") |
| 62 | if err != nil { |
| 63 | logger.Fatalf(ctx, "could-not-process-the-credentials", log.Fields{"err": err}) |
| 64 | } |
| 65 | |
| 66 | err = creds.OverrideServerName(coreFlags.GrpcHostName) |
| 67 | if err != nil { |
| 68 | logger.Fatalf(ctx, "Overriding-server-name-failed-at-getDialOpts()", log.Fields{"err": err}) |
| 69 | } |
| 70 | |
| 71 | opts = append(opts, g.WithTransportCredentials(creds)) |
| 72 | } else { |
| 73 | logger.Info(ctx, "Trying-to-establish-insecure-connection") |
| 74 | opts = append(opts, g.WithInsecure()) |
| 75 | } |
| 76 | |
| 77 | opts = append(opts, g.WithTimeout(coreFlags.GrpcRetryInterval)) |
| 78 | backoffConfig := g.BackoffConfig{MaxDelay: coreFlags.GrpcBackoffMaxDelay} |
| 79 | opts = append(opts, g.WithBackoffConfig(backoffConfig)) |
| 80 | |
| 81 | return opts |
| 82 | } |
| 83 | |
| 84 | // Connect will establish a connection |
| 85 | func (c *Client) Connect(ctx context.Context) error { |
| 86 | logger.Info(ctx, "Invoked-connectGrpcServer") |
| 87 | // log.Info("Invoked-connectGrpcServer", log.Opts{"peer-ID": peerID}) |
| 88 | opts := c.getDialOpts(ctx) |
| 89 | // Establishing the server connection |
| 90 | conn, err := g.Dial(c.uri, opts...) |
| 91 | if err != nil { |
| 92 | logger.Error(ctx, "Grpc-client-connection-failed", log.Fields{"error": err}) |
| 93 | return err |
| 94 | } |
| 95 | c.conn = conn |
| 96 | logger.Info(ctx, "Connection-established", log.Fields{"conn": conn}) |
| 97 | // Constructing a client object |
| 98 | c.hwMgmtSvcClient = dmi.NewNativeHWManagementServiceClient(conn) |
| 99 | return nil |
| 100 | } |
| 101 | |
| 102 | // Disconnect will remove the connection |
| 103 | func (c *Client) Disconnect(ctx context.Context) error { |
| 104 | logger.Infow(ctx, "Invoked-Disconnect", log.Fields{"client": c}) |
| 105 | return c.conn.Close() |
| 106 | } |