From 9dc78a4101f47450edee5c2850c85193d7768c51 Mon Sep 17 00:00:00 2001 From: Karim Radhouani Date: Thu, 20 Jun 2024 14:35:24 -0700 Subject: [PATCH] add gNMI depth extension --- docs/cmd/get.md | 4 ++++ docs/cmd/subscribe.md | 4 ++++ docs/user_guide/subscriptions.md | 2 ++ pkg/api/gnmi_msgs.go | 24 ++++++++++++++++++++++++ pkg/api/types/subscription.go | 1 + pkg/app/get.go | 1 + pkg/app/subscribe.go | 1 + pkg/config/config.go | 5 +++++ pkg/config/subscriptions.go | 10 ++++++++-- 9 files changed, 50 insertions(+), 2 deletions(-) diff --git a/docs/cmd/get.md b/docs/cmd/get.md index 0826bc48..ea46b00d 100644 --- a/docs/cmd/get.md +++ b/docs/cmd/get.md @@ -62,6 +62,10 @@ The `[--processor]` flag allow to list [event processor](../user_guide/event_pro The processors are run in the order they are specified (`--processor proc1,proc2` or `--processor proc1 --processor proc2`). +#### depth + +The `[--depth]` flag set the gNMI extension depth value as defined [here](https://github.com/openconfig/reference/blob/master/rpc/gnmi/gnmi-depth.md) + ### Examples ```bash diff --git a/docs/cmd/subscribe.md b/docs/cmd/subscribe.md index 924a5334..e4c64add 100644 --- a/docs/cmd/subscribe.md +++ b/docs/cmd/subscribe.md @@ -144,6 +144,10 @@ The value can be either nanoseconds since Unix epoch or a date in RFC3339 format The `[--history-end]` flag sets the end value in the subscribe request Time Range [gNMI History extension](https://github.com/openconfig/reference/blob/master/rpc/gnmi/gnmi-history.md). +#### depth + +The `[--depth]` flag set the gNMI extension depth value as defined [here](https://github.com/openconfig/reference/blob/master/rpc/gnmi/gnmi-depth.md) + ### Examples #### 1. streaming, target-defined, 10s interval diff --git a/docs/user_guide/subscriptions.md b/docs/user_guide/subscriptions.md index a99cb0ea..065c8841 100644 --- a/docs/user_guide/subscriptions.md +++ b/docs/user_guide/subscriptions.md @@ -103,6 +103,8 @@ subscriptions: # string, nanoseconds since Unix epoch or RFC3339 format. # if set, the history extension type will be a Range request end: + # uint32, depth value as per: https://github.com/openconfig/reference/blob/master/rpc/gnmi/gnmi-depth.md + depth: 0 ``` #### Subscription config to gNMI SubscribeRequest diff --git a/pkg/api/gnmi_msgs.go b/pkg/api/gnmi_msgs.go index 89796b0c..249f9048 100644 --- a/pkg/api/gnmi_msgs.go +++ b/pkg/api/gnmi_msgs.go @@ -400,6 +400,30 @@ func Extension_CommitSetRollbackDuration(id string, dur time.Duration) func(msg } } +func Extension_Depth(lvl uint32) func(msg proto.Message) error { + return func(msg proto.Message) error { + if msg == nil { + return ErrInvalidMsgType + } + + switch msg := msg.ProtoReflect().Interface().(type) { + case *gnmi.GetRequest, *gnmi.SubscribeRequest: + fn := Extension( + &gnmi_ext.Extension{ + Ext: &gnmi_ext.Extension_Depth{ + Depth: &gnmi_ext.Depth{ + Level: lvl, + }, + }, + }, + ) + return fn(msg) + default: + return fmt.Errorf("option Extension_Depth: %w: %T", ErrInvalidMsgType, msg) + } + } +} + // Extension_HistorySnapshotTime creates a GNMIOption that adds a gNMI extension of // type History Snapshot with the supplied snapshot time. // the snapshot value can be nanoseconds since Unix epoch or a date in RFC3339 format diff --git a/pkg/api/types/subscription.go b/pkg/api/types/subscription.go index 20052c8d..cde8d50b 100644 --- a/pkg/api/types/subscription.go +++ b/pkg/api/types/subscription.go @@ -38,6 +38,7 @@ type SubscriptionConfig struct { History *HistoryConfig `mapstructure:"history,omitempty" json:"history,omitempty"` StreamSubscriptions []*SubscriptionConfig `mapstructure:"stream-subscriptions,omitempty" json:"stream-subscriptions,omitempty"` Outputs []string `mapstructure:"outputs,omitempty" json:"outputs,omitempty"` + Depth uint32 `mapstructure:"depth,omitempty" json:"depth,omitempty"` } type HistoryConfig struct { diff --git a/pkg/app/get.go b/pkg/app/get.go index 8429ab1d..6af813c4 100644 --- a/pkg/app/get.go +++ b/pkg/app/get.go @@ -175,6 +175,7 @@ func (a *App) InitGetFlags(cmd *cobra.Command) { cmd.Flags().StringVarP(&a.Config.LocalFlags.GetTarget, "target", "", "", "get request target") cmd.Flags().BoolVarP(&a.Config.LocalFlags.GetValuesOnly, "values-only", "", false, "print GetResponse values only") cmd.Flags().StringArrayVarP(&a.Config.LocalFlags.GetProcessor, "processor", "", []string{}, "list of processor names to run") + cmd.Flags().Uint32VarP(&a.Config.LocalFlags.GetDepth, "depth", "", 0, "depth extension value") cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) { a.Config.FileConfig.BindPFlag(fmt.Sprintf("%s-%s", cmd.Name(), flag.Name), flag) diff --git a/pkg/app/subscribe.go b/pkg/app/subscribe.go index aca6be42..7dd5fa14 100644 --- a/pkg/app/subscribe.go +++ b/pkg/app/subscribe.go @@ -182,6 +182,7 @@ func (a *App) InitSubscribeFlags(cmd *cobra.Command) { cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeHistorySnapshot, "history-snapshot", "", "", "sets the snapshot time in a historical subscription, nanoseconds since Unix epoch or RFC3339 format") cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeHistoryStart, "history-start", "", "", "sets the start time in a historical range subscription, nanoseconds since Unix epoch or RFC3339 format") cmd.Flags().StringVarP(&a.Config.LocalFlags.SubscribeHistoryEnd, "history-end", "", "", "sets the end time in a historical range subscription, nanoseconds since Unix epoch or RFC3339 format") + cmd.Flags().Uint32VarP(&a.Config.LocalFlags.SubscribeDepth, "depth", "", 0, "depth extension value") // cmd.LocalFlags().VisitAll(func(flag *pflag.Flag) { a.Config.FileConfig.BindPFlag(fmt.Sprintf("%s-%s", cmd.Name(), flag.Name), flag) diff --git a/pkg/config/config.go b/pkg/config/config.go index 8de190a4..cb6a9406 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -134,6 +134,7 @@ type LocalFlags struct { GetTarget string `mapstructure:"get-target,omitempty" json:"get-target,omitempty" yaml:"get-target,omitempty"` GetValuesOnly bool `mapstructure:"get-values-only,omitempty" json:"get-values-only,omitempty" yaml:"get-values-only,omitempty"` GetProcessor []string `mapstructure:"get-processor,omitempty" json:"get-processor,omitempty" yaml:"get-processor,omitempty"` + GetDepth uint32 `mapstructure:"get-depth,omitempty" yaml:"get-depth,omitempty" json:"get-depth,omitempty"` // Set SetPrefix string `mapstructure:"set-prefix,omitempty" json:"set-prefix,omitempty" yaml:"set-prefix,omitempty"` SetDelete []string `mapstructure:"set-delete,omitempty" json:"set-delete,omitempty" yaml:"set-delete,omitempty"` @@ -186,6 +187,7 @@ type LocalFlags struct { SubscribeHistorySnapshot string `mapstructure:"subscribe-history-snapshot,omitempty" json:"subscribe-history-snapshot,omitempty" yaml:"subscribe-history-snapshot,omitempty"` SubscribeHistoryStart string `mapstructure:"subscribe-history-start,omitempty" json:"subscribe-history-start,omitempty" yaml:"subscribe-history-start,omitempty"` SubscribeHistoryEnd string `mapstructure:"subscribe-history-end,omitempty" json:"subscribe-history-end,omitempty" yaml:"subscribe-history-end,omitempty"` + SubscribeDepth uint32 `mapstructure:"subscribe-depth,omitempty" yaml:"subscribe-depth,omitempty" json:"subscribe-depth,omitempty"` // Path PathPathType string `mapstructure:"path-path-type,omitempty" json:"path-path-type,omitempty" yaml:"path-path-type,omitempty"` PathWithDescr bool `mapstructure:"path-descr,omitempty" json:"path-descr,omitempty" yaml:"path-descr,omitempty"` @@ -456,6 +458,9 @@ func (c *Config) CreateGetRequest(tc *types.TargetConfig) (*gnmi.GetRequest, err for _, p := range c.LocalFlags.GetPath { gnmiOpts = append(gnmiOpts, api.Path(strings.TrimSpace(p))) } + if c.LocalFlags.GetDepth > 0 { + gnmiOpts = append(gnmiOpts, api.Extension_Depth(c.LocalFlags.GetDepth)) + } return api.NewGetRequest(gnmiOpts...) } diff --git a/pkg/config/subscriptions.go b/pkg/config/subscriptions.go index 486d73bc..98ebfa01 100644 --- a/pkg/config/subscriptions.go +++ b/pkg/config/subscriptions.go @@ -103,6 +103,7 @@ func (c *Config) subscriptionConfigFromFlags(cmd *cobra.Command) (map[string]*ty SetTarget: c.LocalFlags.SubscribeSetTarget, Paths: c.LocalFlags.SubscribePath, Mode: c.LocalFlags.SubscribeMode, + Depth: c.LocalFlags.SubscribeDepth, } // if globalFlagIsSet(cmd, "encoding") { // sub.Encoding = &c.Encoding @@ -192,6 +193,9 @@ func (c *Config) setSubscriptionFieldsFromFlags(sub *types.SubscriptionConfig, c if sub.Qos == nil && flagIsSet(cmd, "qos") { sub.Qos = &c.LocalFlags.SubscribeQos } + if flagIsSet(cmd, "depth") { + sub.Depth = c.LocalFlags.SubscribeDepth + } if sub.History == nil && flagIsSet(cmd, "history-snapshot") { snapshot, err := time.Parse(time.RFC3339Nano, c.LocalFlags.SubscribeHistorySnapshot) if err != nil { @@ -200,7 +204,6 @@ func (c *Config) setSubscriptionFieldsFromFlags(sub *types.SubscriptionConfig, c sub.History = &types.HistoryConfig{ Snapshot: snapshot, } - return nil } if sub.History == nil && flagIsSet(cmd, "history-start") && flagIsSet(cmd, "history-end") { start, err := time.Parse(time.RFC3339Nano, c.LocalFlags.SubscribeHistoryStart) @@ -215,7 +218,6 @@ func (c *Config) setSubscriptionFieldsFromFlags(sub *types.SubscriptionConfig, c Start: start, End: end, } - return nil } return nil } @@ -334,6 +336,10 @@ func (c *Config) subscriptionOpts(sc *types.SubscriptionConfig, tc *types.Target ) } + // Depth extension + if sc.Depth > 0 { + gnmiOpts = append(gnmiOpts, api.Extension_Depth(sc.Depth)) + } return gnmiOpts, nil }