Skip to content

Commit 9a5fbf0

Browse files
committed
Load Defaults
1 parent e796c46 commit 9a5fbf0

File tree

8 files changed

+103
-55
lines changed

8 files changed

+103
-55
lines changed

pkg/datastore/intent_rpc_set_update.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"context"
1919
"errors"
2020
"fmt"
21-
"math"
2221
"sort"
2322
"strings"
2423

@@ -48,8 +47,8 @@ func (d *Datastore) populateTreeWithRunning(ctx context.Context, tc *tree.TreeCo
4847
return err
4948
}
5049
for _, upd := range upds {
51-
newUpd := cache.NewUpdate(upd.GetPath(), upd.Bytes(), math.MaxInt32, "running", 0)
52-
err = r.AddCacheUpdateRecursive(ctx, newUpd, false)
50+
newUpd := cache.NewUpdate(upd.GetPath(), upd.Bytes(), tree.RunningValuesPrio, tree.RunningIntentName, 0)
51+
_, err = r.AddCacheUpdateRecursive(ctx, newUpd, false)
5352
if err != nil {
5453
return err
5554
}
@@ -117,7 +116,7 @@ func (d *Datastore) populateTree(ctx context.Context, req *sdcpb.SetIntentReques
117116
// now add the cache.Updates from the actual request, after marking the old once for deletion.
118117
for _, upd := range newCacheUpdates {
119118
// add the cache.Update to the tree
120-
err = root.AddCacheUpdateRecursive(ctx, upd, true)
119+
_, err = root.AddCacheUpdateRecursive(ctx, upd, true)
121120
if err != nil {
122121
return nil, err
123122
}
@@ -180,11 +179,7 @@ func (d *Datastore) SetIntentUpdate(ctx context.Context, req *sdcpb.SetIntentReq
180179
return nil, err
181180
}
182181

183-
fmt.Printf("Tree:%s\n", root.String())
184-
185-
// retrieve the data that is meant to be send southbound (towards the device)
186-
updates := root.GetHighestPrecedence(true)
187-
deletes := root.GetDeletes(true)
182+
fmt.Printf("Tree before Validate:%s\n", root.String())
188183

189184
// perform validation
190185
// we use a channel and cumulate all the errors
@@ -208,6 +203,12 @@ func (d *Datastore) SetIntentUpdate(ctx context.Context, req *sdcpb.SetIntentReq
208203

209204
logger.Debug("intent is validated")
210205

206+
fmt.Printf("Tree after Validate:%s\n", root.String())
207+
208+
// retrieve the data that is meant to be send southbound (towards the device)
209+
updates := root.GetHighestPrecedence(true)
210+
deletes := root.GetDeletes(true)
211+
211212
// set request to be applied into the candidate
212213
setDataReq := &sdcpb.SetDataRequest{
213214
Name: req.GetName(),

pkg/datastore/target/netconf/utils.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ import (
2121
"strings"
2222

2323
"github.com/beevik/etree"
24+
"github.com/sdcio/data-server/pkg/utils"
2425
sdcpb "github.com/sdcio/sdc-protos/sdcpb"
25-
26-
"github.com/sdcio/data-server/pkg/datastore/target/netconf/conversion"
2726
)
2827

2928
func getNamespaceFromGetSchemaResponse(sr *sdcpb.GetSchemaResponse) string {
@@ -80,7 +79,7 @@ func valueAsString(v *sdcpb.TypedValue) (string, error) {
8079
}
8180

8281
func StringElementToTypedValue(s string, ls *sdcpb.LeafSchema) (*sdcpb.TypedValue, error) {
83-
return conversion.Convert(s, ls.Type)
82+
return utils.Convert(s, ls.Type)
8483
}
8584

8685
// pathElem2EtreePath takes the given pathElem and creates an xpath expression out of it,

pkg/tree/entry.go

Lines changed: 71 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ import (
1111
"sync"
1212

1313
"github.com/sdcio/data-server/pkg/cache"
14+
"github.com/sdcio/data-server/pkg/utils"
1415
"github.com/sdcio/yang-parser/xpath"
1516
"github.com/sdcio/yang-parser/xpath/grammars/expr"
17+
"google.golang.org/protobuf/proto"
1618

1719
sdcpb "github.com/sdcio/sdc-protos/sdcpb"
1820
)
1921

2022
const (
21-
KeysIndexSep = "_"
23+
KeysIndexSep = "_"
24+
DefaultValuesPrio = int32(math.MaxInt32 - 90)
25+
DefaultsIntentName = "default"
26+
RunningValuesPrio = int32(math.MaxInt32 - 100)
27+
RunningIntentName = "running"
2228
)
2329

2430
type EntryImpl struct {
@@ -50,7 +56,7 @@ type Entry interface {
5056
// addChild Add a child entry
5157
addChild(context.Context, Entry) error
5258
// AddCacheUpdateRecursive Add the given cache.Update to the tree
53-
AddCacheUpdateRecursive(ctx context.Context, u *cache.Update, new bool) error
59+
AddCacheUpdateRecursive(ctx context.Context, u *cache.Update, new bool) (Entry, error)
5460
// StringIndent debug tree struct as indented string slice
5561
StringIndent(result []string) []string
5662
// GetHighesPrio return the new cache.Update entried from the tree that are the highes priority.
@@ -469,21 +475,61 @@ func (s *sharedEntryAttributes) NavigateSdcpbPath(ctx context.Context, pathElems
469475
return nil, fmt.Errorf("navigating tree, reached %v but child %v does not exist", s.Path(), pathElems)
470476
}
471477

472-
// func (s *sharedEntryAttributes) tryLoadingDefault(ctx context.Context, path []string) (Entry, error) {
478+
func (s *sharedEntryAttributes) tryLoadingDefault(ctx context.Context, path []string) (Entry, error) {
473479

474-
// schema, err := s.treeContext.treeSchemaCacheClient.GetSchema(ctx, path)
475-
// if err != nil {
476-
// fmt.Errorf("Error tried loading defaults: %s", s.Path().String())
477-
// }
480+
schema, err := s.treeContext.treeSchemaCacheClient.GetSchema(ctx, path)
481+
if err != nil {
482+
return nil, fmt.Errorf("error trying to load defaults for %s: %v", strings.Join(path, "->"), err)
483+
}
478484

479-
// switch schem := schema.GetSchema().(type) {
480-
// case *sdcpb.SchemaElem_Field:
481-
// schem.Field.GetDefault()
482-
// }
485+
var tv *sdcpb.TypedValue
483486

484-
// return nil
487+
switch schem := schema.GetSchema().GetSchema().(type) {
488+
case *sdcpb.SchemaElem_Field:
489+
defaultVal := schem.Field.GetDefault()
490+
if defaultVal == "" {
491+
return nil, fmt.Errorf("error no default defined for %s", strings.Join(path, " -> "))
492+
}
493+
tv, err = utils.Convert(defaultVal, schem.Field.GetType())
494+
if err != nil {
495+
return nil, err
496+
}
497+
case *sdcpb.SchemaElem_Leaflist:
498+
listDefaults := schem.Leaflist.GetDefaults()
499+
tvlist := make([]*sdcpb.TypedValue, 0, len(listDefaults))
500+
for _, dv := range schem.Leaflist.GetDefaults() {
501+
tvelem, err := utils.Convert(dv, schem.Leaflist.GetType())
502+
if err != nil {
503+
return nil, fmt.Errorf("error converting default to typed value for %s, type: %s ; value: %s; err: %v", strings.Join(path, " -> "), schem.Leaflist.GetType().GetTypeName(), dv, err)
504+
}
505+
tvlist = append(tvlist, tvelem)
506+
}
507+
tv = &sdcpb.TypedValue{
508+
Value: &sdcpb.TypedValue_LeaflistVal{
509+
LeaflistVal: &sdcpb.ScalarArray{
510+
Element: tvlist,
511+
},
512+
},
513+
}
514+
default:
515+
return nil, fmt.Errorf("error no defaults defined for schema path: %s", strings.Join(path, "->"))
516+
}
485517

486-
// }
518+
// convert value to []byte for cache insertion
519+
val, err := proto.Marshal(tv)
520+
if err != nil {
521+
return nil, err
522+
}
523+
524+
upd := cache.NewUpdate(path, val, DefaultValuesPrio, DefaultsIntentName, 0)
525+
526+
result, err := s.AddCacheUpdateRecursive(ctx, upd, false)
527+
if err != nil {
528+
return nil, fmt.Errorf("failed adding default value for %s to tree; %v", strings.Join(path, "/"), err)
529+
}
530+
531+
return result, nil
532+
}
487533

488534
// Navigate move through the tree, returns the Entry that is present under the given path
489535
func (s *sharedEntryAttributes) Navigate(ctx context.Context, path []string, isRootPath bool) (Entry, error) {
@@ -494,20 +540,23 @@ func (s *sharedEntryAttributes) Navigate(ctx context.Context, path []string, isR
494540
if isRootPath {
495541
return s.GetRoot().Navigate(ctx, path, false)
496542
}
497-
543+
var err error
498544
switch path[0] {
499545
case ".":
500546
return s.Navigate(ctx, path[1:], false)
501547
case "..":
502548
return s.parent.Navigate(ctx, path[1:], false)
503549
default:
504550
e, exists := s.filterActiveChoiceCaseChilds()[path[0]]
505-
if exists {
506-
return e.Navigate(ctx, path[1:], false)
551+
if !exists {
552+
e, err = s.tryLoadingDefault(ctx, append(s.Path(), path...))
553+
if err != nil {
554+
return nil, fmt.Errorf("navigating tree, reached %v but child %v does not exist, trying to load defaults yielded %v", s.Path(), path, err)
555+
}
556+
return e, nil
507557
}
558+
return e.Navigate(ctx, path[1:], false)
508559
}
509-
510-
return nil, fmt.Errorf("navigating tree, reached %v but child %v does not exist", s.Path(), path)
511560
}
512561

513562
func (s *sharedEntryAttributes) tryLoading(ctx context.Context, path []string) (Entry, error) {
@@ -518,7 +567,7 @@ func (s *sharedEntryAttributes) tryLoading(ctx context.Context, path []string) (
518567
if upd == nil {
519568
return nil, fmt.Errorf("reached %v but child %s does not exist", s.Path(), path[0])
520569
}
521-
err = s.treeContext.root.AddCacheUpdateRecursive(ctx, upd, false)
570+
_, err = s.treeContext.root.AddCacheUpdateRecursive(ctx, upd, false)
522571
if err != nil {
523572
return nil, err
524573
}
@@ -964,7 +1013,7 @@ func (s *sharedEntryAttributes) getKeyName() (string, error) {
9641013

9651014
// AddCacheUpdateRecursive recursively adds the given cache.Update to the tree. Thereby creating all the entries along the path.
9661015
// if the entries along th path already exist, the existing entries are called to add the Update.
967-
func (s *sharedEntryAttributes) AddCacheUpdateRecursive(ctx context.Context, c *cache.Update, new bool) error {
1016+
func (s *sharedEntryAttributes) AddCacheUpdateRecursive(ctx context.Context, c *cache.Update, new bool) (Entry, error) {
9681017
idx := 0
9691018
// if it is the root node, index remains == 0
9701019
if s.parent != nil {
@@ -987,7 +1036,7 @@ func (s *sharedEntryAttributes) AddCacheUpdateRecursive(ctx context.Context, c *
9871036
s.leafVariants = append(s.leafVariants, NewLeafEntry(c, new, s))
9881037
}
9891038

990-
return nil
1039+
return s, nil
9911040
}
9921041

9931042
var e Entry
@@ -997,7 +1046,7 @@ func (s *sharedEntryAttributes) AddCacheUpdateRecursive(ctx context.Context, c *
9971046
if e, exists = s.childs[c.GetPath()[idx]]; !exists {
9981047
e, err = newEntry(ctx, s, c.GetPath()[idx], s.treeContext)
9991048
if err != nil {
1000-
return err
1049+
return nil, err
10011050
}
10021051
}
10031052
return e.AddCacheUpdateRecursive(ctx, c, new)

pkg/tree/entry_test.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Test_Entry(t *testing.T) {
4242
}
4343

4444
for _, u := range []*cache.Update{u1, u2, u3} {
45-
err = root.AddCacheUpdateRecursive(ctx, u, true)
45+
_, err = root.AddCacheUpdateRecursive(ctx, u, true)
4646
if err != nil {
4747
t.Error(err)
4848
}
@@ -88,7 +88,7 @@ func Test_Entry_One(t *testing.T) {
8888

8989
// start test
9090
for _, u := range []*cache.Update{u1, u2, u3} {
91-
err := root.AddCacheUpdateRecursive(ctx, u, true)
91+
_, err := root.AddCacheUpdateRecursive(ctx, u, true)
9292
if err != nil {
9393
t.Error(err)
9494
}
@@ -154,7 +154,7 @@ func Test_Entry_Two(t *testing.T) {
154154

155155
// start test add "existing" data
156156
for _, u := range []*cache.Update{u1} {
157-
err := root.AddCacheUpdateRecursive(ctx, u, false)
157+
_, err := root.AddCacheUpdateRecursive(ctx, u, false)
158158
if err != nil {
159159
t.Error(err)
160160
}
@@ -167,7 +167,7 @@ func Test_Entry_Two(t *testing.T) {
167167
n1 := cache.NewUpdate([]string{"interface", "ethernet-0/0", "subinterface", "10", "description"}, overwriteDesc, prio50, owner1, ts1)
168168

169169
for _, u := range []*cache.Update{n1} {
170-
err := root.AddCacheUpdateRecursive(ctx, u, true)
170+
_, err = root.AddCacheUpdateRecursive(ctx, u, true)
171171
if err != nil {
172172
t.Error(err)
173173
}
@@ -212,7 +212,7 @@ func Test_Entry_Three(t *testing.T) {
212212

213213
// start test add "existing" data
214214
for _, u := range []*cache.Update{u1, u2, u3, u4} {
215-
err := root.AddCacheUpdateRecursive(ctx, u, false)
215+
_, err := root.AddCacheUpdateRecursive(ctx, u, false)
216216
if err != nil {
217217
t.Error(err)
218218
}
@@ -258,7 +258,7 @@ func Test_Entry_Three(t *testing.T) {
258258
n2 := cache.NewUpdate([]string{"interface", "ethernet-0/0", "subinterface", "11", "description"}, overwriteDesc, prio50, owner1, ts1)
259259

260260
for _, u := range []*cache.Update{n1, n2} {
261-
err := root.AddCacheUpdateRecursive(ctx, u, true)
261+
_, err := root.AddCacheUpdateRecursive(ctx, u, true)
262262
if err != nil {
263263
t.Error(err)
264264
}
@@ -327,7 +327,7 @@ func Test_Entry_Four(t *testing.T) {
327327

328328
// start test add "existing" data
329329
for _, u := range []*cache.Update{u1o1, u2o1, u3, u4, u1o2, u2o2} {
330-
err := root.AddCacheUpdateRecursive(ctx, u, false)
330+
_, err := root.AddCacheUpdateRecursive(ctx, u, false)
331331
if err != nil {
332332
t.Error(err)
333333
}
@@ -360,7 +360,7 @@ func Test_Entry_Four(t *testing.T) {
360360
n2 := cache.NewUpdate([]string{"interface", "ethernet-0/1", "subinterface", "11", "description"}, overwriteDesc, prio50, owner1, ts1)
361361

362362
for _, u := range []*cache.Update{n1, n2} {
363-
err := root.AddCacheUpdateRecursive(ctx, u, true)
363+
_, err := root.AddCacheUpdateRecursive(ctx, u, true)
364364
if err != nil {
365365
t.Error(err)
366366
}
@@ -434,7 +434,7 @@ func Test_Validation_Leaflist_Min_Max(t *testing.T) {
434434

435435
// start test add "existing" data
436436
for _, u := range []*cache.Update{u1} {
437-
err := root.AddCacheUpdateRecursive(ctx, u, false)
437+
_, err := root.AddCacheUpdateRecursive(ctx, u, false)
438438
if err != nil {
439439
t.Fatal(err)
440440
}
@@ -485,7 +485,7 @@ func Test_Validation_Leaflist_Min_Max(t *testing.T) {
485485

486486
// start test add "existing" data
487487
for _, u := range []*cache.Update{u1} {
488-
err := root.AddCacheUpdateRecursive(ctx, u, false)
488+
_, err := root.AddCacheUpdateRecursive(ctx, u, false)
489489
if err != nil {
490490
t.Fatal(err)
491491
}
@@ -540,7 +540,7 @@ func Test_Validation_Leaflist_Min_Max(t *testing.T) {
540540

541541
// start test add "existing" data
542542
for _, u := range []*cache.Update{u1} {
543-
err := root.AddCacheUpdateRecursive(ctx, u, false)
543+
_, err := root.AddCacheUpdateRecursive(ctx, u, false)
544544
if err != nil {
545545
t.Fatal(err)
546546
}
@@ -596,7 +596,7 @@ func Test_Entry_Delete_Aggregation(t *testing.T) {
596596

597597
// start test add "existing" data
598598
for _, u := range []*cache.Update{u1, u2, u3, u4, u5, u6} {
599-
err := root.AddCacheUpdateRecursive(ctx, u, false)
599+
_, err := root.AddCacheUpdateRecursive(ctx, u, false)
600600
if err != nil {
601601
t.Fatal(err)
602602
}
@@ -610,7 +610,7 @@ func Test_Entry_Delete_Aggregation(t *testing.T) {
610610

611611
// start test add "new" / request data
612612
for _, u := range []*cache.Update{u1n, u2n} {
613-
err := root.AddCacheUpdateRecursive(ctx, u, true)
613+
_, err := root.AddCacheUpdateRecursive(ctx, u, true)
614614
if err != nil {
615615
t.Fatal(err)
616616
}
@@ -1204,7 +1204,7 @@ func Test_Validation_String_Pattern(t *testing.T) {
12041204
u1 := cache.NewUpdate([]string{"patterntest"}, leafval, prio50, owner1, ts1)
12051205

12061206
for _, u := range []*cache.Update{u1} {
1207-
err := root.AddCacheUpdateRecursive(ctx, u, true)
1207+
_, err := root.AddCacheUpdateRecursive(ctx, u, true)
12081208
if err != nil {
12091209
t.Fatal(err)
12101210
}
@@ -1245,7 +1245,7 @@ func Test_Validation_String_Pattern(t *testing.T) {
12451245
u1 := cache.NewUpdate([]string{"patterntest"}, leafval, prio50, owner1, ts1)
12461246

12471247
for _, u := range []*cache.Update{u1} {
1248-
err := root.AddCacheUpdateRecursive(ctx, u, true)
1248+
_, err := root.AddCacheUpdateRecursive(ctx, u, true)
12491249
if err != nil {
12501250
t.Fatal(err)
12511251
}

pkg/tree/leaf_variants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (lv LeafVariants) shouldDelete() bool {
2121
for _, l := range lv {
2222
// if not running is set and not the owner is running then
2323
// it should not be deleted
24-
if !(l.Delete || l.Update.Owner() == "running") {
24+
if !(l.Delete || l.Update.Owner() == RunningIntentName) {
2525
return false
2626
}
2727
}

0 commit comments

Comments
 (0)