| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package ddprofiledefinition |
| 4 | |
| 5 | import ( |
| 6 | "slices" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | type LicenseSignalKind string |
| 11 | |
| 12 | const ( |
| 13 | LicenseSignalExpiryTimestamp LicenseSignalKind = "expiry_timestamp" |
| 14 | LicenseSignalExpiryRemaining LicenseSignalKind = "expiry_remaining" |
| 15 | LicenseSignalAuthorizationTimestamp LicenseSignalKind = "authorization_timestamp" |
| 16 | LicenseSignalAuthorizationRemaining LicenseSignalKind = "authorization_remaining" |
| 17 | LicenseSignalCertificateTimestamp LicenseSignalKind = "certificate_timestamp" |
| 18 | LicenseSignalCertificateRemaining LicenseSignalKind = "certificate_remaining" |
| 19 | LicenseSignalGraceTimestamp LicenseSignalKind = "grace_timestamp" |
| 20 | LicenseSignalGraceRemaining LicenseSignalKind = "grace_remaining" |
| 21 | LicenseSignalUsageUsed LicenseSignalKind = "usage_used" |
| 22 | LicenseSignalUsageCapacity LicenseSignalKind = "usage_capacity" |
| 23 | LicenseSignalUsageAvailable LicenseSignalKind = "usage_available" |
| 24 | LicenseSignalUsagePercent LicenseSignalKind = "usage_percent" |
| 25 | LicenseSignalStateSeverity LicenseSignalKind = "state_severity" |
| 26 | ) |
| 27 | |
| 28 | var validLicenseSignalKinds = map[LicenseSignalKind]struct{}{ |
| 29 | LicenseSignalExpiryTimestamp: {}, |
| 30 | LicenseSignalExpiryRemaining: {}, |
| 31 | LicenseSignalAuthorizationTimestamp: {}, |
| 32 | LicenseSignalAuthorizationRemaining: {}, |
| 33 | LicenseSignalCertificateTimestamp: {}, |
| 34 | LicenseSignalCertificateRemaining: {}, |
| 35 | LicenseSignalGraceTimestamp: {}, |
| 36 | LicenseSignalGraceRemaining: {}, |
| 37 | LicenseSignalUsageUsed: {}, |
| 38 | LicenseSignalUsageCapacity: {}, |
| 39 | LicenseSignalUsageAvailable: {}, |
| 40 | LicenseSignalUsagePercent: {}, |
| 41 | LicenseSignalStateSeverity: {}, |
| 42 | } |
| 43 | |
| 44 | func IsValidLicenseSignalKind(kind LicenseSignalKind) bool { |
| 45 | _, ok := validLicenseSignalKinds[kind] |
| 46 | return ok |
| 47 | } |
| 48 | |
| 49 | type LicenseSentinelPolicy string |
| 50 | |
| 51 | const ( |
| 52 | LicenseSentinelTimerZeroOrNegative LicenseSentinelPolicy = "timer_zero_or_negative" |
| 53 | LicenseSentinelTimerU32Max LicenseSentinelPolicy = "timer_u32_max" |
| 54 | LicenseSentinelTimerPre1971 LicenseSentinelPolicy = "timer_pre_1971" |
| 55 | ) |
| 56 | |
| 57 | var validLicenseSentinelPolicies = map[LicenseSentinelPolicy]struct{}{ |
| 58 | LicenseSentinelTimerZeroOrNegative: {}, |
| 59 | LicenseSentinelTimerU32Max: {}, |
| 60 | LicenseSentinelTimerPre1971: {}, |
| 61 | } |
| 62 | |
| 63 | func IsValidLicenseSentinelPolicy(policy LicenseSentinelPolicy) bool { |
| 64 | _, ok := validLicenseSentinelPolicies[policy] |
| 65 | return ok |
| 66 | } |
| 67 | |
| 68 | type LicenseStatePolicy string |
| 69 | |
| 70 | const ( |
| 71 | LicenseStatePolicyDefault LicenseStatePolicy = "default" |
| 72 | LicenseStatePolicySophos LicenseStatePolicy = "sophos" |
| 73 | ) |
| 74 | |
| 75 | var validLicenseStatePolicies = map[LicenseStatePolicy]struct{}{ |
| 76 | LicenseStatePolicyDefault: {}, |
| 77 | LicenseStatePolicySophos: {}, |
| 78 | } |
| 79 | |
| 80 | func IsValidLicenseStatePolicy(policy LicenseStatePolicy) bool { |
| 81 | _, ok := validLicenseStatePolicies[policy] |
| 82 | return ok |
| 83 | } |
| 84 | |
| 85 | type LicensingConfig struct { |
| 86 | OriginProfileID string `yaml:"-" json:"-"` |
| 87 | ID string `yaml:"id,omitempty" json:"id,omitempty"` |
| 88 | MIB string `yaml:"MIB,omitempty" json:"MIB,omitempty"` |
| 89 | Table SymbolConfig `yaml:"table,omitempty" json:"table"` |
| 90 | |
| 91 | Identity LicenseIdentityConfig `yaml:"identity,omitempty" json:"identity"` |
| 92 | Descriptors LicenseDescriptorsConfig `yaml:"descriptors,omitempty" json:"descriptors"` |
| 93 | State LicenseStateConfig `yaml:"state,omitempty" json:"state"` |
| 94 | Signals LicenseSignalsConfig `yaml:"signals,omitempty" json:"signals"` |
| 95 | |
| 96 | StaticTags []StaticMetricTagConfig `yaml:"static_tags,omitempty" json:"-"` |
| 97 | MetricTags MetricTagConfigList `yaml:"metric_tags,omitempty" json:"metric_tags,omitempty"` |
| 98 | } |
| 99 | |
| 100 | func (c LicensingConfig) Clone() LicensingConfig { |
| 101 | return LicensingConfig{ |
| 102 | OriginProfileID: c.OriginProfileID, |
| 103 | ID: c.ID, |
| 104 | MIB: c.MIB, |
| 105 | Table: c.Table.Clone(), |
| 106 | Identity: c.Identity.Clone(), |
| 107 | Descriptors: c.Descriptors.Clone(), |
| 108 | State: c.State.Clone(), |
| 109 | Signals: c.Signals.Clone(), |
| 110 | StaticTags: slices.Clone(c.StaticTags), |
| 111 | MetricTags: cloneSlice(c.MetricTags), |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | type LicenseIdentityConfig struct { |
| 116 | ID LicenseValueConfig `yaml:"id,omitempty" json:"id"` |
| 117 | Name LicenseValueConfig `yaml:"name,omitempty" json:"name"` |
| 118 | Feature LicenseValueConfig `yaml:"feature,omitempty" json:"feature"` |
| 119 | Component LicenseValueConfig `yaml:"component,omitempty" json:"component"` |
| 120 | } |
| 121 | |
| 122 | func (c LicenseIdentityConfig) Clone() LicenseIdentityConfig { |
| 123 | return LicenseIdentityConfig{ |
| 124 | ID: c.ID.Clone(), |
| 125 | Name: c.Name.Clone(), |
| 126 | Feature: c.Feature.Clone(), |
| 127 | Component: c.Component.Clone(), |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | type LicenseDescriptorsConfig struct { |
| 132 | Type LicenseValueConfig `yaml:"type,omitempty" json:"type"` |
| 133 | Impact LicenseValueConfig `yaml:"impact,omitempty" json:"impact"` |
| 134 | Perpetual LicenseValueConfig `yaml:"perpetual,omitempty" json:"perpetual"` |
| 135 | Unlimited LicenseValueConfig `yaml:"unlimited,omitempty" json:"unlimited"` |
| 136 | } |
| 137 | |
| 138 | func (c LicenseDescriptorsConfig) Clone() LicenseDescriptorsConfig { |
| 139 | return LicenseDescriptorsConfig{ |
| 140 | Type: c.Type.Clone(), |
| 141 | Impact: c.Impact.Clone(), |
| 142 | Perpetual: c.Perpetual.Clone(), |
| 143 | Unlimited: c.Unlimited.Clone(), |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | type LicenseStateConfig struct { |
| 148 | LicenseValueConfig `yaml:",inline" json:",inline"` |
| 149 | Policy LicenseStatePolicy `yaml:"policy,omitempty" json:"policy,omitempty"` |
| 150 | } |
| 151 | |
| 152 | func (c LicenseStateConfig) Clone() LicenseStateConfig { |
| 153 | return LicenseStateConfig{ |
| 154 | LicenseValueConfig: c.LicenseValueConfig.Clone(), |
| 155 | Policy: c.Policy, |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | type LicenseSignalsConfig struct { |
| 160 | Expiry LicenseTimerSignalsConfig `yaml:"expiry,omitempty" json:"expiry"` |
| 161 | Authorization LicenseTimerSignalsConfig `yaml:"authorization,omitempty" json:"authorization"` |
| 162 | Certificate LicenseTimerSignalsConfig `yaml:"certificate,omitempty" json:"certificate"` |
| 163 | Grace LicenseTimerSignalsConfig `yaml:"grace,omitempty" json:"grace"` |
| 164 | Usage LicenseUsageSignalsConfig `yaml:"usage,omitempty" json:"usage"` |
| 165 | } |
| 166 | |
| 167 | func (c LicenseSignalsConfig) Clone() LicenseSignalsConfig { |
| 168 | return LicenseSignalsConfig{ |
| 169 | Expiry: c.Expiry.Clone(), |
| 170 | Authorization: c.Authorization.Clone(), |
| 171 | Certificate: c.Certificate.Clone(), |
| 172 | Grace: c.Grace.Clone(), |
| 173 | Usage: c.Usage.Clone(), |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | type LicenseTimerSignalsConfig struct { |
| 178 | LicenseValueConfig `yaml:",inline" json:",inline"` |
| 179 | Timestamp LicenseValueConfig `yaml:"timestamp,omitempty" json:"timestamp"` |
| 180 | Remaining LicenseValueConfig `yaml:"remaining,omitempty" json:"remaining"` |
| 181 | } |
| 182 | |
| 183 | func (c LicenseTimerSignalsConfig) Clone() LicenseTimerSignalsConfig { |
| 184 | return LicenseTimerSignalsConfig{ |
| 185 | LicenseValueConfig: c.LicenseValueConfig.Clone(), |
| 186 | Timestamp: c.Timestamp.Clone(), |
| 187 | Remaining: c.Remaining.Clone(), |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | type LicenseUsageSignalsConfig struct { |
| 192 | Used LicenseValueConfig `yaml:"used,omitempty" json:"used"` |
| 193 | Capacity LicenseValueConfig `yaml:"capacity,omitempty" json:"capacity"` |
| 194 | Available LicenseValueConfig `yaml:"available,omitempty" json:"available"` |
| 195 | Percent LicenseValueConfig `yaml:"percent,omitempty" json:"percent"` |
| 196 | } |
| 197 | |
| 198 | func (c LicenseUsageSignalsConfig) Clone() LicenseUsageSignalsConfig { |
| 199 | return LicenseUsageSignalsConfig{ |
| 200 | Used: c.Used.Clone(), |
| 201 | Capacity: c.Capacity.Clone(), |
| 202 | Available: c.Available.Clone(), |
| 203 | Percent: c.Percent.Clone(), |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | type LicenseValueConfig struct { |
| 208 | Value string `yaml:"value,omitempty" json:"value,omitempty"` |
| 209 | From string `yaml:"from,omitempty" json:"from,omitempty"` |
| 210 | |
| 211 | Index uint `yaml:"index,omitempty" json:"index,omitempty"` |
| 212 | IndexTransform []MetricIndexTransform `yaml:"index_transform,omitempty" json:"index_transform,omitempty"` |
| 213 | |
| 214 | Symbol SymbolConfig `yaml:"symbol,omitempty" json:"symbol"` |
| 215 | OID string `yaml:"OID,omitempty" json:"OID,omitempty" jsonschema:"-"` |
| 216 | Name string `yaml:"name,omitempty" json:"name,omitempty" jsonschema:"-"` |
| 217 | |
| 218 | Format string `yaml:"format,omitempty" json:"format,omitempty"` |
| 219 | Mapping MappingConfig `yaml:"mapping,omitempty" json:"mapping"` |
| 220 | Sentinel []LicenseSentinelPolicy `yaml:"sentinel,omitempty" json:"sentinel,omitempty"` |
| 221 | Kind LicenseSignalKind `yaml:"kind,omitempty" json:"kind,omitempty"` |
| 222 | } |
| 223 | |
| 224 | func (c LicenseValueConfig) IsSet() bool { |
| 225 | return c.Value != "" || |
| 226 | c.From != "" || |
| 227 | c.Index != 0 || |
| 228 | len(c.IndexTransform) > 0 || |
| 229 | c.Symbol.OID != "" || |
| 230 | c.Symbol.Name != "" || |
| 231 | c.OID != "" || |
| 232 | c.Name != "" || |
| 233 | c.Format != "" || |
| 234 | c.Mapping.HasItems() || |
| 235 | c.Mapping.Mode != "" || |
| 236 | len(c.Sentinel) > 0 || |
| 237 | c.Kind != "" |
| 238 | } |
| 239 | |
| 240 | func (c LicenseValueConfig) Clone() LicenseValueConfig { |
| 241 | return LicenseValueConfig{ |
| 242 | Value: c.Value, |
| 243 | From: c.From, |
| 244 | Index: c.Index, |
| 245 | IndexTransform: slices.Clone(c.IndexTransform), |
| 246 | Symbol: c.Symbol.Clone(), |
| 247 | OID: c.OID, |
| 248 | Name: c.Name, |
| 249 | Format: c.Format, |
| 250 | Mapping: c.Mapping.Clone(), |
| 251 | Sentinel: slices.Clone(c.Sentinel), |
| 252 | Kind: c.Kind, |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | type LicenseSignalValueRef struct { |
| 257 | Kind LicenseSignalKind |
| 258 | Value LicenseValueConfig |
| 259 | } |
| 260 | |
| 261 | func LicenseSignalValueRefs(row LicensingConfig) []LicenseSignalValueRef { |
| 262 | var values []LicenseSignalValueRef |
| 263 | add := func(kind LicenseSignalKind, value LicenseValueConfig) { |
| 264 | values = append(values, LicenseSignalValueRef{Kind: kind, Value: value}) |
| 265 | } |
| 266 | add(LicenseSignalStateSeverity, row.State.LicenseValueConfig) |
| 267 | addLicenseTimerSignalValueRefs(row.Signals.Expiry, LicenseSignalExpiryTimestamp, LicenseSignalExpiryRemaining, add) |
| 268 | addLicenseTimerSignalValueRefs(row.Signals.Authorization, LicenseSignalAuthorizationTimestamp, LicenseSignalAuthorizationRemaining, add) |
| 269 | addLicenseTimerSignalValueRefs(row.Signals.Certificate, LicenseSignalCertificateTimestamp, LicenseSignalCertificateRemaining, add) |
| 270 | addLicenseTimerSignalValueRefs(row.Signals.Grace, LicenseSignalGraceTimestamp, LicenseSignalGraceRemaining, add) |
| 271 | add(LicenseSignalUsageUsed, row.Signals.Usage.Used) |
| 272 | add(LicenseSignalUsageCapacity, row.Signals.Usage.Capacity) |
| 273 | add(LicenseSignalUsageAvailable, row.Signals.Usage.Available) |
| 274 | add(LicenseSignalUsagePercent, row.Signals.Usage.Percent) |
| 275 | return values |
| 276 | } |
| 277 | |
| 278 | func addLicenseTimerSignalValueRefs(cfg LicenseTimerSignalsConfig, timestampKind, remainingKind LicenseSignalKind, add func(LicenseSignalKind, LicenseValueConfig)) { |
| 279 | add(timestampKind, cfg.LicenseValueConfig) |
| 280 | add(timestampKind, cfg.Timestamp) |
| 281 | add(remainingKind, cfg.Remaining) |
| 282 | } |
| 283 | |
| 284 | func LicenseStructuralIdentity(row LicensingConfig) string { |
| 285 | origin := row.OriginProfileID |
| 286 | if origin == "" { |
| 287 | origin = "<profile>" |
| 288 | } |
| 289 | return strings.Join([]string{origin, LicenseMergeIdentity(row)}, "|") |
| 290 | } |
| 291 | |
| 292 | func LicenseMergeIdentity(row LicensingConfig) string { |
| 293 | if row.Table.OID != "" { |
| 294 | return strings.Join([]string{"table", TrimLicenseOID(row.Table.OID)}, "|") |
| 295 | } |
| 296 | if row.ID != "" { |
| 297 | return strings.Join([]string{"scalar-group", row.ID}, "|") |
| 298 | } |
| 299 | for _, sig := range LicenseSignalValueRefs(row) { |
| 300 | if oid := LicenseValueSourceOID(sig.Value); oid != "" { |
| 301 | return strings.Join([]string{"scalar", TrimLicenseOID(oid)}, "|") |
| 302 | } |
| 303 | } |
| 304 | return strings.Join([]string{"scalar", "<missing-source>"}, "|") |
| 305 | } |
| 306 | |
| 307 | func LicenseValueSourceOID(value LicenseValueConfig) string { |
| 308 | switch { |
| 309 | case value.From != "": |
| 310 | return value.From |
| 311 | case value.Symbol.OID != "": |
| 312 | return value.Symbol.OID |
| 313 | case value.OID != "": |
| 314 | return value.OID |
| 315 | default: |
| 316 | return "" |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | func TrimLicenseOID(oid string) string { |
| 321 | return strings.TrimPrefix(strings.TrimSpace(oid), ".") |
| 322 | } |