3.6.1. 编码操作资源输入参数

如果“rpc”或“action”语句具有“input”部分,那么这些输入参数的实例将在定义了“rpc”或“action”语句的模块名称空间中,在名为XML的元素或JSON对象中对“input”进行编码,它位于定义“rpc”或“action”语句的模块名称空间中。

如果“rpc”或“action”语句有一个“input”部分,并且“input”对象树包含任何被认为是强制节点的子数据节点,则消息体必须由客户端在请求中发送。

如果“rpc”或“action”语句具有“input”部分并且“input”对象树不包含任何被认为是强制节点的子节点,则客户端可以在请求中发送消息体。

如果“rpc”或“action”语句没有“input”部分,请求消息必须不包含消息体。

例子:

下面的YANG模块用于本节中的RPC操作示例。

  1. module example-ops {
  2. namespace "https://example.com/ns/example-ops";
  3. prefix "ops";
  4. organization "Example, Inc.";
  5. contact "support at example.com";
  6. description "Example Operations Data Model Module.";
  7. revision "2016-07-07" {
  8. description "Initial version.";
  9. reference "example.com document 3-3373.";
  10. }
  11. rpc reboot {
  12. description "Reboot operation.";
  13. input {
  14. leaf delay {
  15. type uint32;
  16. units "seconds";
  17. default 0;
  18. description
  19. "Number of seconds to wait before initiating the
  20. reboot operation.";
  21. }
  22. leaf message {
  23. type string;
  24. description
  25. "Log message to display when reboot is started.";
  26. }
  27. leaf language {
  28. type string;
  29. description "Language identifier string.";
  30. reference "RFC 5646.";
  31. }
  32. }
  33. }
  34. rpc get-reboot-info {
  35. description
  36. "Retrieve parameters used in the last reboot operation.";
  37. output {
  38. leaf reboot-time {
  39. type uint32;
  40. description
  41. "The 'delay' parameter used in the last reboot
  42. operation.";
  43. }
  44. leaf message {
  45. type string;
  46. description
  47. "The 'message' parameter used in the last reboot
  48. operation.";
  49. }
  50. leaf language {
  51. type string;
  52. description
  53. "The 'language' parameter used in the last reboot
  54. operation.";
  55. }
  56. }
  57. }
  58. }

以下的YANG模块用于本节的YANG动作示例。

  1. module example-actions {
  2. yang-version 1.1;
  3. namespace "https://example.com/ns/example-actions";
  4. prefix "act";
  5. import ietf-yang-types { prefix yang; }
  6. organization "Example, Inc.";
  7. contact "support at example.com";
  8. description "Example Actions Data Model Module.";
  9. revision "2016-07-07" {
  10. description "Initial version.";
  11. reference "example.com document 2-9973.";
  12. }
  13. container interfaces {
  14. description "System interfaces.";
  15. list interface {
  16. key name;
  17. description "One interface entry.";
  18. leaf name {
  19. type string;
  20. description "Interface name.";
  21. }
  22. action reset {
  23. description "Reset an interface.";
  24. input {
  25. leaf delay {
  26. type uint32;
  27. units "seconds";
  28. default 0;
  29. description
  30. "Number of seconds to wait before starting the
  31. interface reset.";
  32. }
  33. }
  34. }
  35. action get-last-reset-time {
  36. description
  37. "Retrieve the last interface reset time.";
  38. output {
  39. leaf last-reset {
  40. type yang:date-and-time;
  41. mandatory true;
  42. description
  43. "Date and time of the last interface reset, or
  44. the last reboot time of the device.";
  45. }
  46. }
  47. }
  48. }
  49. }
  50. }

RPC输入示例:

客户端可能会发送以下POST请求消息来调用“reboot”这个RPC操作:

  1. POST /restconf/operations/example-ops:reboot HTTP/1.1
  2. Host: example.com
  3. Content-Type: application/yang-data+xml
  4. <input xmlns="https://example.com/ns/example-ops">
  5. <delay>600</delay>
  6. <message>Going down for system maintenance</message>
  7. <language>en-US</language>
  8. </input>

服务器可能会如下回应:

  1. HTTP/1.1 204 No Content
  2. Date: Thu, 26 Jan 2017 20:56:30 GMT
  3. Server: example-server

这里使用JSON编码显示了相同的示例请求消息:

  1. POST /restconf/operations/example-ops:reboot HTTP/1.1
  2. Host: example.com
  3. Content-Type: application/yang-data+json
  4. {
  5. "example-ops:input" : {
  6. "delay" : 600,
  7. "message" : "Going down for system maintenance",
  8. "language" : "en-US"
  9. }
  10. }

操作输入示例:

客户端可能会发送以下POST请求消息来调用“reset”操作:

  1. POST /restconf/data/example-actions:interfaces/\
  2. interface=eth0/reset HTTP/1.1
  3. Host: example.com
  4. Content-Type: application/yang-data+xml
  5. <input xmlns="https://example.com/ns/example-actions">
  6. <delay>600</delay>
  7. </input>

服务器可能会如下回应:

  1. HTTP/1.1 204 No Content
  2. Date: Thu, 26 Jan 2017 20:56:30 GMT
  3. Server: example-server

这里使用JSON编码显示了相同的示例请求消息:

  1. POST /restconf/data/example-actions:interfaces/\
  2. interface=eth0/reset HTTP/1.1
  3. Host: example.com
  4. Content-Type: application/yang-data+json
  5. { "example-actions:input" : {
  6. "delay" : 600
  7. }
  8. }