@@ -22,6 +22,7 @@ package cvl
22
22
import (
23
23
"strings"
24
24
"encoding/xml"
25
+ "encoding/json"
25
26
"github.com/antchfx/xmlquery"
26
27
"github.com/antchfx/jsonquery"
27
28
"github.com/Azure/sonic-mgmt-common/cvl/internal/yparser"
@@ -691,6 +692,97 @@ func (c *CVL) setOperation(op CVLOperation) {
691
692
}
692
693
}
693
694
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
+
694
786
//Check delete constraint for leafref if key/field is deleted
695
787
func (c * CVL ) checkDeleteConstraint (cfgData []CVLEditConfigData ,
696
788
tableName , keyVal , field string ) CVLRetCode {
0 commit comments