-
Notifications
You must be signed in to change notification settings - Fork 82
/
cvl_syntax.go
286 lines (236 loc) · 10.4 KB
/
cvl_syntax.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
////////////////////////////////////////////////////////////////////////////////
// //
// 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 (
cmn "github.com/Azure/sonic-mgmt-common/cvl/common"
"github.com/Azure/sonic-mgmt-common/cvl/internal/yparser"
"github.com/antchfx/jsonquery"
//lint:ignore ST1001 This is safe to dot import for util package
. "github.com/Azure/sonic-mgmt-common/cvl/internal/util"
)
// This function should be called before adding any new entry
// Checks max-elements defined with (current number of entries
// getting added + entries already added and present in request
// cache + entries present in Redis DB)
func (c *CVL) checkMaxElemConstraint(op cmn.CVLOperation, tableName string, key string) CVLRetCode {
if (op != cmn.OP_CREATE) && (op != cmn.OP_DELETE) {
//Nothing to do, just return
return CVL_SUCCESS
}
tblListName := getRedisTblToYangList(tableName, key)
if modelInfo.tableInfo[tblListName].redisTableSize == -1 {
//No limit for table size
return CVL_SUCCESS
}
curSize, exists := c.maxTableElem[tableName]
if !exists { //fetch from Redis first time in the session
s := cmn.Search{Pattern: tableName + "|*"}
redisEntries, err := c.dbAccess.Count(s).Result()
if err != nil {
CVL_LOG(WARNING, "Unable to fetch current size of table %s from Redis, err= %v",
tableName, err)
return CVL_FAILURE
}
curSize = int(redisEntries)
//Store the current table size
c.maxTableElem[tableName] = curSize
}
if op == cmn.OP_DELETE {
//For delete operation we need to reduce the count.
//Because same table can be deleted and added back
//in same session.
if curSize > 0 {
c.maxTableElem[tableName] = (curSize - 1)
}
return CVL_SUCCESS
}
//Otherwise CREATE case
if curSize >= modelInfo.tableInfo[tblListName].redisTableSize {
CVL_LOG(WARNING, "%s table size has already reached to max-elements %d",
tableName, modelInfo.tableInfo[tblListName].redisTableSize)
return CVL_SYNTAX_ERROR
}
curSize = curSize + 1
if curSize > modelInfo.tableInfo[tblListName].redisTableSize {
//Does not meet the constraint
CVL_LOG(WARNING, "Max-elements check failed for table '%s',"+
" current size = %v, size in schema = %v",
tableName, curSize, modelInfo.tableInfo[tblListName].redisTableSize)
return CVL_SYNTAX_ERROR
}
//Update current size
c.maxTableElem[tableName] = curSize
return CVL_SUCCESS
}
// Add child node to a parent node
func (c *CVL) addChildNode(tableName string, parent *yparser.YParserNode, name string) *yparser.YParserNode {
//return C.lyd_new(parent, modelInfo.tableInfo[tableName].module, C.CString(name))
return c.yp.AddChildNode(modelInfo.tableInfo[tableName].module, parent, name)
}
func (c *CVL) addChildLeaf(config bool, tableName string, parent *yparser.YParserNode, name string, value string, multileaf *[]*yparser.YParserLeafValue) {
/* If there is no value then assign default space string. */
if len(value) == 0 {
value = " "
}
//Batch leaf creation
*multileaf = append(*multileaf, &yparser.YParserLeafValue{Name: name, Value: value})
}
func (c *CVL) generateTableFieldsData(config bool, tableName string, jsonNode *jsonquery.Node,
parent *yparser.YParserNode, multileaf *[]*yparser.YParserLeafValue) CVLErrorInfo {
var cvlErrObj CVLErrorInfo
//Traverse fields
for jsonFieldNode := jsonNode.FirstChild; jsonFieldNode != nil; jsonFieldNode = jsonFieldNode.NextSibling {
//Add fields as leaf to the list
if jsonFieldNode.Type == jsonquery.ElementNode &&
jsonFieldNode.FirstChild != nil &&
jsonFieldNode.FirstChild.Type == jsonquery.TextNode {
if len(modelInfo.tableInfo[tableName].mapLeaf) == 2 { //mapping should have two leaf always
batchInnerListLeaf := make([]*yparser.YParserLeafValue, 0)
//Values should be stored inside another list as map table
listNode := c.addChildNode(tableName, parent, tableName) //Add the list to the top node
c.addChildLeaf(config, tableName,
listNode, modelInfo.tableInfo[tableName].mapLeaf[0],
jsonFieldNode.Data, &batchInnerListLeaf)
c.addChildLeaf(config, tableName,
listNode, modelInfo.tableInfo[tableName].mapLeaf[1],
jsonFieldNode.FirstChild.Data, &batchInnerListLeaf)
if errObj := c.yp.AddMultiLeafNodes(modelInfo.tableInfo[tableName].module, listNode, batchInnerListLeaf); errObj.ErrCode != yparser.YP_SUCCESS {
cvlErrObj = createCVLErrObj(errObj, jsonNode)
CVL_LOG(WARNING, "Failed to create innner list leaf nodes, data = %v", batchInnerListLeaf)
return cvlErrObj
}
} else {
//check if it is hash-ref, then need to add only key from "TABLE|k1"
hashRefMatch := reHashRef.FindStringSubmatch(jsonFieldNode.FirstChild.Data)
if len(hashRefMatch) == 3 {
c.addChildLeaf(config, tableName,
parent, jsonFieldNode.Data,
hashRefMatch[2], multileaf) //take hashref key value
} else {
c.addChildLeaf(config, tableName,
parent, jsonFieldNode.Data,
jsonFieldNode.FirstChild.Data, multileaf)
}
}
} else if jsonFieldNode.Type == jsonquery.ElementNode &&
jsonFieldNode.FirstChild != nil &&
jsonFieldNode.FirstChild.Type == jsonquery.ElementNode {
//Array data e.g. VLAN members
for arrayNode := jsonFieldNode.FirstChild; arrayNode != nil; arrayNode = arrayNode.NextSibling {
c.addChildLeaf(config, tableName,
parent, jsonFieldNode.Data,
arrayNode.FirstChild.Data, multileaf)
}
}
}
cvlErrObj.ErrCode = CVL_SUCCESS
return cvlErrObj
}
func (c *CVL) generateTableData(config bool, jsonNode *jsonquery.Node) (*yparser.YParserNode, CVLErrorInfo) {
var cvlErrObj CVLErrorInfo
tableName := jsonNode.Data
origTableName := jsonNode.Data
topNodesAdded := false
c.batchLeaf = nil
c.batchLeaf = make([]*yparser.YParserLeafValue, 0)
//Every Redis table is mapped as list within a container,
//E.g. ACL_RULE is mapped as
// container ACL_RULE { list ACL_RULE_LIST {} }
var topNode *yparser.YParserNode
var listConatinerNode *yparser.YParserNode
//Traverse each key instance
for jsonNode = jsonNode.FirstChild; jsonNode != nil; jsonNode = jsonNode.NextSibling {
//For each field check if is key
//If it is key, create list as child of top container
// Get all key name/value pairs
if yangListName := getRedisTblToYangList(origTableName, jsonNode.Data); yangListName != "" {
tableName = yangListName
}
if _, exists := modelInfo.tableInfo[tableName]; !exists {
CVL_LOG(WARNING, "Schema details not found for %s", tableName)
cvlErrObj.ErrCode = CVL_SYNTAX_ERROR
cvlErrObj.TableName = origTableName
cvlErrObj.Msg = "Schema details not found"
return nil, cvlErrObj
}
if !topNodesAdded {
// Add top most conatiner e.g. 'container sonic-acl {...}'
topNode = c.yp.AddChildNode(modelInfo.tableInfo[tableName].module,
nil, modelInfo.tableInfo[tableName].modelName)
//Add the container node for each list
//e.g. 'container ACL_TABLE { list ACL_TABLE_LIST ...}
listConatinerNode = c.yp.AddChildNode(modelInfo.tableInfo[tableName].module,
topNode, origTableName)
topNodesAdded = true
}
keyValuePair := getRedisToYangKeys(tableName, jsonNode.Data)
keyCompCount := len(keyValuePair)
totalKeyComb := 1
var keyIndices []int
//Find number of all key combinations
//Each key can have one or more key values, which results in nk1 * nk2 * nk2 combinations
idx := 0
for i := range keyValuePair {
totalKeyComb = totalKeyComb * len(keyValuePair[i].values)
keyIndices = append(keyIndices, 0)
}
for ; totalKeyComb > 0; totalKeyComb-- {
//Get the YANG list name from Redis table name
//Ideally they are same except when one Redis table is split
//into multiple YANG lists
//Add table i.e. create list element
listNode := c.addChildNode(tableName, listConatinerNode, tableName+"_LIST") //Add the list to the top node
//For each key combination
//Add keys as leaf to the list
for idx = 0; idx < keyCompCount; idx++ {
c.addChildLeaf(config, tableName,
listNode, keyValuePair[idx].key,
keyValuePair[idx].values[keyIndices[idx]], &c.batchLeaf)
}
//Get all fields under the key field and add them as children of the list
if fldDataErrObj := c.generateTableFieldsData(config, tableName, jsonNode, listNode, &c.batchLeaf); fldDataErrObj.ErrCode != CVL_SUCCESS {
return nil, fldDataErrObj
}
//Check which key elements left after current key element
var next int = keyCompCount - 1
for (next > 0) && ((keyIndices[next] + 1) >= len(keyValuePair[next].values)) {
next--
}
//No more combination possible
if next < 0 {
break
}
keyIndices[next]++
//Reset indices for all other key elements
for idx = next + 1; idx < keyCompCount; idx++ {
keyIndices[idx] = 0
}
TRACE_LOG(TRACE_CACHE, "Starting batch leaf creation - %v\n", c.batchLeaf)
//process batch leaf creation
if errObj := c.yp.AddMultiLeafNodes(modelInfo.tableInfo[tableName].module, listNode, c.batchLeaf); errObj.ErrCode != yparser.YP_SUCCESS {
cvlErrObj = createCVLErrObj(errObj, jsonNode)
CVL_LOG(WARNING, "Failed to create leaf nodes, data = %v", c.batchLeaf)
return nil, cvlErrObj
}
c.batchLeaf = nil
c.batchLeaf = make([]*yparser.YParserLeafValue, 0)
}
}
return topNode, cvlErrObj
}