Skip to content

Commit

Permalink
Merge pull request #1 from renukamanavalan/statistics
Browse files Browse the repository at this point in the history
Statistics -- update added
  • Loading branch information
renukamanavalan authored Aug 30, 2022
2 parents 5751f32 + d5ba1cb commit 9a9f37f
Show file tree
Hide file tree
Showing 9 changed files with 339 additions and 63 deletions.
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ resources:
repositories:
- repository: sonic-mgmt-common
type: github
name: Azure/sonic-mgmt-common
endpoint: build
name: sonic-net/sonic-mgmt-common
endpoint: sonic-net

stages:
- stage: Build
Expand Down
10 changes: 8 additions & 2 deletions gnmi_server/client_subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (c *Client) Run(stream gnmipb.GNMI_SubscribeServer) (err error) {

log.V(1).Infof("Client %s running", c)
go c.recv(stream)
err = c.send(stream)
err = c.send(stream, dc)
c.Close()
// Wait until all child go routines exited
c.w.Wait()
Expand Down Expand Up @@ -247,8 +247,9 @@ func (c *Client) recv(stream gnmipb.GNMI_SubscribeServer) {
}

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

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

var resp *gnmipb.SubscribeResponse

switch v := items[0].(type) {
case sdc.Value:
if resp, err = sdc.ValToResp(v); err != nil {
c.errors++
return err
}
val = &v;
default:
log.V(1).Infof("Unknown data type %v for %s in queue", items[0], c)
c.errors++
Expand All @@ -278,8 +281,11 @@ func (c *Client) send(stream gnmipb.GNMI_SubscribeServer) error {
if err != nil {
log.V(1).Infof("Client %s sending error:%v", c, err)
c.errors++
dc.FailedSend()
return err
}

dc.SentOne(val)
log.V(5).Infof("Client %s done sending, msg count %d, msg %v", c, c.sendMsg, resp)
}
}
1 change: 1 addition & 0 deletions gnmi_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func authenticate(UserAuth AuthTypes, ctx context.Context) (context.Context, err
if !success {
return ctx, status.Error(codes.Unauthenticated, "Unauthenticated")
}
log.V(5).Infof("authenticate user %v, roles %v", rc.Auth.User, rc.Auth.Roles)

return ctx, nil
}
Expand Down
70 changes: 70 additions & 0 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/user"
"reflect"
"testing"
"time"
Expand All @@ -42,6 +43,7 @@ import (
gclient "github.com/jipanyang/gnmi/client/gnmi"
"github.com/jipanyang/gnxi/utils/xpath"
gnoi_system_pb "github.com/openconfig/gnoi/system"
"github.com/agiledragon/gomonkey"
)

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

func createAuthServer(t *testing.T, port int64) *Server {
certificate, err := testcert.NewCert()
if err != nil {
t.Errorf("could not load server key pair: %s", err)
}
tlsCfg := &tls.Config{
ClientAuth: tls.RequestClientCert,
Certificates: []tls.Certificate{certificate},
}

opts := []grpc.ServerOption{grpc.Creds(credentials.NewTLS(tlsCfg))}
cfg := &Config{Port: port, UserAuth: AuthTypes{"password": true, "cert": true, "jwt": true}}
s, err := NewServer(cfg, opts)
if err != nil {
t.Errorf("Failed to create gNMI server: %v", err)
}
return s
}

// runTestGet requests a path from the server by Get grpc call, and compares if
// the return code and response value are expected.
func runTestGet(t *testing.T, ctx context.Context, gClient pb.GNMIClient, pathTarget string,
Expand Down Expand Up @@ -2509,8 +2530,57 @@ func TestBulkSet(t *testing.T) {
if !ok {
t.Fatal("got a non-grpc error from grpc call")
}
})
}

type loginCreds struct {
Username, Password string
}

func (c *loginCreds) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return map[string]string{
"username": c.Username,
"password": c.Password,
}, nil
}

func (c *loginCreds) RequireTransportSecurity() bool {
return true
}

func TestAuthCapabilities(t *testing.T) {
mock1 := gomonkey.ApplyFunc(UserPwAuth, func(username string, passwd string) (bool, error) {
return true, nil
})
defer mock1.Reset()

s := createAuthServer(t, 8089)
go runServer(t, s)

currentUser, _ := user.Current()
tlsConfig := &tls.Config{InsecureSkipVerify: true}
cred := &loginCreds{Username: currentUser.Username, Password: "dummy"}
opts := []grpc.DialOption{grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)), grpc.WithPerRPCCredentials(cred)}

targetAddr := "127.0.0.1:8089"
conn, err := grpc.Dial(targetAddr, opts...)
if err != nil {
t.Fatalf("Dialing to %q failed: %v", targetAddr, err)
}
defer conn.Close()

gClient := pb.NewGNMIClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

var req pb.CapabilityRequest
resp, err := gClient.Capabilities(ctx, &req)
if err != nil {
t.Fatalf("Failed to get Capabilities: %v", err)
}
if len(resp.SupportedModels) == 0 {
t.Fatalf("No Supported Models found!")
}

}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.12
require (
github.com/Azure/sonic-mgmt-common v0.0.0-00010101000000-000000000000
github.com/Workiva/go-datastructures v1.0.50
github.com/agiledragon/gomonkey v2.0.2+incompatible
github.com/c9s/goprocinfo v0.0.0-20191125144613-4acdd056c72d
github.com/dgrijalva/jwt-go v3.2.1-0.20210802184156-9742bd7fca1c+incompatible
github.com/go-redis/redis v6.15.6+incompatible
Expand Down
15 changes: 13 additions & 2 deletions sonic_data_client/db_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ type Client interface {

// Close provides implemenation for explicit cleanup of Client
Close() error

// callbacks on send failed
FailedSend()

// callback on sent
SentOne(*Value)
}

type Stream interface {
Expand Down Expand Up @@ -271,7 +277,6 @@ func (c *DbClient) PollRun(q *queue.PriorityQueue, poll chan struct{}, w *sync.W
SyncResponse: false,
Val: val,
}

c.q.Put(Value{spbv})
log.V(6).Infof("Added spbv #%v", spbv)
}
Expand Down Expand Up @@ -488,7 +493,7 @@ func populateDbtablePath(prefix, path *gnmipb.Path, pathG2S *map[*gnmipb.Path][]
}

if targetDbName == "COUNTERS_DB" {
err := initCountersPortNameMap()
err := initCountersPortNameMap()
if err != nil {
return err
}
Expand Down Expand Up @@ -1230,6 +1235,12 @@ func (c *DbClient) Capabilities() []gnmipb.ModelData {
return nil
}

func (c *DbClient) SentOne(val *Value) {
}

func (c *DbClient) FailedSend() {
}

// validateSampleInterval validates the sampling interval of the given subscription.
func validateSampleInterval(sub *gnmipb.Subscription) (time.Duration, error) {
requestedInterval := time.Duration(sub.GetSampleInterval())
Expand Down
Loading

0 comments on commit 9a9f37f

Please sign in to comment.