Skip to content

Commit

Permalink
add gNMI depth extension
Browse files Browse the repository at this point in the history
  • Loading branch information
karimra committed Jun 20, 2024
1 parent 5f1fdc5 commit 9dc78a4
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/cmd/get.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/cmd/subscribe.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/user_guide/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions pkg/api/gnmi_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/api/types/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/app/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions pkg/app/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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...)
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/config/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -215,7 +218,6 @@ func (c *Config) setSubscriptionFieldsFromFlags(sub *types.SubscriptionConfig, c
Start: start,
End: end,
}
return nil
}
return nil
}
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 9dc78a4

Please sign in to comment.