@@ -22,6 +22,7 @@ package cvl
2222import (
2323 "strings"
2424 "encoding/xml"
25+ "encoding/json"
2526 "github.com/antchfx/xmlquery"
2627 "github.com/antchfx/jsonquery"
2728 "github.com/Azure/sonic-mgmt-common/cvl/internal/yparser"
@@ -691,6 +692,97 @@ func (c *CVL) setOperation(op CVLOperation) {
691692 }
692693}
693694
695+ //Add given YANG data buffer to Yang Validator
696+ //redisKeys - Set of redis keys
697+ //redisKeyFilter - Redis key filter in glob style pattern
698+ //keyNames - Names of all keys separated by "|"
699+ //predicate - Condition on keys/fields
700+ //fields - Fields to retrieve, separated by "|"
701+ //Return "," separated list of leaf nodes if only one leaf is requested
702+ //One leaf is used as xpath query result in other nested xpath
703+ func (c * CVL ) addDepYangData (redisKeys []string , redisKeyFilter ,
704+ keyNames , predicate , fields , count string ) string {
705+
706+ var v interface {}
707+ tmpPredicate := ""
708+
709+ //Get filtered Redis data based on lua script
710+ //filter derived from Xpath predicate
711+ if (predicate != "" ) {
712+ tmpPredicate = "return (" + predicate + ")"
713+ }
714+
715+ cfgData , err := luaScripts ["filter_entries" ].Run (redisClient , []string {},
716+ redisKeyFilter , keyNames , tmpPredicate , fields , count ).Result ()
717+
718+ singleLeaf := "" //leaf data for single leaf
719+
720+ TRACE_LOG (INFO_API , TRACE_SEMANTIC , "addDepYangData() with redisKeyFilter=%s, " +
721+ "predicate=%s, fields=%s, returned cfgData = %s, err=%v" ,
722+ redisKeyFilter , predicate , fields , cfgData , err )
723+
724+ if (cfgData == nil ) {
725+ return ""
726+ }
727+
728+ //Parse the JSON map received from lua script
729+ b := []byte (cfgData .(string ))
730+ if err := json .Unmarshal (b , & v ); err != nil {
731+ return ""
732+ }
733+
734+ var dataMap map [string ]interface {} = v .(map [string ]interface {})
735+
736+ dataTop , _ := jsonquery .ParseJsonMap (& dataMap )
737+
738+ for jsonNode := dataTop .FirstChild ; jsonNode != nil ; jsonNode = jsonNode .NextSibling {
739+ //Generate YANG data for Yang Validator from Redis JSON
740+ topYangNode , _ := c .generateYangListData (jsonNode , false )
741+
742+ if topYangNode == nil {
743+ continue
744+ }
745+
746+ if (topYangNode .FirstChild != nil ) &&
747+ (topYangNode .FirstChild .FirstChild != nil ) {
748+ //Add attribute mentioning that data is from db
749+ addAttrNode (topYangNode .FirstChild .FirstChild , "db" , "" )
750+ }
751+
752+ //Build single leaf data requested
753+ singleLeaf = ""
754+ for redisKey := topYangNode .FirstChild .FirstChild ;
755+ redisKey != nil ; redisKey = redisKey .NextSibling {
756+
757+ for field := redisKey .FirstChild ; field != nil ;
758+ field = field .NextSibling {
759+ if (field .Data == fields ) {
760+ //Single field requested
761+ singleLeaf = singleLeaf + field .FirstChild .Data + ","
762+ break
763+ }
764+ }
765+ }
766+
767+ //Merge with main YANG data cache
768+ doc := & xmlquery.Node {Type : xmlquery .DocumentNode }
769+ doc .FirstChild = topYangNode
770+ doc .LastChild = topYangNode
771+ topYangNode .Parent = doc
772+ if c .mergeYangData (c .yv .root , doc ) != CVL_SUCCESS {
773+ continue
774+ }
775+ }
776+
777+
778+ //remove last comma in case mulitple values returned
779+ if (singleLeaf != "" ) {
780+ return singleLeaf [:len (singleLeaf ) - 1 ]
781+ }
782+
783+ return ""
784+ }
785+
694786//Check delete constraint for leafref if key/field is deleted
695787func (c * CVL ) checkDeleteConstraint (cfgData []CVLEditConfigData ,
696788 tableName , keyVal , field string ) CVLRetCode {
0 commit comments