Skip to content

Commit 9a9f37f

Browse files
Merge pull request sonic-net#1 from renukamanavalan/statistics
Statistics -- update added
2 parents 5751f32 + d5ba1cb commit 9a9f37f

9 files changed

+339
-63
lines changed

azure-pipelines.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ resources:
1717
repositories:
1818
- repository: sonic-mgmt-common
1919
type: github
20-
name: Azure/sonic-mgmt-common
21-
endpoint: build
20+
name: sonic-net/sonic-mgmt-common
21+
endpoint: sonic-net
2222

2323
stages:
2424
- stage: Build

gnmi_server/client_subscribe.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (c *Client) Run(stream gnmipb.GNMI_SubscribeServer) (err error) {
176176

177177
log.V(1).Infof("Client %s running", c)
178178
go c.recv(stream)
179-
err = c.send(stream)
179+
err = c.send(stream, dc)
180180
c.Close()
181181
// Wait until all child go routines exited
182182
c.w.Wait()
@@ -247,8 +247,9 @@ func (c *Client) recv(stream gnmipb.GNMI_SubscribeServer) {
247247
}
248248

249249
// send runs until process Queue returns an error.
250-
func (c *Client) send(stream gnmipb.GNMI_SubscribeServer) error {
250+
func (c *Client) send(stream gnmipb.GNMI_SubscribeServer, dc sdc.Client) error {
251251
for {
252+
var val *sdc.Value
252253
items, err := c.q.Get(1)
253254

254255
if items == nil {
@@ -262,12 +263,14 @@ func (c *Client) send(stream gnmipb.GNMI_SubscribeServer) error {
262263
}
263264

264265
var resp *gnmipb.SubscribeResponse
266+
265267
switch v := items[0].(type) {
266268
case sdc.Value:
267269
if resp, err = sdc.ValToResp(v); err != nil {
268270
c.errors++
269271
return err
270272
}
273+
val = &v;
271274
default:
272275
log.V(1).Infof("Unknown data type %v for %s in queue", items[0], c)
273276
c.errors++
@@ -278,8 +281,11 @@ func (c *Client) send(stream gnmipb.GNMI_SubscribeServer) error {
278281
if err != nil {
279282
log.V(1).Infof("Client %s sending error:%v", c, err)
280283
c.errors++
284+
dc.FailedSend()
281285
return err
282286
}
287+
288+
dc.SentOne(val)
283289
log.V(5).Infof("Client %s done sending, msg count %d, msg %v", c, c.sendMsg, resp)
284290
}
285291
}

gnmi_server/server.go

+1
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ func authenticate(UserAuth AuthTypes, ctx context.Context) (context.Context, err
203203
if !success {
204204
return ctx, status.Error(codes.Unauthenticated, "Unauthenticated")
205205
}
206+
log.V(5).Infof("authenticate user %v, roles %v", rc.Auth.User, rc.Auth.Roles)
206207

207208
return ctx, nil
208209
}

gnmi_server/server_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"io/ioutil"
1717
"os"
1818
"os/exec"
19+
"os/user"
1920
"reflect"
2021
"testing"
2122
"time"
@@ -42,6 +43,7 @@ import (
4243
gclient "github.com/jipanyang/gnmi/client/gnmi"
4344
"github.com/jipanyang/gnxi/utils/xpath"
4445
gnoi_system_pb "github.com/openconfig/gnoi/system"
46+
"github.com/agiledragon/gomonkey"
4547
)
4648

4749
var clientTypes = []string{gclient.Type}
@@ -104,6 +106,25 @@ func createServer(t *testing.T, port int64) *Server {
104106
return s
105107
}
106108

109+
func createAuthServer(t *testing.T, port int64) *Server {
110+
certificate, err := testcert.NewCert()
111+
if err != nil {
112+
t.Errorf("could not load server key pair: %s", err)
113+
}
114+
tlsCfg := &tls.Config{
115+
ClientAuth: tls.RequestClientCert,
116+
Certificates: []tls.Certificate{certificate},
117+
}
118+
119+
opts := []grpc.ServerOption{grpc.Creds(credentials.NewTLS(tlsCfg))}
120+
cfg := &Config{Port: port, UserAuth: AuthTypes{"password": true, "cert": true, "jwt": true}}
121+
s, err := NewServer(cfg, opts)
122+
if err != nil {
123+
t.Errorf("Failed to create gNMI server: %v", err)
124+
}
125+
return s
126+
}
127+
107128
// runTestGet requests a path from the server by Get grpc call, and compares if
108129
// the return code and response value are expected.
109130
func runTestGet(t *testing.T, ctx context.Context, gClient pb.GNMIClient, pathTarget string,
@@ -2509,8 +2530,57 @@ func TestBulkSet(t *testing.T) {
25092530
if !ok {
25102531
t.Fatal("got a non-grpc error from grpc call")
25112532
}
2533+
})
2534+
}
2535+
2536+
type loginCreds struct {
2537+
Username, Password string
2538+
}
25122539

2540+
func (c *loginCreds) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
2541+
return map[string]string{
2542+
"username": c.Username,
2543+
"password": c.Password,
2544+
}, nil
2545+
}
2546+
2547+
func (c *loginCreds) RequireTransportSecurity() bool {
2548+
return true
2549+
}
2550+
2551+
func TestAuthCapabilities(t *testing.T) {
2552+
mock1 := gomonkey.ApplyFunc(UserPwAuth, func(username string, passwd string) (bool, error) {
2553+
return true, nil
25132554
})
2555+
defer mock1.Reset()
2556+
2557+
s := createAuthServer(t, 8089)
2558+
go runServer(t, s)
2559+
2560+
currentUser, _ := user.Current()
2561+
tlsConfig := &tls.Config{InsecureSkipVerify: true}
2562+
cred := &loginCreds{Username: currentUser.Username, Password: "dummy"}
2563+
opts := []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), grpc.WithPerRPCCredentials(cred)}
2564+
2565+
targetAddr := "127.0.0.1:8089"
2566+
conn, err := grpc.Dial(targetAddr, opts...)
2567+
if err != nil {
2568+
t.Fatalf("Dialing to %q failed: %v", targetAddr, err)
2569+
}
2570+
defer conn.Close()
2571+
2572+
gClient := pb.NewGNMIClient(conn)
2573+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
2574+
defer cancel()
2575+
2576+
var req pb.CapabilityRequest
2577+
resp, err := gClient.Capabilities(ctx, &req)
2578+
if err != nil {
2579+
t.Fatalf("Failed to get Capabilities: %v", err)
2580+
}
2581+
if len(resp.SupportedModels) == 0 {
2582+
t.Fatalf("No Supported Models found!")
2583+
}
25142584

25152585
}
25162586

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.12
55
require (
66
github.com/Azure/sonic-mgmt-common v0.0.0-00010101000000-000000000000
77
github.com/Workiva/go-datastructures v1.0.50
8+
github.com/agiledragon/gomonkey v2.0.2+incompatible
89
github.com/c9s/goprocinfo v0.0.0-20191125144613-4acdd056c72d
910
github.com/dgrijalva/jwt-go v3.2.1-0.20210802184156-9742bd7fca1c+incompatible
1011
github.com/go-redis/redis v6.15.6+incompatible

sonic_data_client/db_client.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ type Client interface {
5151

5252
// Close provides implemenation for explicit cleanup of Client
5353
Close() error
54+
55+
// callbacks on send failed
56+
FailedSend()
57+
58+
// callback on sent
59+
SentOne(*Value)
5460
}
5561

5662
type Stream interface {
@@ -271,7 +277,6 @@ func (c *DbClient) PollRun(q *queue.PriorityQueue, poll chan struct{}, w *sync.W
271277
SyncResponse: false,
272278
Val: val,
273279
}
274-
275280
c.q.Put(Value{spbv})
276281
log.V(6).Infof("Added spbv #%v", spbv)
277282
}
@@ -488,7 +493,7 @@ func populateDbtablePath(prefix, path *gnmipb.Path, pathG2S *map[*gnmipb.Path][]
488493
}
489494

490495
if targetDbName == "COUNTERS_DB" {
491-
err := initCountersPortNameMap()
496+
err := initCountersPortNameMap()
492497
if err != nil {
493498
return err
494499
}
@@ -1230,6 +1235,12 @@ func (c *DbClient) Capabilities() []gnmipb.ModelData {
12301235
return nil
12311236
}
12321237

1238+
func (c *DbClient) SentOne(val *Value) {
1239+
}
1240+
1241+
func (c *DbClient) FailedSend() {
1242+
}
1243+
12331244
// validateSampleInterval validates the sampling interval of the given subscription.
12341245
func validateSampleInterval(sub *gnmipb.Subscription) (time.Duration, error) {
12351246
requestedInterval := time.Duration(sub.GetSampleInterval())

0 commit comments

Comments
 (0)