自定义运行 Junos OS 的设备上的 YANG RPC 输出
您可以在 YANG 中为运行 Junos OS 的设备创建自定义 RPC。这样您便可精确定义输入参数和操作以及输出字段,并格式化运行 Junos OS 的设备上的特定操作任务。
在运行 Junos OS 的设备上执行 RPC 时,将 RPC 回复作为 XML 文档返回。Junos OS 输出定义语言 (ODL) 定义了 XML 数据转换为在 CLI 中执行命令或以文本格式请求 RPC 输出时显示的格式化 ASCII 输出。Junos OS ODL 扩展模块定义 Junos OS ODL 的 YANG 扩展,您可以在自定义 RPC 中包括该扩展,以便为输出指定 CLI 格式。有关在 RPC output
语句中的所有语句及其放置的摘要,请参阅 了解用于格式化 RPC 输出的 Junos OS YANG 扩展。
从 Junos OS 17.3 版开始,用于自定义 RPC 的 CLI 格式在扩展语句中 junos-odl:format
定义。在早期版本中,CLI 格式使用包含语句的 junos-odl:cli-format
容器进行定义。
以下部分概述如何使用 Junos OS ODL 扩展语句。同一部分中会显示与此密切相关的语句,在某些情况下,多个部分中可能包含一个语句。示例假设密闭 YANG 模块导入 Junos OS ODL 扩展模块并将其绑定到 junos-odl
前缀。这些示例使用 format
Junos OS 版本 17.3 中引入的语句来定义 CLI 格式。
空线
当 RPC 回复返回多个实体的相同一组信息时,语 blank-line
句会在每个数据重复之间插入一行。例如,如果 RPC 回复返回多个接口的数据,格式化的 ASCII 输出会在每个接口的数据集之间插入一条空线。
Physical interface: so-1/1/0, Enabled, Physical link is Down Interface index: 11, SNMP ifIndex: 41 ... Active defects : LOL, LOF, LOS, SEF, AIS-L, AIS-P Physical interface: so-1/1/1, Enabled, Physical link is Down Interface index: 12, SNMP ifIndex: 42 ... Active defects : LOL, LOF, LOS, SEF, AIS-L, AIS-P
要在每个实体的数据集之间插入空线,请将语句直接包含 blank-line
在 format
语句下。
rpc get-xyz-information { output { container xyz-information { // leaf definitions junos-odl:format xyz-information-format { junos-odl:blank-line; // CLI formatting } } } }
利用
该 capitalize
语句将节点值的第一个词在输出字段中大写。它不影响节点正式名称的资本化。例如,如果 RPC 输出中包含 state
具有该值 online
的节点,则该 capitalize
语句会使值在输出中大写。
State: Online
要将节点值的第一个词大写,请在 capitalize
语句中 format
包含语句。语句的放置决定了语句的范围,以及它是否影响单个字段、单行中的所有字段或所有行。
rpc get-xyz-information { output { container xyz-information { leaf state { junos-odl:formal-name "State"; type string; description "Interface state"; } junos-odl:format xyz-information-format { junos-odl:header "xyz information\n"; junos-odl:line { junos-odl:field "state" { junos-odl:leading; junos-odl:colon; junos-odl:capitalize; } } } } } }
cli 格式
在运行 Junos OS 的设备上执行 RPC 时,将 RPC 回复作为 XML 文档返回。RPC output
语句下的容器和叶节点在 RPC 回复中转换为 XML 元素。在运行 Junos OS 的设备的 YANG RPC 中,您还可以定义自定义格式的 ASCII 输出,当您在 Junos OS 命令行界面 (CLI) 上执行 RPC 或以文本格式请求 RPC 输出时显示。
在 Junos OS 版本 17.2 和早期版本中,要为特定 RPC 输出容器创建自定义命令输出,请创建包含 cli-format
语句的儿童容器。语 cli-format
句表示密闭容器定义了父层容器的 CLI 格式,并且此容器不应作为 RPC 回复 XML 数据中的节点包括在内。在格式化容器中,将父层容器的数据映射到输出字段,并使用 Junos OS ODL 扩展模块中的语句指定如何显示该父层容器的输出。
rpc get-xyz-information { output { container xyz-information { // leaf definitions container xyz-information-format { junos-odl:cli-format; // CLI formatting for the parent container } } } }
要为 Junos OS 版本 17.3 和更高版本中的特定 RPC 输出容器创建自定义命令输出,请参阅 格式。
冒号、正式名称和领先
节点的正式名称或标签是输出中节点内容之前的文本,当 leading
该节点输出字段的格式说明中包含该语句时。要为节点创建标签,必须在叶节点的定义中包含 formal-name
语句。在以下示例中 version
,节点有正式名称 Version
:
rpc get-xyz-information { output { container xyz-information { leaf version { junos-odl:formal-name "Version"; type string; description "Version"; } ... } } }
该 colon
语句在输出字段中插入节点标签后的结肠。如果格式化说明同时 colon
包含和 leading
语句,则节点的标签和结肠插入节点在输出中的值之前。例如:
Version: value
要在输出字段中插入标签和结肠,请在语句中format
包含leading
和colon
语句。语句的放置决定了语句的范围以及语句是否影响单个字段、单行中的所有字段或所有行。
rpc get-xyz-information { output { container xyz-information { leaf version { junos-odl:formal-name "Version"; type string; description "Version"; } junos-odl:format xyz-information-format { junos-odl:line { junos-odl:field "version" { junos-odl:colon; junos-odl:leading; } } } } } }
执行 RPC 时,该字段的输出中包含标签和结肠。
Version: value
逗号
该 comma
语句将逗号附加到节点的输出值。它与语 space
句一起用于在一行输出中创建逗号划定的字段。例如:
value1, Label2: value2, value3
要在输出字段中生成逗号和节点值之后的空间,请在语句中format
包含comma
和space
语句。语句中format
语句的放置决定了范围。将语句放置在单个字段中只会生成该字段的逗号和空间。将语句直接放在语句下format
将格式应用于所有字段。
rpc get-xyz-information { output { container xyz-information { leaf version { type string; description "Version"; } // additional leaf definitions junos-odl:format xyz-information-format { junos-odl:comma; junos-odl:space; junos-odl:line { junos-odl:field "version"; // additional fields } } } } }
如果以此示例中的格式说明省略 comma
语句,则字段仅使用一个空间分隔。Junos OS 会在输出线的最后一个字段之后自动省略逗号和空间。
默认文本
当缺少对应于输出字段的节点时,语 default-text
句会指定显示文本。
要在缺少映射到字段的节点时,定义以格式化 ASCII 输出显示的字符串,请在该节点的default-text
field
语句中包含语句和字符串。
rpc get-xyz-information { output { container xyz-information { leaf my-model { type string; description "Model"; } junos-odl:format xyz-information-format { junos-odl:line { junos-odl:field "my-model" { junos-odl:default-text "Model number not available."; } } } } } }
当 RPC 回复中缺少节点时,CLI 输出将显示默认文本。
Model number not available.
设备仅在缺少节点时显示默认文本。它不会显示存在但空的节点的文本。
明确
语 explicit
句仅在 Junos OS RPC 中使用,不能包含在自定义 RPC 中。
字段和线路
field
和line
语句定义 RPC 格式化 ASCII 输出中的行以及这些线路中的字段。这些语句也可与语picture
句一起使用,以便创建结构化程度更高的表,定义严格的列宽度和文本理由。
要在格式化的 ASCII 输出中定义一行,请在语句中包含 line
语 format
句。语 line
句中包含 field
将输出树中的叶节点映射到线路中的字段的语句。语 field
句的论点是叶标识符。字段的发出顺序必须与定义叶语句相同。
以下 RPC 的 CLI 输出是具有三个值的单行。请注意,您可以在 和 line
语句中field
包含其他 ODL 语句,以便分别自定义单个字段或该行内所有字段的格式。
rpc get-xyz-information { output { container xyz-information { leaf my-version { type string; description "Version"; } leaf my-model { type string; description "Model"; } leaf comment { type string; description "Comment"; } junos-odl:format xyz-information-format { junos-odl:comma; junos-odl:space; junos-odl:line { junos-odl:field "my-version" { junos-odl:capitalize; } junos-odl:field "my-model"; junos-odl:field "comment"; } } } } }
字段覆盖和字写
当线路宽度大于显示器宽度时,这些 fieldwrap
和 wordwrap
语句使您能够更合乎逻辑地包装内容。默认情况下,延伸至显示屏边缘的内容将在其符合适当边距的点包装,无需担心单词边界。
当当前线路过长,延伸至显示的右边缘时,该 fieldwrap
语句会将字段的完整内容包裹到下一行。如果不使用此语句,字符串将自动包装,但不考虑适当的单词中断或普遍幅度。
考虑以下输出行:
Output errors: Carrier transitions: 1, Errors: 0, Collisions: 0, Drops: 0, Aged packets: 0
如果显示屏比平常更窄,则线条可以包裹在单词的中间,如以下示例输出所示:
Output errors: Carrier transitions: 1, Errors: 0, Collisions: 0, Dro ps: 0, Aged packets: 0
fieldwrap
当语句包含在字段中时,整个字段将移至下一行。
Output errors: Carrier transitions: 1, Errors: 0, Collisions: 0, Drops: 0, Aged packets: 0
语 wordwrap
句仅在表最右侧列上使用,以便在当前行过长时将多字值部分包裹到后续行。这实际上创建了一列文本。在以下示例中,语 wordwrap
句将说明字符串划分在单词边界上:
Packet type Total Last 5 seconds Description Sent Received Sent Received Hello 0 0 4 5 Establish and maintain neighbor relationships. DbD 20 25 0 0 (Database description packets) Describe the contents of the topological database. LSReq 6 5 0 0 (Link-State Request packets) Request a precise instance of the database.
要改善 RPC 格式化 ASCII 输出中的包装行为,请在每个字段的格式说明中包含 fieldwrap
该语句。要将最右侧的列包入表中,请将语句包含 wordwrap
在最右侧字段的格式说明中。
rpc get-xyz-information { output { container xyz-information { leaf version { type string; description "Version"; } leaf desc { type string; description "Description"; } junos-odl:format xyz-information-format { junos-odl:picture "@<<<<<<<<<<<<<<@<<<<<<<<<<<<<<"; junos-odl:line { junos-odl:field "version" { junos-odl:fieldwrap; } junos-odl:field "desc" { junos-odl:wordwrap; } } } } } }
浮子、标头、图片和截断
您可以在 RPC 格式化的 ASCII 输出中创建表,方法是定义语句、picture
语句和一个header
或多个line
语句。可选header
语句定义表的列标题,但它也可以定义常规文本。该picture
语句以图形方式描绘了列在表中的放置、理由和宽度。field
和line
语句定义表行及其字段。
语句的 picture
论点是一个字符串,其中包含的字符串小于 (<),大于 (>) 和垂直条 (|) 符号,用于定义表列的放置、理由和宽度。@ 符号定义字段中可能占用的值的列中最左侧的位置。<、>和|符号分别指示左、右和中心理由。重复<、>或|符号定义列宽度。 表 1 汇总了这些符号。您也可在列之间插入一个或多个空格。
象征 |
描述 |
---|---|
@ |
在字段中的值可以占用的列中定义最左边的位置。 |
| |
以字段内容为中心。重复符号定义列宽度。 |
< |
左侧指定字段的内容。重复符号定义列宽度。 |
> |
正确地为字段内容提供正当理由。重复符号定义列宽度。 |
以下 picture
语句定义了左侧合理列、一个中心列和一个右侧合理列,每六个字符宽,并由一个空间分隔:
junos-odl:picture " @<<<<< @||||| @>>>>>";
要定义表行,请将语 line
句包括在内,然后将叶节点映射到线路中的字段。语 field
句的论点是叶标识符。
junos-odl:line { junos-odl:field "slot"; junos-odl:field "state"; junos-odl:field "comment"; }
当表字段必须包含几个相互排斥的值之一时,您可以对每个潜在价值在语句中 picture
重复 @ 符号,并且在第一个值之后将每个相互排他值的语句包含 float
在语句中 field
。然后,如果第一个元素没有值,则会对带有语句的 float
后续可能元素进行测试,直至返回值。该值浮入由第一个 @ 符号定义的位置,而不是留下空字段。
例如,以下 picture
语句会使输出在第二列中包含两个相互排斥的值之一:
junos-odl:picture " @<<<<< @@<<<<<"; junos-odl:line { junos-odl:field "slot"; junos-odl:field "state"; junos-odl:field "comment"{ junos-odl:float; } }
如果您知道在某些情况下可能缺少对应于特定表字段的标记,并且您希望消除额外的空白空间,您也可以使用 float
该语句。
该 truncate
语句保证字段的值不会超过语句定义 picture
的列的宽度。语 truncate
句会使输出省略节点值中可能导致其超过字段宽度的任何字符。如果省略语 truncate
句,并且输出超过字段宽度,则显示完整内容,这可能会扭曲表。您应小心使用此语句,特别是数字,因为输出不提供任何值被截断的迹象。
为以下 RPC 格式化的 CLI 定义了带有两列的小表。字 comment
段包括 float
和 truncate
语句。 state
如果输出元素包含值,则该值放置在第二列中。但是,如果 state
输出元素为空,则该节点的值 comment
(如果存在)包含在表中并移至第二列。如果评论超过该列的宽度,则其被截断以适应列宽度。
rpc get-xyz-information { output { container xyz-information { leaf slot { type string; description "Slot number"; } leaf state { type string; description "State"; } leaf comment { type string; } junos-odl:format xyz-information-format { junos-odl:header "Slot State \n"; junos-odl:picture "@<<<<< @@|||||||||||||||||||"; junos-odl:line { junos-odl:field "slot"; junos-odl:field "state"; junos-odl:field "comment"{ junos-odl:float; junos-odl:truncate; } } } } } }
格式
在运行 Junos OS 的设备上执行 RPC 时,将 RPC 回复作为 XML 文档返回。RPC output
语句下的容器和叶节点在 RPC 回复中转换为 XML 元素。在运行 Junos OS 的设备的 YANG RPC 中,您还可以定义自定义格式的 ASCII 输出,当您在 Junos OS 命令行界面 (CLI) 上执行 RPC 或以文本格式请求 RPC 输出时显示。
从 Junos OS 版本 17.3 开始,要为特定 RPC 输出容器创建自定义命令输出,请定义语 format
句。该 format
语句定义父层容器的 CLI 格式,不包含在 RPC 回复的 XML 数据中作为节点。在语 format
句中,将父层容器的数据映射到输出字段,并使用 Junos OS ODL 扩展模块中的语句指定如何显示该父层容器的输出。
rpc get-xyz-information { output { container xyz-information { // leaf definitions junos-odl:format xyz-information-format { // CLI formatting for the parent container } } } }
要为 Junos OS 版本 17.2 和早期版本中的特定 RPC 输出容器创建自定义命令输出,请参阅 cli 格式。
标题和标头组
该 header
语句允许您定义 RPC 格式化 ASCII 输出中的一组字段之前的标头字符串, header-group
并且当输出中包含同一标头组中的两个或多个标头时,语句只会发出第一个标头字符串。
要定义标头字符串并将其与标头组相关联,请在语句中format
分别包括header
和header-group
语句。该header-group
论点是用户定义的字符串,用于标识特定标头组。包含具有相同标识符的header-group
语句的每一个语句都format
属于同一表头组。以下示例定义format
了与标头组color-tags
关联的语句。
junos-odl:format red-format { junos-odl:header-group "color-tags"; junos-odl:header "Color tags\n"; ... }
当多个 format
语句与同一标头组相关联,并且其中两个或更多语句发出的标记存在于输出中时,CLI 输出只会发出其遇到的第一个标头,并抑制属于该标头组的任何后续标头。
要仅为 RPC CLI 输出中的报头组发出第一个标头字符串, header-group
请将语句和标识符包含在属于该标头组的所有 format
语句中。以下示例 RPC output
语句将两个容器及其格式语句与标头组相关 color-tags
联。
output { container red-group { container red { leaf redtag1 { type string; } leaf redtag2 { type string; } junos-odl:format red-format { junos-odl:header-group "color-tags"; junos-odl:header "Color tags\n"; junos-odl:picture "@<<<<<<<<<<<< @<<<<<<<<<<<<"; junos-odl:indent 5; junos-odl:line { junos-odl:field "redtag1"; junos-odl:field "redtag2"; } } } } container blue-group { container blue { leaf bluetag1 { type string; } leaf bluetag2 { type string; } junos-odl:format blue-format { junos-odl:header-group "color-tags"; junos-odl:header "Color tags\n"; junos-odl:picture "@<<<<<<<<<<<< @<<<<<<<<<<<<"; junos-odl:indent 5; junos-odl:line { junos-odl:field "bluetag1"; junos-odl:field "bluetag2"; } } } } }
考虑使用以下 XML 标记的 RPC 回复:
<rpc-reply> <red-group> <red> <redtag1>red-1</redtag1> <redtag2>red-2</redtag2> </red> </red-group> <blue-group> <blue> <bluetag1>blue-1</bluetag1> <bluetag2>blue-2</bluetag2> </blue> </blue-group> </rpc-reply>
当 RPC 答复在 CLI 中提供,并且每个format
语句中都存在相同的header-group
语句时,输出中只会发出第一个标头字符串,在这种情况下,这是语句中format
定义的标头字符串,带有标识符red-format
。
Color tags red-1 red-2 blue-1 blue-2
如果省略语句中的header-group
format
语句,则输出中包含为每组字段定义的标头字符串。
Color tags red-1 red-2 Color tags blue-1 blue-2
缩 进
语 indent
句会使语句范围中除标头行之外的所有行由指定的字符数进行缩入。
要表示线路,请包括 indent
语句和要在语句顶层 format
对线路进行注解的空间数量 以下 RPC 的格式化 ASCII 输出显示输出中由 10 个空格分列的线路。
rpc get-xyz-information { output { container xyz-information { leaf version { type string; description "Version"; } leaf model { type string; description "Model"; } junos-odl:format xyz-information-format { junos-odl:header "xyz information\n"; junos-odl:indent 10; junos-odl:line { junos-odl:field "version"; junos-odl:field "model"; } } } } }
执行 RPC 时,标头将具有合理性,包含两个字段的行则包含十个空格。
xyz information version model
无线路中断
该 no-line-break
语句用于在同一行上显示多个值,如果输出发出具有相同标记名称的多个实体。当包含语 no-line-break
句时,重复格式将放在同一行中。如果省略语句,则会将重复格式设置在单独的行上。
例如,您可能需要在同一行中一起显示所有 SONET 错误。
SONET errors: BPI-B1 0 BIP-B2 0 REI-L 0 BIP-B3 0 REI-P 0
要将多个实体的标记放置在同一输出线内,请在该容器的 no-line-break
语句中 format
包含该语句。
rpc get-sonet-errors { output { container sonet-error-information { container sonet-errors { leaf sonet-error-name { type string; description "SONET error name"; } leaf sonet-error-count { type integer; description "SONET error count"; } junos-odl:format sonet-errors-format { junos-odl:no-line-break; junos-odl:space; junos-odl:header "SONET errors:\n"; junos-odl:line { junos-odl:field "sonet-error-name"; junos-odl:field "sonet-error-count"; } } } } } }
如果 RPC 输出返回多个实体,则输出会将每个重复的字段设置在同一行上。
SONET errors: BPI-B1 0 BIP-B2 0 REI-L 0 BIP-B3 0 REI-P 0
如果省略该 no-line-break
语句,则输出会将每个重复的字段集放在自己的线路上。
SONET errors: BPI-B1 0 BIP-B2 0 REI-L 0 BIP-B3 0 REI-P 0
空间
该 space
语句将一个空间附加到 RPC 格式化 ASCII 输出中节点的值。例如:
value1 value2 Label3: value3
语 space
句通常与 comma
语句一起使用,以划定输出线中的字段,并使用逗号,然后划定一个空间。
要在输出字段中以值后生成空间,请在语句中format
包含space
语句。语句的放置决定了语句的范围。将语句放置在单个字段中只会在该字段之后生成一个空间。
rpc get-xyz-information { output { container xyz-information { leaf version { type string; description "Version"; } // additional leaf definitions junos-odl:format xyz-information-format { junos-odl:space; junos-odl:line { junos-odl:field "version"; // additional fields } } } } }
风格
语 style
句定义了 RPC 输出的多种格式之一。有关使用 语 style
句创建不同级别输出的详细信息,请参阅 为运行 Junos OS 的设备在自定义 YANG RPC 中定义不同级别的输出。
模板
该 template
语句明确定义给定节点的输出字段的格式,包括节点值在字符串中的输出字符串和放置。如果语 template
句为叶节点定义,则相应的输出字段会自动使用模板字符串。
要为节点创建模板字符串,必须在节点定义中包含 template
语句,然后定义字符串。占位符 %s
和 %d
字符串中定义了节点值的类型和放置。用于 %s
插入字符串值并 %d
插入整数值。例如:
rpc get-xyz-information { output { container xyz-information { leaf version { junos-odl:template " Version: %s"; type string; description "Version"; } } } }
如果为节点定义语 template
句,则该节点的输出字段将自动使用模板文本。
rpc get-xyz-information { output { container xyz-information { leaf version { junos-odl:template " Version: %s"; type string; description "Version"; } junos-odl:format xyz-information-format { junos-odl:line { junos-odl:field "version"; // additional fields } } } } }
执行 RPC 时,模板用于该字段的输出中。
Version: value
如果叶语句定义一个 template
语句和一个 formal-name
语句,并且 leading
语句包含在该字段的格式说明中,则输出将使用为语句定义的 formal-name
文本,而不是为语句定义的 template
文本。
junos-odl:format
定义。在早期版本中,CLI 格式使用包含语句的
junos-odl:cli-format
容器进行定义。