前面有一篇介绍了NETCONF的消息结构是使用XML来描述的,但是由谁来给出这个消息结构的定义呢?目前居于这个位置的是一种叫做YANG的数据描述语言。

一点历史

我们来看一下这篇IETF的邮件列表归档[YANG Why NETCONF needs a data modeling language](https://mailarchive.ietf.org/arch/msg/yang/OCkpRWQrdsRLitqJF-P1sS3SAfg)。文中回答了几个问题,有一个这里需要特别点出:

Q4) Why isn’t XSD or RelaxNG good enough?

其实XML有自己的模式描述语言,从Wikipedia的XML Schema (W3C)可以看出,XML有至少XSD和RELAX NG这两种模式描述语言。对此,上面提到那篇归档邮件是这么解释的:

XSD and RelaxNG are both complicated.
Network operators are probably not trained programmers like
protocol developers. There are way more operators than there
are protocol developers, and they are much less likely to
be involved in the standards process, than the protocol developer.

The NETCONF WG evaluated XSD and RelaxNG last year.
Examples of data models including the DIFFSERV Info Model
were examined.  Nobody got excited about RNG.  For a real
data model, RNG seemed to be just as impenetrable as XSD.

Data model development in the NETCONF WG has been brutally
slow with XSD.  XSD is a good language to use if you want
to spend a long time learning what to do, and then spend
a long time fixing what you did wrong.  Relatively few
WG members have actually been able to learn either XSD or RNG.

这里只是引用别人的观点。总的一句话,就是XSD和RELAX NG太复杂了,收效甚差。

一些理解

YANG是什么

Wikipedia的解释,YANG是Yet Another Next Generation data modeling language。

YIN是什么

其实外国人对我们风水、阴阳这些慨念玩的挺嗨的。YIN在RFC6020里面的定义是YANG Independent Notation (YIN)。其实就是把YANG无缝地转为XML表示的格式。注意,这里转化的是YANG模型本身,而不是它所描述的数据。

数据模型和信息模型

数据模型叫做data model,信息模型叫做information model。 个人理解,

  • 数据模型通常用在解决方案侧,而信息模型用在原始问题侧
  • 数据模型表达的是intentions,而信息模型描述的是结构

YANG的作用是在解决方案侧提供对数据模型的描述。 其作用对象主要是XML结构的数据,也就是YANG的"on the wire"格式是XML。

XML是一种强结构,但是弱类型的数据描述方式。 XML可以以树状结构对数据进行表示,但是那颗树干上的叶子应该长什么样却却是在schema里面进行描述的。 如何将XML转化为其他类型的数据,也是需要通过其他方式,比如XSD进行描述。

YANG作为数据描述语言,可以生成用于约束XML数据的schema,所以YANG要回答的问题如下:

  • XML树中应当存在哪些枝叶,以及枝叶枝叶可能存在的联系
  • 每个枝干,每片叶子,或者一组叶子应该具有哪些属性,且如何被对待

定义了以上信息之后,给定一颗XML树,就可以判断此树是否符合某个YANG模型的定义了。

其他参考

(此处是底部)