Skip to content

Commit 62e57b3

Browse files
committed
adapt lldp config to sonic build branch
1 parent 4995cc3 commit 62e57b3

File tree

10 files changed

+86
-25
lines changed

10 files changed

+86
-25
lines changed

cmd/generate.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,25 @@ import (
88
"github.com/metal-stack/sonic-configdb-utils/configdb"
99
p "github.com/metal-stack/sonic-configdb-utils/platform"
1010
"github.com/metal-stack/sonic-configdb-utils/values"
11+
v "github.com/metal-stack/sonic-configdb-utils/version"
1112
"github.com/spf13/cobra"
1213
)
1314

1415
var generateCmd = &cobra.Command{
1516
Use: "generate",
1617
Short: "Generate a config_db.json",
1718
RunE: func(cmd *cobra.Command, args []string) error {
19+
inputFile, _ := cmd.Flags().GetString("input-file")
20+
outputFile, _ := cmd.Flags().GetString("output-file")
21+
deviceDir, _ := cmd.Flags().GetString("device-dir")
1822
sonicEnvFile, _ := cmd.Flags().GetString("env-file")
23+
sonicVersionFile, _ := cmd.Flags().GetString("version-file")
24+
1925
env, err := p.GetEnvironment(sonicEnvFile)
2026
if err != nil {
2127
return fmt.Errorf("failed to get environment information:%w", err)
2228
}
2329

24-
platformIdentifier := env.Platform
25-
inputFile, _ := cmd.Flags().GetString("input-file")
26-
outputFile, _ := cmd.Flags().GetString("output-file")
27-
deviceDir, _ := cmd.Flags().GetString("device-dir")
28-
2930
inputBytes, err := os.ReadFile(inputFile)
3031
if err != nil {
3132
fmt.Printf("failed to read input file, %v\n", err)
@@ -38,8 +39,7 @@ var generateCmd = &cobra.Command{
3839
os.Exit(1)
3940
}
4041

41-
platformFile := fmt.Sprintf("%s/%s/platform.json", deviceDir, platformIdentifier)
42-
42+
platformFile := fmt.Sprintf("%s/%s/platform.json", deviceDir, env.Platform)
4343
platformBytes, err := os.ReadFile(platformFile)
4444
if err != nil {
4545
return fmt.Errorf("failed to read platform.json file:%w", err)
@@ -50,7 +50,17 @@ var generateCmd = &cobra.Command{
5050
return fmt.Errorf("failed to parse platform.json:%w", err)
5151
}
5252

53-
configDB, err := configdb.GenerateConfigDB(values, platform, env)
53+
versionBytes, err := os.ReadFile(sonicVersionFile)
54+
if err != nil {
55+
return fmt.Errorf("failed to read version file:%w", err)
56+
}
57+
58+
version, err := v.UnmarshalVersion(versionBytes)
59+
if err != nil {
60+
return fmt.Errorf("failed to parse version file:%w", err)
61+
}
62+
63+
configDB, err := configdb.GenerateConfigDB(values, platform, env, version)
5464
if err != nil {
5565
return fmt.Errorf("failed to generate config:%w", err)
5666
}
@@ -76,4 +86,5 @@ func init() {
7686
generateCmd.Flags().StringP("output-file", "o", "config_db.json", "path to output file")
7787
generateCmd.Flags().String("device-dir", "/usr/share/sonic/device", "directory which holds all device-specific files")
7888
generateCmd.Flags().StringP("env-file", "e", "/etc/sonic/sonic-environment", "sonic-environment file holding platform information")
89+
generateCmd.Flags().StringP("version-file", "v", "/etc/sonic/sonic_version.yaml", "sonic version file")
7990
}

configdb/configdb.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import (
77
"slices"
88
"strconv"
99

10-
"github.com/metal-stack/sonic-configdb-utils/platform"
1110
p "github.com/metal-stack/sonic-configdb-utils/platform"
1211
"github.com/metal-stack/sonic-configdb-utils/values"
12+
v "github.com/metal-stack/sonic-configdb-utils/version"
1313
)
1414

1515
type ConfigDB struct {
@@ -44,7 +44,7 @@ type ConfigDB struct {
4444
VXLANTunnelMap VXLANTunnelMap `json:"VXLAN_TUNNEL_MAP,omitempty"`
4545
}
4646

47-
func GenerateConfigDB(input *values.Values, platform *p.Platform, environment *platform.Environment) (*ConfigDB, error) {
47+
func GenerateConfigDB(input *values.Values, platform *p.Platform, environment *p.Environment, version *v.Version) (*ConfigDB, error) {
4848
if input == nil {
4949
return nil, fmt.Errorf("no input values provided")
5050
}
@@ -74,12 +74,13 @@ func GenerateConfigDB(input *values.Values, platform *p.Platform, environment *p
7474
DNSNameservers: getDNSNameservers(input.Nameservers),
7575
Features: features,
7676
Interfaces: getInterfaces(input.Ports, input.BGPPorts, input.Interconnects),
77-
LLDP: getLLDP(input.LLDPHelloTime),
77+
LLDP: getLLDP(input.LLDPHelloTime, version),
7878
LoopbackInterface: getLoopbackInterface(input.LoopbackAddress),
7979
MCLAGDomains: getMCLAGDomains(input.MCLAG),
8080
MCLAGInterfaces: getMCLAGInterfaces(input.MCLAG),
8181
MCLAGUniqueIPs: getMCLAGUniqueIPs(input.MCLAG),
8282
MgmtInterfaces: getMgmtInterfaces(input.MgmtInterface),
83+
// FIX: some switches don't have an eth0
8384
MgmtPorts: map[string]MgmtPort{
8485
"eth0": {
8586
AdminStatus: AdminStatusUp,
@@ -169,7 +170,7 @@ func getACLRulesAndTables(sourceRanges []string) (map[string]ACLRule, map[string
169170
return rules, tables
170171
}
171172

172-
func getDeviceMetadata(input *values.Values, environment *platform.Environment) (*DeviceMetadata, error) {
173+
func getDeviceMetadata(input *values.Values, environment *p.Environment) (*DeviceMetadata, error) {
173174
if environment.Platform == "" {
174175
return nil, fmt.Errorf("no platform identifiert found in environment file")
175176
}
@@ -266,15 +267,28 @@ func getInterfaces(ports values.Ports, bgpPorts []string, interconnects map[stri
266267
return interfaces
267268
}
268269

269-
func getLLDP(interval int) *LLDP {
270+
func getLLDP(interval int, version *v.Version) *LLDP {
270271
if interval < 1 {
271272
return nil
272273
}
273-
return &LLDP{
274-
Global: LLDPGlobal{
275-
HelloTime: fmt.Sprintf("%d", interval),
276-
},
274+
275+
lldp := &LLDP{}
276+
global := LLDPGlobal{
277+
HelloTime: fmt.Sprintf("%d", interval),
277278
}
279+
280+
switch version.Branch {
281+
case string(v.Branch202111):
282+
global202111 := LLDPGlobal202111(global)
283+
lldp.Global202111 = &global202111
284+
case string(v.Branch202211):
285+
global202211 := LLDPGlobal202211(global)
286+
lldp.Global202211 = &global202211
287+
default:
288+
lldp = nil
289+
}
290+
291+
return lldp
278292
}
279293

280294
func getLoopbackInterface(loopback string) map[string]struct{} {

configdb/fields.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,22 @@ const (
8484
)
8585

8686
type LLDP struct {
87-
Global LLDPGlobal `json:"Global"`
87+
Global202111 *LLDPGlobal202111 `json:"Global,omitempty"`
88+
Global202211 *LLDPGlobal202211 `json:"GLOBAL,omitempty"`
8889
}
8990

9091
type LLDPGlobal struct {
92+
HelloTime string
93+
}
94+
95+
type LLDPGlobal202111 struct {
9196
HelloTime string `json:"hello_timer,omitempty"`
9297
}
9398

99+
type LLDPGlobal202211 struct {
100+
HelloTime string `json:"hello_time,omitempty"`
101+
}
102+
94103
type MCLAGDomain struct {
95104
MCLAGSystemID string `json:"mclag_system_id,omitempty"`
96105
PeerIP string `json:"peer_ip,omitempty"`

tests/1/sonic_version.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
branch: 'ec202111'

tests/2/expected.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,6 @@
126126
"vrf_name": "Vrf42"
127127
}
128128
},
129-
"LLDP": {
130-
"Global": {
131-
"hello_timer": "10"
132-
}
133-
},
134129
"LOOPBACK_INTERFACE": {
135130
"Loopback0": {},
136131
"Loopback0|10.7.7.7/32": {}

tests/2/sonic_version.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
branch: 'unknown-version'

tests/3/sonic_version.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
branch: 'ec202111'

tests/4/expected.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@
178178
"10.0.0.10": {}
179179
},
180180
"LLDP": {
181-
"Global": {
182-
"hello_timer": "10"
181+
"GLOBAL": {
182+
"hello_time": "10"
183183
}
184184
},
185185
"LOOPBACK_INTERFACE": {

tests/4/sonic_version.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
branch: 'ec202211_ecsonic'

version/version.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package version
2+
3+
import "gopkg.in/yaml.v3"
4+
5+
type Branch string
6+
7+
const (
8+
Branch202111 Branch = "ec202111"
9+
Branch202211 Branch = "ec202211_ecsonic"
10+
)
11+
12+
type Version struct {
13+
Branch string `yaml:"branch"`
14+
}
15+
16+
func UnmarshalVersion(in []byte) (*Version, error) {
17+
var version Version
18+
err := yaml.Unmarshal(in, &version)
19+
if err != nil {
20+
return nil, err
21+
}
22+
23+
return &version, nil
24+
}

0 commit comments

Comments
 (0)