Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pkg/datastore/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ func TestDatastore_expandUpdateLeafAsKeys(t *testing.T) {
{
Name: "doublekey",
Key: map[string]string{
"key1": "foo",
"key2": "123",
"key1": "k1foo",
"key2": "k2123",
},
},
{
Expand All @@ -186,8 +186,8 @@ func TestDatastore_expandUpdateLeafAsKeys(t *testing.T) {
{
Name: "doublekey",
Key: map[string]string{
"key1": "foo",
"key2": "123",
"key1": "k1foo",
"key2": "k2123",
},
},
{
Expand All @@ -196,7 +196,7 @@ func TestDatastore_expandUpdateLeafAsKeys(t *testing.T) {
},
},
Value: &sdcpb.TypedValue{
Value: &sdcpb.TypedValue_StringVal{StringVal: "foo"},
Value: &sdcpb.TypedValue_StringVal{StringVal: "k1foo"},
},
},
{
Expand All @@ -205,8 +205,8 @@ func TestDatastore_expandUpdateLeafAsKeys(t *testing.T) {
{
Name: "doublekey",
Key: map[string]string{
"key1": "foo",
"key2": "123",
"key1": "k1foo",
"key2": "k2123",
},
},
{
Expand All @@ -215,7 +215,7 @@ func TestDatastore_expandUpdateLeafAsKeys(t *testing.T) {
},
},
Value: &sdcpb.TypedValue{
Value: &sdcpb.TypedValue_StringVal{StringVal: "123"},
Value: &sdcpb.TypedValue_StringVal{StringVal: "k2123"},
},
},
},
Expand Down
234 changes: 115 additions & 119 deletions pkg/datastore/tree_operation_test.go

Large diffs are not rendered by default.

22 changes: 15 additions & 7 deletions pkg/tree/sharedEntryAttributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"github.com/sdcio/data-server/pkg/utils"
logf "github.com/sdcio/logger"
sdcpb "github.com/sdcio/sdc-protos/sdcpb"
sdcpbutils "github.com/sdcio/sdc-protos/utils"
"github.com/sdcio/sdc-protos/tree_persist"
sdcpbutils "github.com/sdcio/sdc-protos/utils"
)

var (
Expand Down Expand Up @@ -1236,24 +1236,32 @@ func (s *sharedEntryAttributes) validateMandatory(ctx context.Context, resultCha
if s.schema != nil {
switch s.schema.GetSchema().(type) {
case *sdcpb.SchemaElem_Container:
for _, c := range s.schema.GetContainer().GetMandatoryChildrenConfig() {
containerSchema := s.schema.GetContainer()
for _, c := range containerSchema.GetMandatoryChildrenConfig() {
attributes := []string{}
choiceName := ""
// check if it is a ChildContainer
if slices.Contains(s.schema.GetContainer().GetChildren(), c.Name) {
if slices.Contains(containerSchema.GetChildren(), c.Name) {
attributes = append(attributes, c.Name)
}

// check if it is a Key
if slices.ContainsFunc(containerSchema.GetKeys(), func(x *sdcpb.LeafSchema) bool {
return x.Name == c.Name
}) {
attributes = append(attributes, c.Name)
}

// check if it is a Field
if slices.ContainsFunc(s.schema.GetContainer().GetFields(), func(x *sdcpb.LeafSchema) bool {
if slices.ContainsFunc(containerSchema.GetFields(), func(x *sdcpb.LeafSchema) bool {
return x.Name == c.Name
}) {
attributes = append(attributes, c.Name)
}

// otherwise it will probably be a choice
if len(attributes) == 0 {
choice := s.schema.GetContainer().GetChoiceInfo().GetChoiceByName(c.Name)
choice := containerSchema.GetChoiceInfo().GetChoiceByName(c.Name)
if choice != nil {
attributes = append(attributes, choice.GetAllAttributes()...)
choiceName = c.Name
Expand All @@ -1264,9 +1272,9 @@ func (s *sharedEntryAttributes) validateMandatory(ctx context.Context, resultCha
log.Error(ValidationError, "mandatory attribute could not be found as child, field or choice", "path", s.SdcpbPath().ToXPath(false), "attribute", c.Name)
}

s.validateMandatoryWithKeys(ctx, len(s.GetSchema().GetContainer().GetKeys()), attributes, choiceName, resultChan)
s.validateMandatoryWithKeys(ctx, len(containerSchema.GetKeys()), attributes, choiceName, resultChan)
}
stats.Add(types.StatTypeMandatory, uint32(len(s.schema.GetContainer().GetMandatoryChildrenConfig())))
stats.Add(types.StatTypeMandatory, uint32(len(containerSchema.GetMandatoryChildrenConfig())))
}
}
}
Expand Down
91 changes: 65 additions & 26 deletions pkg/tree/sharedEntryAttributes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func Test_sharedEntryAttributes_checkAndCreateKeysAsLeafs(t *testing.T) {
p = &sdcpb.Path{
Elem: []*sdcpb.PathElem{
sdcpb.NewPathElem("doublekey", map[string]string{
"key1": "k1.1",
"key2": "k1.3",
"key1": "k11.1",
"key2": "k21.3",
}),
sdcpb.NewPathElem("mandato", nil),
},
Expand Down Expand Up @@ -304,23 +304,23 @@ func Test_sharedEntryAttributes_GetListChilds(t *testing.T) {
d := &sdcio_schema.Device{
Doublekey: map[sdcio_schema.SdcioModel_Doublekey_Key]*sdcio_schema.SdcioModel_Doublekey{
{
Key1: "k1.1",
Key2: "k1.2",
Key1: "k11.1",
Key2: "k21.2",
}: {
Key1: ygot.String("k1.1"),
Key2: ygot.String("k1.2"),
Key1: ygot.String("k11.1"),
Key2: ygot.String("k21.2"),
Mandato: ygot.String("TheMandatoryValueOther"),
Cont: &sdcio_schema.SdcioModel_Doublekey_Cont{
Value1: ygot.String("containerval1.1"),
Value2: ygot.String("containerval1.2"),
},
},
{
Key1: "k2.1",
Key2: "k2.2",
Key1: "k12.1",
Key2: "k22.2",
}: {
Key1: ygot.String("k2.1"),
Key2: ygot.String("k2.2"),
Key1: ygot.String("k12.1"),
Key2: ygot.String("k22.2"),
Mandato: ygot.String("TheMandatoryValue2"),
Cont: &sdcio_schema.SdcioModel_Doublekey_Cont{
Value1: ygot.String("containerval2.1"),
Expand Down Expand Up @@ -360,18 +360,18 @@ func Test_sharedEntryAttributes_GetListChilds(t *testing.T) {
}{
{
name: "Double Key - pass",
wantNames: []string{"k2.2", "k1.2"},
wantNames: []string{"k22.2", "k21.2"},
wantKeys: []string{"key1", "key2", "cont", "mandato"},
path: &sdcpb.Path{Elem: []*sdcpb.PathElem{sdcpb.NewPathElem("doublekey", nil)}, IsRootBased: true},
},
{
name: "nil schema",
path: &sdcpb.Path{Elem: []*sdcpb.PathElem{sdcpb.NewPathElem("doublekey", map[string]string{"key1": "k1.1"})}, IsRootBased: true},
path: &sdcpb.Path{Elem: []*sdcpb.PathElem{sdcpb.NewPathElem("doublekey", map[string]string{"key1": "k11.1"})}, IsRootBased: true},
wantErr: true,
},
{
name: "non container",
path: &sdcpb.Path{Elem: []*sdcpb.PathElem{sdcpb.NewPathElem("doublekey", map[string]string{"key1": "k1.1", "key2": "k1.2"}), sdcpb.NewPathElem("mandato", nil)}, IsRootBased: true},
path: &sdcpb.Path{Elem: []*sdcpb.PathElem{sdcpb.NewPathElem("doublekey", map[string]string{"key1": "k11.1", "key2": "k21.2"}), sdcpb.NewPathElem("mandato", nil)}, IsRootBased: true},
wantErr: true,
},
{
Expand Down Expand Up @@ -829,8 +829,8 @@ func Test_sharedEntryAttributes_getOrCreateChilds(t *testing.T) {
path: &sdcpb.Path{
Elem: []*sdcpb.PathElem{
sdcpb.NewPathElem("doublekey", map[string]string{
"key1": "k1.1",
"key2": "k1.2",
"key1": "k11.1",
"key2": "k21.2",
}),
sdcpb.NewPathElem("mandato", nil),
},
Expand Down Expand Up @@ -951,11 +951,11 @@ func Test_sharedEntryAttributes_validateMandatory(t *testing.T) {
conf1 := &sdcio_schema.Device{
Doublekey: map[sdcio_schema.SdcioModel_Doublekey_Key]*sdcio_schema.SdcioModel_Doublekey{
{
Key1: "k1.1",
Key2: "k1.2",
Key1: "k11.1",
Key2: "k21.2",
}: {
Key1: ygot.String("k1.1"),
Key2: ygot.String("k1.2"),
Key1: ygot.String("k11.1"),
Key2: ygot.String("k21.2"),
Cont: &sdcio_schema.SdcioModel_Doublekey_Cont{
Value1: ygot.String("containerval1.1"),
Value2: ygot.String("containerval1.2"),
Expand Down Expand Up @@ -987,11 +987,50 @@ func Test_sharedEntryAttributes_validateMandatory(t *testing.T) {
return root
},
want: []*types.ValidationResultEntry{
types.NewValidationResultEntry("unknown", fmt.Errorf("error mandatory child [mandato] does not exist, path: /doublekey[key1=k1.1][key2=k1.2]"), types.ValidationResultEntryTypeError),
types.NewValidationResultEntry("unknown", fmt.Errorf("error mandatory child [mandato] does not exist, path: /doublekey[key1=k11.1][key2=k21.2]"), types.ValidationResultEntryTypeError),
types.NewValidationResultEntry("unknown", fmt.Errorf("error mandatory child [autonomous-system] does not exist, path: /network-instance[name=ni1]/protocol/bgp"), types.ValidationResultEntryTypeError),
types.NewValidationResultEntry("unknown", fmt.Errorf("error mandatory child [router-id] does not exist, path: /network-instance[name=ni1]/protocol/bgp"), types.ValidationResultEntryTypeError),
},
},
{
name: "mandatory key present",
r: func(t *testing.T) *RootEntry {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

scb, err := testhelper.GetSchemaClientBound(t, mockCtrl)
if err != nil {
t.Fatal(err)
}

tc := NewTreeContext(scb, owner1)
root, err := NewTreeRoot(ctx, tc)
if err != nil {
t.Fatal(err)
}

conf1 := &sdcio_schema.Device{
Mandatorykey: map[string]*sdcio_schema.SdcioModel_Mandatorykey{
"mk1": {
Key1: ygot.String("k11"),
Mandato: ygot.String("k2asdf"),
},
},
}
err = testhelper.LoadYgotStructIntoTreeRoot(ctx, conf1, root, owner1, 5, flagsNew)
if err != nil {
t.Fatal(err)
}

err = root.FinishInsertionPhase(ctx)
if err != nil {
t.Fatal(err)
}

return root
},
want: []*types.ValidationResultEntry{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -1055,11 +1094,11 @@ func Test_sharedEntryAttributes_ReApply(t *testing.T) {
conf1 := &sdcio_schema.Device{
Doublekey: map[sdcio_schema.SdcioModel_Doublekey_Key]*sdcio_schema.SdcioModel_Doublekey{
{
Key1: "k1.1",
Key2: "k1.2",
Key1: "k11.1",
Key2: "k21.2",
}: {
Key1: ygot.String("k1.1"),
Key2: ygot.String("k1.2"),
Key1: ygot.String("k11.1"),
Key2: ygot.String("k21.2"),
Cont: &sdcio_schema.SdcioModel_Doublekey_Cont{
Value1: ygot.String("containerval1.1"),
Value2: ygot.String("containerval1.2"),
Expand Down Expand Up @@ -1093,8 +1132,8 @@ func Test_sharedEntryAttributes_ReApply(t *testing.T) {
&sdcpb.Path{
Elem: []*sdcpb.PathElem{
sdcpb.NewPathElem("doublekey", map[string]string{
"key1": "k1.1",
"key2": "k1.2",
"key1": "k11.1",
"key2": "k21.2",
}),
sdcpb.NewPathElem("mandato", nil),
}, IsRootBased: true,
Expand Down
4 changes: 4 additions & 0 deletions tests/schema/sdcio_model.yang
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ module sdcio_model {
import sdcio_model_doublekey {
prefix sdcio_model_doublekey;
}
import sdcio_model_mandatorykey {
prefix sdcio_model_mandatorykey;
}
import sdcio_model_static_route {
prefix sdcio_model_static_route;
}
Expand Down Expand Up @@ -53,6 +56,7 @@ module sdcio_model {
uses sdcio_model_ni:network-instance-top;
uses sdcio_model_choice:choicecase-top;
uses sdcio_model_doublekey:doublekey-top;
uses sdcio_model_mandatorykey:mandatorykey-top;
uses sdcio_model_static_route:static-route-top;
uses sdcio_model_leaflist:leaflist-top;
uses sdcio_model_list_unique:list-unique-top;
Expand Down
4 changes: 2 additions & 2 deletions tests/schema/sdcio_model_doublekey.yang
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ module sdcio_model_doublekey {
key "key2 key1";
leaf key2 {
type string {
pattern '[0-9]+';
pattern 'k2[a-zA-Z0-9.]+';
}
}
leaf key1 {
type string {
pattern '[a-z]+';
pattern 'k1[a-zA-Z0-9.]+';
}
}
leaf mandato {
Expand Down
41 changes: 41 additions & 0 deletions tests/schema/sdcio_model_mandatorykey.yang
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module sdcio_model_mandatorykey {
yang-version 1.1;
namespace "urn:sdcio/model_mandatorykey";
prefix sdcio_model_mandatorykey;

description
"This is part of the test schema for sdcio";

revision 2026-01-29 {
description
"Test schema first attempt";
}

grouping mandatorykey-top {
list mandatorykey {
description
"here we have a container with a mandatory key";
key "key1";
leaf key1 {
type string {
pattern '[a-z]+';
}
mandatory true;
}
leaf mandato {
type string;
mandatory true;
}
container cont {
description
"just a simple container under a mandatory keyed item";
leaf value1 {
type string;
}
leaf value2 {
type string;
}
}
}
}
}
Loading