20
20
package cvl
21
21
import (
22
22
"encoding/json"
23
- "github.com/go-redis/redis"
23
+ "github.com/go-redis/redis/v7 "
24
24
//lint:ignore ST1001 This is safe to dot import for util package
25
25
. "github.com/Azure/sonic-mgmt-common/cvl/internal/util"
26
26
"github.com/Azure/sonic-mgmt-common/cvl/internal/yparser"
27
27
"time"
28
28
"runtime"
29
29
"github.com/antchfx/jsonquery"
30
+ "github.com/antchfx/xmlquery"
30
31
)
31
32
33
+ func (c * CVL ) addTableEntryToCache (tableName string , redisKey string ) {
34
+ if (tableName == "" || redisKey == "" ) {
35
+ return
36
+ }
37
+
38
+ if (c .tmpDbCache [tableName ] == nil ) {
39
+ c .tmpDbCache [tableName ] = map [string ]interface {}{redisKey : nil }
40
+ } else {
41
+ tblMap := c .tmpDbCache [tableName ]
42
+ tblMap .(map [string ]interface {})[redisKey ] = nil
43
+ c .tmpDbCache [tableName ] = tblMap
44
+ }
45
+ }
46
+
32
47
// Fetch dependent data from validated data cache,
33
48
// Returns the data and flag to indicate that if requested data
34
49
// is found in update request, the data should be merged with Redis data
@@ -37,20 +52,18 @@ func (c *CVL) fetchDataFromRequestCache(tableName string, key string) (d map[str
37
52
pd := & d
38
53
pm := & m
39
54
40
- TRACE_LOG (INFO_API , TRACE_CACHE ,
55
+ TRACE_LOG (TRACE_CACHE ,
41
56
"Returning data from request cache, data = %v, merge needed = %v" ,
42
57
* pd , * pm )
43
58
}()
44
59
45
60
cfgDataArr := c.requestCache [tableName ][key ]
46
- if (cfgDataArr != nil ) {
47
- for _ , cfgReqData := range cfgDataArr {
48
- //Delete request doesn't have depedent data
49
- if (cfgReqData .reqData .VOp == OP_CREATE ) {
50
- return cfgReqData .reqData .Data , false
51
- } else if (cfgReqData .reqData .VOp == OP_UPDATE ) {
52
- return cfgReqData .reqData .Data , true
53
- }
61
+ for _ , cfgReqData := range cfgDataArr {
62
+ //Delete request doesn't have depedent data
63
+ if (cfgReqData .reqData .VOp == OP_CREATE ) {
64
+ return cfgReqData .reqData .Data , false
65
+ } else if (cfgReqData .reqData .VOp == OP_UPDATE ) {
66
+ return cfgReqData .reqData .Data , true
54
67
}
55
68
}
56
69
@@ -60,7 +73,7 @@ func (c *CVL) fetchDataFromRequestCache(tableName string, key string) (d map[str
60
73
//Fetch given table entries using pipeline
61
74
func (c * CVL ) fetchTableDataToTmpCache (tableName string , dbKeys map [string ]interface {}) int {
62
75
63
- TRACE_LOG (INFO_API , TRACE_CACHE , "\n %v, Entered fetchTableDataToTmpCache" , time .Now ())
76
+ TRACE_LOG (TRACE_CACHE , "\n %v, Entered fetchTableDataToTmpCache" , time .Now ())
64
77
65
78
totalCount := len (dbKeys )
66
79
if (totalCount == 0 ) {
@@ -102,25 +115,27 @@ func (c *CVL) fetchTableDataToTmpCache(tableName string, dbKeys map[string]inter
102
115
redisKey := tableName + modelInfo .tableInfo [tableName ].redisKeyDelim + dbKey
103
116
//Check in validated cache first and add as dependent data
104
117
if entry , mergeNeeded := c .fetchDataFromRequestCache (tableName , dbKey ); (entry != nil ) {
105
- c .tmpDbCache [tableName ].(map [string ]interface {})[dbKey ] = entry
106
- entryFetched = entryFetched + 1
107
- //Entry found in validated cache, so skip fetching from Redis
108
- //if merging is not required with Redis DB
109
- if (mergeNeeded == false ) {
110
- continue
111
- }
118
+ entryFetched = entryFetched + 1
119
+ //Entry found in validated cache, so skip fetching from Redis
120
+ //if merging is not required with Redis DB
121
+ if ! mergeNeeded {
122
+ fieldMap := c .checkFieldMap (& entry )
123
+ c .tmpDbCache [tableName ].(map [string ]interface {})[dbKey ] = fieldMap
124
+ continue
125
+ }
126
+ c .tmpDbCache [tableName ].(map [string ]interface {})[dbKey ] = entry
112
127
}
113
128
114
129
//Otherwise fetch it from Redis
115
130
mCmd [dbKey ] = pipe .HGetAll (redisKey ) //write into pipeline
116
131
if mCmd [dbKey ] == nil {
117
- CVL_LOG (ERROR , "Failed pipe.HGetAll('%s')" , redisKey )
132
+ CVL_LOG (WARNING , "Failed pipe.HGetAll('%s')" , redisKey )
118
133
}
119
134
}
120
135
121
136
_ , err := pipe .Exec ()
122
137
if err != nil {
123
- CVL_LOG (ERROR , "Failed to fetch details for table %s" , tableName )
138
+ CVL_LOG (WARNING , "Failed to fetch details for table %s" , tableName )
124
139
return 0
125
140
}
126
141
pipe .Close ()
@@ -130,6 +145,11 @@ func (c *CVL) fetchTableDataToTmpCache(tableName string, dbKeys map[string]inter
130
145
131
146
for key , val := range mCmd {
132
147
res , err := val .Result ()
148
+
149
+ if (mapTable == nil ) {
150
+ break
151
+ }
152
+
133
153
if (err != nil || len (res ) == 0 ) {
134
154
//no data found, don't keep blank entry
135
155
delete (mapTable .(map [string ]interface {}), key )
@@ -155,14 +175,14 @@ func (c *CVL) fetchTableDataToTmpCache(tableName string, dbKeys map[string]inter
155
175
runtime .Gosched ()
156
176
}
157
177
158
- TRACE_LOG (INFO_API , TRACE_CACHE ,"\n %v, Exiting fetchTableDataToTmpCache" , time .Now ())
178
+ TRACE_LOG (TRACE_CACHE ,"\n %v, Exiting fetchTableDataToTmpCache" , time .Now ())
159
179
160
180
return entryFetched
161
181
}
162
182
163
183
//populate redis data to cache
164
184
func (c * CVL ) fetchDataToTmpCache () * yparser.YParserNode {
165
- TRACE_LOG (INFO_API , TRACE_CACHE , "\n %v, Entered fetchToTmpCache" , time .Now ())
185
+ TRACE_LOG (TRACE_CACHE , "\n %v, Entered fetchToTmpCache" , time .Now ())
166
186
167
187
entryToFetch := 0
168
188
var root * yparser.YParserNode = nil
@@ -191,7 +211,7 @@ func (c *CVL) fetchDataToTmpCache() *yparser.YParserNode {
191
211
if Tracing {
192
212
jsonDataBytes , _ := json .Marshal (c .tmpDbCache )
193
213
jsonData := string (jsonDataBytes )
194
- TRACE_LOG (INFO_API , TRACE_CACHE , "Top Node=%v\n " , jsonData )
214
+ TRACE_LOG (TRACE_CACHE , "Top Node=%v\n " , jsonData )
195
215
}
196
216
197
217
data , err := jsonquery .ParseJsonMap (& c .tmpDbCache )
@@ -202,7 +222,7 @@ func (c *CVL) fetchDataToTmpCache() *yparser.YParserNode {
202
222
203
223
//Build yang tree for each table and cache it
204
224
for jsonNode := data .FirstChild ; jsonNode != nil ; jsonNode = jsonNode .NextSibling {
205
- TRACE_LOG (INFO_API , TRACE_CACHE , "Top Node=%v\n " , jsonNode .Data )
225
+ TRACE_LOG (TRACE_CACHE , "Top Node=%v\n " , jsonNode .Data )
206
226
//Visit each top level list in a loop for creating table data
207
227
topNode , _ := c .generateTableData (true , jsonNode )
208
228
if (root == nil ) {
@@ -212,15 +232,46 @@ func (c *CVL) fetchDataToTmpCache() *yparser.YParserNode {
212
232
return nil
213
233
}
214
234
}
235
+
236
+ //Generate YANG data for Yang Validator
237
+ topYangNode , cvlYErrObj := c .generateYangListData (jsonNode , true )
238
+ if topYangNode == nil {
239
+ cvlYErrObj .ErrCode = CVL_SYNTAX_ERROR
240
+ CVL_LOG (WARNING , "Unable to translate cache data to YANG format" )
241
+ return nil
242
+ }
243
+
244
+ //Create a full document and merge with main YANG data
245
+ doc := & xmlquery.Node {Type : xmlquery .DocumentNode }
246
+ doc .FirstChild = topYangNode
247
+ doc .LastChild = topYangNode
248
+ topYangNode .Parent = doc
249
+
250
+ if (IsTraceLevelSet (TRACE_CACHE )) {
251
+ TRACE_LOG (TRACE_CACHE , "Before cache merge = %s, source = %s" ,
252
+ c .yv .root .OutputXML (false ),
253
+ doc .OutputXML (false ))
254
+ }
255
+
256
+ if c .mergeYangData (c .yv .root , doc ) != CVL_SUCCESS {
257
+ CVL_LOG (WARNING , "Unable to merge translated YANG data while " +
258
+ "translating from cache data to YANG format" )
259
+ cvlYErrObj .ErrCode = CVL_SYNTAX_ERROR
260
+ return nil
261
+ }
262
+ if (IsTraceLevelSet (TRACE_CACHE )) {
263
+ TRACE_LOG (TRACE_CACHE , "After cache merge = %s" ,
264
+ c .yv .root .OutputXML (false ))
265
+ }
215
266
}
216
267
} // until all dependent data is fetched
217
268
218
269
if root != nil && Tracing {
219
270
dumpStr := c .yp .NodeDump (root )
220
- TRACE_LOG (INFO_API , TRACE_CACHE , "Dependent Data = %v\n " , dumpStr )
271
+ TRACE_LOG (TRACE_CACHE , "Dependent Data = %v\n " , dumpStr )
221
272
}
222
273
223
- TRACE_LOG (INFO_API , TRACE_CACHE , "\n %v, Exiting fetchToTmpCache" , time .Now ())
274
+ TRACE_LOG (TRACE_CACHE , "\n %v, Exiting fetchToTmpCache" , time .Now ())
224
275
return root
225
276
}
226
277
@@ -230,5 +281,3 @@ func (c *CVL) clearTmpDbCache() {
230
281
delete (c .tmpDbCache , key )
231
282
}
232
283
}
233
-
234
-
0 commit comments