19
19
20
20
package cvl
21
21
import (
22
- "fmt"
23
22
"github.com/antchfx/jsonquery"
24
23
"github.com/Azure/sonic-mgmt-common/cvl/internal/yparser"
25
24
//lint:ignore ST1001 This is safe to dot import for util package
@@ -33,15 +32,16 @@ func(c *CVL) addChildNode(tableName string, parent *yparser.YParserNode, name st
33
32
return c .yp .AddChildNode (modelInfo .tableInfo [tableName ].module , parent , name )
34
33
}
35
34
36
- func (c * CVL ) addChildLeaf (config bool , tableName string , parent * yparser.YParserNode , name string , value string ) {
35
+ func (c * CVL ) addChildLeaf (config bool , tableName string , parent * yparser.YParserNode , name string , value string , multileaf * [] * yparser. YParserLeafValue ) {
37
36
38
37
/* If there is no value then assign default space string. */
39
38
if len (value ) == 0 {
40
39
value = " "
41
40
}
42
41
43
42
//Batch leaf creation
44
- c .batchLeaf = c .batchLeaf + name + "#" + value + "#"
43
+ * multileaf = append (* multileaf , & yparser.YParserLeafValue {Name : name , Value : value })
44
+
45
45
//Check if this leaf has leafref,
46
46
//If so add the add redis key to its table so that those
47
47
// details can be fetched for dependency validation
@@ -50,7 +50,8 @@ func (c *CVL) addChildLeaf(config bool, tableName string, parent *yparser.YParse
50
50
}
51
51
52
52
func (c * CVL ) generateTableFieldsData (config bool , tableName string , jsonNode * jsonquery.Node ,
53
- parent * yparser.YParserNode ) CVLRetCode {
53
+ parent * yparser.YParserNode , multileaf * []* yparser.YParserLeafValue ) CVLErrorInfo {
54
+ var cvlErrObj CVLErrorInfo
54
55
55
56
//Traverse fields
56
57
for jsonFieldNode := jsonNode .FirstChild ; jsonFieldNode != nil ;
@@ -61,32 +62,35 @@ parent *yparser.YParserNode) CVLRetCode {
61
62
jsonFieldNode .FirstChild .Type == jsonquery .TextNode ) {
62
63
63
64
if (len (modelInfo .tableInfo [tableName ].mapLeaf ) == 2 ) {//mapping should have two leaf always
65
+ batchInnerListLeaf := make ([]* yparser.YParserLeafValue , 0 )
64
66
//Values should be stored inside another list as map table
65
67
listNode := c .addChildNode (tableName , parent , tableName ) //Add the list to the top node
66
68
c .addChildLeaf (config , tableName ,
67
69
listNode , modelInfo .tableInfo [tableName ].mapLeaf [0 ],
68
- jsonFieldNode .Data )
70
+ jsonFieldNode .Data , & batchInnerListLeaf )
69
71
70
72
c .addChildLeaf (config , tableName ,
71
73
listNode , modelInfo .tableInfo [tableName ].mapLeaf [1 ],
72
- jsonFieldNode .FirstChild .Data )
74
+ jsonFieldNode .FirstChild .Data , & batchInnerListLeaf )
73
75
76
+ if errObj := c .yp .AddMultiLeafNodes (modelInfo .tableInfo [tableName ].module , listNode , batchInnerListLeaf ); errObj .ErrCode != yparser .YP_SUCCESS {
77
+ cvlErrObj = createCVLErrObj (errObj )
78
+ CVL_LOG (ERROR , "Failed to create innner list leaf nodes, data = %v" , batchInnerListLeaf )
79
+ return cvlErrObj
80
+ }
74
81
} else {
75
82
//check if it is hash-ref, then need to add only key from "TABLE|k1"
76
83
hashRefMatch := reHashRef .FindStringSubmatch (jsonFieldNode .FirstChild .Data )
77
84
78
- if (hashRefMatch != nil && len (hashRefMatch ) == 3 ) {
79
- /*if (strings.HasPrefix(jsonFieldNode.FirstChild.Data, "[")) &&
80
- (strings.HasSuffix(jsonFieldNode.FirstChild.Data, "]")) &&
81
- (strings.Index(jsonFieldNode.FirstChild.Data, "|") > 0) {*/
85
+ if len (hashRefMatch ) == 3 {
82
86
83
87
c .addChildLeaf (config , tableName ,
84
88
parent , jsonFieldNode .Data ,
85
- hashRefMatch [2 ]) //take hashref key value
89
+ hashRefMatch [2 ], multileaf ) //take hashref key value
86
90
} else {
87
91
c .addChildLeaf (config , tableName ,
88
92
parent , jsonFieldNode .Data ,
89
- jsonFieldNode .FirstChild .Data )
93
+ jsonFieldNode .FirstChild .Data , multileaf )
90
94
}
91
95
}
92
96
@@ -99,28 +103,34 @@ parent *yparser.YParserNode) CVLRetCode {
99
103
arrayNode = arrayNode .NextSibling {
100
104
c .addChildLeaf (config , tableName ,
101
105
parent , jsonFieldNode .Data ,
102
- arrayNode .FirstChild .Data )
106
+ arrayNode .FirstChild .Data , multileaf )
103
107
}
104
108
}
105
109
}
106
110
107
- return CVL_SUCCESS
111
+ cvlErrObj .ErrCode = CVL_SUCCESS
112
+ return cvlErrObj
108
113
}
109
114
110
115
func (c * CVL ) generateTableData (config bool , jsonNode * jsonquery.Node )(* yparser.YParserNode , CVLErrorInfo ) {
111
116
var cvlErrObj CVLErrorInfo
112
117
113
- tableName := fmt .Sprintf ("%s" ,jsonNode .Data )
114
- c .batchLeaf = ""
118
+ tableName := jsonNode .Data
119
+ c .batchLeaf = nil
120
+ c .batchLeaf = make ([]* yparser.YParserLeafValue , 0 )
115
121
116
122
//Every Redis table is mapped as list within a container,
117
123
//E.g. ACL_RULE is mapped as
118
124
// container ACL_RULE { list ACL_RULE_LIST {} }
119
125
var topNode * yparser.YParserNode
120
126
121
127
// Add top most conatiner e.g. 'container sonic-acl {...}'
122
- if _ , exists := modelInfo .tableInfo [tableName ]; exists == false {
123
- return nil , cvlErrObj
128
+ if _ , exists := modelInfo .tableInfo [tableName ]; ! exists {
129
+ CVL_LOG (ERROR , "Schema details not found for %s" , tableName )
130
+ cvlErrObj .ErrCode = CVL_SYNTAX_ERROR
131
+ cvlErrObj .TableName = tableName
132
+ cvlErrObj .Msg = "Schema details not found"
133
+ return nil , cvlErrObj
124
134
}
125
135
topNode = c .yp .AddChildNode (modelInfo .tableInfo [tableName ].module ,
126
136
nil , modelInfo .tableInfo [tableName ].modelName )
@@ -147,7 +157,7 @@ func (c *CVL) generateTableData(config bool, jsonNode *jsonquery.Node)(*yparser.
147
157
//Find number of all key combinations
148
158
//Each key can have one or more key values, which results in nk1 * nk2 * nk2 combinations
149
159
idx := 0
150
- for i , _ := range keyValuePair {
160
+ for i := range keyValuePair {
151
161
totalKeyComb = totalKeyComb * len (keyValuePair [i ].values )
152
162
keyIndices = append (keyIndices , 0 )
153
163
}
@@ -165,11 +175,13 @@ func (c *CVL) generateTableData(config bool, jsonNode *jsonquery.Node)(*yparser.
165
175
for idx = 0 ; idx < keyCompCount ; idx ++ {
166
176
c .addChildLeaf (config , tableName ,
167
177
listNode , keyValuePair [idx ].key ,
168
- keyValuePair [idx ].values [keyIndices [idx ]])
178
+ keyValuePair [idx ].values [keyIndices [idx ]], & c . batchLeaf )
169
179
}
170
180
171
181
//Get all fields under the key field and add them as children of the list
172
- c .generateTableFieldsData (config , tableName , jsonNode , listNode )
182
+ if fldDataErrObj := c .generateTableFieldsData (config , tableName , jsonNode , listNode , & c .batchLeaf ); fldDataErrObj .ErrCode != CVL_SUCCESS {
183
+ return nil , fldDataErrObj
184
+ }
173
185
174
186
//Check which key elements left after current key element
175
187
var next int = keyCompCount - 1
@@ -188,13 +200,17 @@ func (c *CVL) generateTableData(config bool, jsonNode *jsonquery.Node)(*yparser.
188
200
keyIndices [idx ] = 0
189
201
}
190
202
191
- TRACE_LOG (INFO_API , TRACE_CACHE , "Starting batch leaf creation - %s \n " , c .batchLeaf )
203
+ TRACE_LOG (INFO_API , TRACE_CACHE , "Starting batch leaf creation - %v \n " , c .batchLeaf )
192
204
//process batch leaf creation
193
205
if errObj := c .yp .AddMultiLeafNodes (modelInfo .tableInfo [tableName ].module , listNode , c .batchLeaf ); errObj .ErrCode != yparser .YP_SUCCESS {
194
- cvlErrObj = CreateCVLErrObj (errObj )
195
- return nil , cvlErrObj
206
+ cvlErrObj = createCVLErrObj (errObj )
207
+ CVL_LOG (ERROR , "Failed to create leaf nodes, data = %v" ,
208
+ c .batchLeaf )
209
+ return nil , cvlErrObj
196
210
}
197
- c .batchLeaf = ""
211
+
212
+ c .batchLeaf = nil
213
+ c .batchLeaf = make ([]* yparser.YParserLeafValue , 0 )
198
214
}
199
215
}
200
216
0 commit comments