Skip to content
This repository was archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
refactor target name vs targe config use, fix various typos
Browse files Browse the repository at this point in the history
  • Loading branch information
karimra committed Apr 1, 2022
1 parent 375d90b commit b19dc2e
Show file tree
Hide file tree
Showing 23 changed files with 394 additions and 377 deletions.
5 changes: 3 additions & 2 deletions app/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,13 @@ func (a *App) handleTargetsPost(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
return
}
if _, ok := a.Config.Targets[id]; !ok {
tc, ok := a.Config.Targets[id]
if !ok {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(APIErrors{Errors: []string{fmt.Sprintf("target %q not found", id)}})
return
}
go a.TargetSubscribeStream(a.ctx, id)
go a.TargetSubscribeStream(a.ctx, tc)
}

func (a *App) handleTargetsDelete(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func (a *App) loadTargets(e fsnotify.Event) {
}
a.AddTargetConfig(tc)
a.wg.Add(1)
go a.TargetSubscribeStream(a.ctx, n)
go a.TargetSubscribeStream(a.ctx, tc)
}
}
return
Expand Down
21 changes: 11 additions & 10 deletions app/capabilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/karimra/gnmic/types"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/gnmi/proto/gnmi_ext"
"github.com/openconfig/grpctunnel/tunnel"
Expand Down Expand Up @@ -44,35 +45,35 @@ func (a *App) CapRunE(cmd *cobra.Command, args []string) error {
numTargets := len(a.Config.Targets)
a.errCh = make(chan error, numTargets*2)
a.wg.Add(numTargets)
for tName := range a.Config.Targets {
go a.ReqCapabilities(ctx, tName)
for _, tc := range a.Config.Targets {
go a.ReqCapabilities(ctx, tc)
}
a.wg.Wait()
return a.checkErrors()
}

func (a *App) ReqCapabilities(ctx context.Context, tName string) {
func (a *App) ReqCapabilities(ctx context.Context, tc *types.TargetConfig) {
defer a.wg.Done()
ext := make([]*gnmi_ext.Extension, 0) //
if a.Config.PrintRequest {
err := a.PrintMsg(tName, "Capabilities Request:", &gnmi.CapabilityRequest{
err := a.PrintMsg(tc.Name, "Capabilities Request:", &gnmi.CapabilityRequest{
Extension: ext,
})
if err != nil {
a.logError(fmt.Errorf("target %q: %v", tName, err))
a.logError(fmt.Errorf("target %q: %v", tc.Name, err))
}
}

a.Logger.Printf("sending gNMI CapabilityRequest: gnmi_ext.Extension='%v' to %s", ext, tName)
response, err := a.ClientCapabilities(ctx, tName, ext...)
a.Logger.Printf("sending gNMI CapabilityRequest: gnmi_ext.Extension='%v' to %s", ext, tc.Name)
response, err := a.ClientCapabilities(ctx, tc, ext...)
if err != nil {
a.logError(fmt.Errorf("target %q, capabilities request failed: %v", tName, err))
a.logError(fmt.Errorf("target %q, capabilities request failed: %v", tc.Name, err))
return
}

err = a.PrintMsg(tName, "Capabilities Response:", response)
err = a.PrintMsg(tc.Name, "Capabilities Response:", response)
if err != nil {
a.logError(fmt.Errorf("target %q: %v", tName, err))
a.logError(fmt.Errorf("target %q: %v", tc.Name, err))
}
}

Expand Down
5 changes: 3 additions & 2 deletions app/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/karimra/gnmic/outputs"
"github.com/karimra/gnmic/target"
"github.com/karimra/gnmic/types"
"github.com/karimra/gnmic/utils"
"github.com/openconfig/gnmi/proto/gnmi"
"google.golang.org/protobuf/proto"
Expand Down Expand Up @@ -192,8 +193,8 @@ func (a *App) subscriptionMode(name string) string {
return ""
}

func (a *App) GetModels(ctx context.Context, tName string) ([]*gnmi.ModelData, error) {
capRsp, err := a.ClientCapabilities(ctx, tName)
func (a *App) GetModels(ctx context.Context, tc *types.TargetConfig) ([]*gnmi.ModelData, error) {
capRsp, err := a.ClientCapabilities(ctx, tc)
if err != nil {
return nil, err
}
Expand Down
155 changes: 78 additions & 77 deletions app/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/karimra/gnmic/config"
"github.com/karimra/gnmic/formatters"
"github.com/karimra/gnmic/types"
"github.com/openconfig/gnmi/proto/gnmi"
"github.com/openconfig/grpctunnel/tunnel"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -108,27 +109,29 @@ func (a *App) DiffRunE(cmd *cobra.Command, args []string) error {
a.errCh = make(chan error, numTargets*2)
a.wg.Add(numTargets)

compares := make([]string, 0, len(targetsConfig))
for t := range targetsConfig {
compares := make([]*types.TargetConfig, 0, len(targetsConfig))
for _, t := range targetsConfig {
compares = append(compares, t)
}
sort.Strings(compares)
sort.Slice(compares, func(i, j int) bool {
return compares[i].Name < compares[j].Name
})

err = a.diff(ctx, cmd, refTarget.Name, compares)
err = a.diff(ctx, cmd, refTarget, compares)
if err != nil {
a.logError(err)
}
return a.checkErrors()
}

func (a *App) diff(ctx context.Context, cmd *cobra.Command, ref string, compare []string) error {
func (a *App) diff(ctx context.Context, cmd *cobra.Command, ref *types.TargetConfig, compare []*types.TargetConfig) error {
if a.Config.DiffSub {
return a.subscribeBasedDiff(ctx, cmd, ref, compare)
}
return a.getBasedDiff(ctx, ref, compare)
}

func (a *App) subscribeBasedDiff(ctx context.Context, cmd *cobra.Command, ref string, compare []string) error {
func (a *App) subscribeBasedDiff(ctx context.Context, cmd *cobra.Command, ref *types.TargetConfig, compare []*types.TargetConfig) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
subReq, err := a.Config.CreateDiffSubscribeRequest(cmd)
Expand All @@ -138,88 +141,86 @@ func (a *App) subscribeBasedDiff(ctx context.Context, cmd *cobra.Command, ref st
numCompares := len(compare)
refResponse := make([]proto.Message, 0)
rspChan := make(chan *targetDiffResponse, numCompares)
err = a.CreateTarget(ref)
a.operLock.Lock()
refTarget, err := a.initTarget(ref)
a.operLock.Unlock()
if err != nil {
return err
}
if refTarget, ok := a.Targets[ref]; ok {
go func() {

go func() {
defer a.wg.Done()
err = refTarget.CreateGNMIClient(ctx, a.dialOpts...)
if err != nil {
a.logError(err)
return
}
a.Logger.Printf("sending gNMI SubscribeRequest: subscribe='%+v', mode='%+v', encoding='%+v', to %s",
subReq.Request, subReq.GetSubscribe().GetMode(), subReq.GetSubscribe().GetEncoding(), ref)
rspChan, errChan := refTarget.SubscribeOnceChan(ctx, subReq)
for {
select {
case r := <-rspChan:
switch r.Response.(type) {
case *gnmi.SubscribeResponse_Update:
refResponse = append(refResponse, r)
case *gnmi.SubscribeResponse_SyncResponse:
return
}
case err := <-errChan:
if err != io.EOF {
a.logError(err)
}
return
}
}
}()

for _, tc := range compare {
a.operLock.Lock()
t, err := a.initTarget(tc)
a.operLock.Unlock()
if err != nil {
return err
}
go func(tName string) {
defer a.wg.Done()
err = refTarget.CreateGNMIClient(ctx, a.dialOpts...)
err = t.CreateGNMIClient(ctx, a.dialOpts...)
if err != nil {
a.logError(err)
return
}
responses := make([]proto.Message, 0)
a.Logger.Printf("sending gNMI SubscribeRequest: subscribe='%+v', mode='%+v', encoding='%+v', to %s",
subReq.Request, subReq.GetSubscribe().GetMode(), subReq.GetSubscribe().GetEncoding(), ref)
rspChan, errChan := refTarget.SubscribeOnceChan(ctx, subReq)
subReq.Request, subReq.GetSubscribe().GetMode(), subReq.GetSubscribe().GetEncoding(), tName)
subRspChan, errChan := t.SubscribeOnceChan(ctx, subReq)
for {
select {
case r := <-rspChan:
case r := <-subRspChan:
switch r.Response.(type) {
case *gnmi.SubscribeResponse_Update:
refResponse = append(refResponse, r)
responses = append(responses, r)
case *gnmi.SubscribeResponse_SyncResponse:
rspChan <- &targetDiffResponse{
t: tName,
rs: responses,
}
return
}
case err := <-errChan:
if err != io.EOF {
a.logError(err)
}
return
}
}
}()

} else {
return fmt.Errorf("unknown reference target %q", ref)
}
for _, tName := range compare {
err = a.CreateTarget(tName)
if err != nil {
return err
}
if t, ok := a.Targets[tName]; ok {
go func(tName string) {
defer a.wg.Done()
err = t.CreateGNMIClient(ctx, a.dialOpts...)
if err != nil {
a.logError(err)
return
}
responses := make([]proto.Message, 0)
a.Logger.Printf("sending gNMI SubscribeRequest: subscribe='%+v', mode='%+v', encoding='%+v', to %s",
subReq.Request, subReq.GetSubscribe().GetMode(), subReq.GetSubscribe().GetEncoding(), tName)
subRspChan, errChan := t.SubscribeOnceChan(ctx, subReq)
for {
select {
case r := <-subRspChan:
switch r.Response.(type) {
case *gnmi.SubscribeResponse_Update:
responses = append(responses, r)
case *gnmi.SubscribeResponse_SyncResponse:
rspChan <- &targetDiffResponse{
t: tName,
rs: responses,
}
return
}
case err := <-errChan:
if err == io.EOF {
rspChan <- &targetDiffResponse{
t: tName,
rs: responses,
}
return
if err == io.EOF {
rspChan <- &targetDiffResponse{
t: tName,
rs: responses,
}
a.logError(err)
return
}
a.logError(err)
return
}
}(tName)
continue
}
a.logError(fmt.Errorf("unknown target %q", tName))
}
}(tc.Name)
continue
}
a.wg.Wait()
close(rspChan)
Expand All @@ -234,7 +235,7 @@ func (a *App) subscribeBasedDiff(ctx context.Context, cmd *cobra.Command, ref st
}

for _, cr := range rsps {
fmt.Fprintf(os.Stderr, "%q vs %q\n", ref, cr.t)
fmt.Fprintf(os.Stderr, "%q vs %q\n", ref.Name, cr.t)
err = a.responsesDiff(refResponse, cr.rs)
if err != nil {
a.logError(err)
Expand All @@ -243,7 +244,7 @@ func (a *App) subscribeBasedDiff(ctx context.Context, cmd *cobra.Command, ref st
return nil
}

func (a *App) getBasedDiff(ctx context.Context, ref string, compare []string) error {
func (a *App) getBasedDiff(ctx context.Context, ref *types.TargetConfig, compare []*types.TargetConfig) error {
getReq, err := a.Config.CreateDiffGetRequest()
if err != nil {
return err
Expand All @@ -265,21 +266,21 @@ func (a *App) getBasedDiff(ctx context.Context, ref string, compare []string) er
}
}()
rspChan := make(chan *targetDiffResponse, numCompares)
for _, tName := range compare {
go func(tName string) {
for _, tc := range compare {
go func(tc *types.TargetConfig) {
defer a.wg.Done()
a.Logger.Printf("sending gNMI GetRequest: prefix='%v', path='%v', type='%v', encoding='%v', models='%+v', extension='%+v' to %s",
getReq.Prefix, getReq.Path, getReq.Type, getReq.Encoding, getReq.UseModels, getReq.Extension, tName)
response, err := a.ClientGet(ctx, tName, getReq)
getReq.Prefix, getReq.Path, getReq.Type, getReq.Encoding, getReq.UseModels, getReq.Extension, tc.Name)
response, err := a.ClientGet(ctx, tc, getReq)
if err != nil {
a.logError(fmt.Errorf("target %q get request failed: %v", tName, err))
a.logError(fmt.Errorf("target %q get request failed: %v", tc.Name, err))
return
}
rspChan <- &targetDiffResponse{
t: tName,
t: tc.Name,
r: response,
}
}(tName)
}(tc)
}
a.wg.Wait()
close(rspChan)
Expand All @@ -295,7 +296,7 @@ func (a *App) getBasedDiff(ctx context.Context, ref string, compare []string) er
return rsps[i].t < rsps[j].t
})
for _, cr := range rsps {
fmt.Fprintf(os.Stderr, "%q vs %q\n", ref, cr.t)
fmt.Fprintf(os.Stderr, "%q vs %q\n", ref.Name, cr.t)
err = a.responsesDiff([]proto.Message{refResponse}, []proto.Message{cr.r})
if err != nil {
a.logError(err)
Expand Down
Loading

0 comments on commit b19dc2e

Please sign in to comment.