forked from sonic-net/sonic-mgmt-framework
-
Notifications
You must be signed in to change notification settings - Fork 16
/
cvl_api.go
837 lines (681 loc) · 25.1 KB
/
cvl_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright 2019 Broadcom. The term Broadcom refers to Broadcom Inc. and/or //
// its subsidiaries. //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// //
////////////////////////////////////////////////////////////////////////////////
package cvl
import (
"fmt"
"reflect"
"encoding/json"
"github.com/go-redis/redis"
toposort "github.com/philopon/go-toposort"
"cvl/internal/yparser"
. "cvl/internal/util"
"strings"
"github.com/antchfx/xmlquery"
"unsafe"
"runtime"
custv "cvl/custom_validation"
"time"
"sync"
)
type CVLValidateType uint
const (
VALIDATE_NONE CVLValidateType = iota //Data is used as dependent data
VALIDATE_SYNTAX //Syntax is checked and data is used as dependent data
VALIDATE_SEMANTICS //Semantics is checked
VALIDATE_ALL //Syntax and Semantics are checked
)
type CVLOperation uint
const (
OP_NONE CVLOperation = 0 //Used to just validate the config without any operation
OP_CREATE = 1 << 0//For Create operation
OP_UPDATE = 1 << 1//For Update operation
OP_DELETE = 1 << 2//For Delete operation
)
var cvlErrorMap = map[CVLRetCode]string {
CVL_SUCCESS : "Config Validation Success",
CVL_SYNTAX_ERROR : "Config Validation Syntax Error",
CVL_SEMANTIC_ERROR : "Config Validation Semantic Error",
CVL_SYNTAX_MISSING_FIELD : "Required Field is Missing",
CVL_SYNTAX_INVALID_FIELD : "Invalid Field Received",
CVL_SYNTAX_INVALID_INPUT_DATA : "Invalid Input Data Received",
CVL_SYNTAX_MULTIPLE_INSTANCE : "Multiple Field Instances Received",
CVL_SYNTAX_DUPLICATE : "Duplicate Instances Received",
CVL_SYNTAX_ENUM_INVALID : "Invalid Enum Value Received",
CVL_SYNTAX_ENUM_INVALID_NAME : "Invalid Enum Value Received",
CVL_SYNTAX_ENUM_WHITESPACE : "Enum name with leading/trailing whitespaces Received",
CVL_SYNTAX_OUT_OF_RANGE : "Value out of range/length/pattern (data)",
CVL_SYNTAX_MINIMUM_INVALID : "min-elements constraint not honored",
CVL_SYNTAX_MAXIMUM_INVALID : "max-elements constraint not honored",
CVL_SEMANTIC_DEPENDENT_DATA_MISSING : "Dependent Data is missing",
CVL_SEMANTIC_MANDATORY_DATA_MISSING : "Mandatory Data is missing",
CVL_SEMANTIC_KEY_ALREADY_EXIST : "Key already existing.",
CVL_SEMANTIC_KEY_NOT_EXIST : "Key is missing.",
CVL_SEMANTIC_KEY_DUPLICATE : "Duplicate key received",
CVL_SEMANTIC_KEY_INVALID : "Invalid Key Received",
CVL_INTERNAL_UNKNOWN : "Internal Unknown Error",
CVL_ERROR : "Generic Error",
CVL_NOT_IMPLEMENTED : "Error Not Implemented",
CVL_FAILURE : "Generic Failure",
}
//Error code
type CVLRetCode int
const (
CVL_SUCCESS CVLRetCode = iota
CVL_ERROR
CVL_NOT_IMPLEMENTED
CVL_INTERNAL_UNKNOWN
CVL_FAILURE
CVL_SYNTAX_ERROR = CVLRetCode(yparser.YP_SYNTAX_ERROR)
CVL_SEMANTIC_ERROR = CVLRetCode(yparser.YP_SEMANTIC_ERROR)
CVL_SYNTAX_MISSING_FIELD = CVLRetCode(yparser.YP_SYNTAX_MISSING_FIELD)
CVL_SYNTAX_INVALID_FIELD = CVLRetCode(yparser.YP_SYNTAX_INVALID_FIELD) /* Invalid Field */
CVL_SYNTAX_INVALID_INPUT_DATA = CVLRetCode(yparser.YP_SYNTAX_INVALID_INPUT_DATA) /*Invalid Input Data */
CVL_SYNTAX_MULTIPLE_INSTANCE = CVLRetCode(yparser.YP_SYNTAX_MULTIPLE_INSTANCE) /* Multiple Field Instances */
CVL_SYNTAX_DUPLICATE = CVLRetCode(yparser.YP_SYNTAX_DUPLICATE) /* Duplicate Fields */
CVL_SYNTAX_ENUM_INVALID = CVLRetCode(yparser.YP_SYNTAX_ENUM_INVALID) /* Invalid enum value */
CVL_SYNTAX_ENUM_INVALID_NAME = CVLRetCode(yparser.YP_SYNTAX_ENUM_INVALID_NAME) /* Invalid enum name */
CVL_SYNTAX_ENUM_WHITESPACE = CVLRetCode(yparser.YP_SYNTAX_ENUM_WHITESPACE) /* Enum name with leading/trailing whitespaces */
CVL_SYNTAX_OUT_OF_RANGE = CVLRetCode(yparser.YP_SYNTAX_OUT_OF_RANGE) /* Value out of range/length/pattern (data) */
CVL_SYNTAX_MINIMUM_INVALID = CVLRetCode(yparser.YP_SYNTAX_MINIMUM_INVALID) /* min-elements constraint not honored */
CVL_SYNTAX_MAXIMUM_INVALID = CVLRetCode(yparser.YP_SYNTAX_MAXIMUM_INVALID) /* max-elements constraint not honored */
CVL_SEMANTIC_DEPENDENT_DATA_MISSING = CVLRetCode(yparser.YP_SEMANTIC_DEPENDENT_DATA_MISSING) /* Dependent Data is missing */
CVL_SEMANTIC_MANDATORY_DATA_MISSING = CVLRetCode(yparser.YP_SEMANTIC_MANDATORY_DATA_MISSING) /* Mandatory Data is missing */
CVL_SEMANTIC_KEY_ALREADY_EXIST = CVLRetCode(yparser.YP_SEMANTIC_KEY_ALREADY_EXIST) /* Key already existing. */
CVL_SEMANTIC_KEY_NOT_EXIST = CVLRetCode(yparser.YP_SEMANTIC_KEY_NOT_EXIST) /* Key is missing. */
CVL_SEMANTIC_KEY_DUPLICATE = CVLRetCode(yparser.YP_SEMANTIC_KEY_DUPLICATE) /* Duplicate key. */
CVL_SEMANTIC_KEY_INVALID = CVLRetCode(yparser.YP_SEMANTIC_KEY_INVALID)
)
//Strcture for key and data in API
type CVLEditConfigData struct {
VType CVLValidateType //Validation type
VOp CVLOperation //Operation type
Key string //Key format : "PORT|Ethernet4"
Data map[string]string //Value : {"alias": "40GE0/28", "mtu" : 9100, "admin_status": down}
}
//CVL validations stats
//Maintain time stats for call to ValidateEditConfig().
//Hits : Total number of times ValidateEditConfig() called
//Time : Total time spent in ValidateEditConfig()
//Peak : Highest time spent in ValidateEditConfig()
type ValidationTimeStats struct {
Hits uint
Time time.Duration
Peak time.Duration
}
//Global data structure for maintaining validation stats
var cfgValidationStats ValidationTimeStats
var statsMutex *sync.Mutex
func Initialize() CVLRetCode {
if (cvlInitialized == true) {
//CVL has already been initialized
return CVL_SUCCESS
}
//Initialize redis Client
redisClient = redis.NewClient(&redis.Options{
Addr: ":6379",
Password: "", // no password set
DB: int(CONFIG_DB), // use APP DB
})
if (redisClient == nil) {
CVL_LOG(FATAL, "Unable to connect with Redis Config DB")
return CVL_ERROR
}
//Load lua script into redis
luaScripts = make(map[string]*redis.Script)
loadLuaScript(luaScripts)
yparser.Initialize()
modelInfo.modelNs = make(map[string]*modelNamespace) //redis table to model name
modelInfo.tableInfo = make(map[string]*modelTableInfo) //model namespace
modelInfo.allKeyDelims = make(map[string]bool) //all key delimiter
modelInfo.redisTableToYangList = make(map[string][]string) //Redis table to Yang list map
dbNameToDbNum = map[string]uint8{"APPL_DB": APPL_DB, "CONFIG_DB": CONFIG_DB}
// Load all YIN schema files
if retCode := loadSchemaFiles(); retCode != CVL_SUCCESS {
return retCode
}
//Compile leafref path
compileLeafRefPath()
//Compile all must exps
compileMustExps()
//Compile all when exps
compileWhenExps()
//Add all table names to be fetched to validate 'must' expression
addTableNamesForMustExp()
//Build reverse leafref info i.e. which table/field uses one table through leafref
buildRefTableInfo()
cvlInitialized = true
return CVL_SUCCESS
}
func Finish() {
yparser.Finish()
}
func ValidationSessOpen() (*CVL, CVLRetCode) {
cvl := &CVL{}
cvl.tmpDbCache = make(map[string]interface{})
cvl.requestCache = make(map[string]map[string][]*requestCacheType)
cvl.maxTableElem = make(map[string]int)
cvl.yp = &yparser.YParser{}
cvl.yv = &YValidator{}
cvl.yv.root = &xmlquery.Node{Type: xmlquery.DocumentNode}
if (cvl == nil || cvl.yp == nil) {
return nil, CVL_FAILURE
}
return cvl, CVL_SUCCESS
}
func ValidationSessClose(c *CVL) CVLRetCode {
c.yp.DestroyCache()
c = nil
return CVL_SUCCESS
}
func (c *CVL) ValidateStartupConfig(jsonData string) CVLRetCode {
//Check config data syntax
//Finally validate
return CVL_NOT_IMPLEMENTED
}
//Steps:
// Check config data syntax
// Fetch the depedent data
// Merge config and dependent data
// Finally validate
func (c *CVL) ValidateIncrementalConfig(jsonData string) CVLRetCode {
c.clearTmpDbCache()
var v interface{}
b := []byte(jsonData)
if err := json.Unmarshal(b, &v); err != nil {
return CVL_SYNTAX_ERROR
}
var dataMap map[string]interface{} = v.(map[string]interface{})
root, _ := c.translateToYang(&dataMap)
if root == nil {
return CVL_SYNTAX_ERROR
}
errObj := c.yp.ValidateSyntax(root, nil)
if yparser.YP_SUCCESS != errObj.ErrCode {
return CVL_FAILURE
}
//Add and fetch entries if already exists in Redis
for tableName, data := range dataMap {
for key, _ := range data.(map[string]interface{}) {
c.addTableEntryToCache(tableName, key)
}
}
existingData := c.fetchDataToTmpCache()
//Merge existing data for update syntax or checking duplicate entries
if (existingData != nil) {
if root, errObj = c.yp.MergeSubtree(root, existingData);
errObj.ErrCode != yparser.YP_SUCCESS {
return CVL_ERROR
}
}
//Clear cache
c.clearTmpDbCache()
//Perform validation
if cvlErrObj := c.validateCfgSemantics(c.yv.root); cvlErrObj.ErrCode != CVL_SUCCESS {
return cvlErrObj.ErrCode
}
return CVL_SUCCESS
}
//Validate data for operation
func (c *CVL) ValidateConfig(jsonData string) CVLRetCode {
c.clearTmpDbCache()
var v interface{}
b := []byte(jsonData)
if err := json.Unmarshal(b, &v); err == nil {
var value map[string]interface{} = v.(map[string]interface{})
root, _ := c.translateToYang(&value)
if root == nil {
return CVL_FAILURE
}
if (c.validate(root) != CVL_SUCCESS) {
return CVL_FAILURE
}
}
return CVL_SUCCESS
}
//Validate config data based on edit operation
func (c *CVL) ValidateEditConfig(cfgData []CVLEditConfigData) (cvlErr CVLErrorInfo, ret CVLRetCode) {
ts := time.Now()
defer func() {
if (cvlErr.ErrCode != CVL_SUCCESS) {
CVL_LOG(ERROR, "ValidateEditConfig() failed , %+v", cvlErr)
}
//Update validation time stats
updateValidationTimeStats(time.Since(ts))
}()
var cvlErrObj CVLErrorInfo
caller := ""
if (IsTraceSet()) {
pc := make([]uintptr, 10)
runtime.Callers(2, pc)
f := runtime.FuncForPC(pc[0])
caller = f.Name()
}
TRACE_LOG(INFO_TRACE, "ValidateEditConfig() called from %s() : %v", caller, cfgData)
if (SkipValidation() == true) {
CVL_LOG(INFO_TRACE, "Skipping CVL validation.")
return cvlErrObj, CVL_SUCCESS
}
//Type cast to custom validation cfg data
sliceHeader := *(*reflect.SliceHeader)(unsafe.Pointer(&cfgData))
custvCfg := *(*[]custv.CVLEditConfigData)(unsafe.Pointer(&sliceHeader))
c.clearTmpDbCache()
//c.yv.root.FirstChild = nil
//c.yv.root.LastChild = nil
//Step 1: Get requested data first
//add all dependent data to be fetched from Redis
requestedData := make(map[string]interface{})
cfgDataLen := len(cfgData)
for i := 0; i < cfgDataLen; i++ {
if (VALIDATE_ALL != cfgData[i].VType) {
continue
}
//Add config data item to be validated
tbl,key := c.addCfgDataItem(&requestedData, cfgData[i])
//Add to request cache
reqTbl, exists := c.requestCache[tbl]
if (exists == false) {
//Create new table key data
reqTbl = make(map[string][]*requestCacheType)
}
cfgDataItemArr, _ := reqTbl[key]
cfgDataItemArr = append(cfgDataItemArr, &requestCacheType{cfgData[i], nil})
reqTbl[key] = cfgDataItemArr
c.requestCache[tbl] = reqTbl
//Invalid table name or invalid key separator
if key == "" {
cvlErrObj.ErrCode = CVL_SYNTAX_ERROR
cvlErrObj.Msg = "Invalid table or key for " + cfgData[i].Key
cvlErrObj.CVLErrDetails = cvlErrorMap[cvlErrObj.ErrCode]
return cvlErrObj, CVL_SYNTAX_ERROR
}
switch cfgData[i].VOp {
case OP_CREATE:
//Check max-element constraint
if ret := c.checkMaxElemConstraint(OP_CREATE, tbl); ret != CVL_SUCCESS {
cvlErrObj.ErrCode = CVL_SYNTAX_ERROR
cvlErrObj.ErrAppTag = "too-many-elements"
cvlErrObj.Msg = "Max elements limit reached"
cvlErrObj.CVLErrDetails = cvlErrorMap[cvlErrObj.ErrCode]
cvlErrObj.ConstraintErrMsg = fmt.Sprintf("Max elements limit %v reached",
modelInfo.tableInfo[tbl].redisTableSize)
return cvlErrObj, CVL_SYNTAX_ERROR
}
case OP_UPDATE:
//Get the existing data from Redis to cache, so that final
//validation can be done after merging this dependent data
c.addTableEntryToCache(tbl, key)
case OP_DELETE:
if (len(cfgData[i].Data) > 0) {
//Check constraints for deleting field(s)
for field, _ := range cfgData[i].Data {
if (c.checkDeleteConstraint(cfgData, tbl, key, field) != CVL_SUCCESS) {
cvlErrObj.ErrCode = CVL_SEMANTIC_ERROR
cvlErrObj.Msg = "Delete constraint failed"
cvlErrObj.CVLErrDetails = cvlErrorMap[cvlErrObj.ErrCode]
return cvlErrObj, CVL_SEMANTIC_ERROR
}
}
} else {
//Entire entry to be deleted
//Update max-elements count
c.checkMaxElemConstraint(OP_DELETE, tbl)
//Now check delete constraints
if (c.checkDeleteConstraint(cfgData, tbl, key, "") != CVL_SUCCESS) {
cvlErrObj.ErrCode = CVL_SEMANTIC_ERROR
cvlErrObj.Msg = "Delete constraint failed"
cvlErrObj.CVLErrDetails = cvlErrorMap[cvlErrObj.ErrCode]
return cvlErrObj, CVL_SEMANTIC_ERROR
}
}
c.addTableEntryToCache(tbl, key)
}
}
if (IsTraceSet()) {
//Only for tracing
jsonData := ""
jsonDataBytes, err := json.Marshal(requestedData)
if (err == nil) {
jsonData = string(jsonDataBytes)
} else {
cvlErrObj.ErrCode = CVL_SYNTAX_ERROR
cvlErrObj.CVLErrDetails = cvlErrorMap[cvlErrObj.ErrCode]
return cvlErrObj, CVL_SYNTAX_ERROR
}
TRACE_LOG(TRACE_LIBYANG, "Requested JSON Data = [%s]\n", jsonData)
}
//Step 2 : Perform syntax validation only
yang, errN := c.translateToYang(&requestedData)
if (errN.ErrCode == CVL_SUCCESS) {
if cvlErrObj, cvlRetCode := c.validateSyntax(yang); cvlRetCode != CVL_SUCCESS {
return cvlErrObj, cvlRetCode
}
} else {
return errN,errN.ErrCode
}
//Step 3 : Check keys and perform semantics validation
for i := 0; i < cfgDataLen; i++ {
if (cfgData[i].VType != VALIDATE_ALL && cfgData[i].VType != VALIDATE_SEMANTICS) {
continue
}
tbl, key := splitRedisKey(cfgData[i].Key)
//Step 3.1 : Check keys
switch cfgData[i].VOp {
case OP_CREATE:
//Check key should not already exist
n, err1 := redisClient.Exists(cfgData[i].Key).Result()
if (err1 == nil && n > 0) {
//Check if key deleted and CREATE done in same session,
//allow to create the entry
deletedInSameSession := false
if tbl != "" && key != "" {
for _, cachedCfgData := range c.requestCache[tbl][key] {
if cachedCfgData.reqData.VOp == OP_DELETE {
deletedInSameSession = true
break
}
}
}
if deletedInSameSession == false {
CVL_LOG(ERROR, "\nValidateEditConfig(): Key = %s already exists", cfgData[i].Key)
cvlErrObj.ErrCode = CVL_SEMANTIC_KEY_ALREADY_EXIST
cvlErrObj.CVLErrDetails = cvlErrorMap[cvlErrObj.ErrCode]
return cvlErrObj, CVL_SEMANTIC_KEY_ALREADY_EXIST
} else {
TRACE_LOG(TRACE_CREATE, "\nKey %s is deleted in same session, " +
"skipping key existence check for OP_CREATE operation", cfgData[i].Key)
}
}
c.yp.SetOperation("CREATE")
case OP_UPDATE:
n, err1 := redisClient.Exists(cfgData[i].Key).Result()
if (err1 != nil || n == 0) { //key must exists
CVL_LOG(ERROR, "\nValidateEditConfig(): Key = %s does not exist", cfgData[i].Key)
cvlErrObj.ErrCode = CVL_SEMANTIC_KEY_NOT_EXIST
cvlErrObj.CVLErrDetails = cvlErrorMap[cvlErrObj.ErrCode]
return cvlErrObj, CVL_SEMANTIC_KEY_NOT_EXIST
}
c.yp.SetOperation("UPDATE")
case OP_DELETE:
n, err1 := redisClient.Exists(cfgData[i].Key).Result()
if (err1 != nil || n == 0) { //key must exists
CVL_LOG(ERROR, "\nValidateEditConfig(): Key = %s does not exist", cfgData[i].Key)
cvlErrObj.ErrCode = CVL_SEMANTIC_KEY_NOT_EXIST
cvlErrObj.CVLErrDetails = cvlErrorMap[cvlErrObj.ErrCode]
return cvlErrObj, CVL_SEMANTIC_KEY_NOT_EXIST
}
c.yp.SetOperation("DELETE")
}
yangListName := getRedisTblToYangList(tbl, key)
//Get the YANG validator node
var node *xmlquery.Node = nil
if (c.requestCache[tbl][key][0].yangData != nil) { //get the node for CREATE/UPDATE or DELETE operation
node = c.requestCache[tbl][key][0].yangData
} else {
//Find the node from YANG tree
node = c.moveToYangList(yangListName, key)
}
if (node == nil) {
CVL_LOG(WARNING, "Could not find data for semantic validation, " +
"table %s , key %s", tbl, key)
continue
}
//Step 3.2 : Run all custom validations
cvlErrObj= c.doCustomValidation(node, custvCfg, &custvCfg[i], yangListName,
tbl, key)
if cvlErrObj.ErrCode != CVL_SUCCESS {
return cvlErrObj,cvlErrObj.ErrCode
}
//Step 3.3 : Perform semantic validation
if cvlErrObj = c.validateSemantics(node, yangListName, key, &cfgData[i]);
cvlErrObj.ErrCode != CVL_SUCCESS {
return cvlErrObj,cvlErrObj.ErrCode
}
}
c.yp.DestroyCache()
return cvlErrObj, CVL_SUCCESS
}
/* Fetch the Error Message from CVL Return Code. */
func GetErrorString(retCode CVLRetCode) string{
return cvlErrorMap[retCode]
}
//Validate key only
func (c *CVL) ValidateKeys(key []string) CVLRetCode {
return CVL_NOT_IMPLEMENTED
}
//Validate key and data
func (c *CVL) ValidateKeyData(key string, data string) CVLRetCode {
return CVL_NOT_IMPLEMENTED
}
//Validate key, field and value
func (c *CVL) ValidateFields(key string, field string, value string) CVLRetCode {
return CVL_NOT_IMPLEMENTED
}
func (c *CVL) addDepEdges(graph *toposort.Graph, tableList []string) {
//Add all the depedency edges for graph nodes
for ti :=0; ti < len(tableList); ti++ {
redisTblTo := getYangListToRedisTbl(tableList[ti])
for tj :=0; tj < len(tableList); tj++ {
if (tableList[ti] == tableList[tj]) {
//same table, continue
continue
}
redisTblFrom := getYangListToRedisTbl(tableList[tj])
//map for checking duplicate edge
dupEdgeCheck := map[string]string{}
for _, leafRefs := range modelInfo.tableInfo[tableList[tj]].leafRef {
for _, leafRef := range leafRefs {
if (strings.Contains(leafRef.path, tableList[ti] + "_LIST")) == false {
continue
}
toName, exists := dupEdgeCheck[redisTblFrom]
if (exists == true) && (toName == redisTblTo) {
//Don't add duplicate edge
continue
}
//Add and store the edge in map
graph.AddEdge(redisTblFrom, redisTblTo)
dupEdgeCheck[redisTblFrom] = redisTblTo
CVL_LOG(INFO_DEBUG,
"addDepEdges(): Adding edge %s -> %s", redisTblFrom, redisTblTo)
}
}
}
}
}
//Sort list of given tables as per their dependency
func (c *CVL) SortDepTables(inTableList []string) ([]string, CVLRetCode) {
tableListMap := make(map[string]bool)
//Skip all unknown tables
for ti := 0; ti < len(inTableList); ti++ {
_, exists := modelInfo.tableInfo[inTableList[ti]]
if exists == false {
continue
}
//Add to map to avoid duplicate nodes
tableListMap[inTableList[ti]] = true
}
tableList := []string{}
//Add all the table names in graph nodes
graph := toposort.NewGraph(len(tableListMap))
for tbl, _ := range tableListMap {
graph.AddNodes(tbl)
tableList = append(tableList, tbl)
}
//Add all dependency egdes
c.addDepEdges(graph, tableList)
//Now perform topological sort
result, ret := graph.Toposort()
if ret == false {
return nil, CVL_ERROR
}
return result, CVL_SUCCESS
}
//Get the order list(parent then child) of tables in a given YANG module
//within a single model this is obtained using leafref relation
func (c *CVL) GetOrderedTables(yangModule string) ([]string, CVLRetCode) {
tableList := []string{}
//Get all the table names under this model
for tblName, tblNameInfo := range modelInfo.tableInfo {
if (tblNameInfo.modelName == yangModule) {
tableList = append(tableList, tblName)
}
}
return c.SortDepTables(tableList)
}
func (c *CVL) addDepTables(tableMap map[string]bool, tableName string) {
//Mark it is added in list
tableMap[tableName] = true
//Now find all tables referred in leafref from this table
for _, leafRefs := range modelInfo.tableInfo[tableName].leafRef {
for _, leafRef := range leafRefs {
for _, refTbl := range leafRef.yangListNames {
c.addDepTables(tableMap, getYangListToRedisTbl(refTbl)) //call recursively
}
}
}
}
//Get the list of dependent tables for a given table in a YANG module
func (c *CVL) GetDepTables(yangModule string, tableName string) ([]string, CVLRetCode) {
tableList := []string{}
tblMap := make(map[string]bool)
if _, exists := modelInfo.tableInfo[tableName]; exists == false {
CVL_LOG(INFO_DEBUG, "GetDepTables(): Unknown table %s\n", tableName)
return []string{}, CVL_ERROR
}
c.addDepTables(tblMap, tableName)
for tblName, _ := range tblMap {
tableList = append(tableList, tblName)
}
//Add all the table names in graph nodes
graph := toposort.NewGraph(len(tableList))
for ti := 0; ti < len(tableList); ti++ {
CVL_LOG(INFO_DEBUG, "GetDepTables(): Adding node %s\n", tableList[ti])
graph.AddNodes(tableList[ti])
}
//Add all dependency egdes
c.addDepEdges(graph, tableList)
//Now perform topological sort
result, ret := graph.Toposort()
if ret == false {
return nil, CVL_ERROR
}
return result, CVL_SUCCESS
}
//Get the dependent (Redis keys) to be deleted or modified
//for a given entry getting deleted
func (c *CVL) GetDepDataForDelete(redisKey string) ([]string, []string) {
tableName, key := splitRedisKey(redisKey)
if (tableName == "") || (key == "") {
CVL_LOG(INFO_DEBUG, "GetDepDataForDelete(): Unknown or invalid table %s\n",
tableName)
}
if _, exists := modelInfo.tableInfo[tableName]; exists == false {
CVL_LOG(INFO_DEBUG, "GetDepDataForDelete(): Unknown table %s\n", tableName)
return []string{}, []string{}
}
mCmd := map[string]*redis.StringSliceCmd{}
mFilterScripts := map[string]string{}
pipe := redisClient.Pipeline()
for _, refTbl := range modelInfo.tableInfo[tableName].refFromTables {
//check if ref field is a key
numKeys := len(modelInfo.tableInfo[refTbl.tableName].keys)
idx := 0
for ; idx < numKeys; idx++ {
if (modelInfo.tableInfo[refTbl.tableName].keys[idx] == refTbl.field) {
//field is key comp
mCmd[refTbl.tableName] = pipe.Keys(fmt.Sprintf("%s|*%s*",
refTbl.tableName, key)) //write into pipeline}
break
}
}
if (idx == numKeys) {
//field is hash-set field, not a key, match with hash-set field
//prepare the lua filter script
// ex: (h.members: == 'Ethernet4,' or (string.find(h['members@'], 'Ethernet4') != nil)
//',' to include leaf-list case
mFilterScripts[refTbl.tableName] =
fmt.Sprintf("return (h.%s == '%s') or " +
"h['ports@'] ~= nil and (string.find(h['%s@'], '%s') ~= nil)",
refTbl.field, key, refTbl.field, key)
}
}
_, err := pipe.Exec()
if err != nil {
CVL_LOG(ERROR, "Failed to fetch dependent key details for table %s", tableName)
}
pipe.Close()
//Add dependent keys which should be modified
depKeysForMod := []string{}
for tableName, mFilterScript := range mFilterScripts {
refKeys, err := luaScripts["filter_keys"].Run(redisClient, []string{},
tableName + "|*", "", mFilterScript).Result()
if (err != nil) {
CVL_LOG(ERROR, "Lua script error (%v)", err)
}
if (refKeys == nil) {
//No reference field found
continue
}
refKeysStr := string(refKeys.(string))
if (refKeysStr != "") {
//Add all keys whose fields to be updated
depKeysForMod = append(depKeysForMod,
strings.Split(refKeysStr, ",")...)
}
}
depKeys := []string{}
for tblName, keys := range mCmd {
res, err := keys.Result()
if (err != nil) {
CVL_LOG(ERROR, "Failed to fetch dependent key details for table %s", tblName)
continue
}
//Add keys found
depKeys = append(depKeys, res...)
//For each key, find dependent data for delete recursively
for i :=0; i< len(res); i++ {
retDepKeys, retDepKeysForMod := c.GetDepDataForDelete(res[i])
depKeys = append(depKeys, retDepKeys...)
depKeysForMod = append(depKeysForMod, retDepKeysForMod...)
}
}
return depKeys, depKeysForMod
}
//Update global stats for all sessions
func updateValidationTimeStats(td time.Duration) {
statsMutex.Lock()
cfgValidationStats.Hits++
if (td > cfgValidationStats.Peak) {
cfgValidationStats.Peak = td
}
cfgValidationStats.Time += td
statsMutex.Unlock()
}
//Retrieve global stats
func GetValidationTimeStats() ValidationTimeStats {
return cfgValidationStats
}
//Clear global stats
func ClearValidationTimeStats() {
statsMutex.Lock()
cfgValidationStats.Hits = 0
cfgValidationStats.Peak = 0
cfgValidationStats.Time = 0
statsMutex.Unlock()
}