[YANGTOOLS-895] MAC address does not match regular expression pattern Created: 24/Aug/18  Updated: 27/Aug/18  Resolved: 27/Aug/18

Status: Resolved
Project: yangtools
Component/s: codecs
Affects Version/s: 2.0.6, 2.0.7, 2.0.10
Fix Version/s: None

Type: Bug Priority: Highest
Reporter: Michal Banik Assignee: Robert Varga
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

yang-data-codec-gson-2.0.6.jar



 Description   

Hello.

There is a problem with parsing MAC address from json

Caused by: java.lang.IllegalArgumentException: Value '00:00:00:00:00:00' does not match regular expression '^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$'
	at org.opendaylight.yangtools.yang.data.impl.codec.CompiledPatternContext.validate(CompiledPatternContext.java:48)
	at org.opendaylight.yangtools.yang.data.impl.codec.StringPatternCheckingCodec.validate(StringPatternCheckingCodec.java:39)
	at org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec.deserialize(StringStringCodec.java:43)
	at org.opendaylight.yangtools.yang.data.impl.codec.StringStringCodec.deserialize(StringStringCodec.java:22)
	at org.opendaylight.yangtools.yang.data.codec.gson.AbstractJSONCodec.parseValue(AbstractJSONCodec.java:34)
	at org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream.translateValueByType(JsonParserStream.java:341)
	at org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream.setValue(JsonParserStream.java:335)
	at org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream.read(JsonParserStream.java:240)
	at org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream.read(JsonParserStream.java:300)
	at org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream.parse(JsonParserStream.java:163)

But the pattern actually matches the string.

 

When java.util.regex.Pattern is created, I found out that it receives pattern string

^(?:\^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}\$)$

instead of what is defined in yang file

^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$

 

I am using 2.0.6 version but checked also 2.0.7 and 2.0.10 and it happened too.



 Comments   
Comment by Robert Varga [ 24/Aug/18 ]

What is the corresponding model?

Comment by Robert Varga [ 25/Aug/18 ]

This is not a bug, your model is wrong – YANG regular expressions are implicitly anchored and '^' and '$' are normal characters. Fix your models.

Comment by Michal Banik [ 27/Aug/18 ]

sorry I forgot to mention the model

 

I uses openconfig models openconfig-if-ethernet.yang - leaf mac-address has type oc-yang:mac-address from openconfig-yang-types.yang

mac-address is defined:

typedef mac-address {
    type string {
      pattern '^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$';
    }
    description
      "An IEEE 802 MAC address";
  }

 

I still think there is a bug.

 

Actually, the pattern string that is in yang model

final Pattern pattern = Pattern.compile("^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$");
pattern.matcher("00:00:00:00:00:00").matches();

returns

true

but, the pattern string that is actually set in pattern instance in CompiledPatternContext class

final Pattern pattern = Pattern.compile("^(?:\\^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}\\$)$");
pattern.matcher("00:00:00:00:00:00").matches();

returns

false

 

 

Comment by Robert Varga [ 27/Aug/18 ]

OpenConfig is violating RFC7950 section 9.4.5, as detailed in https://github.com/openconfig/public/issues/44 . In the context of YANG specification, pattern statement argument is interpreted according to https://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#regexs . That specifically means that both caret and ampersand are character literals – and for the purposes of use with java.util.regex.Pattern are escaped.

POSIX (used by OpenConfig), XSD (used by YANG) and java.util.regex.Pattern regular expressions, while sharing basic concepts and basic lexical structure in simple cases, are not interchangeable in the general sense.

Generated at Wed Feb 07 20:54:36 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.