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