Skip to content

ygot Validate error, when yang leaf type is union #123

@songlinglingpml

Description

@songlinglingpml

Hi, @faraazbrcm

I am using version 202012
When I use restful to send delete messages, sometimes the following error occurs

URL:
restconf/data/openconfig-network-instance:network-instances/network-instance=DEFAULT/protocols/protocol=oc-pol-types:ISIS,DEFAULT/isis/interfaces/interface=Ethernet8/levels/level=2/afi-safi/af=oc-isis-types:IPV4,oc-isis-types:UNICAST/segment-routing/adjacency-sids/adjacency-sid=172.60.61.1,321536

Code1:

baseTreeNode, err := ytypes.GetNode(ygSchema.RootSchema(), deviceObj, path)
basePathObj, ok := (baseTreeNode[0].Data).(ygot.ValidatedGoStruct)
if ok {
err := basePathObj.Validate(&ytypes.LeafrefOptions{IgnoreMissingData: true})
......

Code2:
In the following code, 'fieldVal(321536)' can be converted to both int32 and string, which was committed in ygot patch (
49e5a22)

in sonic-mgmt-common/vendor/github.com/openconfig/ygot/ytypes/list.go
if keyLeafKind == yang.Yunion && strings.HasSuffix(ft.Name(), "_Union") {
	sks, err := getUnionKindsNotEnums(cschema)
	if err != nil {
		return err
	}
	for _, sk := range sks {
		gv, err := StringToType(reflect.TypeOf(yangBuiltinTypeToGoType(sk)), fieldVal)
		if err == nil {
			mn := "To_" + ft.Name()
			mapMethod := val.MethodByName(mn)
			if !mapMethod.IsValid() {
				return fmt.Errorf("%s does not have a %s function", val, mn)
			}
			ec := mapMethod.Call([]reflect.Value{gv})
			if len(ec) != 2 {
				return fmt.Errorf("%s %s function returns %d params", ft.Name(), mn, len(ec))
			}
			ei := ec[0].Interface()
			ee := ec[1].Interface()
			if ee != nil {
				return fmt.Errorf("unmarshaled %v type %T does not have a union type: %v", fieldVal, fieldVal, ee)
			}
			nv = reflect.ValueOf(ei)
			break
		}
	}
	...
}

error-message
"/device/network-instances/network-instance: /device/network-instances/network-instance/protocols/protocol: /device/network-instances/network-instance/protocols/protocol/isis/interfaces: /device/network-instances/network-instance/protocols/protocol/isis/interfaces/interface: /device/network-instances/network-instance/protocols/protocol/isis/interfaces/interface/levels/level: /device/network-instances/network-instance/protocols/protocol/isis/interfaces/interface/levels/level/afi-safi/af: /device/network-instances/network-instance/protocols/protocol/isis/interfaces/interface/levels/level/afi-safi/af/segment-routing/adjacency-sids: /device/network-instances/network-instance/protocols/protocol/isis/interfaces/interface/levels/level/afi-safi/af/segment-routing/adjacency-sids/adjacency-sid: "321536" does not match regular expression pattern "^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$" for schema "

yang info:

sid-id is in openconfig-segment-routing.yang,and it's type union

leaf sid-id {
  type oc-srt:sr-sid-type;
  description
    "The Segment Identifier to be used when advertising the IGP Prefix SID.";
}
typedef sr-sid-type {
type union {
type oc-mplst:mpls-label; ===》openconfig-mpls-types.yang
type oc-inet:ipv6-address; ===》openconfig-inet-types.yang
}
description
"The defined value of a segment identifier.";
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions