From 8c5da07c1003fbf6437c4cf3018f2cc0145dc974 Mon Sep 17 00:00:00 2001 From: Ivo Gosemann Date: Wed, 16 Sep 2026 10:22:43 +0200 Subject: [PATCH] add openconfig implementation for (ext-)communityset Signed-off-by: Ivo Gosemann --- internal/provider/openconfig/communityset.go | 97 +++++++++++++++++++ .../testdata/openconfig/communityset.txtar | 58 +++++++++++ .../testdata/openconfig/extcommunityset.txtar | 58 +++++++++++ 3 files changed, 213 insertions(+) create mode 100644 internal/provider/openconfig/communityset.go create mode 100644 test/gnmi/testdata/openconfig/communityset.txtar create mode 100644 test/gnmi/testdata/openconfig/extcommunityset.txtar diff --git a/internal/provider/openconfig/communityset.go b/internal/provider/openconfig/communityset.go new file mode 100644 index 000000000..6c6089e78 --- /dev/null +++ b/internal/provider/openconfig/communityset.go @@ -0,0 +1,97 @@ +// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors +// SPDX-License-Identifier: Apache-2.0 + +package openconfig + +import ( + "context" + "fmt" + + "github.com/ironcore-dev/network-operator/internal/provider" + "github.com/ironcore-dev/network-operator/internal/transport/gnmiext" +) + +var ( + _ provider.CommunitySetProvider = (*Provider)(nil) + _ provider.ExtCommunitySetProvider = (*Provider)(nil) +) + +// Compile-time assertions. +var ( + _ gnmiext.DataElement = (*CommunitySetElement)(nil) + _ gnmiext.DataElement = (*ExtCommunitySetElement)(nil) +) + +func (p *Provider) EnsureCommunitySet(ctx context.Context, req *provider.CommunitySetRequest) error { + spec := req.CommunitySet.Spec + + cs := &CommunitySetElement{ + Name: spec.Name, + Config: &CommunitySetConfig{ + Name: spec.Name, + }, + } + for _, m := range spec.Members { + cs.Config.Members = append(cs.Config.Members, m.Regex) + } + + return p.client.Update(ctx, cs) +} + +func (p *Provider) DeleteCommunitySet(ctx context.Context, req *provider.CommunitySetRequest) error { + cs := &CommunitySetElement{Name: req.CommunitySet.Spec.Name} + return p.client.Delete(ctx, cs) +} + +func (p *Provider) EnsureExtCommunitySet(ctx context.Context, req *provider.ExtCommunitySetRequest) error { + spec := req.ExtCommunitySet.Spec + + cs := &ExtCommunitySetElement{ + Name: spec.Name, + Config: &ExtCommunitySetConfig{ + Name: spec.Name, + }, + } + for _, m := range spec.Members { + cs.Config.Members = append(cs.Config.Members, m.Regex) + } + + return p.client.Update(ctx, cs) +} + +func (p *Provider) DeleteExtCommunitySet(ctx context.Context, req *provider.ExtCommunitySetRequest) error { + cs := &ExtCommunitySetElement{Name: req.ExtCommunitySet.Spec.Name} + return p.client.Delete(ctx, cs) +} + +// CommunitySetElement targets a community-set entry under bgp-defined-sets. +type CommunitySetElement struct { + Name string `json:"-"` + Config *CommunitySetConfig `json:"config,omitempty"` +} + +func (cs *CommunitySetElement) XPath() string { + return fmt.Sprintf("openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/community-sets/community-set[community-set-name=%s]", cs.Name) +} + +// CommunitySetConfig holds the community-set config. +type CommunitySetConfig struct { + Name string `json:"community-set-name"` + Members []string `json:"community-member"` +} + +// ExtCommunitySetElement targets an ext-community-set entry under bgp-defined-sets. +type ExtCommunitySetElement struct { + Name string `json:"-"` + Config *ExtCommunitySetConfig `json:"config,omitempty"` +} + +func (cs *ExtCommunitySetElement) XPath() string { + return fmt.Sprintf("openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/ext-community-sets/ext-community-set[ext-community-set-name=%s]", cs.Name) +} + +// ExtCommunitySetConfig holds the ext-community-set config. +type ExtCommunitySetConfig struct { + Name string `json:"ext-community-set-name"` + Members []string `json:"ext-community-member"` +} diff --git a/test/gnmi/testdata/openconfig/communityset.txtar b/test/gnmi/testdata/openconfig/communityset.txtar new file mode 100644 index 000000000..cb987c759 --- /dev/null +++ b/test/gnmi/testdata/openconfig/communityset.txtar @@ -0,0 +1,58 @@ +# CommunitySet with regex members +-- communitysets/cs-wireapi -- +apiVersion: networking.metal.ironcore.dev/v1alpha1 +kind: CommunitySet +metadata: + name: cs-wireapi + namespace: default +spec: + deviceRef: + name: device + name: WIREAPI + members: + - sequence: 5 + regex: "50000:[0-9][0-9]" + - sequence: 10 + regex: "65001:[0-9]+" + +-- state/preload -- +{} + +-- state/expect -- + +{ + "openconfig-routing-policy:routing-policy": { + "defined-sets": { + "openconfig-bgp-policy:bgp-defined-sets": { + "community-sets": { + "community-set": [ + { + "community-set-name": "WIREAPI", + "config": { + "community-set-name": "WIREAPI", + "community-member": [ + "50000:[0-9][0-9]", + "65001:[0-9]+" + ] + } + } + ] + } + } + } + } +} + +-- state/delete -- + +{ + "openconfig-routing-policy:routing-policy": { + "defined-sets": { + "openconfig-bgp-policy:bgp-defined-sets": { + "community-sets": { + "community-set": [] + } + } + } + } +} diff --git a/test/gnmi/testdata/openconfig/extcommunityset.txtar b/test/gnmi/testdata/openconfig/extcommunityset.txtar new file mode 100644 index 000000000..cca8fbe62 --- /dev/null +++ b/test/gnmi/testdata/openconfig/extcommunityset.txtar @@ -0,0 +1,58 @@ +# ExtCommunitySet with regex members +-- extcommunitysets/ecs-wireapi -- +apiVersion: networking.metal.ironcore.dev/v1alpha1 +kind: ExtCommunitySet +metadata: + name: ecs-wireapi + namespace: default +spec: + deviceRef: + name: device + name: WIREAPI + members: + - sequence: 5 + regex: "65200:[0-9][0-9]" + - sequence: 15 + regex: "65300:[0-9]+" + +-- state/preload -- +{} + +-- state/expect -- + +{ + "openconfig-routing-policy:routing-policy": { + "defined-sets": { + "openconfig-bgp-policy:bgp-defined-sets": { + "ext-community-sets": { + "ext-community-set": [ + { + "ext-community-set-name": "WIREAPI", + "config": { + "ext-community-set-name": "WIREAPI", + "ext-community-member": [ + "65200:[0-9][0-9]", + "65300:[0-9]+" + ] + } + } + ] + } + } + } + } +} + +-- state/delete -- + +{ + "openconfig-routing-policy:routing-policy": { + "defined-sets": { + "openconfig-bgp-policy:bgp-defined-sets": { + "ext-community-sets": { + "ext-community-set": [] + } + } + } + } +}