/
subscription.go
104 lines (89 loc) · 4.02 KB
/
subscription.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// © 2022 Nokia.
//
// This code is a Contribution to the gNMIc project (“Work”) made under the Google Software Grant and Corporate Contributor License Agreement (“CLA”) and governed by the Apache License 2.0.
// No other rights or licenses in or to any of Nokia’s intellectual property are granted for any other purpose.
// This code is provided on an “as is” basis without any warranties of any kind.
//
// SPDX-License-Identifier: Apache-2.0
package types
import (
"encoding/json"
"fmt"
"strings"
"time"
)
const (
notApplicable = "NA"
)
// SubscriptionConfig //
type SubscriptionConfig struct {
Name string `mapstructure:"name,omitempty" json:"name,omitempty"`
Models []string `mapstructure:"models,omitempty" json:"models,omitempty"`
Prefix string `mapstructure:"prefix,omitempty" json:"prefix,omitempty"`
Target string `mapstructure:"target,omitempty" json:"target,omitempty"`
SetTarget bool `mapstructure:"set-target,omitempty" json:"set-target,omitempty"`
Paths []string `mapstructure:"paths,omitempty" json:"paths,omitempty"`
Mode string `mapstructure:"mode,omitempty" json:"mode,omitempty"`
StreamMode string `mapstructure:"stream-mode,omitempty" json:"stream-mode,omitempty"`
Encoding *string `mapstructure:"encoding,omitempty" json:"encoding,omitempty"`
Qos *uint32 `mapstructure:"qos,omitempty" json:"qos,omitempty"`
SampleInterval *time.Duration `mapstructure:"sample-interval,omitempty" json:"sample-interval,omitempty"`
HeartbeatInterval *time.Duration `mapstructure:"heartbeat-interval,omitempty" json:"heartbeat-interval,omitempty"`
SuppressRedundant bool `mapstructure:"suppress-redundant,omitempty" json:"suppress-redundant,omitempty"`
UpdatesOnly bool `mapstructure:"updates-only,omitempty" json:"updates-only,omitempty"`
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"`
}
type HistoryConfig struct {
Snapshot time.Time `mapstructure:"snapshot,omitempty" json:"snapshot,omitempty"`
Start time.Time `mapstructure:"start,omitempty" json:"start,omitempty"`
End time.Time `mapstructure:"end,omitempty" json:"end,omitempty"`
}
// String //
func (sc *SubscriptionConfig) String() string {
b, err := json.Marshal(sc)
if err != nil {
return ""
}
return string(b)
}
func (sc *SubscriptionConfig) PathsString() string {
return fmt.Sprintf("- %s", strings.Join(sc.Paths, "\n- "))
}
func (sc *SubscriptionConfig) PrefixString() string {
if sc.Prefix == "" {
return notApplicable
}
return sc.Prefix
}
func (sc *SubscriptionConfig) ModeString() string {
if strings.ToLower(sc.Mode) == "stream" {
return fmt.Sprintf("%s/%s", strings.ToLower(sc.Mode), strings.ToLower(sc.StreamMode))
}
return strings.ToLower(sc.Mode)
}
func (sc *SubscriptionConfig) SampleIntervalString() string {
if strings.ToLower(sc.Mode) == "stream" && strings.ToLower(sc.StreamMode) == "sample" {
return sc.SampleInterval.String()
}
return notApplicable
}
func (sc *SubscriptionConfig) ModelsString() string {
return fmt.Sprintf("- %s", strings.Join(sc.Models, "\n- "))
}
func (sc *SubscriptionConfig) QosString() string {
if sc.Qos == nil {
return notApplicable
}
return fmt.Sprintf("%d", *sc.Qos)
}
func (sc *SubscriptionConfig) HeartbeatIntervalString() string {
return sc.HeartbeatInterval.String()
}
func (sc *SubscriptionConfig) SuppressRedundantString() string {
return fmt.Sprintf("%t", sc.SuppressRedundant)
}
func (sc *SubscriptionConfig) UpdatesOnlyString() string {
return fmt.Sprintf("%t", sc.UpdatesOnly)
}