blob: 37dac960a7cf55b05983feac633323101b19edae [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
Elia Battistonac8d23f2022-03-14 17:54:56 +010025 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
Elia Battistonbe9edc12022-03-09 11:35:58 +010026 "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
34const (
Elia Battistonac8d23f2022-03-14 17:54:56 +010035 nbiInitialBackoffInterval = time.Second
36 nbiMaxBackoffInterval = time.Second * 10
Elia Battistonbe9edc12022-03-09 11:35:58 +010037)
38
39//Used to keep track of a connection to a grpc endpoint of the northbound api
40type VolthaNbiClient struct {
41 conn *grpc.ClientConn
42 Service voltha.VolthaServiceClient
43 endpoint string
44}
45
46// Creates a new voltha northbound client
47func 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
54func (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 Battistonac8d23f2022-03-14 17:54:56 +010086 backoff := vgrpc.NewBackoff(nbiInitialBackoffInterval, nbiMaxBackoffInterval, vgrpc.DefaultBackoffMaxElapsedTime)
Elia Battistonbe9edc12022-03-09 11:35:58 +010087 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 Battistonac8d23f2022-03-14 17:54:56 +010094 if err := backoff.Backoff(ctx); err != nil {
Elia Battistonbe9edc12022-03-09 11:35:58 +010095 return fmt.Errorf("voltha-nbi-connection-stopped-due-to-context-done")
Elia Battistonbe9edc12022-03-09 11:35:58 +010096 }
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 Battistonac8d23f2022-03-14 17:54:56 +0100107func (c *VolthaNbiClient) Close(ctx context.Context) {
Elia Battistonbe9edc12022-03-09 11:35:58 +0100108 c.conn.Close()
109 c.Service = nil
Elia Battistonac8d23f2022-03-14 17:54:56 +0100110
111 logger.Debug(ctx, "closed-voltha-nbi-grpc-connection")
Elia Battistonbe9edc12022-03-09 11:35:58 +0100112}